Skip to content

Instantly share code, notes, and snippets.

@exu
Created September 9, 2011 06:04
Show Gist options
  • Save exu/1205574 to your computer and use it in GitHub Desktop.
Save exu/1205574 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
class ServerMonitor {
public $hosts = array(
'DNS Server' => '10.0.0.215',
'CRM RC' => 'crm.rc.edp',
'CRM Work' => 'crm.w.edp',
'CRM Prod' => 'crm.pl.edp',
'Jenikins' => 'ci.w.edp',
);
public $hostsByIp = array(
'CRM RC' => '10.0.0.222',
'CRM Work' => '10.0.0.221',
'CRM Prod' => '10.0.0.217',
);
function check($host) {
exec("ping -c 1 $host", $o, $r);
return (int)$r === 0;
}
function alert($msg) {
`DISPLAY=:0.0 /usr/bin/notify-send -u critical -i /usr/share/icons/Faenza/status/scalable/error.svg 'Hosts are down' '$msg' `;
}
function run() {
$msg = '';
$hosts = $this->hosts;
if(!$this->check($this->hosts['DNS Server'])) {
$msg .= "DNS is down\n";
$hosts = $this->hostsByIp;
}
foreach($hosts as $name => $host) {
if (!$this->check($host)) $msg .= "$name ($host)\n";
}
if($msg) {
$this->alert($msg);
}
}
}
$Monitoring = new ServerMonitor();
$Monitoring->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment