Page 1 sur 1

Un service windows en PHP (surveillance de server/sms)

Posté : 31 août 2009, 10:47
par 131
Toujours plus fort. Aujourd'hui je vais vous cuisiner un service windows de monitoring d'un serveur web (via , par exemple, http://check.myserver.com/ping url qui devra répondre 'pong' quand tout va bien ). Si cette url répond mal, notre script nous enverra même un sms (c'etait facile :) )nous avertissant d'un problème…
Super facile grâce à l'extension PECL win32service et ma classe win32_service - vous en trouverez la documentation ici.

Pour le monitoring ce qui de l'environnement, ce script repose sur la presence du tookit yks que j'ai déjà présenté dans ma précédente contribution.
/*
php mydaemon.php install
REM #Service 'myhost_survey' sucessfully installed
php mydaemon start
REM #Service 'myhost_survey' sucessfully started
php mydaemon status
REM #Service 'myhost_survey' is running
php mydaemon stop
php mydaemon uninstall
REM #Service 'myhost_survey' sucessfully uninstalled
*/


class myhost_survey extends win32_service {
   var $test_url = "http://check.myhost.com/server.status?auto";
  const ping_challenge = 'pong';
 
  function run(){
    try {
        $res = http::ping_url($this->test_url);
        if($res != self::ping_challenge)
           throw new Exception("Invalid response $res");
    } catch(Exception $e) {
        $this->wake_up_neo("Server ping is down {$this->test_url}".$e->getMessage());
    }
  }
 
 
  function wake_up_neo($message){
    try {
        $res = sms::send_message("0698675432", $message);
    } catch(Exception $e){
        die("Error manager reports an error, you won");
    }
    die;
  }
 
}
 
new myhost_survey($argv[1]);