[ Index ]

PHP Cross Reference of Yamoon 0.9.0

title

Body

[close]

/Yamoon/ -> Host.php (source)

   1  <?php
   2  
   3  require_once  ('Config.php');
   4  
   5  /**
   6    * Host.php - manage host creation, update and deletes.
   7    *
   8    * obs: each host must have a company associated
   9    */
  10  
  11  class Host {
  12      var $config;
  13  
  14      var $company;
  15      var $id;
  16      var $name;
  17      var $ip;
  18  
  19      /**
  20        * Host Constructor
  21        * $host = new Host($config, $id);
  22        *
  23        * param $config Configuration class
  24        * param $id When provided, grab all info for that host
  25        */
  26  	function Host ($config, $id="") {
  27          $this->config = $config;
  28          if ($id > 0) {
  29              $this->id = $id;
  30              $rows = $this->config->select("select id, company_id, name, ip from hosts where id = ?", $id);
  31              if (isset($rows[0]) && $rows[0]->id > 0) {
  32                  $this->company = new Company($this->config, $rows[0]->company_id);
  33                  $this->name = $rows[0]->name;
  34                  $this->ip = $rows[0]->ip;
  35              } else {
  36                  error_log("Host not found");
  37                  return false;
  38              }
  39          }
  40          return true;
  41      }
  42  
  43      /**
  44        * Create a new host if there's no host with that name yet.
  45        * Construct object first.
  46        * $host->create("my company", "myserver.mycompany.com", "127.0.0.1");
  47        *
  48        * param $company_id The company this host is part
  49        * param $name The hostname
  50        * param $ip Its IP, if not provided try to check it
  51        * return $id Newly created host's ID
  52        */
  53  	function create ($company_id, $name, $ip="") {
  54          if ($this->config->select("select id from hosts where name = ?", $name))
  55              return false;
  56          else {
  57              if (!$ip)
  58                  $this->getIP();
  59              $values = array($company_id, $name, $ip);
  60              $this->config->query("insert into hosts values (NULL, ?, ?, ?)", $values);
  61              $rows = $this->config->select("select last_insert_id() as id");
  62              $this->id = $rows[0]->id;
  63              $this->name = $name;
  64              $this->ip = $ip;
  65              return $this->id;
  66          }
  67      }
  68  
  69      /**
  70        * Update the current host on the database using the current parameters. If IP
  71        * is empty try again to determine it automatically. This can be used to change
  72        * IPs when DNS has changed.
  73        * $host->name = "changedname.mycompany.com";
  74        * $host->update()
  75        */
  76  	function update () {
  77          if (!$this->ip)
  78              $this->getIP();
  79          $values = array( $this->name, $this->ip, $this->id );
  80          $this->config->query("update hosts set name = ?, ip = ? where id = ?", $values);
  81      }
  82  
  83      /**
  84        * Get the host's IP using system("host hostname") and automatically update $this->ip.
  85        * $host->getIP()
  86        */
  87  	function getIP () {
  88          $this->ip = chop(shell_exec("host ".$this->name." | grep address | cut -d \" \" -f 4"));
  89      }
  90  
  91      /**
  92        * Delete the current host IF and only IF there's no probe using it.
  93        * $host->delete();
  94        */
  95  	function delete () {
  96          $values = array( $this->id );
  97          if ($this->config->select("select id from probes where host_id = ?", $values))
  98              return false;
  99          else
 100              $this->config->query("delete from hosts where id = ?", $values);
 101          return true;
 102      }
 103  
 104      /**
 105        * Dumper.
 106        * $host->dump();
 107        */
 108  	function dump () {
 109          print "\t\t* Host: $this->name\n";
 110          print "\t\t  IP: $this->ip\n";
 111      }
 112  }
 113  ?>


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