=0) { $r--; $fp = @fsockopen( $host, 80 ); if($fp==false) continue; $i = @fwrite($fp, 'GET /' . $file . ' HTTP/1.1' . "\n" . 'Host: ' . $host . "\n" . 'Connection: close' . "\n\n"); if($i==false){ @fclose($fp); continue; } $err = false; $h = true; while(!$err) { $ct = @fgets($fp); if($ct===FALSE){ @fclose($fp); continue(2); } $ct = trim($ct); if(empty($ct)) $h = false; if($h) continue; $ct = @fgets($fp); if($first) break; $ct = @fgets($fp); break; } @fclose($fp); if(!empty($ct)) $r = -1; } if($ct==false) die('ERREUR: fetch(' . $host . '/' . $file . ')'); return $ct; } // ************************************************************************************* function getfilebyline($file, $maxlines, $cache = true){ global $readed_files; if($maxlines<=0) die(__LINE__ . ': $maxlines negative or null'); if($maxlines>50000) die(__LINE__ . ': $maxlines higher than 50000'); if($cache) if(array_key_exists($file, $readed_files)) return $readed_files[$file]; $f=@fopen($file, 'r'); if($f===FALSE) die(__LINE__.': unable to open ' . $file); $r=array(); $c=0; while(!@feof($f)) { $b=@fgets($f, 8192); if($b===FALSE) break; $c++; if($c>$maxlines) die(__LINE__.': too much lignes in ' . $file); $r[]=$b; } @fclose($f); if($cache) $readed_files[$file] = $r; return($r); } // ************************************************************************************* function execcached($cmd, $cache = true) { global $exec_cache; if ( $cache && array_key_exists($cmd, $exec_cache) ) return $exec_cache[$cmd]; exec($cmd, $output, $ret); $r = array('output' => $output, 'ret' => $ret); if ( $cache ) { $exec_cache[$cmd] = $r; } return($r); } // ********************************************************************* /******************* * TESTS FUNCTIONS * *******************/ // legacy wrapper function checkmounts($what, $on, $type) { global $test_mount_args; $test_mount_args[] = array($what, $on, $type); } // ************************************************************************************* function test_mount($active, $test_mount_args) { if ( ! $active ) { return; } $mtab = getfilebyline('/etc/mtab', 100); foreach ( $test_mount_args as $mount ) { $found = FALSE; foreach ($mtab as $mtab_line) { if ( preg_match("|^$mount[0]\s+$mount[1]\s+$mount[2]\s+|", $mtab_line) ) { $found = TRUE; } } if ( ! $found ) { die('ERREUR: test_mount() : ' . $mount[0] . ' ' . $mount[1] . ' ' . $mount[2]); } } } // ************************************************************************************* function test_listen($active, $mustlisten, $cache = true) { if ( ! $active ) { return; } global $readed_files; if(!is_array($mustlisten)) { die('mustlisten not array'); } // cache or ss if ( $cache && array_key_exists('sslisten', $readed_files) ) { $r = $readed_files['sslisten']; } else { $r = array(); exec('/sbin/ss -an -A tcp,udp', $output); foreach($output as $line) { if ( preg_match('/^(udp|tcp)\s+\S+\s+\d+\s+\d+\s+(\S+):(\d+)\s+(\S+):\*\s*$/', $line, $matches) ) { $ip = $matches[2]; $port = $matches[3]; $type = $matches[1]; if ( preg_match('/:/', $ip) ) { $type .= '6'; } if ( $ip == '*' ) { $ip = '0.0.0.0'; } $r[$type . '/' . $ip . ':' . $port] = TRUE; } } if ($cache) { $readed_files['sslisten'] = $r; } } // check mustlisten foreach($mustlisten AS $socket) { if(!array_key_exists($socket, $r)) { die('listen error : ' . $socket); } } } // ********************************************************************* function test_io($active, $dirs) { if ( ! $active ) { return; } global $hostname_short; foreach ($dirs as $dir) { $file = $dir . '/.testsdf.' . $hostname_short . '.txt'; $chaine = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; srand((double)microtime()*1000000); $buf = $hostname_short; for($i=0; $i<32; $i++) { $buf .= $chaine{rand()%strlen($chaine)}; } $r = file_put_contents($file, $buf); if ($r===FALSE) { unlink($file); die('file_put_contents "' . $file . '" failed! buf:"'.$buf.'"'); } $r = file_get_contents($file); if($r != $buf) { unlink($file); die('Erreur i/o "' . $file . '" rc:"'.$rc.'" buf:"'.$buf.'"'); } unlink($file); } } // ********************************************************************* function test_ipfailover($active, $ipfailover) { if ( ! $active ) { return; } if ( ! is_array($ipfailover) ) { die('ipfailover not array'); } if ( empty($ipfailover) ) { return; } // get ips $ipa = execcached('/sbin/ip a'); if ( $ipa['ret'] != 0 ) { die('ip a failed: ' . implode($ipa['output'])); } // check foreach($ipfailover AS $ipinfo) { if ( ! is_array($ipinfo) ) { die('ipinfo not array'); } $ip = $ipinfo['ip']; if ( preg_match('#(inet|inet6)\s+' . preg_quote($ip) . '\s+#', implode($ipa['output'])) ) { if ( ! $ipinfo['ismaster'] ) { die('have the ip ' . $ip); } } else { if ( $ipinfo['ismaster']) { die('have no the ip ' . $ip); } } } } // ********************************************************************* function test_mysql($active, $mysqls) { if ( ! $active ) { return; } ini_set('mysql.connect_timeout', 5); if ( ! is_array($mysqls) ) { die('mysqls not array'); } foreach ( $mysqls AS $mysql ) { if ( ! is_array($mysql) ) { die('mysql not array'); } // Connection $link = mysql_connect($mysql[0], $mysql[1], $mysql[2]); if ( ! $link ) { die('ERREUR: mysql_connect() : ' . mysql_error()); } $db_selected = mysql_select_db($mysql[3], $link); if ( ! $db_selected ) { die ('ERREUR: mysql_select_db() : ' . mysql_error()); } // slave if ( $mysql[4] == 'slave') { $res = mysql_query('SHOW SLAVE STATUS'); if ( $res === FALSE ) { die('ERREUR: mysql_query() : ' . mysql_error()); } $row = mysql_fetch_array($res, MYSQL_ASSOC); if ( $row === FALSE ) { die('ERREUR: mysql_fetch_array() : ' . mysql_error()); } if ( ! is_array($row) ) { die('ERREUR: mysql_fetch_array()!array() : ' . mysql_error()); } @mysql_free_result($res); // delay $delay = 60; // default if ( count($mysql) == 6 && ! empty($mysql[5]) ) // time slots { if ( ! is_array($mysql[5]) ) { die('mysql[5] not array'); } $m = $mysql[5]; ksort($m); if ( implode(array_keys($m)) != implode(array_keys($mysql[5])) ) { die('mysql[5] has an incorrect ordered'); } $time_now = date('H:i'); $tmp_array = array_values($mysql[5]); $delay = array_pop($tmp_array); // in case we are before the first timeslot foreach ( $mysql[5] AS $key => $value ) { if ( strcmp($time_now, $key) >= 0 ) { $delay = $value; } } } //ob_start(); var_dump($row); $e = ob_get_clean(); ob_end_clean(); if ( $row['Last_Errno'] != 0 ) { die('ERREUR: mysql master replication failed (Last_Errno = ' . $row['Last_Errno'] . ')'); } if ( $row['Slave_SQL_Running'] != 'Yes' ) { die('ERREUR: mysql master replication failed (Slave_SQL_Running = ' . $row['Slave_SQL_Running'] . ')'); } if ( is_null($row['Seconds_Behind_Master']) ) { die('ERREUR: mysql master replication failed (Seconds_Behind_Master is NULL)'); } if ( $row['Seconds_Behind_Master'] > $delay ) { die('ERREUR: mysql master replication failed (Seconds_Behind_Master = ' . $row['Seconds_Behind_Master'] . ')'); } unset($res, $row); } } mysql_close($link); } // ********************************************************************* function test_postgres($active, $postgress) { if ( ! $active ) { return; } if ( ! is_array($postgress) ) { die('postgress not array'); } foreach ( $postgress AS $postgres ) { if ( ! is_array($postgres) ) { die('postgres not array'); } // Connection $conn_string = 'host=' . $postgres[0] . ' port=' . $postgres[1] . ' dbname=' . $postgres[2] . ' user=' . $postgres[3]. ' password=' . $postgres[4]; $dbconn = pg_connect($conn_string); $stat = pg_connection_status($dbconn); if ($stat !== PGSQL_CONNECTION_OK) { die('ERREUR: PostgreSQL connection failed'); } } pg_close($dbconn); } // ********************************************************************* function test_dns($active, $host1 = 'srv341.sd-france.net', $ip1 = '81.93.243.194', $host2 = 'mxsec.euro-web.fr', $ip2 = '81.93.243.199' ) { if ( ! $active ) { return; } // On essaye de résoudre un nom $ip = gethostbyname($host1); if ($ip != $ip1) { die('ERREUR: gethostbyname()'); } // On essaye de résoudre une adresse $host = gethostbyaddr($ip2); if ($host != $host2) { die('ERREUR: gethostbyaddr()'); } } // ********************************************************************* function test_mail($active, $address = 'mail.test@s4u.fr') { global $hostname_short; if ( ! $active ) { return; } // On essaye d envoyer un mail $r = mail($address, $hostname_short . ' - mail test', 'Ceci est un test ' . time()); if ( ! $r ) { die('ERREUR: mail ne marche pas !'); } } // ********************************************************************* function test_kernel($active) { if ( ! $active ) { return; } // kernel version $min = (int) fetch('kernelv.html', true); $kr = explode('-', php_uname('r')); if ( count($kr) > 1 ) { $kv = (int) substr($kr[1], 3); } else { $kv = 0; } if ($kv < $min) { die('ERREUR: hackable kernel version ' . php_uname('r') . ', min: ' . $min); } } // ********************************************************************* function test_time($active, $limit = 300) { if ( ! $active ) { return; } // On verifie que l heure est +/- X secondes que srv341 $rtime = (double) fetch('time.php', false); if( abs($rtime - time()) > $limit ) { die('ERREUR: ce serveur nest pas a lheure ' . $rtime); } } // ********************************************************************* function test_diskfree($active, $partitions) { if ( ! $active ) { return; } if ( ! is_array($partitions) ) { die('diskfree not array'); } foreach ($partitions AS $partition) { $total = disk_total_space($partition[0]); if ( $total === FALSE ) { die('disk_total_space() failed for ' . $partition[0]); } $free = disk_free_space($partition[0]); if ( $free === FALSE ) { die('disk_fre_space() failed for ' . $partition[0]); } if ( ($total - $free)*100/$total > $partition[1] ) { die('Not enough free space on ' . $partition[0]); } } } // ********************************************************************* function test_memory($active, $free_min = 10) { if ( ! $active ) { return; } if ( !is_numeric($free_min) || $free_min < 0 || $free_min > 100 ) { die('Invalid memeory minimum: ' . $free_min); } $r = execcached('/usr/bin/free -m'); if ( $r['ret'] != 0 ) { die('free -m failed'); } if ( ! preg_match('/Mem:\s+(\d+)\s+.+\-\/\+ buffers\/cache:\s+\d+\s+(\d+)\s*Swap/', implode($r['output']), $matches) ) { die('free returned something strange: ' . implode($r['output'])); } if ( $matches[2]*100/$matches[1] < $free_min ) { die('Free memory (' . ($matches[2]*100/$matches[1]) . '%) below limit (' . $free_min . '%)'); } } // ********************************************************************* function test_load($active, $load_max = 100) { if ( ! $active ) { return; } $r = getfilebyline('/proc/loadavg', 2); if ( ! preg_match('/^[\d\.]+\s+([\d\.]+)\s+/', $r[0], $matches) ) { die('/proc/loadavg looks weird'); } if ( $matches[1] > $load_max ) { die('Load is too high: ' . $matches[1] . ' (max=' . $load_max . ')'); } } // ********************************************************************* function test_smart($active, $smart_file) { if ( ! $active ) { return; } $content = implode(getfilebyline($smart_file, 10)); if ( $content != 'OK' ) { die('smart failed: ' . $content); } } // ********************************************************************* function test_raid($active, $test_raid_args) { if ( ! $active ) { return; } foreach ($test_raid_args as $raid) { $content = implode(getfilebyline('/var/run/sdf-raid-' . $raid[0], 2)); if ( $content != 'RAIDOK' ) { die('raid failed (' . $raid[0] . '): ' . $content . ')'); } } } // ********************************************************************* function test_supervise($active, $test_supervise_file) { if ( ! $active ) { return; } $content = implode(getfilebyline($test_supervise_file, 10)); if ( $content != 'OK' ) { die($content); } } // ********************************************************************* function test_command($command) { exec($command, $output, $r); if ( $r != 0 ) { die('Command test failed' . 'Command = ' . $command . "\n" . 'Return code = ' . $r . "\n" . 'Output = ' . "\n" . implode("\n", $output)); } }