PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Prohits/msManager/auto_run_shell.php

http://prohits.googlecode.com/
PHP | 332 lines | 278 code | 17 blank | 37 comment | 38 complexity | 3907f6c34871c3acc007015ecd41fe37 MD5 | raw file
  1. <?
  2. /***********************************************************************
  3. Copyright 2010 Gingras and Tyers labs,
  4. Samuel Lunenfeld Research Institute, Mount Sinai Hospital.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. *************************************************************************/
  15. $searchLogfile = '../logs/search.log';
  16. $backupLogFile = '../logs/raw_back.log';
  17. $autorunLogFile = '../logs/auto_run.log';
  18. $backup_script_name = 'raw_backup_shell.php';
  19. $backup_script = './autoBackup/' . $backup_script_name;
  20. $search_script_name = 'auto_search_table_shell.php';
  21. $search_script = './autoSearch/' . $search_script_name;
  22. $start_time = @date("Y-m-j G:i:s");
  23. $taskTableNames_arr = array();
  24. $phpProcess_arr = array(); //$phpProcess_arr['PID']['time']['script']['arg1']
  25. //$theTask_arr = array();
  26. //$searchEngine_arr = array();
  27. //$searchEngines_parameter_arr = array();
  28. //$theEngine = '';
  29. //$theTaskID = 0;
  30. //$raw_file_source_dir = '';
  31. //change cwd to this directory
  32. chdir(dirname(__FILE__));
  33. include('../config/conf.inc.php');
  34. include ( "./is_dir_file.inc.php");
  35. $phpProcess_arr = getPhpProcess_arr();
  36. function getPhpProcess_arr(){
  37. $phpProcess_arr = array();
  38. //get all php processes
  39. exec("ps axu | grep .php", $output);
  40. //USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
  41. //print_r($output);
  42. foreach($output as $tmp_str){
  43. $tmp_arr = preg_split('/[ ]+/', trim($tmp_str));
  44. //print_r($tmp_arr);
  45. if(count($tmp_arr) > 11){
  46. $tmp_command = $tmp_arr[10];
  47. if( preg_match('/php$/', $tmp_command)){
  48. $phpProcess_arr[$tmp_arr[1]] = array('user' => $tmp_arr[0],
  49. '%cpu' => $tmp_arr[2],
  50. '%mem' => $tmp_arr[3],
  51. 'vsz' => $tmp_arr[4], //virtual size in kilobytes
  52. 'stat'=> $tmp_arr[7],
  53. 'start'=>$tmp_arr[8],
  54. 'time' => $tmp_arr[9],
  55. 'script' => $tmp_arr[11]);
  56. if(count($tmp_arr) > 12){
  57. $i = 12;
  58. while(isset($tmp_arr[$i])){
  59. if($i == 12){
  60. $tmp_arg = 'Machine';
  61. }else if($i == 13){
  62. $tmp_arg = 'TaskID';
  63. }else if($i == '15'){
  64. $tmp_arg = 'StartEveryHours';
  65. }else{
  66. $tmp_arg = '';
  67. }
  68. if($tmp_arg) $phpProcess_arr[$tmp_arr[1]][$tmp_arg] = $tmp_arr[$i];
  69. $i++;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. return $phpProcess_arr;
  76. }//end of function
  77. //******************************************************************************
  78. //display php processes. required from web.
  79. if(array_key_exists('REQUEST_METHOD', $_SERVER)){
  80. $tableName = '';
  81. $taskID = '';
  82. $output = 'No';
  83. if( $_SERVER['REQUEST_METHOD'] == "POST"){
  84. $request_arr = $_POST;
  85. }else{
  86. $request_arr = $_GET;
  87. }
  88. foreach ($request_arr as $key => $value) {
  89. $$key=$value;
  90. }
  91. //check if $tablename has running task.
  92. if($tableName){
  93. foreach($phpProcess_arr as $tmp_ps_arr){
  94. if($tmp_ps_arr['Machine'] == $tableName){
  95. if(isset($tmp_ps_arr['script'])){
  96. if(strpos($tmp_ps_arr['script'], 'auto_search_table_shell.php')){
  97. if($taskID){
  98. if($taskID == $tmp_ps_arr['TaskID']){
  99. $output = 'Yes';
  100. break;
  101. }
  102. }else{
  103. $output = 'Yes';
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. echo $output;
  111. }else{
  112. echo "<HTML>
  113. <HEAD><SCRIPT LANGUAGE=\"JavaScript\">
  114. <!-- Begin hiding Javascript from old browsers.
  115. var theWait = 30000;
  116. function reloadMe(){
  117. window.location.reload(true);
  118. }
  119. //-- End hiding Javascript from old browsers. -->
  120. </SCRIPT><TITLE>Process status</TITLE></HEAD>
  121. <BODY onLoad=\"timerID=setTimeout('reloadMe()', theWait)\">
  122. ";
  123. echo "<h2>Prohits background process status</h2>";
  124. foreach($phpProcess_arr as $key => $value){
  125. echo "<br><b>Process ID: $key</b><br>";
  126. foreach($value as $tmp_key => $tmp_value){
  127. if($tmp_key == 'StartEveryHours') $tmp_value = $tmp_value / 3600;
  128. echo "$tmp_key: <font color='#008080'>$tmp_value</font> ";
  129. }
  130. }
  131. if(!$phpProcess_arr){
  132. echo "<b><font color=red>No search task is running.</font></b>";
  133. }
  134. echo "</body></html>";
  135. }
  136. exit;
  137. }
  138. //**********************************************************
  139. if(count($_SERVER['argv']) > 1){
  140. if($_SERVER['argv'][1] != 'connect'){
  141. echo "Usage: php ".__FILE__." connect\n";exit;
  142. }
  143. }
  144. mount_msComputers();
  145. //*********************************************************
  146. if(count($_SERVER['argv']) > 1){
  147. if($_SERVER['argv'][1] == 'connect'){
  148. exit;
  149. }
  150. }
  151. sleep(20);
  152. //print_r($phpProcess_arr);
  153. //run backup *******************************************************************
  154. if(is_file($backupLogFile)){
  155. $backup_log_contents = file($backupLogFile);
  156. $linenumber = sizeof($backup_log_contents)-1;
  157. $backup_PSID = 0;
  158. $backup_is_running = false;
  159. while($linenumber > 0){
  160. if(preg_match('/^PSID:([0-9]+)/', $backup_log_contents[$linenumber], $matchs) ){
  161. $backup_PSID = $matchs[1];
  162. break;
  163. }
  164. $linenumber--;
  165. }
  166. if($backup_PSID and isset($phpProcess_arr[$backup_PSID])){
  167. if(strstr($phpProcess_arr[$backup_PSID]['script'], $backup_script_name) ){
  168. $backup_is_running = true;
  169. $logfile = $autorunLogFile;
  170. writeLog("running: " . $phpProcess_arr[$backup_PSID]['script']. "\r\n" . @date("Y-m-j G:i:s"));
  171. }
  172. }
  173. if(!$backup_is_running){
  174. $logfile = $autorunLogFile;
  175. $cmd = PHP_PATH . " " . $backup_script." > /dev/null & echo \$!";
  176. writeLog($cmd . "\r\n" . @date("Y-m-j G:i:s"));
  177. $tmp_PID = system($cmd);
  178. $logfile = $backupLogFile;
  179. writeLog("PSID:$tmp_PID\r\n" . @date("Y-m-j G:i:s"));
  180. }
  181. unset($backup_log_contents,$linenumber);
  182. }else{
  183. $logfile = $autorunLogFile;
  184. $msg = "Warning : $backupLogFile doesn't exist.";
  185. writeLog($msg);
  186. }
  187. exit;
  188. //----------------------------------------------
  189. function fatalError($msg='', $line=0){
  190. //----------------------------------------------
  191. global $start_time;
  192. $msg = "Fatal Error--$msg;";
  193. $msg .= " Script Name: " . $_SERVER['PHP_SELF']. ";";
  194. $msg .= " Start time: ". $start_time . ";";
  195. if($line){
  196. $msg .= " Line number: $line;";
  197. }
  198. writeLog($msg);
  199. exit;
  200. }
  201. //---------------------------------------------
  202. function writeLog($msg){
  203. //---------------------------------------------
  204. global $logfile;
  205. $log = fopen($logfile, 'a+');
  206. fwrite($log, "\r\n" . $msg);
  207. fclose($log);
  208. //echo "write to $logfile: '".$msg ."'";
  209. }
  210. //--------------------------------------------
  211. function mount_msComputers(){
  212. /*
  213. #! /bin/bash
  214. #1. This shell script is used to check mounted mass spectrometer computers
  215. # from mass spec lab.
  216. #2. Please make sure Prohits admin email address 'ADMIN_EMAIL' is correct in conf file.
  217. #3. Run auto_run_shell.php from root user. It will read conf.inic.php file and
  218. automatically mount mss spec computers. Fix errors if there are any.
  219. >sudo /usr/bin/php /var/www/html/Prohits/msManager/auto_run_shell.php connect
  220. #4. Login to the ProHits computer using sudo user and add a cron job for root
  221. using the following line
  222. >sudo crontab -eu root
  223. (if you login as root)
  224. >crontab -e
  225. Add the following lines into crontab. This will set backup procedures to
  226. run automatically every day at 23:20 server time.
  227. ########### start root cron ###########################################
  228. #check php command path and Apache document root then modify following line.
  229. #Usage: http://en.wikipedia.org/wiki/Crontab
  230. 20 23 * * * /usr/bin/php /var/www/html/Prohits/msManager/auto_run_shell.php > /dev/null 2>&1
  231. ############ end ######### #############################################
  232. array(
  233. 'SOURCE'=>'/mnt/LTQ1/',
  234. 'DEFAULT_PROJECT_ID'=>'17',
  235. 'SOURCE_COMPUTER'=>array(
  236. 'ADDRESS'=>'LTQ10269.ad.mshri.on.ca',
  237. 'RAW_DATA_FOLDER'=>'MSdata',
  238. 'SHARED_TO_USER'=>'msusers',
  239. 'SHARED_TO_USER_PASSWD'=>'msuser',
  240. 'WINDOWS_ACTIVE_DIRECTORY'=>'slri_lan1'
  241. ));
  242. */
  243. global $BACKUP_SOURCE_FOLDERS;
  244. $error="";
  245. foreach($BACKUP_SOURCE_FOLDERS as $key => $value){
  246. if($value['SOURCE'] and isset($value['SOURCE_COMPUTER']['ADDRESS']) and $value['SOURCE_COMPUTER']['ADDRESS']){
  247. print "\nConnecting $key:\n";
  248. $MS_arr = $value['SOURCE_COMPUTER'];
  249. $source_dir = $value['SOURCE'];
  250. $MSname = $key;
  251. if(_is_dir($source_dir)){
  252. $cmd = "umount -fl $source_dir";
  253. system($cmd);
  254. }else{
  255. mkdir("$source_dir", 0755);
  256. }
  257. $cmd = "mount -o noserverino -t cifs //";
  258. $cmd .= $MS_arr['ADDRESS'];
  259. $cmd .= "/".$MS_arr['RAW_DATA_FOLDER'] . " $source_dir";
  260. $cmd .= " -o user=". $MS_arr['SHARED_TO_USER']. ",password=".$MS_arr['SHARED_TO_USER_PASSWD'];
  261. if(isset($MS_arr['WINDOWS_ACTIVE_DIRECTORY']) and $MS_arr['WINDOWS_ACTIVE_DIRECTORY']){
  262. $cmd .= ",domain=".$MS_arr['WINDOWS_ACTIVE_DIRECTORY'];
  263. }
  264. print $cmd."\n";
  265. system($cmd);
  266. if(is_empty_dir("/mnt/$MSname")){
  267. $the_msg = "\nStaorge conn't connect $MSname .\n$cmd\n";
  268. echo "$the_msg\nPlease check the above command line. If the parameters are not correct you should change parameters from Prohtis conf file.\n";
  269. $error .= $the_msg;
  270. }else{
  271. echo "$MSname is connected now\n";
  272. }
  273. }
  274. }
  275. if($error){
  276. $msg="Hi Prohits admin,
  277. \nIt is ". @date("Y-m-j G:i:s") ."
  278. \nThis is an auto message from ProHits data management. Do not reply to this address.
  279. \nFollowing computer connections have problem and ought to be fixed as soon as possible.
  280. One possibility is that the computer(s) were turned off.
  281. Turn on all relevant computers if they are not on.
  282. They will be auto-connected tomorrow. You also can login as root and run script:
  283. --------------------
  284. php ".__FILE__." connect
  285. --------------------
  286. to connect them now.
  287. If problem sustained, it could be due to networking problem.
  288. \n$error
  289. \n\nthanks";
  290. mail(ADMIN_EMAIL, "Prohits ms computer connection error", "$msg", "From: Prohits\r\n"."Reply-To: ProhitsServer\r\n");
  291. }
  292. }
  293. //---------------------------------------
  294. function is_empty_dir($dir_path){
  295. //---------------------------------------
  296. $is_empty = true;
  297. $files = @scandir($dir_path);
  298. if(count($files) > 2 ){
  299. $is_empty = false;
  300. }
  301. return $is_empty;
  302. }
  303. ?>