config = $config; if ($id > 0) { $this->id = $id; $rows = $this->config->select("select id, company_id, name, ip from hosts where id = ?", $id); if (isset($rows[0]) && $rows[0]->id > 0) { $this->company = new Company($this->config, $rows[0]->company_id); $this->name = $rows[0]->name; $this->ip = $rows[0]->ip; } else { error_log("Host not found"); return false; } } return true; } /** * Create a new host if there's no host with that name yet. * Construct object first. * $host->create("my company", "myserver.mycompany.com", "127.0.0.1"); * * param $company_id The company this host is part * param $name The hostname * param $ip Its IP, if not provided try to check it * return $id Newly created host's ID */ function create ($company_id, $name, $ip="") { if ($this->config->select("select id from hosts where name = ?", $name)) return false; else { if (!$ip) $this->getIP(); $values = array($company_id, $name, $ip); $this->config->query("insert into hosts values (NULL, ?, ?, ?)", $values); $rows = $this->config->select("select last_insert_id() as id"); $this->id = $rows[0]->id; $this->name = $name; $this->ip = $ip; return $this->id; } } /** * Update the current host on the database using the current parameters. If IP * is empty try again to determine it automatically. This can be used to change * IPs when DNS has changed. * $host->name = "changedname.mycompany.com"; * $host->update() */ function update () { if (!$this->ip) $this->getIP(); $values = array( $this->name, $this->ip, $this->id ); $this->config->query("update hosts set name = ?, ip = ? where id = ?", $values); } /** * Get the host's IP using system("host hostname") and automatically update $this->ip. * $host->getIP() */ function getIP () { $this->ip = chop(shell_exec("host ".$this->name." | grep address | cut -d \" \" -f 4")); } /** * Delete the current host IF and only IF there's no probe using it. * $host->delete(); */ function delete () { $values = array( $this->id ); if ($this->config->select("select id from probes where host_id = ?", $values)) return false; else $this->config->query("delete from hosts where id = ?", $values); return true; } /** * Dumper. * $host->dump(); */ function dump () { print "\t\t* Host: $this->name\n"; print "\t\t IP: $this->ip\n"; } } ?>