cvs: pear /Net_Monitor Monitor.php /Net_Monitor/Monitor Alert.php Service.php /Net_Monitor/Monitor/Alert SMS.php SMTP.php
/Net_Monitor/Monitor/Service DNS.php FTP.php HTTP.php HTTPS.php SMTP.php
| From: | Robert Peake | Date: | Tue, 29 Mar 2005 00:25:58 +0000 |
| Subject: | cvs: pear /Net_Monitor Monitor.php /Net_Monitor/Monitor Alert.php Service.php /Net_Monitor/Monitor/Alert SMS.php SMTP.php /Net_Monitor/Monitor/Service DNS.php FTP.php HTTP.php HTTPS.php SMTP.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-30746@lists.php.net to get a copy of this message | ||
cyberscribe Mon Mar 28 19:25:58 2005 EDT
Added files:
/pear/Net_Monitor Monitor.php
/pear/Net_Monitor/Monitor Alert.php Service.php
/pear/Net_Monitor/Monitor/Alert SMS.php SMTP.php
/pear/Net_Monitor/Monitor/Service DNS.php FTP.php HTTP.php
HTTPS.php SMTP.php
Log:
First release since proposal approval
http://cvs.php.net/co.php/pear/Net_Monitor/Monitor.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor.php +++ pear/Net_Monitor/Monitor.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: Monitor.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * Net_Monitor * * A unified interface for checking the availability services on external * servers and sending meaningful alerts through a variety of media if a * service becomes unavailable. * * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) * */ /** * Requires the main Pear class */ require_once 'PEAR.php'; /** * class Net_Monitor * * @access public * @package Net_Monitor */ class Net_Monitor { /**#@+ * @access private * @var array */ var $_services = array(); var $_alerts = array(); var $_options = array('state_directory' => '/tmp/', 'state_file' => 'Net_Monitor_State', 'subject_line' => 'Net_Monitor Alert', 'alert_line' => '%h: %s: %m', 'notify_change' => 1, 'notify_ok' => 1 ); var $_clients = array(); var $_alerters = array(); var $_results = array(); var $_results_diff = array(); /**#@-*/ /** * function Net_Monitor * * @access public * @param array services * @param array alerts * @return void */ function Net_Monitor($services=array(),$alerts=array(),$options=array()) { if ($options && sizeof($options) > 0) { $this->setOptions($options); } if ($services && sizeof($services) > 0) { $this->setServices($services); } if ($alerts && sizeof($alerts) > 0) { $this->setAlerts($alerts); } } /** * function setOptions * * <p>Sets additional options for the class</p> * <p>Merges input array with $_options</p> * * @access public * @param array options * @return void */ function setOptions($options) { foreach($options as $key => $value) { $this->_options[$key] = $value; } } /** * function setServices * * <p>Sets the services to monitor for the class</p> * <p>Overwrites $_services with input array</p> * <p>Net_Monitor_Services are of the form: <br /> * <pre> * $services = array('foo.example.com'=>array('SMTP','DNS'), * 'bar.example.com'=>array('HTTP','FTP','DNS')); * </pre> * * @access public * @param array services * @return void */ function setServices($services) { $this->_services = $services; } /** * function setAlerts * * <p>Sets the alerts for the class</p> * <p>Overwrites $_alerts with input array</p> * <p>Net_Monitor_Alerts are of the form: <br /> * <pre> * $alerts = array('User1' => array('SMTP'=>'user1@example.com'), * 'User2' => array('SMTP'=>'user2@example.com')); * </pre> * * @param array alerts * @return void */ function setAlerts($alerts) { $this->_alerts = $alerts; } /** * function checkAll * * Checks all services and sends all alerts. * * @access public * @return void */ function checkAll() { //initialize the _results and _results_diff arrays $this->_results = array(); $this->_results_diff = array(); //check all services and populate the _results array if (is_array($this->_services) && sizeof($this->_services) > 0) { $this->loadClients(); //load client objects once and only once per service foreach($this->_services as $server => $services) { for ($i=0; $i<sizeof($services); $i++) { $service = $services[$i]; $result = $this->check($server,$service); if ($result) { $this->_results[] = $result; } } } } else { pear::raiseError('No services found to check.'); } if (is_array($this->_results) && sizeof($this->_results) > 0) { $last_state = $this->getState(); $this->saveState(); $this->_results_diff = $this->stateDiff($this->_results,$last_state); /* UNCOMMENT THE FOLLOWING TO DEBUG DIFFERENTIALS // print "Last state: \n\n"; // print_r($last_state); // print "Current state: \n\n"; // print_r($this->_results); // print "Difference: \n\n"; // print_r($this->_results_diff); */ } if (is_array($this->_results_diff) && sizeof($this->_results_diff) > 0) { if (is_array($this->_alerts) && sizeof($this->_alerts) > 0) { $this->loadAlerters(); //loop through alerts, sending the result message foreach($this->_alerts as $user => $alert_array) { foreach($alert_array as $method => $server) { $this->alert($method,$server); } } } else { //nobody to alert? print the result to STDOUT $results = $this->_results_diff; $sizeof_results = sizeof($results); for($i=0; $i < $sizeof_results; $i++) { $this->printAlert($results[$i]); } } return true; } else { return false; } } /** * function check * * Check a single service on a single server * * @access public * @param mixed server * @param string service * @return mixed */ function check($server,$service) { $client = $this->_clients[$service]; if (!is_object($client)) { //if an client has not yet been created for this service, create one $this->client[$service] = $this->getClient($service); $client = $this->_clients[$service]; } return $client->check($server); } /** * function loadClients * * Load all clients into the Net_Monitor object so that * each type of service client is only instantiated once. * * @access public * @return void */ function loadClients() { $services_array = array_values($this->_services); $types_array = array_keys($this->_clients); for ($i=0;$i<sizeof($services_array);$i++) { $sub_array = $services_array[$i]; for ($j=0; $j<sizeof($sub_array); $j++) { $type = $sub_array[$j]; if (!in_array($type,$types_array)) { $types_array[] = $type; $this->_clients[$type] = $this->getClient($type); } } } } /** * function loadAlerters * * Load alert objects into Net_Monitor so that each type of alert * is only used once. * * @access public */ function loadAlerters() { $alerts_array = array_values($this->_alerts); $current_alerters = array_keys($this->_alerters); for ($i=0; $i<sizeof($alerts_array); $i++) { $sub_array = $alerts_array[$i]; foreach($sub_array as $alert_type => $method) { if (!in_array($alert_type,$current_alerters)) { $current_alerters[] = $alert_type; $this->_alerters[$alert_type] = $this->getAlerter($alert_type); } } } } /** * function getClient * * Returns a client of the specificed type. <em>Note: * does not check to see if this client has already been * loaded into Net_Monitor::clients - that is handled elsewhere.</em> * * @access private * @param string type * @return void */ function getClient($type) { require_once "Net/Monitor/Service/$type.php"; $service = "Net_Monitor_Service_$type"; return new $service(); } /** * function getAlerter * * Returns an alerter of the specificed type. <em>Note: * does not check to see if this alerter has already been * loaded into Net_Monitor::alerters - that is handled elsewhere.</em> * * @access private * @param string type * @return void * */ function getAlerter($type) { require_once "Net/Monitor/Alert/$type.php"; $alerter = "Net_Monitor_Alert_$type"; return new $alerter(); } /** * function alert * * Send a single alert of the specified method to the specified server * * @access private * @param string method * @param mixed $server * */ function alert($method,$server) { $alerter = $this->_alerters[$method]; if (!is_object($alerter)) { //if an alerter for this method has not already been created, create one $this->_alerters[$method] = $this->getAlerter($method); $alerter = $this->_alerters[$method]; } return $alerter->alert($server,$this->_results_diff,$this->_options); } /** * function saveState * * Saves the current _results array to the directory specified in * _options['state_directory'] as a file named _options['state_file']. * If an array is passed to the function, that array is saved as state, * otherwise this function acts upon $_results. * * @access public * @param array results * @return void * */ function saveState($results = NULL) { $options = $this->_options; $path = $options['state_directory']; $file = $options['state_file']; if (!is_writable($path)) { Pear::raiseError($path.' is not writeable'); } if (file_exists($path.$file)) { if (!is_writable($path.$file)) { Pear::raiseError($path.$file.' exists but is not writeable'); } } $fp = @fopen($path.$file,'w'); if (is_null($results)) { $results = $this->_results; } $line = serialize($results); @fwrite($fp,$line); @fclose($fp); } /** * function getState * * Retrieves previous state information from the directory specified in * _options['state_directory'] via a file named _options['state_file'] * * @access public * @return array * */ function getState() { $my_line = ""; $options = $this->_options; $path = $options['state_directory']; $file = $options['state_file']; if (file_exists($path.$file)) { if (!is_readable($path.$file)) { Pear::raiseError($path.$file.' exists but is not readable'); } } else { return array(); } $fp = @fopen($path.$file,'r'); while(!feof($fp)) { $my_line .= fgets($fp,4096); } $return_array = unserialize($my_line); @fclose($fp); return $return_array; } /** * function stateDiff * * Computes the difference between a primary and secondary * array representing state, i.e. all values in primary that * are not already in secondary. * * Also returns an OK status for values in secondary that are * not in primary unless * $_options['notify_ok'] is set to false. * * Returns values in primary whose code value differs * from values in secondary unless * $_options['notify_change'] is set to false. * * @access private * @param array primary * @param array secondary * @return array */ function stateDiff($primary,$secondary) { $return_array = array(); $sizeof_primary = sizeof($primary); $sizeof_secondary = sizeof($secondary); //loop through primary array for($i=0; $i < $sizeof_primary; $i++) { $primary_sub = $primary[$i]; $ps_host = $primary_sub['host']; $ps_service = $primary_sub['service']; $ps_code = $primary_sub['code']; //reindex secondary in case it has had values removed $secondary = array_values($secondary); $sizeof_secondary = sizeof($secondary); //reset primary flags $primary_sub_is_unique = true; $primary_sub_changed = false; //for each primary value, loop through secondary array for($j = 0; $j < $sizeof_secondary; $j++) { $secondary_sub = $secondary[$j]; $ss_host = $secondary_sub['host']; $ss_service = $secondary_sub['service']; $ss_code = $secondary_sub['code']; if ($ss_host == $ps_host && $ss_service == $ps_service) { //host and service identical in secondary and primary if ($ss_code != $ps_code) { //different codes if ($this->_options['notify_change']) { //notify_change on; move to return $return_array[] = $primary_sub; } //flag this primary_sub as a change, not a unique $primary_sub_changed = true; } else { //code also identical in secondary and primary //therefore this primary_sub is not unique $primary_sub_is_unique = false; } //remove from secondary unset($secondary[$j]); } } if ($primary_sub_is_unique && !$primary_sub_changed) { //after all that checking, primary_sub is unique //but not because it represents a host/service that has //changed from one down state to another; move to return. $return_array[] = $primary_sub; } } if ($this->_options['notify_ok'] && sizeof($secondary) > 0) { //remaining states in secondary are marked OK and added to return reset($secondary); $sizeof_secondary = sizeof($secondary); for($k = 0; $k < $sizeof_secondary; $k++) { $secondary[$k]['code'] = 200; $secondary[$k]['message'] = 'OK'; $return_array[] = $secondary[$k]; } } return $return_array; } /** * function resetState * * Resets the results and results differential arrays * and deletes the state file. * * Returns true if the file has been deleted or never existed * in the first place; false otherwise. * * @access public * @return boolean */ function resetState() { $this->_results = array(); $this->_results_diff = array(); $options = $this->_options; $path = $options['state_directory']; $file = $options['state_file']; if (file_exists($path.$file)) { if (!is_writable($path.$file)) { Pear::raiseError($path.$file.' exists but is not writeable'); } return unlink($path.$file); } else { return true; } } /** * function resetHostState * * Resets the state for a single host. Optionally take in a * second parameter, service, whereby the function only * resets the results for that particular host/service test. * * @param string host * @return void * @access public */ function resetHostState($host,$service = NULL) { $last_state = $this->getState(); $sizeof_last_state = sizeof($last_state); for($i=0; $i < $sizeof_last_state; $i++) { $working_state = $last_state[$i]; if ($working_state['host'] == $host) { if ($service != NULL && $working_state['service'] != $service) { //hosts match but services don't match? place in new array $new_array[] = $working_state; } } else { //different hosts? place in new array $new_array[] = $working_state; } } $this->saveState($new_array); } /** * function printAlert * * Prints the alert specified in the associative array * to STDOUT. Formats the alert according to $_options['alert_line']. * * @param array array * @return void * @access public */ function printAlert($array) { if ($this->_options['alert_line']) { $alert_line = $this->_options['alert_line']; } else { $alert_line = '%h: %s: %m'; } $alert_line = str_replace('%h',$array['host'],$alert_line); $alert_line = str_replace('%s',$array['service'],$alert_line); $alert_line = str_replace('%m',$array['message'],$alert_line); $alert_line = str_replace('%c',$array['code'],$alert_line); print $alert_line."\r\n"; } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Alert.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Alert.php +++ pear/Net_Monitor/Monitor/Alert.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: Alert.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * class Net_Monitor_Alert * * This is the generic alert class * * @package Net_Monitor * @access public * */ class Net_Monitor_Alert { var $_service = 'Generic'; var $_alert = NULL; /** * function alert * * Sends the specified results to the specified server * * @access private * @param mixed server * @param array results * @return mixed */ function alert($server,$results,$options=array()) { print "ALERT: $server ".$this->_service." not yet implemented\n"; print_r($results); } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service.php +++ pear/Net_Monitor/Monitor/Service.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: Service.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * class Net_Monitor_Service * * A generic service monitoring class * * @package Net_Monitor * @access public */ class Net_Monitor_Service { /** * @var string _service * @access private */ var $_service = 'Generic'; /** * @var object _client * @access private */ var $_client = NULL; /** * @var int _last_code * @access private */ var $_last_code = -1; /** * function check * * Checks the specified server for availability. * Returns false on success, or a notification array on failure. * * @param mixed server * @return mixed */ function check($server) { return array('host' => $server, 'service' => $this->_service, 'message' => "not yet implemented", 'code' => -1); } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Alert/SMS.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Alert/SMS.php +++ pear/Net_Monitor/Monitor/Alert/SMS.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: SMS.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * requires and extends the Net_Monitor_Alert class */ require_once 'Net/Monitor/Alert.php'; /** * requires and uses the Net_SMS class for SMS alerts */ require_once 'Net/SMS.php'; /** * class Net_Monitor_Alert_SMS * * @package Net_Monitor * @access public * @see Net_Monitor_Alert */ class Net_Monitor_Alert_SMS extends Net_Monitor_Alert { /** * @var string _service * @access private */ var $_service = 'SMS'; /** * @var object _alert * @access private */ var $_alert = NULL; /** * function Net_Monitor_Alert_SMS * * @access public */ function Net_Monitor_Alert_SMS() { $this->_alert = new Net_SMS(); } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Alert/SMTP.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Alert/SMTP.php +++ pear/Net_Monitor/Monitor/Alert/SMTP.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: SMTP.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * requires and extends the Net_Monitor_Alert class */ require_once 'Net/Monitor/Alert.php'; /** * requires and uses the Net_SMTP class to send SMTP alerts */ require_once 'Net/SMTP.php'; /** * class Net_Monitor_Alert_SMTP * * A class for sending alerts via SMTP (email) * * @package Net_Monitor * @access public * @see Net_Monitor_Alert */ class Net_Monitor_Alert_SMTP extends Net_Monitor_Alert { /** * @var string _service * @access private */ var $_service = 'SMTP'; /** * @var object _alert * @access private */ var $_alert = NULL; /** * function Net_Monitor_Alert_SMTP * * @access public */ function Net_Monitor_Alert_SMTP() { $this->_alert = new Net_SMTP(); } /** * function alert * * Sends the specified results to the specified SMTP server * * @access private * @param mixed server * @param array results * @return mixed */ function alert($server,$result_array,$options=array()) { if ($options['subject_line']) { $subject = $options['subject_line']; } else { Pear::raiseError('Using default subject', NULL, PEAR_ERROR_EXCEPTION, E_USER_WARNING); $subject = 'Net_Monitor Alert'; } $server_array = explode('@',$server); $rcpt = $server_array[0]; $server_addr = $server_array[1]; $this->_client = new Net_SMTP($server_addr); $smtp_client = $this->_client; if ($options['smtp_debug']) { $smtp_client->setDebug(true); } $sizeof_result_array = sizeof($result_array); for($i=0;$i<$sizeof_result_array;$i++) { $result = $result_array[$i]; $host = $result['host']; $service = $result['service']; $message = $result['message']; $code = $result['code']; if ($options['alert_line']) { $alert_line = $options['alert_line']; } else { $alert_line = '%h: %s: %m'; } $alert_line = str_replace('%h',$host,$alert_line); $alert_line = str_replace('%s',$service,$alert_line); $alert_line = str_replace('%m',$message,$alert_line); $alert_line = str_replace('%c',$code,$alert_line); $email_message .= $alert_line."\r\n"; } $smtp_client->quotedata(&$email_message); $e = $smtp_client->connect(); if (!PEAR::isError($e)) { $e = $smtp_client->mailFrom($server); $e = $smtp_client->rcptTo($server); $e = $smtp_client->data('Subject: '.$subject."\r\n\r\n".$email_message); $smtp_client->disconnect(); } if (PEAR::isError($e)) { return $e; } else { return true; } } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service/DNS.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service/DNS.php +++ pear/Net_Monitor/Monitor/Service/DNS.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: DNS.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * require and extend the Net_Monitor_Service class */ require_once 'Net/Monitor/Service.php'; /** * require and use the Net_DNS class to check DNS service */ require_once 'Net/DNS.php'; /** * class Net_Monitor_Service_DNS * * A class for checking DNS services * * @package Net_Monitor * @access public * @see Net_Monitor_Service */ class Net_Monitor_Service_DNS extends Net_Monitor_Service { /** * @var string _service * @access private */ var $_service = 'DNS'; /** * @var object _client * @access private */ var $_client = NULL; /** * @var int _last_code * @access private */ var $_last_code = -1; /** * function Net_Monitor_Service_DNS * * @access public */ function Net_Monitor_Service_DNS() { $this->_client = new Net_DNS_Resolver(); } /** * function check * * Checks the specified DNS server for availability. * Returns false on success, or a notification array on failure. * * @param mixed host * @return mixed */ function check($host) { $c = $this->_client; $c->nameservers = array($host); $e = $c->search($host); if (is_object($e) && $e->answer) { return false; } else { $this->_last_code = 0; return array('host' => $host, 'service' => $this->_service, 'message' => 'no response', 'code' => 0); } } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service/FTP.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service/FTP.php +++ pear/Net_Monitor/Monitor/Service/FTP.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: FTP.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * require and extend the Net_Monitor_Service class */ require_once 'Net/Monitor/Service.php'; /** * require and use the Net_FTP class to check FTP services */ require_once 'Net/FTP.php'; /** * class Net_Monitor_Service_FTP * * A class to check FTP services * * @package Net_Monitor * @access public * @see Net_Monitor_Service */ class Net_Monitor_Service_FTP extends Net_Monitor_Service { /** * @var string _service * @access private */ var $_service = 'FTP'; /** * @var object _client * @access private */ var $_client = NULL; /** * @var int _last_code * @access private */ var $_last_code = -1; /** * function Net_Monitor_Service_FTP * * @access public */ function Net_Monitor_Service_FTP() { $this->_client = new Net_FTP(); } /** * function check * * Checks the specified FTP server for availability. * Returns false on success, or a notification array on failure. * * @param mixed host * @return mixed */ function check($host) { $c = $this->_client; $e = $c->connect($host); if (!Pear::isError($e)) { $this->_last_code = '200'; return false; } else { $this->_last_code = 0; return array('host' => $host, 'service' => $this->_service, 'message' => $e->getMessage(), 'code' => 0); } } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service/HTTP.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service/HTTP.php +++ pear/Net_Monitor/Monitor/Service/HTTP.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: HTTP.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * require and extend the Net_Monitor_Service class */ require_once 'Net/Monitor/Service.php'; /** * require and use the HTTP_Request class to check HTTP services */ require_once 'HTTP/Request.php'; /** * class Net_Monitor_Service_HTTP * * A class to check HTTP (web) services * * @package Net_Monitor * @access public * @see Net_Monitor_Service */ class Net_Monitor_Service_HTTP extends Net_Monitor_Service { /** * @var string _service * @access private */ var $_service = 'HTTP'; /** * @var object _client * @access private */ var $_client = NULL; /** * @var int _last_code * @access private */ var $_last_code = -1; /** * @var string _prefix * @access private */ var $_prefix = 'http://'; /** * function Net_Monitor_Service_HTTP * * @access public */ function Net_Monitor_Service_HTTP() { $this->_client = new HTTP_Request(); } /** * function check * * Checks the specified HTTP (web) server for availability. * Returns false on success, or a notification array on failure. * * @param mixed host * @return mixed */ function check($host) { $response = 0; $full_host = $this->_prefix.$host; $this->_client = new HTTP_Request($full_host); $http_client = $this->_client; $e = $http_client->sendRequest(); if (!PEAR::isError($e)) { $e = $http_client->getResponseBody(); if (!PEAR::isError($e)) { $response = $http_client->getResponseCode(); if ($response != 200) { $e = new PEAR_Error($response); } else { $return = false; } } } if (PEAR::isError($e)) { $msg = $e->getMessage(); if (!$msg) { $msg = 'host not found'; } $return = array('host' => $host, 'service' => $this->_service,'message' => $msg, 'code' => $response); } $this->_last_code = $response; return $return; } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service/HTTPS.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service/HTTPS.php +++ pear/Net_Monitor/Monitor/Service/HTTPS.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: HTTPS.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * require and extend the Net_Monitor_Service_HTTP class by re-implementing the prepare() method */ require_once 'Net/Monitor/HTTP.php'; /** * class Net_Monitor_Service_HTTPS * * A class for checking HTTPS (web over SSL) services * * @package Net_Monitor * @access public * @see Net_Monitor_Service_HTTP */ class Net_Monitor_Service_HTTPS extends Net_Monitor_Service_HTTP { /** * @var string _service * @access private */ var $_service = 'HTTPS'; /** * @var string _prefix * @access public */ var $_prefix = 'https://'; /** * function Net_Monitor_Service_HTTPS * * @access public */ function Net_Monitor_Service_HTTPS() { if (!extension_loaded('openssl')) { PEAR::raiseError('Net_Monitor_Service_HTTPS requires OpenSSL'); } $this->Net_Monitor_Service_HTTP(); } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service/SMTP.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service/SMTP.php +++ pear/Net_Monitor/Monitor/Service/SMTP.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: SMTP.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * require and extend the Net_Monitor_Service class */ require_once 'Net/Monitor/Service.php'; /** * reqire and use the Net_SMTP class to check SMTP services */ require_once 'Net/SMTP.php'; /** * class Net_Monitor_Service_SMTP * * A class for checking SMTP (email) services * * @package Net_Monitor * @access public * @see Net_Monitor_Service */ class Net_Monitor_Service_SMTP extends Net_Monitor_Service { /** * @var string _service * @access private */ var $_service = 'SMTP'; /** * @var object _client * @access private */ var $_client = NULL; /** * @var int _last_code * @access private */ var $_last_code = -1; /** * function Net_Monitor_Service_SMTP * * @access public */ function Net_Monitor_Service_SMTP() { $this->_client = new Net_SMTP(); } /** * function check * * Checks the specified SMTP (email delivery) server for availability. * Returns false on success, or a notification array on failure. * * @param mixed host * @return mixed */ function check($host) { $response = 0; $this->_client = new Net_SMTP($host); $c = $this->_client; $e = $c->connect(); if (!PEAR::isError($e)) { //everything is OK $c->disconnect(); $this->_last_code = 200; //set last code to 200 return false; //false signifies no problem } else { //return connection-specific error string $this->_last_code = $response; return array('host' => $host, 'service' => $this->_service, 'message' => $e->getMessage(), 'code' => $response); } } } ?>
http://cvs.php.net/co.php/pear/Net_Monitor/Monitor.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor.php +++ pear/Net_Monitor/Monitor.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: Monitor.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * Net_Monitor * * A unified interface for checking the availability services on external * servers and sending meaningful alerts through a variety of media if a * service becomes unavailable. * * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) * */ /** * Requires the main Pear class */ require_once 'PEAR.php'; /** * class Net_Monitor * * @access public * @package Net_Monitor */ class Net_Monitor { /**#@+ * @access private * @var array */ var $_services = array(); var $_alerts = array(); var $_options = array('state_directory' => '/tmp/', 'state_file' => 'Net_Monitor_State', 'subject_line' => 'Net_Monitor Alert', 'alert_line' => '%h: %s: %m', 'notify_change' => 1, 'notify_ok' => 1 ); var $_clients = array(); var $_alerters = array(); var $_results = array(); var $_results_diff = array(); /**#@-*/ /** * function Net_Monitor * * @access public * @param array services * @param array alerts * @return void */ function Net_Monitor($services=array(),$alerts=array(),$options=array()) { if ($options && sizeof($options) > 0) { $this->setOptions($options); } if ($services && sizeof($services) > 0) { $this->setServices($services); } if ($alerts && sizeof($alerts) > 0) { $this->setAlerts($alerts); } } /** * function setOptions * * <p>Sets additional options for the class</p> * <p>Merges input array with $_options</p> * * @access public * @param array options * @return void */ function setOptions($options) { foreach($options as $key => $value) { $this->_options[$key] = $value; } } /** * function setServices * * <p>Sets the services to monitor for the class</p> * <p>Overwrites $_services with input array</p> * <p>Net_Monitor_Services are of the form: <br /> * <pre> * $services = array('foo.example.com'=>array('SMTP','DNS'), * 'bar.example.com'=>array('HTTP','FTP','DNS')); * </pre> * * @access public * @param array services * @return void */ function setServices($services) { $this->_services = $services; } /** * function setAlerts * * <p>Sets the alerts for the class</p> * <p>Overwrites $_alerts with input array</p> * <p>Net_Monitor_Alerts are of the form: <br /> * <pre> * $alerts = array('User1' => array('SMTP'=>'user1@example.com'), * 'User2' => array('SMTP'=>'user2@example.com')); * </pre> * * @param array alerts * @return void */ function setAlerts($alerts) { $this->_alerts = $alerts; } /** * function checkAll * * Checks all services and sends all alerts. * * @access public * @return void */ function checkAll() { //initialize the _results and _results_diff arrays $this->_results = array(); $this->_results_diff = array(); //check all services and populate the _results array if (is_array($this->_services) && sizeof($this->_services) > 0) { $this->loadClients(); //load client objects once and only once per service foreach($this->_services as $server => $services) { for ($i=0; $i<sizeof($services); $i++) { $service = $services[$i]; $result = $this->check($server,$service); if ($result) { $this->_results[] = $result; } } } } else { pear::raiseError('No services found to check.'); } if (is_array($this->_results) && sizeof($this->_results) > 0) { $last_state = $this->getState(); $this->saveState(); $this->_results_diff = $this->stateDiff($this->_results,$last_state); /* UNCOMMENT THE FOLLOWING TO DEBUG DIFFERENTIALS // print "Last state: \n\n"; // print_r($last_state); // print "Current state: \n\n"; // print_r($this->_results); // print "Difference: \n\n"; // print_r($this->_results_diff); */ } if (is_array($this->_results_diff) && sizeof($this->_results_diff) > 0) { if (is_array($this->_alerts) && sizeof($this->_alerts) > 0) { $this->loadAlerters(); //loop through alerts, sending the result message foreach($this->_alerts as $user => $alert_array) { foreach($alert_array as $method => $server) { $this->alert($method,$server); } } } else { //nobody to alert? print the result to STDOUT $results = $this->_results_diff; $sizeof_results = sizeof($results); for($i=0; $i < $sizeof_results; $i++) { $this->printAlert($results[$i]); } } return true; } else { return false; } } /** * function check * * Check a single service on a single server * * @access public * @param mixed server * @param string service * @return mixed */ function check($server,$service) { $client = $this->_clients[$service]; if (!is_object($client)) { //if an client has not yet been created for this service, create one $this->client[$service] = $this->getClient($service); $client = $this->_clients[$service]; } return $client->check($server); } /** * function loadClients * * Load all clients into the Net_Monitor object so that * each type of service client is only instantiated once. * * @access public * @return void */ function loadClients() { $services_array = array_values($this->_services); $types_array = array_keys($this->_clients); for ($i=0;$i<sizeof($services_array);$i++) { $sub_array = $services_array[$i]; for ($j=0; $j<sizeof($sub_array); $j++) { $type = $sub_array[$j]; if (!in_array($type,$types_array)) { $types_array[] = $type; $this->_clients[$type] = $this->getClient($type); } } } } /** * function loadAlerters * * Load alert objects into Net_Monitor so that each type of alert * is only used once. * * @access public */ function loadAlerters() { $alerts_array = array_values($this->_alerts); $current_alerters = array_keys($this->_alerters); for ($i=0; $i<sizeof($alerts_array); $i++) { $sub_array = $alerts_array[$i]; foreach($sub_array as $alert_type => $method) { if (!in_array($alert_type,$current_alerters)) { $current_alerters[] = $alert_type; $this->_alerters[$alert_type] = $this->getAlerter($alert_type); } } } } /** * function getClient * * Returns a client of the specificed type. <em>Note: * does not check to see if this client has already been * loaded into Net_Monitor::clients - that is handled elsewhere.</em> * * @access private * @param string type * @return void */ function getClient($type) { require_once "Net/Monitor/Service/$type.php"; $service = "Net_Monitor_Service_$type"; return new $service(); } /** * function getAlerter * * Returns an alerter of the specificed type. <em>Note: * does not check to see if this alerter has already been * loaded into Net_Monitor::alerters - that is handled elsewhere.</em> * * @access private * @param string type * @return void * */ function getAlerter($type) { require_once "Net/Monitor/Alert/$type.php"; $alerter = "Net_Monitor_Alert_$type"; return new $alerter(); } /** * function alert * * Send a single alert of the specified method to the specified server * * @access private * @param string method * @param mixed $server * */ function alert($method,$server) { $alerter = $this->_alerters[$method]; if (!is_object($alerter)) { //if an alerter for this method has not already been created, create one $this->_alerters[$method] = $this->getAlerter($method); $alerter = $this->_alerters[$method]; } return $alerter->alert($server,$this->_results_diff,$this->_options); } /** * function saveState * * Saves the current _results array to the directory specified in * _options['state_directory'] as a file named _options['state_file']. * If an array is passed to the function, that array is saved as state, * otherwise this function acts upon $_results. * * @access public * @param array results * @return void * */ function saveState($results = NULL) { $options = $this->_options; $path = $options['state_directory']; $file = $options['state_file']; if (!is_writable($path)) { Pear::raiseError($path.' is not writeable'); } if (file_exists($path.$file)) { if (!is_writable($path.$file)) { Pear::raiseError($path.$file.' exists but is not writeable'); } } $fp = @fopen($path.$file,'w'); if (is_null($results)) { $results = $this->_results; } $line = serialize($results); @fwrite($fp,$line); @fclose($fp); } /** * function getState * * Retrieves previous state information from the directory specified in * _options['state_directory'] via a file named _options['state_file'] * * @access public * @return array * */ function getState() { $my_line = ""; $options = $this->_options; $path = $options['state_directory']; $file = $options['state_file']; if (file_exists($path.$file)) { if (!is_readable($path.$file)) { Pear::raiseError($path.$file.' exists but is not readable'); } } else { return array(); } $fp = @fopen($path.$file,'r'); while(!feof($fp)) { $my_line .= fgets($fp,4096); } $return_array = unserialize($my_line); @fclose($fp); return $return_array; } /** * function stateDiff * * Computes the difference between a primary and secondary * array representing state, i.e. all values in primary that * are not already in secondary. * * Also returns an OK status for values in secondary that are * not in primary unless * $_options['notify_ok'] is set to false. * * Returns values in primary whose code value differs * from values in secondary unless * $_options['notify_change'] is set to false. * * @access private * @param array primary * @param array secondary * @return array */ function stateDiff($primary,$secondary) { $return_array = array(); $sizeof_primary = sizeof($primary); $sizeof_secondary = sizeof($secondary); //loop through primary array for($i=0; $i < $sizeof_primary; $i++) { $primary_sub = $primary[$i]; $ps_host = $primary_sub['host']; $ps_service = $primary_sub['service']; $ps_code = $primary_sub['code']; //reindex secondary in case it has had values removed $secondary = array_values($secondary); $sizeof_secondary = sizeof($secondary); //reset primary flags $primary_sub_is_unique = true; $primary_sub_changed = false; //for each primary value, loop through secondary array for($j = 0; $j < $sizeof_secondary; $j++) { $secondary_sub = $secondary[$j]; $ss_host = $secondary_sub['host']; $ss_service = $secondary_sub['service']; $ss_code = $secondary_sub['code']; if ($ss_host == $ps_host && $ss_service == $ps_service) { //host and service identical in secondary and primary if ($ss_code != $ps_code) { //different codes if ($this->_options['notify_change']) { //notify_change on; move to return $return_array[] = $primary_sub; } //flag this primary_sub as a change, not a unique $primary_sub_changed = true; } else { //code also identical in secondary and primary //therefore this primary_sub is not unique $primary_sub_is_unique = false; } //remove from secondary unset($secondary[$j]); } } if ($primary_sub_is_unique && !$primary_sub_changed) { //after all that checking, primary_sub is unique //but not because it represents a host/service that has //changed from one down state to another; move to return. $return_array[] = $primary_sub; } } if ($this->_options['notify_ok'] && sizeof($secondary) > 0) { //remaining states in secondary are marked OK and added to return reset($secondary); $sizeof_secondary = sizeof($secondary); for($k = 0; $k < $sizeof_secondary; $k++) { $secondary[$k]['code'] = 200; $secondary[$k]['message'] = 'OK'; $return_array[] = $secondary[$k]; } } return $return_array; } /** * function resetState * * Resets the results and results differential arrays * and deletes the state file. * * Returns true if the file has been deleted or never existed * in the first place; false otherwise. * * @access public * @return boolean */ function resetState() { $this->_results = array(); $this->_results_diff = array(); $options = $this->_options; $path = $options['state_directory']; $file = $options['state_file']; if (file_exists($path.$file)) { if (!is_writable($path.$file)) { Pear::raiseError($path.$file.' exists but is not writeable'); } return unlink($path.$file); } else { return true; } } /** * function resetHostState * * Resets the state for a single host. Optionally take in a * second parameter, service, whereby the function only * resets the results for that particular host/service test. * * @param string host * @return void * @access public */ function resetHostState($host,$service = NULL) { $last_state = $this->getState(); $sizeof_last_state = sizeof($last_state); for($i=0; $i < $sizeof_last_state; $i++) { $working_state = $last_state[$i]; if ($working_state['host'] == $host) { if ($service != NULL && $working_state['service'] != $service) { //hosts match but services don't match? place in new array $new_array[] = $working_state; } } else { //different hosts? place in new array $new_array[] = $working_state; } } $this->saveState($new_array); } /** * function printAlert * * Prints the alert specified in the associative array * to STDOUT. Formats the alert according to $_options['alert_line']. * * @param array array * @return void * @access public */ function printAlert($array) { if ($this->_options['alert_line']) { $alert_line = $this->_options['alert_line']; } else { $alert_line = '%h: %s: %m'; } $alert_line = str_replace('%h',$array['host'],$alert_line); $alert_line = str_replace('%s',$array['service'],$alert_line); $alert_line = str_replace('%m',$array['message'],$alert_line); $alert_line = str_replace('%c',$array['code'],$alert_line); print $alert_line."\r\n"; } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Alert.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Alert.php +++ pear/Net_Monitor/Monitor/Alert.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: Alert.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * class Net_Monitor_Alert * * This is the generic alert class * * @package Net_Monitor * @access public * */ class Net_Monitor_Alert { var $_service = 'Generic'; var $_alert = NULL; /** * function alert * * Sends the specified results to the specified server * * @access private * @param mixed server * @param array results * @return mixed */ function alert($server,$results,$options=array()) { print "ALERT: $server ".$this->_service." not yet implemented\n"; print_r($results); } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service.php +++ pear/Net_Monitor/Monitor/Service.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: Service.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * class Net_Monitor_Service * * A generic service monitoring class * * @package Net_Monitor * @access public */ class Net_Monitor_Service { /** * @var string _service * @access private */ var $_service = 'Generic'; /** * @var object _client * @access private */ var $_client = NULL; /** * @var int _last_code * @access private */ var $_last_code = -1; /** * function check * * Checks the specified server for availability. * Returns false on success, or a notification array on failure. * * @param mixed server * @return mixed */ function check($server) { return array('host' => $server, 'service' => $this->_service, 'message' => "not yet implemented", 'code' => -1); } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Alert/SMS.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Alert/SMS.php +++ pear/Net_Monitor/Monitor/Alert/SMS.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: SMS.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * requires and extends the Net_Monitor_Alert class */ require_once 'Net/Monitor/Alert.php'; /** * requires and uses the Net_SMS class for SMS alerts */ require_once 'Net/SMS.php'; /** * class Net_Monitor_Alert_SMS * * @package Net_Monitor * @access public * @see Net_Monitor_Alert */ class Net_Monitor_Alert_SMS extends Net_Monitor_Alert { /** * @var string _service * @access private */ var $_service = 'SMS'; /** * @var object _alert * @access private */ var $_alert = NULL; /** * function Net_Monitor_Alert_SMS * * @access public */ function Net_Monitor_Alert_SMS() { $this->_alert = new Net_SMS(); } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Alert/SMTP.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Alert/SMTP.php +++ pear/Net_Monitor/Monitor/Alert/SMTP.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: SMTP.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * requires and extends the Net_Monitor_Alert class */ require_once 'Net/Monitor/Alert.php'; /** * requires and uses the Net_SMTP class to send SMTP alerts */ require_once 'Net/SMTP.php'; /** * class Net_Monitor_Alert_SMTP * * A class for sending alerts via SMTP (email) * * @package Net_Monitor * @access public * @see Net_Monitor_Alert */ class Net_Monitor_Alert_SMTP extends Net_Monitor_Alert { /** * @var string _service * @access private */ var $_service = 'SMTP'; /** * @var object _alert * @access private */ var $_alert = NULL; /** * function Net_Monitor_Alert_SMTP * * @access public */ function Net_Monitor_Alert_SMTP() { $this->_alert = new Net_SMTP(); } /** * function alert * * Sends the specified results to the specified SMTP server * * @access private * @param mixed server * @param array results * @return mixed */ function alert($server,$result_array,$options=array()) { if ($options['subject_line']) { $subject = $options['subject_line']; } else { Pear::raiseError('Using default subject', NULL, PEAR_ERROR_EXCEPTION, E_USER_WARNING); $subject = 'Net_Monitor Alert'; } $server_array = explode('@',$server); $rcpt = $server_array[0]; $server_addr = $server_array[1]; $this->_client = new Net_SMTP($server_addr); $smtp_client = $this->_client; if ($options['smtp_debug']) { $smtp_client->setDebug(true); } $sizeof_result_array = sizeof($result_array); for($i=0;$i<$sizeof_result_array;$i++) { $result = $result_array[$i]; $host = $result['host']; $service = $result['service']; $message = $result['message']; $code = $result['code']; if ($options['alert_line']) { $alert_line = $options['alert_line']; } else { $alert_line = '%h: %s: %m'; } $alert_line = str_replace('%h',$host,$alert_line); $alert_line = str_replace('%s',$service,$alert_line); $alert_line = str_replace('%m',$message,$alert_line); $alert_line = str_replace('%c',$code,$alert_line); $email_message .= $alert_line."\r\n"; } $smtp_client->quotedata(&$email_message); $e = $smtp_client->connect(); if (!PEAR::isError($e)) { $e = $smtp_client->mailFrom($server); $e = $smtp_client->rcptTo($server); $e = $smtp_client->data('Subject: '.$subject."\r\n\r\n".$email_message); $smtp_client->disconnect(); } if (PEAR::isError($e)) { return $e; } else { return true; } } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service/DNS.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service/DNS.php +++ pear/Net_Monitor/Monitor/Service/DNS.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: DNS.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * require and extend the Net_Monitor_Service class */ require_once 'Net/Monitor/Service.php'; /** * require and use the Net_DNS class to check DNS service */ require_once 'Net/DNS.php'; /** * class Net_Monitor_Service_DNS * * A class for checking DNS services * * @package Net_Monitor * @access public * @see Net_Monitor_Service */ class Net_Monitor_Service_DNS extends Net_Monitor_Service { /** * @var string _service * @access private */ var $_service = 'DNS'; /** * @var object _client * @access private */ var $_client = NULL; /** * @var int _last_code * @access private */ var $_last_code = -1; /** * function Net_Monitor_Service_DNS * * @access public */ function Net_Monitor_Service_DNS() { $this->_client = new Net_DNS_Resolver(); } /** * function check * * Checks the specified DNS server for availability. * Returns false on success, or a notification array on failure. * * @param mixed host * @return mixed */ function check($host) { $c = $this->_client; $c->nameservers = array($host); $e = $c->search($host); if (is_object($e) && $e->answer) { return false; } else { $this->_last_code = 0; return array('host' => $host, 'service' => $this->_service, 'message' => 'no response', 'code' => 0); } } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service/FTP.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service/FTP.php +++ pear/Net_Monitor/Monitor/Service/FTP.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: FTP.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * require and extend the Net_Monitor_Service class */ require_once 'Net/Monitor/Service.php'; /** * require and use the Net_FTP class to check FTP services */ require_once 'Net/FTP.php'; /** * class Net_Monitor_Service_FTP * * A class to check FTP services * * @package Net_Monitor * @access public * @see Net_Monitor_Service */ class Net_Monitor_Service_FTP extends Net_Monitor_Service { /** * @var string _service * @access private */ var $_service = 'FTP'; /** * @var object _client * @access private */ var $_client = NULL; /** * @var int _last_code * @access private */ var $_last_code = -1; /** * function Net_Monitor_Service_FTP * * @access public */ function Net_Monitor_Service_FTP() { $this->_client = new Net_FTP(); } /** * function check * * Checks the specified FTP server for availability. * Returns false on success, or a notification array on failure. * * @param mixed host * @return mixed */ function check($host) { $c = $this->_client; $e = $c->connect($host); if (!Pear::isError($e)) { $this->_last_code = '200'; return false; } else { $this->_last_code = 0; return array('host' => $host, 'service' => $this->_service, 'message' => $e->getMessage(), 'code' => 0); } } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service/HTTP.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service/HTTP.php +++ pear/Net_Monitor/Monitor/Service/HTTP.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: HTTP.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * require and extend the Net_Monitor_Service class */ require_once 'Net/Monitor/Service.php'; /** * require and use the HTTP_Request class to check HTTP services */ require_once 'HTTP/Request.php'; /** * class Net_Monitor_Service_HTTP * * A class to check HTTP (web) services * * @package Net_Monitor * @access public * @see Net_Monitor_Service */ class Net_Monitor_Service_HTTP extends Net_Monitor_Service { /** * @var string _service * @access private */ var $_service = 'HTTP'; /** * @var object _client * @access private */ var $_client = NULL; /** * @var int _last_code * @access private */ var $_last_code = -1; /** * @var string _prefix * @access private */ var $_prefix = 'http://'; /** * function Net_Monitor_Service_HTTP * * @access public */ function Net_Monitor_Service_HTTP() { $this->_client = new HTTP_Request(); } /** * function check * * Checks the specified HTTP (web) server for availability. * Returns false on success, or a notification array on failure. * * @param mixed host * @return mixed */ function check($host) { $response = 0; $full_host = $this->_prefix.$host; $this->_client = new HTTP_Request($full_host); $http_client = $this->_client; $e = $http_client->sendRequest(); if (!PEAR::isError($e)) { $e = $http_client->getResponseBody(); if (!PEAR::isError($e)) { $response = $http_client->getResponseCode(); if ($response != 200) { $e = new PEAR_Error($response); } else { $return = false; } } } if (PEAR::isError($e)) { $msg = $e->getMessage(); if (!$msg) { $msg = 'host not found'; } $return = array('host' => $host, 'service' => $this->_service,'message' => $msg, 'code' => $response); } $this->_last_code = $response; return $return; } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service/HTTPS.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service/HTTPS.php +++ pear/Net_Monitor/Monitor/Service/HTTPS.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: HTTPS.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * require and extend the Net_Monitor_Service_HTTP class by re-implementing the prepare() method */ require_once 'Net/Monitor/HTTP.php'; /** * class Net_Monitor_Service_HTTPS * * A class for checking HTTPS (web over SSL) services * * @package Net_Monitor * @access public * @see Net_Monitor_Service_HTTP */ class Net_Monitor_Service_HTTPS extends Net_Monitor_Service_HTTP { /** * @var string _service * @access private */ var $_service = 'HTTPS'; /** * @var string _prefix * @access public */ var $_prefix = 'https://'; /** * function Net_Monitor_Service_HTTPS * * @access public */ function Net_Monitor_Service_HTTPS() { if (!extension_loaded('openssl')) { PEAR::raiseError('Net_Monitor_Service_HTTPS requires OpenSSL'); } $this->Net_Monitor_Service_HTTP(); } } ?> http://cvs.php.net/co.php/pear/Net_Monitor/Monitor/Service/SMTP.php?r=1.1&p=1 Index: pear/Net_Monitor/Monitor/Service/SMTP.php +++ pear/Net_Monitor/Monitor/Service/SMTP.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // // $Id: SMTP.php,v 1.1 2005/03/29 00:25:58 cyberscribe Exp $ // // Remote service monitor /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt * @version 0.0.6 (proposal) */ /** * require and extend the Net_Monitor_Service class */ require_once 'Net/Monitor/Service.php'; /** * reqire and use the Net_SMTP class to check SMTP services */ require_once 'Net/SMTP.php'; /** * class Net_Monitor_Service_SMTP * * A class for checking SMTP (email) services * * @package Net_Monitor * @access public * @see Net_Monitor_Service */ class Net_Monitor_Service_SMTP extends Net_Monitor_Service { /** * @var string _service * @access private */ var $_service = 'SMTP'; /** * @var object _client * @access private */ var $_client = NULL; /** * @var int _last_code * @access private */ var $_last_code = -1; /** * function Net_Monitor_Service_SMTP * * @access public */ function Net_Monitor_Service_SMTP() { $this->_client = new Net_SMTP(); } /** * function check * * Checks the specified SMTP (email delivery) server for availability. * Returns false on success, or a notification array on failure. * * @param mixed host * @return mixed */ function check($host) { $response = 0; $this->_client = new Net_SMTP($host); $c = $this->_client; $e = $c->connect(); if (!PEAR::isError($e)) { //everything is OK $c->disconnect(); $this->_last_code = 200; //set last code to 200 return false; //false signifies no problem } else { //return connection-specific error string $this->_last_code = $response; return array('host' => $host, 'service' => $this->_service, 'message' => $e->getMessage(), 'code' => $response); } } } ?>