[ Index ]

PHP Cross Reference of Yamoon 0.9.0

title

Body

[close]

/Yamoon/ -> Probe.php (source)

   1  <?php
   2  
   3  require_once  ('Config.php');
   4  require_once  ('Service.php');
   5  require_once  ('Host.php');
   6  require_once  ('Company.php');
   7  
   8  /**
   9    * Probe.php - manage probe creation, update and deletes.
  10    *
  11    * Probes are the heart of Yamoon, it manage also the execution
  12    * of all services on all hosts of all companies. An external script
  13    * (poller.php) will get all active probes and call it's method run().
  14    *
  15    * It will execute the probe, backup the data from the last run and update
  16    * all new data to the probes. This class also get the probe's uptime status
  17    * for the last year, month and day for SLA purposes.
  18    *
  19    * The backup log will be used for the MRTG generator to show graphics for
  20    * status, quality and delay for each probe.
  21    *
  22    * obs: each probe must have a service and a host associated
  23    */
  24  
  25  class Probe {
  26      var $config;
  27      var $company;
  28      var $id;
  29      var $service;
  30      var $host;
  31      var $active;
  32      var $status;
  33      var $quality;
  34      var $delay;
  35  
  36      /**
  37        * Probe Constructor
  38        * $probe = new Probe($config, $id);
  39        *
  40        * param $config Configuration class
  41        * param $id When provided, grab all info for that probe
  42        */
  43  	function Probe ($config, $id="") {
  44          $this->config = $config;
  45          if ($id > 0) {
  46              $this->id = $id;
  47              $rows = $this->config->select("select id, company_id, service_id, host_id, active, status, quality, delay from probes where id = ?", $this->id);
  48              if (isset($rows[0]) && $rows[0]->id > 0) {
  49                  $this->company = new Company($config, $rows[0]->company_id);
  50                  $this->service = new Service($config, $rows[0]->service_id);
  51                  $this->host = new Host($config, $rows[0]->host_id);
  52                  $this->active = $rows[0]->active;
  53                  $this->status = $rows[0]->status;
  54                  $this->quality = $rows[0]->quality;
  55                  $this->delay = $rows[0]->delay;
  56              } else {
  57                  error_log("Probe not found");
  58                  return false;
  59              }
  60          }
  61          return true;
  62      }
  63  
  64      /**
  65        * Create a new probe. Construct object first.
  66        *
  67        * $host->create($company->id, $service->id, $host->id, 'Y');
  68        *
  69        * param $company_id The company this host is part
  70        * param $service The service ID that will run on the host
  71        * param $host The host ID
  72        * param $active An active probe will run every time poller.php is called
  73        * return $id Newly created host's ID
  74        */
  75  	function create ($company_id, $service, $host, $active='Y') {
  76          $values = array($company_id, $service, $host);
  77          if ($this->config->select("select id from probes where company_id = ? and service_id = ? and host_id = ?", $values))
  78              return false;
  79          else {
  80              $values = array($company_id, $service, $host, $active);
  81              $this->config->query("insert into probes values (NULL, ?, ?, ?, ?, 'down', 0, 0, NULL)", $values);
  82              $rows = $this->config->select("select last_insert_id() as id");
  83              $this->id = $rows[0]->id;
  84              $this->company = new Company($this->config, $company_id);
  85              $this->service = new Service($this->config, $service);
  86              $this->host = new Host($this->config, $host);
  87              $this->active = $active;
  88              return $this->id;
  89          }
  90      }
  91  
  92      /**
  93        * Run the probe, this is the main execution entry point. This method will
  94        * call the execute() method, measuring the elapsed time and update the
  95        * status of the probe, logging the last result for MRTG graphics construction.
  96        *
  97        * print $probe->run()
  98        *
  99        * see execute()
 100        * see updateStatus()
 101        * return $results A log line for integration with other tools or just debug
 102        */
 103  	function run () {
 104          $this->quality = 0;
 105          $start = time();
 106          $this->execute();
 107          $this->delay = time() - $start;
 108          $this->updateStatus();
 109          return "[".date("Y-m-d H:i:s")."] [".
 110                  $this->company->name." / ".
 111                  $this->host->name."] [".
 112                  $this->delay." sec] [".
 113                  $this->quality."] [".
 114                  $this->status."]\n";
 115      }
 116  
 117      /**
 118        * Execute the probe. Set environment variables YM_HOST and YM_IP and
 119        * call for "command code". Than, use the regular expressions to get the
 120        * status (result, for up or down) and the current quality (quality).
 121        *
 122        * No example (do not use this function directly, call method run() instead)
 123        */
 124  	function execute () {
 125          $env = "YM_HOST=".$this->host->name."; YM_IP=".$this->host->ip."; ";
 126          $cmd = $this->service->language->cmd." \"".$this->service->code." 2>&1\"";
 127          $return = shell_exec($env . $cmd);
 128          if ($this->service->quality && preg_match("/".$this->service->quality."/", $return, $match))
 129              $this->quality = $match[1];
 130          if ($this->service->result && preg_match("/".$this->service->result."/", $return))
 131              $this->status = "up";
 132          else
 133              $this->status = "down";
 134          return true;
 135      }
 136  
 137      /**
 138        * Backup and update the current probe on the database using the current parameters.
 139        * This function is used to keep the logs for MRTG graphics used on $probe->run() method.
 140        *
 141        * No example (do not use this function directly, call method run() instead)
 142        */
 143  	function updateStatus () {
 144          $this->config->query("insert into log (probe_id, active, status, quality, delay) select id, active, status, quality, delay from probes where id = ?", $this->id);
 145          return $this->update();
 146      }
 147  
 148      /**
 149        * Update the current probe on the database using the current parameters.
 150        * $probe->active = "N";
 151        * $probe->update()
 152        */
 153  	function update () {
 154          $values = array( $this->company->id, $this->service->id, $this->host->id, $this->active, $this->status, $this->quality, $this->delay, $this->id );
 155          return $this->config->query("update probes set company_id = ?, service_id = ?, host_id = ?, active = ?, status = ?, quality = ?, delay = ? where id = ?", $values);
 156      }
 157  
 158      /**
 159        * Delete the current probe.
 160        * $probe->delete();
 161        */
 162  	function delete () {
 163          $values = array( $this->id );
 164          $this->clearLogs();
 165          return $this->config->query("delete from probes where id = ?", $values);
 166      }
 167  
 168      /**
 169        * Clear all logs
 170        * $probe->clearLogs();
 171        */
 172  	function clearLogs () {
 173          $values = array( $this->id );
 174          return $this->config->query("delete from log where probe_id = ?", $values);
 175      }
 176  
 177      /**
 178        * Get uptime status for this probe: last year, last month and last day.
 179        * $data = $probe->getStatus();
 180        *
 181        * return $data A hash with three fields: year, month and day status
 182        */
 183  	function getStats () {
 184          $data = array();
 185          $up;
 186          $down;
 187          // last year status
 188          $result = $this->config->select("select status, sum(status) as sum from log where probe_id = ? and ts > date_sub(now(), interval 12 month) group by status order by status", $this->id);
 189          foreach ($result as $row) {
 190              if ($row->status == "up")
 191                  $up = $row->sum;
 192              else
 193                  $down = $row->sum;
 194          }
 195          $data['year_status_percent'] = sprintf("%0.2f", 100 * $up / ($up + $down));
 196          $up = $down = 0;
 197  
 198          // last month status
 199          $result = $this->config->select("select status, sum(status) as sum from log where probe_id = ? and ts > date_sub(now(), interval 1 month) group by status order by status", $this->id);
 200          foreach ($result as $row) {
 201              if ($row->status == "up")
 202                  $up = $row->sum;
 203              else
 204                  $down = $row->sum;
 205          }
 206          $data['month_status_percent'] = sprintf("%0.2f", 100 * $up / ($up + $down));
 207          $up = $down = 0;
 208  
 209          // today status
 210          $result = $this->config->select("select status, sum(status) as sum from log where probe_id = ? and ts > date_sub(now(), interval 1 day) group by status order by status", $this->id);
 211          foreach ($result as $row) {
 212              if ($row->status == "up")
 213                  $up = $row->sum;
 214              else
 215                  $down = $row->sum;
 216          }
 217          $data['day_status_percent'] = sprintf("%0.2f", 100 * $up / ($up + $down));
 218  
 219          return $data;
 220      }
 221  
 222      /**
 223        * Dumper.
 224        * $probe->dump();
 225        */
 226  	function dump () {
 227          print "\t* Active: $this->active\n";
 228          $this->service->dump();
 229          $this->host->dump();
 230      }
 231  }
 232  ?>


Generated: Sat Feb 19 17:29:53 2005 Cross-referenced by PHPXref 0.6