This script is from 2004 and mostly obselete. It is here for reference on various functions. There are better ways to do this now.
This is a script written in PHP that will monitor an IP via ICMP and report via syslog if the system goes down and also reports how long it was down once it comes back up. I needed this to monitor not only my internet connection at home but also my hosted connection and server. It generates the ping itself (in this version) so it requires root access but it would be easy to write a new ping() routine to just call /bin/ping. It runs either as a daemon or at the console but I recommend it as a daemon.
PHP has to be compiled with –enable-pcntl and –enable-sockets.
While I would still call this script beta, it does it’s job. Note, that you can send the process a HUP to re-read the configuration without restarting it. Enjoy!
1 2 3 4 |
#debug daemonize monitor host.domain.com monitor 192.168.1.100 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
#!/usr/local/bin/php <?php // Requires PHP 5 declare(ticks = 1); require_once 'Console/Getopt.php'; $monitored = array(); $pidfile = "/tmp/pinger.pid"; $pidwritten = false; $configfile = "/etc/pinger.conf"; $pname = basename($argv[0]); if(!$pname) $pname = "pinger"; /* * * Read and setup config values * */ function read_config() { global $configfile; global $debug; global $daemon; global $pidfile; $debug = false; $daemon = false; if(!file_exists($configfile)) { fwrite(STDERR, sprintf("Could not open config file '%s'\n", $configfile)); exit(-1); } monitor_host('clearhosts'); // reset the monitor list $config = fopen($configfile, "r"); while(!feof($config)) { $value = ''; $line = rtrim(fgets($config)); if($line == "") continue; $result = explode(" ", $line, 2); if(count($result) == 2) list($key, $value) = $result; else $key = $result[0]; if($key[0] == '#') continue; switch($key) { case 'daemonize'; $daemon = true; break; case 'debug'; $debug = true; break; case 'pidfile'; $pidfile = $value; break; case 'monitor'; monitor_host($value); break; } if($debug) printf("config line %s=%s\n", $key, $value); } } /* * * Creates a ping - requires root * */ function ping($host) { $package = "\x08\x00\x19\x2f\x00\x00\x00\x00\x70\x69\x6e\x67"; /* create the socket, the last '1' denotes ICMP */ $socket = socket_create(AF_INET, SOCK_RAW, 1); if(!is_bool($socket)) { /* set socket receive timeout to 1 second */ socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" => 0)); /* connect to socket */ socket_connect($socket, $host, null); /* record start time */ list($start_usec, $start_sec) = explode(" ", microtime()); $start_time = ((float) $start_usec + (float) $start_sec); socket_send($socket, $package, strlen($package), 0); if(@socket_read($socket, 255)) { list($end_usec, $end_sec) = explode(" ", microtime()); $end_time = ((float) $end_usec + (float) $end_sec); $total_time = $end_time - $start_time; return $total_time; } else { return false; } } socket_close($socket); } /* * * Manages adding a host to the monitor list * */ function monitor_host($host) { global $monitored; global $debug; if($host == "clearhosts") { $monitored = array(); return; } if(preg_match("/^\d*\.\d*\.\d*\.\d*$/", $host)) { $monitored[$host] = array( "last" => time(), "status" => true, ); } else { $ip = gethostbyname($host); if($host == $ip) { fwrite(STDERR, sprintf("Error: cannot monitor %s!\n", $host)); return; } if($debug) printf("Adding %s (%s) to monitor.\n", $host, $ip); $monitored[$host] = array( "last" => time(), "status" => true, ); } } /* * * Signal handler routine * */ function handle_signal($signal) { global $pidfile; global $daemon; switch($signal) { case SIGTERM: if(!$daemon) exit; unlink($pidfile); exit; break; case SIGHUP: if(!$daemon) return; read_config(); break; default: break; } } /* * * Displays the configuration - for debugging * */ function show_config() { global $daemon; global $pidfile; global $monitored; printf("daemon: %s\n", ($daemon)?'true':'false'); printf("pidfile: %s\n", $pidfile); printf("monitored:\n"); print_r($monitored); } /* * * Start of the main routine * */ // Get command line options $args = Console_Getopt::readPHPArgv(); $short_opts = "dp::c::"; $long_opts = array( "debug", "config=", ); $options = Console_Getopt::getOpt($args, $short_opts, $long_opts); if (PEAR::isError($options)) { fwrite(STDERR, $options->getMessage() . "\n"); return 4; } foreach($options[0] as $opt) { list($key, $value) = $opt; switch($key) { case 'd': $debug = true; break; case '--debug': $debug = true; break; case 'c': $configfile = $value; break; case '--config': $configfile = $value; break; } } // Install signal handlers pcntl_signal(SIGHUP, "handle_signal"); pcntl_signal(SIGTERM, "handle_signal"); // Process config file read_config(); if($debug) show_config(); // Time to form and write a pid if($daemon) { $pid = pcntl_fork(); if($pid == -1) { fwrite(STDERR, "Failed to fork!\n"); return 1; } if($pid) { $pf = fopen($pidfile, "w"); if($pf === false) { fwrite(STDERR, sprintf("Error: cannot write to pidfile: %s\n", $pidfile)); exit(5); } else { $pidwritten = true; fwrite($pf, $pid); fclose($pf); } return 0; } } openlog($pname, LOG_NDELAY | LOG_PID, LOG_LOCAL0); while(true) { if($debug) print_r($monitored); foreach($monitored as $key => $value) { $res = (ping($key))?true:false; if($res != $monitored[$key]['status']) { if($res == false) { if(!$debug) syslog(LOG_NOTICE, sprintf("lost communication with %s at %s", $key, date("g:i a m-d-Y", time()))); else printf("lost communication with %s at %s\n", $key, date("g:i a m-d-Y", time())); } else { if(!$debug) syslog(LOG_NOTICE, sprintf("restored communication with %s at %s (%ds)", $key, date("g:i a m-d-Y", time()), time() - $monitored[$key]['last'])); else printf("restored communication with %s at %s (%ds)\n", $key, date("g:i a m-d-Y", time()), time() - $monitored[$key]['last']); } $monitored[$key]['status'] = $res; $monitored[$key]['last'] = time(); } } sleep(5); } if($pidwritten) unlink($pidfile); ?> |