#!/usr/local/bin/php -n '; $config['hostname'] = 'full.server.name'; $config['init_script_ok'] = 'done'; $config['sshd_init'] = 'sshd'; $config['httpd_init'] = '/etc/httpd/bin/apachectl'; $config['mysqld_init'] = 'mysql'; $config['mysqld_user'] = 'root'; $config['mysqld_pass'] = ''; $config['load_limit'] = 4; $config['filesystem_warning_limit'] = 10; $config['filesystem_error_limit'] = 5; // must be less than filesystem_warning_limit! // INIT ERROR VARIABLE $error = 0; // TEST MySQL if(function_exists("mysql_connect")) { if(mysql_connect('localhost',$config['mysqld_user'],$config['mysqld_pass']) === FALSE) { $mysql_status = 'ERROR (could not connect)'; $error = 1; // here, we want to try to restart the mysql process [TODO] /* $result = ''; system('/etc/init.d/'.$config['mysqld_init'].' stop &'); sleep(5); system('killall -9 mysqld'); system('killall -9 save_mysqld'); system('/etc/init.d/'.$config['mysqld_init'].' start && /root/scripts/watchdog.php &'); exit; */ } else { $mysql_status = 'Ok'; } } else { $mysql_status = 'ERROR (no MySQL support in PHP)'; $error = 1; } // TEST HTTPD WEBSERVER if(fsockopen('localhost',80,$errno,$errstr,30) === FALSE) { $httpd_status = 'ERROR ('.$errstr.')'; $error = 1; system($config['httpd_init'].' stop'); system($config['httpd_init'].' start'); if(fsockopen('localhost',80,$errno,$errstr,30) !== FALSE) { $httpd_status .= ' - RESTART SUCCESSFUL - NO ACTION REQUIRED'; } } else { $httpd_status = 'Ok'; } // TEST SSHD if(fsockopen('localhost',22,$errno,$errstr,30) === FALSE) { $sshd_status = 'ERROR ('.$errstr.')'; $error = 1; system('/etc/init.d/'.$config['sshd_init'].' stop'); system('/etc/init.d/'.$config['sshd_init'].' start'); if(fsockopen('localhost',22,$errno,$errstr,30) !== FALSE) { $sshd_status .= ' - RESTART SUCCESSFUL - NO ACTION REQUIRED'; } } else { $sshd_status = 'Ok'; } // TEST FREE BOOT SPACE $boot_free_space = round(100 * (disk_free_space('/boot') / disk_total_space('/boot'))); if($boot_free_space <= 10) { $boot_status = "WARNING - ONLY $boot_free_space % FREE ON BOOT DEVICE"; $error = 1; } else { $boot_status = "Ok - $boot_free_space % free"; } // TEST FREE FILESYSTEM SPACE $free_space = round(100 * (disk_free_space('/') / disk_total_space('/'))); if($free_space == 0) { $filesystem_status = "ERROR - NO SPACE LEFT ON DEVICE"; $error = 1; } elseif($free_space <= $config['filesystem_error_limit']) { $filesystem_status = "WARNING - ONLY $free_space % FREE ON DEVICE"; $error = 1; } elseif($free_space <= $config['filesystem_warning_limit']) { $filesystem_status = "WARNING - ONLY $free_space % FREE ON DEVICE"; } else { $filesystem_status = "Ok - $free_space % free"; } // TEST SERVER LOAD $fp = fopen('/proc/loadavg', 'r'); $loadavg = fgetcsv($fp, 1000, ' '); fclose($fp); if(is_numeric($loadavg[1])) { $load = $loadavg[1]; } else { $load = -1; } if($load >= $config['load_limit']) { $load_status = "WARNING - SERVER LOAD IS $load"; $error = 1; } else { $load_status = "Ok - $load (5min avg)"; } // TEST SYSTEM MEMORY $mem_file = file('/proc/meminfo'); while(list($key, $value) = each($mem_file)) { if(strpos($value, ':') !== FALSE) { $line_tok = explode(':', $value); $value_tok = explode(' ', trim($line_tok[1])); if(is_numeric($value_tok[0])) { $mem_array[trim($line_tok[0])] = trim($value_tok[0]); } } } // test array size, just to be sure, that there will be no errors... if(count($mem_array) > 10) { $swap_free_space = round(100 * ($mem_array['SwapFree'] / $mem_array['SwapTotal'])); if($swap_free_space >= 20) { $swap_status = "Ok - $swap_free_space % swap space free"; } elseif($swap_free_space > 0) { $swap_status = "WARNING - ONLY $swap_free_space % SWAP SPACE FREE"; $error = 1; } elseif($swap_free_space == 0) { $swap_status = "ERROR - NO FREE SWAP SPACE"; $error = 1; } } else { $swap_status = 'ERROR - PROBLEMS WHILE READING MEMORY INFORMATION'; $error = 1; } // COMPOSE MAIL $body = "SERVICES\n". "MySQL : $mysql_status\n". "HTTPD : $httpd_status\n". "SSHD : $sshd_status\n\n". "FILESYSTEM STATUS\nTotal space: ".round(disk_total_space('/') / 1048576)." MB\n". "Free space : ".round(disk_free_space('/') / 1048576)." MB\n". "Status : ".$filesystem_status."\n". "Boot Device: ".$boot_status."\n\n". "OPERATING SYSTEM\n". "Server Load : $load_status\n". "Total Memory: ".round($mem_array['MemTotal'] / 1024)." MB\n". "Free Memory : ".round($mem_array['MemFree'] / 1024)." MB\n". "Swap Total : ".round($mem_array['SwapTotal'] / 1024)." MB\n". "Swap Free : ".round($mem_array['SwapFree'] / 1024)." MB\n". "Swap Status : $swap_status\n\n". "Report generated by ".$config['hostname']." (".date('D M j G:i:s T Y').")"; // SEND MAIL if($error == 0 && $argc > 0 && $argv[1] == 'send_mail') { $subject = 'Server Status Report ('.$config['hostname'].')'; $body .= "\nNote: This mail was sent though there is no error on this system. There is no action required."; mail($config['mail_to'], $subject, $body, 'From: '.$config['mail_from']); } elseif($error == 1) { $subject = 'CRITICAL SYSTEM FAILURE DETECTED ('.$config['hostname'].')'; mail($config['mail_to'], $subject, $body, 'From: '.$config['mail_from']); } ?>