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:36:45 +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-30750@lists.php.net to get a copy of this message | ||
cyberscribe Mon Mar 28 19:36:45 2005 EDT
Modified 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:
Now includes SMS support for clickatell and sms2email gateways
http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor.php diff -u pear/Net_Monitor/Monitor.php:1.4 pear/Net_Monitor/Monitor.php:1.5 --- pear/Net_Monitor/Monitor.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor.php Mon Mar 28 19:36:44 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: Monitor.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: Monitor.php,v 1.5 2005/03/29 00:36:44 cyberscribe Exp $ // // Remote service monitor /** @@ -29,7 +29,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 * */ /** @@ -53,7 +53,7 @@ */ var $_services = array(); /** - * Array of alerts to be sent + * Array of alerts to be sent organized by protocols * * @access private * @var array $_alerts @@ -152,7 +152,7 @@ function setOptions($options) { - foreach($options as $key => $value) { + foreach ($options as $key => $value) { $this->_options[$key] = $value; } } @@ -193,7 +193,14 @@ function setAlerts($alerts) { - $this->_alerts = $alerts; + foreach ($alerts as $user => $parAlert) { + foreach ($parAlert as $proto => $param) { + if (!isset($this->_alerts[$proto])) { + $this->_alerts[$proto] = array(); + } + $this->_alerts[$proto][$user] = $param; + } + } } /** * function checkAll @@ -212,12 +219,11 @@ //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]; + foreach ($this->_services as $server => $services) { + foreach ($services as $service) { $result = $this->check($server,$service); if ($result) { - $this->_results[] = $result; + $this->_results[$server][$service] = $result; } } } @@ -227,7 +233,7 @@ if (is_array($this->_results) && sizeof($this->_results) > 0) { $last_state = $this->getState(); $this->saveState(); - $this->_results_diff = $this->stateDiff($this->_results,$last_state); + $this->_results_diff = $this->stateDiff($last_state); /* UNCOMMENT THE FOLLOWING TO DEBUG DIFFERENTIALS // print "Last state: \n\n"; // print_r($last_state); @@ -242,17 +248,15 @@ 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); - } + foreach ($this->_alerts as $method => $alert_array) { + $this->alert($method, $alert_array); } } 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]); + foreach ($this->_results_diff as $host=>$results) { + foreach ($results as $service=>$result) { + $this->printAlert($host, $service, $result); + } } } return true; @@ -313,15 +317,11 @@ 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); - } + $alerts_array = array_keys($this->_alerts); + foreach ($alerts_array as $alert_type) { + if (!isset($this->_alerters[$alert_type])) { + $this->_alerters[$alert_type] = + & $this->getAlerter($alert_type); } } } @@ -462,68 +462,36 @@ * $_options['notify_change'] is set to false. * * @access private - * @param array primary - * @param array secondary + * @param array $secondary states to compare to current * @return array */ - function stateDiff($primary,$secondary) - - { + function stateDiff($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) { + foreach ($this->_results as $host=>$services) { + foreach ($services as $service=>$result) { + if (isset($secondary[$host][$service])) { + // host and service identical in current and secondary + if ($result[0] !== $secondary[$host][$service][0]) { //different codes - if ($this->_options['notify_change']) { + if($this->_options['notify_change']) { //notify_change on; move to return - $return_array[] = $primary_sub; + $return_array[$host][$service] = $result; } - //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]); + // anyway unset so ok to withdrawn services to the end + unset( $secondary[$host][$service]); + } else { // it's a new host/service so to be announced + $return_array[$host][$service] = $result; } } - 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]; + if($this->_options['notify_ok']) { + foreach ($secondary as $host=>$services) { + foreach ($services as $service=>$result) { + //remaining states in secondary added OK to return + $return_array[$host][$service] = array( 200, $result[1]); + } } } return $return_array; @@ -561,56 +529,56 @@ * function resetHostState * * Resets the state for a single host ($host). Optionally takes in a - * second parameter, $service, whereby the function only - * resets the results for that particular host/service test. + * second parameter, $service which maybe an array, whereby the function only + * resets the results for that/those particular host/service test. * * @param string $host - * @param string $service + * @param mixed $service * @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); - } + $last_state = $this->getState(); + if (!isset($last_state[$host])) { + return; + } + if ($service != null) { + if (is_array($service)) { + foreach ($service as $serelt) { + unset($last_state[$host][$serelt]); + } + } else { + unset($last_state[$host][$service]); + } + } else { + unset($last_state[$host]); + } + $this->saveState($last_state); + } /** * function printAlert * - * Prints the alert specified in the associative array ($array) + * Prints the alert for a host/service * to STDOUT. Formats the alert according to $_options['alert_line']. * - * @param array array + * @param string $host + * @param string $service + * @param array $result (code, message) * @return void * @access public */ - function printAlert($array) - - { + function printAlert($host, $service, $result) { 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"; + print str_replace( + array('%h', '%s', '%c', '%m'), + array($host,$service,$result[0],$result[1]), + $alert_line)."\r\n"; } } ?> http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Alert.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Alert.php diff -u pear/Net_Monitor/Monitor/Alert.php:1.4 pear/Net_Monitor/Monitor/Alert.php:1.5 --- pear/Net_Monitor/Monitor/Alert.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Alert.php Mon Mar 28 19:36:44 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: Alert.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: Alert.php,v 1.5 2005/03/29 00:36:44 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * class Net_Monitor_Alert http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service.php diff -u pear/Net_Monitor/Monitor/Service.php:1.4 pear/Net_Monitor/Monitor/Service.php:1.5 --- pear/Net_Monitor/Monitor/Service.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service.php Mon Mar 28 19:36:44 2005 @@ -16,7 +16,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: Service.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: Service.php,v 1.5 2005/03/29 00:36:44 cyberscribe Exp $ // // Remote service monitor /** @@ -24,7 +24,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * class Net_Monitor_Service @@ -61,7 +61,7 @@ * function check * * Checks the specified server ($server) for availability. - * Returns false on success, or a notification array on failure. + * Returns false on success, or a notification array ( code, message) on failure. * * @param mixed server * @return mixed @@ -69,7 +69,7 @@ function check($server) { - return array('host' => $server, 'service' => $this->_service, 'message' => 'Not yet implemented.', 'code' => -1); + return array( -1, 'not yet implemented'); } } ?> http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Alert/SMS.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Alert/SMS.php diff -u pear/Net_Monitor/Monitor/Alert/SMS.php:1.4 pear/Net_Monitor/Monitor/Alert/SMS.php:1.5 --- pear/Net_Monitor/Monitor/Alert/SMS.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Alert/SMS.php Mon Mar 28 19:36:45 2005 @@ -1,9 +1,8 @@ <?php - // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ -// | Copyright (c) 1997-2004 The PHP Group | +// | Copyright (c) 1997-2005 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 | @@ -16,15 +15,15 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: SMS.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: SMS.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // -// Remote service monitor +// Remote service monitor alerter thru SMS /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> - * @copyright 2004 + * @copyright 2005 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * requires and extends the Net_Monitor_Alert class @@ -67,5 +66,149 @@ { $this->_alert = new Net_SMS(); } -} + /** + * function alert + * + * Sends the alerts thru the specified SMS servers and accounts + * <li> $server is an array of user=>parameter + * where parameter is either an array or string. + * A string should be a phone number (common user) + * An array describes the SMS server as required by Net/SMS.php + * and must contains an extra 'phone_number' key (prioritary user) + * SMS server description (extra or common option['SMS_default']) contains: + * <ul> + * <li> SMS_provider - The server to connect. Mandatory. + * <li> username - The username to use for SMS authentication. Mandatory. + * <li> password - The password to use for SMS authentication. + * <li> ... more, depending upon provider + * </ul> + * <li> $results is the array of results to send + * </ul> + * Returns true on success, PEAR_Error object on failure + * + * @access private + * @param mixed server + * @param array results + * @param array options + * @return mixed true or PEAR_Error + */ + function alert($server,$result_array,$options=array()) + + { + // max size of a message + $max = 160; + // construct $SMS_message from $result_array + // prepare once the template + if (isset($options['sms_line'])) { + $alert_line = $options['sms_line']; + } else { + $alert_line = '%h(%s)>%c '; + } + // store each message case total > $max + $SMS_array = array(); + // buid one message + $SMS_message = ''; + foreach ($result_array as $host=>$services) { + foreach ($services as $service=>$result) { + // insert the values + $out = str_replace( + array('%h', '%s', '%c', '%m'), + array($host,$service,$result[0],$result[1]), + $alert_line)."\r\n"; + // result line too long, store old, cut to max and store + if (strlen($out) >= $max) { + if ($SMS_message) { + $SMS_array[] = $SMS_message; + $SMS_message = ''; + } + $SMS_array[] = substr($out, 0, $max); + } else { + // it will become too long, sore old first + if ((strlen($SMS_message) + strlen($out)) > $max) { + $SMS_array[] = $SMS_message; + $SMS_message = $out; + } else { + $SMS_message .= $out; + } + } + } + } + // rest in buffer ? + if ($SMS_message) { + $SMS_array[] = $SMS_message; + } + if (!$SMS_array) { // nothing to do + return true; + } + + //parse $server to group by SMS provider/subscriber and prepare params + $toSend = $accPar = array(); + foreach ($server as $where) { + // only phone as string specified ? => to cumulate + if (is_string($where)) { + $where = array_merge($options['SMS_default'], + array('phone_number' => $where)); + } elseif (!is_array($where)) { + PEAR::raiseError( + 'user param is not a string or array -- unable to send alert'); + return false; + } + $SMS_provider = $where['SMS_provider']; + $username = $where['username']; + // first time for this provider + if (!array_key_exists ($SMS_provider, $toSend)) { + $toSend[$SMS_provider] = array(); + $accPar[$SMS_provider] = array(); + } + // first time for this subscriber by this provider + if (!array_key_exists ($username, $toSend[$SMS_provider])) { + $toSend[$SMS_provider][$username] = array(); + // to ensure future compatibility with Net_SMS take everything + $accPar[$SMS_provider][$username] = $where; + unset($accPar[$SMS_provider][$username]['phone_number']); + unset($accPar[$SMS_provider][$username]['SMS_provider']); + unset($accPar[$SMS_provider][$username]['username']); + $accPar[$SMS_provider][$username]['user'] = $username; + } + // store the SMS destination + $toSend[$SMS_provider][$username][] = $where['phone_number']; + } + $SMS = array(); + if (isset($options['sms_from'])) { + $SMS['from'] = $options['sms_from']; + } else { + $SMS['from'] = 'Net_Monitor'; + } + // loop on providers + foreach ($toSend as $SMS_provider => $sublist) { + // loop on subscribers + foreach ($sublist as $username => $toList) { + $SMS['to'] = $toList; + if (isset($options['sms_debug']) and $options['sms_debug']) { + echo "{$username} by {$SMS_provider}\n"; + print_r($SMS); + print_r($accPar[$SMS_provider][$username]); + continue; + } + $sender =& Net_SMS::factory($SMS_provider, + $accPar[$SMS_provider][$username]); + if (PEAR::isError($sender)) { + return $sender; + } + $i = 1; + foreach ($SMS_array as $SMS_message) { + $SMS['id'] = $i; + $SMS['text'] = $SMS_message; + //send message and return result + $e = $sender->send($SMS); + if (PEAR::isError($e)) { + return $e; + } + } + + } + } + return true; + } // alert() +} // end class Net_Monitor_Alert_SMS ?> http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Alert/SMTP.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Alert/SMTP.php diff -u pear/Net_Monitor/Monitor/Alert/SMTP.php:1.4 pear/Net_Monitor/Monitor/Alert/SMTP.php:1.5 --- pear/Net_Monitor/Monitor/Alert/SMTP.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Alert/SMTP.php Mon Mar 28 19:36:45 2005 @@ -16,7 +16,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: SMTP.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: SMTP.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -24,7 +24,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * requires and extends the Net_Monitor_Alert class @@ -72,11 +72,14 @@ /** * function alert * - * Sends the specified results to the specified SMTP server - * <ul> - * <li> $server is the SMTP server as either an array or string. - * A string should be a fully qualified email address - * An array must contain an 'email' key and, optionally: + * Sends the alerts to the specified SMTP servers + * First each dedicaded prioritary user (with own smtp parameters as array) + * <li> $server is an array of user=>parameter + * where parameter is either an array or string. + * A string should be a fully qualified email address (common user) + * An array describes the SMTP server as required my Mail/smtp.php + * and must contain an extra 'email' key (prioritary user) + * the SMTP server description contains optionally: * <ul> * <li> host - The server to connect. Default is localhost * <li> port - The port to connect. Default is 25 @@ -96,82 +99,118 @@ function alert($server,$result_array,$options=array()) { - $email = ''; + //construct $email_message from $result_array $email_message = ''; + if (isset($options['alert_line'])) { + $model_line = $options['alert_line']; + } else { + $model_line = '%h: %s: %m'; + } + foreach ($result_array as $host=>$services) { + foreach ($services as $service=>$result) { + $email_message .= str_replace( + array('%h', '%s', '%c', '%m'), + array($host,$service,$result[0],$result[1]), + $model_line)."\r\n"; + } + } + if (!$email_message) { // nothing to do + return true; + } + + //construct header's subject and from $headers = array(); - - //construct subject if ($options['subject_line']) { - $subject = $options['subject_line']; + $headers['Subject'] = $options['subject_line']; } else { - PEAR::raiseError('Using default subject', null, PEAR_ERROR_EXCEPTION, E_USER_WARNING); - $subject = 'Net_Monitor Alert'; + PEAR::raiseError('Using default subject', + null, PEAR_ERROR_EXCEPTION, E_USER_WARNING); + $headers['Subject'] = 'Net_Monitor Alert'; + } + if (isset($options['from_email'])) { + $headers['From'] = $options['from_email']; + } else { + $headers['From'] = ''; } - $headers['Subject'] = $subject; - //parse $server into $email and $params and - //set $this->_alert to the new Mail::factory() object - if (is_string($server)) { - $email = $server; - $this->_alert =& Mail::factory('smtp'); - } else if (is_array($server)) { + //parse $server to proceed prioritary ones and cumulate the others + $common = array(); + $email = ''; + foreach ($server as $user=>$where) { + // only email as string specified ? => to cumulate + if (is_string($where)) { + $common[] = $where; + continue; + } + if (!is_array($where)) { + PEAR::raiseError( + '$server is not a string or array -- unable to send alert'); + return false; + } $params = array(); - $email = $server['email']; - if (is_string($server['host'])) { - $params['host'] = $server['host']; - } - if (is_int($server['port'])) { - $params['port'] = $server['port']; - } - if ($server['auth']) { - $params['auth'] = $server['auth']; - if (is_string($server['username'])) { - $params['username'] = $server['username']; + $email = $where['email']; + if (is_string($where['host'])) { + $params['host'] = $where['host']; + } + if (is_int($where['port'])) { + $params['port'] = $where['port']; + } + if ($where['auth']) { + $params['auth'] = $where['auth']; + if (is_string($where['username'])) { + $params['username'] = $where['username']; } - if (is_string($server['password'])) { - $params['password'] = $server['password']; + if (is_string($where['password'])) { + $params['password'] = $where['password']; } } if (sizeof($params) > 0) { - $this->_alert =& Mail::factory('smtp',$params); + //send message and return result error if any + $e = $this->sendAlert($email, $params, $headers, $email_message); + if (PEAR::isError($e)) { + return $e; + } } else { - $this->_alert =& Mail::factory('smtp'); + // it was only 'email' given => as normal user + $common[] = $email; } - } else { - PEAR::raiseEror('$server is not a string or array -- unable to send alert'); - return false; + } + + if (!$common) { // nothing left to do + return true; + } + //send message to normal users and return result + $params = array(); + if (isset($options['SMTP_default'])) { + $params = $options['SMTP_default']; + } + return $this->sendAlert($common, $params, $headers, $email_message); + } + /** + * function sendAlert + * + * Sends the specified results to the specified SMTP server + * Returns true on success, PEAR_Error object on failure + * + * @access private + * @param mixed email(s) address(es) + * @param array smtp server parameters + * @param array headers + * @param string message + * @return mixed + */ + function sendAlert($email, $params, $headers, $email_message) + + { + $mailer =& Mail::factory('smtp',$params); + if (PEAR::isError($mailer)) { + return $mailer; } $headers['To'] = $email; - $headers['From'] = $email; $smtp_client =& $this->_alert; - - //construct $email_message from $result_array - $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"; - } - //send message and retun result - $e = $smtp_client->send($email, $headers, $email_message); - if (PEAR::isError($e)) { - return $e; - } else { - return true; - } + //send message and return result + return $mailer->send($email, $headers, $email_message); } } ?> http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service/DNS.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service/DNS.php diff -u pear/Net_Monitor/Monitor/Service/DNS.php:1.4 pear/Net_Monitor/Monitor/Service/DNS.php:1.5 --- pear/Net_Monitor/Monitor/Service/DNS.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service/DNS.php Mon Mar 28 19:36:45 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: DNS.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: DNS.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** @@ -95,7 +95,7 @@ return false; } else { $this->_last_code = 0; - return array('host' => $host, 'service' => $this->_service, 'message' => 'no response', 'code' => 0); + return array(0, 'no response'); } } } http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service/FTP.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service/FTP.php diff -u pear/Net_Monitor/Monitor/Service/FTP.php:1.4 pear/Net_Monitor/Monitor/Service/FTP.php:1.5 --- pear/Net_Monitor/Monitor/Service/FTP.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service/FTP.php Mon Mar 28 19:36:45 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: FTP.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: FTP.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** @@ -95,7 +95,7 @@ return false; } else { $this->_last_code = 0; - return array('host' => $host, 'service' => $this->_service, 'message' => $e->getMessage(), 'code' => 0); + return array(0, $e->getMessage()); } } } http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service/HTTP.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service/HTTP.php diff -u pear/Net_Monitor/Monitor/Service/HTTP.php:1.4 pear/Net_Monitor/Monitor/Service/HTTP.php:1.5 --- pear/Net_Monitor/Monitor/Service/HTTP.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service/HTTP.php Mon Mar 28 19:36:45 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: HTTP.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: HTTP.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * require and extend the Net_Monitor_Service class @@ -113,7 +113,7 @@ if (!$msg) { $msg = 'host not found'; } - $return = array('host' => $host, 'service' => $this->_service,'message' => $msg, 'code' => $response); + $return = array($response, $msg); } $this->_last_code = $response; return $return; http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service/HTTPS.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service/HTTPS.php diff -u pear/Net_Monitor/Monitor/Service/HTTPS.php:1.4 pear/Net_Monitor/Monitor/Service/HTTPS.php:1.5 --- pear/Net_Monitor/Monitor/Service/HTTPS.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service/HTTPS.php Mon Mar 28 19:36:45 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: HTTPS.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: HTTPS.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * require and extend the Net_Monitor_Service_HTTP class by re-implementing the prepare() method http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service/SMTP.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service/SMTP.php diff -u pear/Net_Monitor/Monitor/Service/SMTP.php:1.4 pear/Net_Monitor/Monitor/Service/SMTP.php:1.5 --- pear/Net_Monitor/Monitor/Service/SMTP.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service/SMTP.php Mon Mar 28 19:36:45 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: SMTP.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: SMTP.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * require and extend the Net_Monitor_Service class @@ -94,7 +94,7 @@ if (PEAR::isError($e)) { //return connection-specific error string $this->_last_code = $response; - return array('host' => $host, 'service' => $this->_service, 'message' => $e->getMessage(), 'code' => $response); + return array($response, $e->getMessage()); } else { //everything is OK $c->disconnect();
http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor.php diff -u pear/Net_Monitor/Monitor.php:1.4 pear/Net_Monitor/Monitor.php:1.5 --- pear/Net_Monitor/Monitor.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor.php Mon Mar 28 19:36:44 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: Monitor.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: Monitor.php,v 1.5 2005/03/29 00:36:44 cyberscribe Exp $ // // Remote service monitor /** @@ -29,7 +29,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 * */ /** @@ -53,7 +53,7 @@ */ var $_services = array(); /** - * Array of alerts to be sent + * Array of alerts to be sent organized by protocols * * @access private * @var array $_alerts @@ -152,7 +152,7 @@ function setOptions($options) { - foreach($options as $key => $value) { + foreach ($options as $key => $value) { $this->_options[$key] = $value; } } @@ -193,7 +193,14 @@ function setAlerts($alerts) { - $this->_alerts = $alerts; + foreach ($alerts as $user => $parAlert) { + foreach ($parAlert as $proto => $param) { + if (!isset($this->_alerts[$proto])) { + $this->_alerts[$proto] = array(); + } + $this->_alerts[$proto][$user] = $param; + } + } } /** * function checkAll @@ -212,12 +219,11 @@ //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]; + foreach ($this->_services as $server => $services) { + foreach ($services as $service) { $result = $this->check($server,$service); if ($result) { - $this->_results[] = $result; + $this->_results[$server][$service] = $result; } } } @@ -227,7 +233,7 @@ if (is_array($this->_results) && sizeof($this->_results) > 0) { $last_state = $this->getState(); $this->saveState(); - $this->_results_diff = $this->stateDiff($this->_results,$last_state); + $this->_results_diff = $this->stateDiff($last_state); /* UNCOMMENT THE FOLLOWING TO DEBUG DIFFERENTIALS // print "Last state: \n\n"; // print_r($last_state); @@ -242,17 +248,15 @@ 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); - } + foreach ($this->_alerts as $method => $alert_array) { + $this->alert($method, $alert_array); } } 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]); + foreach ($this->_results_diff as $host=>$results) { + foreach ($results as $service=>$result) { + $this->printAlert($host, $service, $result); + } } } return true; @@ -313,15 +317,11 @@ 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); - } + $alerts_array = array_keys($this->_alerts); + foreach ($alerts_array as $alert_type) { + if (!isset($this->_alerters[$alert_type])) { + $this->_alerters[$alert_type] = + & $this->getAlerter($alert_type); } } } @@ -462,68 +462,36 @@ * $_options['notify_change'] is set to false. * * @access private - * @param array primary - * @param array secondary + * @param array $secondary states to compare to current * @return array */ - function stateDiff($primary,$secondary) - - { + function stateDiff($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) { + foreach ($this->_results as $host=>$services) { + foreach ($services as $service=>$result) { + if (isset($secondary[$host][$service])) { + // host and service identical in current and secondary + if ($result[0] !== $secondary[$host][$service][0]) { //different codes - if ($this->_options['notify_change']) { + if($this->_options['notify_change']) { //notify_change on; move to return - $return_array[] = $primary_sub; + $return_array[$host][$service] = $result; } - //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]); + // anyway unset so ok to withdrawn services to the end + unset( $secondary[$host][$service]); + } else { // it's a new host/service so to be announced + $return_array[$host][$service] = $result; } } - 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]; + if($this->_options['notify_ok']) { + foreach ($secondary as $host=>$services) { + foreach ($services as $service=>$result) { + //remaining states in secondary added OK to return + $return_array[$host][$service] = array( 200, $result[1]); + } } } return $return_array; @@ -561,56 +529,56 @@ * function resetHostState * * Resets the state for a single host ($host). Optionally takes in a - * second parameter, $service, whereby the function only - * resets the results for that particular host/service test. + * second parameter, $service which maybe an array, whereby the function only + * resets the results for that/those particular host/service test. * * @param string $host - * @param string $service + * @param mixed $service * @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); - } + $last_state = $this->getState(); + if (!isset($last_state[$host])) { + return; + } + if ($service != null) { + if (is_array($service)) { + foreach ($service as $serelt) { + unset($last_state[$host][$serelt]); + } + } else { + unset($last_state[$host][$service]); + } + } else { + unset($last_state[$host]); + } + $this->saveState($last_state); + } /** * function printAlert * - * Prints the alert specified in the associative array ($array) + * Prints the alert for a host/service * to STDOUT. Formats the alert according to $_options['alert_line']. * - * @param array array + * @param string $host + * @param string $service + * @param array $result (code, message) * @return void * @access public */ - function printAlert($array) - - { + function printAlert($host, $service, $result) { 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"; + print str_replace( + array('%h', '%s', '%c', '%m'), + array($host,$service,$result[0],$result[1]), + $alert_line)."\r\n"; } } ?> http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Alert.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Alert.php diff -u pear/Net_Monitor/Monitor/Alert.php:1.4 pear/Net_Monitor/Monitor/Alert.php:1.5 --- pear/Net_Monitor/Monitor/Alert.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Alert.php Mon Mar 28 19:36:44 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: Alert.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: Alert.php,v 1.5 2005/03/29 00:36:44 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * class Net_Monitor_Alert http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service.php diff -u pear/Net_Monitor/Monitor/Service.php:1.4 pear/Net_Monitor/Monitor/Service.php:1.5 --- pear/Net_Monitor/Monitor/Service.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service.php Mon Mar 28 19:36:44 2005 @@ -16,7 +16,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: Service.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: Service.php,v 1.5 2005/03/29 00:36:44 cyberscribe Exp $ // // Remote service monitor /** @@ -24,7 +24,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * class Net_Monitor_Service @@ -61,7 +61,7 @@ * function check * * Checks the specified server ($server) for availability. - * Returns false on success, or a notification array on failure. + * Returns false on success, or a notification array ( code, message) on failure. * * @param mixed server * @return mixed @@ -69,7 +69,7 @@ function check($server) { - return array('host' => $server, 'service' => $this->_service, 'message' => 'Not yet implemented.', 'code' => -1); + return array( -1, 'not yet implemented'); } } ?> http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Alert/SMS.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Alert/SMS.php diff -u pear/Net_Monitor/Monitor/Alert/SMS.php:1.4 pear/Net_Monitor/Monitor/Alert/SMS.php:1.5 --- pear/Net_Monitor/Monitor/Alert/SMS.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Alert/SMS.php Mon Mar 28 19:36:45 2005 @@ -1,9 +1,8 @@ <?php - // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ -// | Copyright (c) 1997-2004 The PHP Group | +// | Copyright (c) 1997-2005 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 | @@ -16,15 +15,15 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: SMS.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: SMS.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // -// Remote service monitor +// Remote service monitor alerter thru SMS /** * @package Net_Monitor * @author Robert Peake <robert@peakepro.com> - * @copyright 2004 + * @copyright 2005 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * requires and extends the Net_Monitor_Alert class @@ -67,5 +66,149 @@ { $this->_alert = new Net_SMS(); } -} + /** + * function alert + * + * Sends the alerts thru the specified SMS servers and accounts + * <li> $server is an array of user=>parameter + * where parameter is either an array or string. + * A string should be a phone number (common user) + * An array describes the SMS server as required by Net/SMS.php + * and must contains an extra 'phone_number' key (prioritary user) + * SMS server description (extra or common option['SMS_default']) contains: + * <ul> + * <li> SMS_provider - The server to connect. Mandatory. + * <li> username - The username to use for SMS authentication. Mandatory. + * <li> password - The password to use for SMS authentication. + * <li> ... more, depending upon provider + * </ul> + * <li> $results is the array of results to send + * </ul> + * Returns true on success, PEAR_Error object on failure + * + * @access private + * @param mixed server + * @param array results + * @param array options + * @return mixed true or PEAR_Error + */ + function alert($server,$result_array,$options=array()) + + { + // max size of a message + $max = 160; + // construct $SMS_message from $result_array + // prepare once the template + if (isset($options['sms_line'])) { + $alert_line = $options['sms_line']; + } else { + $alert_line = '%h(%s)>%c '; + } + // store each message case total > $max + $SMS_array = array(); + // buid one message + $SMS_message = ''; + foreach ($result_array as $host=>$services) { + foreach ($services as $service=>$result) { + // insert the values + $out = str_replace( + array('%h', '%s', '%c', '%m'), + array($host,$service,$result[0],$result[1]), + $alert_line)."\r\n"; + // result line too long, store old, cut to max and store + if (strlen($out) >= $max) { + if ($SMS_message) { + $SMS_array[] = $SMS_message; + $SMS_message = ''; + } + $SMS_array[] = substr($out, 0, $max); + } else { + // it will become too long, sore old first + if ((strlen($SMS_message) + strlen($out)) > $max) { + $SMS_array[] = $SMS_message; + $SMS_message = $out; + } else { + $SMS_message .= $out; + } + } + } + } + // rest in buffer ? + if ($SMS_message) { + $SMS_array[] = $SMS_message; + } + if (!$SMS_array) { // nothing to do + return true; + } + + //parse $server to group by SMS provider/subscriber and prepare params + $toSend = $accPar = array(); + foreach ($server as $where) { + // only phone as string specified ? => to cumulate + if (is_string($where)) { + $where = array_merge($options['SMS_default'], + array('phone_number' => $where)); + } elseif (!is_array($where)) { + PEAR::raiseError( + 'user param is not a string or array -- unable to send alert'); + return false; + } + $SMS_provider = $where['SMS_provider']; + $username = $where['username']; + // first time for this provider + if (!array_key_exists ($SMS_provider, $toSend)) { + $toSend[$SMS_provider] = array(); + $accPar[$SMS_provider] = array(); + } + // first time for this subscriber by this provider + if (!array_key_exists ($username, $toSend[$SMS_provider])) { + $toSend[$SMS_provider][$username] = array(); + // to ensure future compatibility with Net_SMS take everything + $accPar[$SMS_provider][$username] = $where; + unset($accPar[$SMS_provider][$username]['phone_number']); + unset($accPar[$SMS_provider][$username]['SMS_provider']); + unset($accPar[$SMS_provider][$username]['username']); + $accPar[$SMS_provider][$username]['user'] = $username; + } + // store the SMS destination + $toSend[$SMS_provider][$username][] = $where['phone_number']; + } + $SMS = array(); + if (isset($options['sms_from'])) { + $SMS['from'] = $options['sms_from']; + } else { + $SMS['from'] = 'Net_Monitor'; + } + // loop on providers + foreach ($toSend as $SMS_provider => $sublist) { + // loop on subscribers + foreach ($sublist as $username => $toList) { + $SMS['to'] = $toList; + if (isset($options['sms_debug']) and $options['sms_debug']) { + echo "{$username} by {$SMS_provider}\n"; + print_r($SMS); + print_r($accPar[$SMS_provider][$username]); + continue; + } + $sender =& Net_SMS::factory($SMS_provider, + $accPar[$SMS_provider][$username]); + if (PEAR::isError($sender)) { + return $sender; + } + $i = 1; + foreach ($SMS_array as $SMS_message) { + $SMS['id'] = $i; + $SMS['text'] = $SMS_message; + //send message and return result + $e = $sender->send($SMS); + if (PEAR::isError($e)) { + return $e; + } + } + + } + } + return true; + } // alert() +} // end class Net_Monitor_Alert_SMS ?> http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Alert/SMTP.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Alert/SMTP.php diff -u pear/Net_Monitor/Monitor/Alert/SMTP.php:1.4 pear/Net_Monitor/Monitor/Alert/SMTP.php:1.5 --- pear/Net_Monitor/Monitor/Alert/SMTP.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Alert/SMTP.php Mon Mar 28 19:36:45 2005 @@ -16,7 +16,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: SMTP.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: SMTP.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -24,7 +24,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * requires and extends the Net_Monitor_Alert class @@ -72,11 +72,14 @@ /** * function alert * - * Sends the specified results to the specified SMTP server - * <ul> - * <li> $server is the SMTP server as either an array or string. - * A string should be a fully qualified email address - * An array must contain an 'email' key and, optionally: + * Sends the alerts to the specified SMTP servers + * First each dedicaded prioritary user (with own smtp parameters as array) + * <li> $server is an array of user=>parameter + * where parameter is either an array or string. + * A string should be a fully qualified email address (common user) + * An array describes the SMTP server as required my Mail/smtp.php + * and must contain an extra 'email' key (prioritary user) + * the SMTP server description contains optionally: * <ul> * <li> host - The server to connect. Default is localhost * <li> port - The port to connect. Default is 25 @@ -96,82 +99,118 @@ function alert($server,$result_array,$options=array()) { - $email = ''; + //construct $email_message from $result_array $email_message = ''; + if (isset($options['alert_line'])) { + $model_line = $options['alert_line']; + } else { + $model_line = '%h: %s: %m'; + } + foreach ($result_array as $host=>$services) { + foreach ($services as $service=>$result) { + $email_message .= str_replace( + array('%h', '%s', '%c', '%m'), + array($host,$service,$result[0],$result[1]), + $model_line)."\r\n"; + } + } + if (!$email_message) { // nothing to do + return true; + } + + //construct header's subject and from $headers = array(); - - //construct subject if ($options['subject_line']) { - $subject = $options['subject_line']; + $headers['Subject'] = $options['subject_line']; } else { - PEAR::raiseError('Using default subject', null, PEAR_ERROR_EXCEPTION, E_USER_WARNING); - $subject = 'Net_Monitor Alert'; + PEAR::raiseError('Using default subject', + null, PEAR_ERROR_EXCEPTION, E_USER_WARNING); + $headers['Subject'] = 'Net_Monitor Alert'; + } + if (isset($options['from_email'])) { + $headers['From'] = $options['from_email']; + } else { + $headers['From'] = ''; } - $headers['Subject'] = $subject; - //parse $server into $email and $params and - //set $this->_alert to the new Mail::factory() object - if (is_string($server)) { - $email = $server; - $this->_alert =& Mail::factory('smtp'); - } else if (is_array($server)) { + //parse $server to proceed prioritary ones and cumulate the others + $common = array(); + $email = ''; + foreach ($server as $user=>$where) { + // only email as string specified ? => to cumulate + if (is_string($where)) { + $common[] = $where; + continue; + } + if (!is_array($where)) { + PEAR::raiseError( + '$server is not a string or array -- unable to send alert'); + return false; + } $params = array(); - $email = $server['email']; - if (is_string($server['host'])) { - $params['host'] = $server['host']; - } - if (is_int($server['port'])) { - $params['port'] = $server['port']; - } - if ($server['auth']) { - $params['auth'] = $server['auth']; - if (is_string($server['username'])) { - $params['username'] = $server['username']; + $email = $where['email']; + if (is_string($where['host'])) { + $params['host'] = $where['host']; + } + if (is_int($where['port'])) { + $params['port'] = $where['port']; + } + if ($where['auth']) { + $params['auth'] = $where['auth']; + if (is_string($where['username'])) { + $params['username'] = $where['username']; } - if (is_string($server['password'])) { - $params['password'] = $server['password']; + if (is_string($where['password'])) { + $params['password'] = $where['password']; } } if (sizeof($params) > 0) { - $this->_alert =& Mail::factory('smtp',$params); + //send message and return result error if any + $e = $this->sendAlert($email, $params, $headers, $email_message); + if (PEAR::isError($e)) { + return $e; + } } else { - $this->_alert =& Mail::factory('smtp'); + // it was only 'email' given => as normal user + $common[] = $email; } - } else { - PEAR::raiseEror('$server is not a string or array -- unable to send alert'); - return false; + } + + if (!$common) { // nothing left to do + return true; + } + //send message to normal users and return result + $params = array(); + if (isset($options['SMTP_default'])) { + $params = $options['SMTP_default']; + } + return $this->sendAlert($common, $params, $headers, $email_message); + } + /** + * function sendAlert + * + * Sends the specified results to the specified SMTP server + * Returns true on success, PEAR_Error object on failure + * + * @access private + * @param mixed email(s) address(es) + * @param array smtp server parameters + * @param array headers + * @param string message + * @return mixed + */ + function sendAlert($email, $params, $headers, $email_message) + + { + $mailer =& Mail::factory('smtp',$params); + if (PEAR::isError($mailer)) { + return $mailer; } $headers['To'] = $email; - $headers['From'] = $email; $smtp_client =& $this->_alert; - - //construct $email_message from $result_array - $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"; - } - //send message and retun result - $e = $smtp_client->send($email, $headers, $email_message); - if (PEAR::isError($e)) { - return $e; - } else { - return true; - } + //send message and return result + return $mailer->send($email, $headers, $email_message); } } ?> http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service/DNS.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service/DNS.php diff -u pear/Net_Monitor/Monitor/Service/DNS.php:1.4 pear/Net_Monitor/Monitor/Service/DNS.php:1.5 --- pear/Net_Monitor/Monitor/Service/DNS.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service/DNS.php Mon Mar 28 19:36:45 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: DNS.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: DNS.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** @@ -95,7 +95,7 @@ return false; } else { $this->_last_code = 0; - return array('host' => $host, 'service' => $this->_service, 'message' => 'no response', 'code' => 0); + return array(0, 'no response'); } } } http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service/FTP.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service/FTP.php diff -u pear/Net_Monitor/Monitor/Service/FTP.php:1.4 pear/Net_Monitor/Monitor/Service/FTP.php:1.5 --- pear/Net_Monitor/Monitor/Service/FTP.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service/FTP.php Mon Mar 28 19:36:45 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: FTP.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: FTP.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** @@ -95,7 +95,7 @@ return false; } else { $this->_last_code = 0; - return array('host' => $host, 'service' => $this->_service, 'message' => $e->getMessage(), 'code' => 0); + return array(0, $e->getMessage()); } } } http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service/HTTP.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service/HTTP.php diff -u pear/Net_Monitor/Monitor/Service/HTTP.php:1.4 pear/Net_Monitor/Monitor/Service/HTTP.php:1.5 --- pear/Net_Monitor/Monitor/Service/HTTP.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service/HTTP.php Mon Mar 28 19:36:45 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: HTTP.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: HTTP.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * require and extend the Net_Monitor_Service class @@ -113,7 +113,7 @@ if (!$msg) { $msg = 'host not found'; } - $return = array('host' => $host, 'service' => $this->_service,'message' => $msg, 'code' => $response); + $return = array($response, $msg); } $this->_last_code = $response; return $return; http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service/HTTPS.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service/HTTPS.php diff -u pear/Net_Monitor/Monitor/Service/HTTPS.php:1.4 pear/Net_Monitor/Monitor/Service/HTTPS.php:1.5 --- pear/Net_Monitor/Monitor/Service/HTTPS.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service/HTTPS.php Mon Mar 28 19:36:45 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: HTTPS.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: HTTPS.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * require and extend the Net_Monitor_Service_HTTP class by re-implementing the prepare() method http://cvs.php.net/diff.php/pear/Net_Monitor/Monitor/Service/SMTP.php?r1=1.4&r2=1.5&ty=u Index: pear/Net_Monitor/Monitor/Service/SMTP.php diff -u pear/Net_Monitor/Monitor/Service/SMTP.php:1.4 pear/Net_Monitor/Monitor/Service/SMTP.php:1.5 --- pear/Net_Monitor/Monitor/Service/SMTP.php:1.4 Mon Mar 28 19:33:49 2005 +++ pear/Net_Monitor/Monitor/Service/SMTP.php Mon Mar 28 19:36:45 2005 @@ -15,7 +15,7 @@ // | Author: Robert Peake <robert@peakepro.com> | // +----------------------------------------------------------------------+ // -// $Id: SMTP.php,v 1.4 2005/03/29 00:33:49 cyberscribe Exp $ +// $Id: SMTP.php,v 1.5 2005/03/29 00:36:45 cyberscribe Exp $ // // Remote service monitor /** @@ -23,7 +23,7 @@ * @author Robert Peake <robert@peakepro.com> * @copyright 2004 * @license http://www.php.net/license/3_0.txt - * @version 0.1.0 + * @version 0.2.0 */ /** * require and extend the Net_Monitor_Service class @@ -94,7 +94,7 @@ if (PEAR::isError($e)) { //return connection-specific error string $this->_last_code = $response; - return array('host' => $host, 'service' => $this->_service, 'message' => $e->getMessage(), 'code' => $response); + return array($response, $e->getMessage()); } else { //everything is OK $c->disconnect();