コード例 #1
0
 /**
  * Check if the server id exists
  * @param int $server_id
  * @return boolean
  * @throws \InvalidArgumentException
  */
 public function serverId($server_id)
 {
     $server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array('server_id' => $server_id), array('server_id'));
     if (empty($server)) {
         throw new \InvalidArgumentException('server_no_match');
     }
     return true;
 }
コード例 #2
0
 /**
  * The function its all about. This one checks whether the given ip and port are up and running!
  * If the server check fails it will try one more time, depending on the $max_runs.
  *
  * Please note: if the server is down but has not met the warning threshold, this will return true
  * to avoid any "we are down" events.
  * @param int $server_id
  * @param int $max_runs how many times should the script recheck the server if unavailable. default is 2
  * @return boolean TRUE if server is up, FALSE otherwise
  */
 public function update($server_id, $max_runs = 2)
 {
     $this->server_id = $server_id;
     $this->error = '';
     $this->rtime = '';
     // get server info from db
     $this->server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array('server_id' => $server_id), array('server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'active', 'warning_threshold', 'warning_threshold_counter', 'timeout', 'website_username', 'website_password'));
     if (empty($this->server)) {
         return false;
     }
     switch ($this->server['type']) {
         case 'service':
             $this->status_new = $this->updateService($max_runs);
             break;
         case 'website':
             $this->status_new = $this->updateWebsite($max_runs);
             break;
     }
     // update server status
     $save = array('last_check' => date('Y-m-d H:i:s'), 'error' => $this->error, 'rtime' => $this->rtime);
     // log the uptime before checking the warning threshold,
     // so that the warnings can still be reviewed in the server history.
     psm_log_uptime($this->server_id, (int) $this->status_new, $this->rtime);
     if ($this->status_new == true) {
         // if the server is on, add the last_online value and reset the error threshold counter
         $save['status'] = 'on';
         $save['last_online'] = date('Y-m-d H:i:s');
         $save['warning_threshold_counter'] = 0;
     } else {
         // server is offline, increase the error counter
         $save['warning_threshold_counter'] = $this->server['warning_threshold_counter'] + 1;
         if ($save['warning_threshold_counter'] < $this->server['warning_threshold']) {
             // the server is offline but the error threshold has not been met yet.
             // so we are going to leave the status "on" for now while we are in a sort of warning state..
             $save['status'] = 'on';
             $this->status_new = true;
         } else {
             $save['status'] = 'off';
         }
     }
     $this->db->save(PSM_DB_PREFIX . 'servers', $save, array('server_id' => $this->server_id));
     return $this->status_new;
 }
コード例 #3
0
 /**
  * Generate data for history graph
  * @param int $server_id
  * @param \DateTime $start_time Lowest DateTime of the graph
  * @param \DateTime $end_time Highest DateTime of the graph
  * @return array
  */
 public function generateGraphHistory($server_id, $start_time, $end_time)
 {
     $lines = array('latency_avg' => array(), 'latency_max' => array(), 'latency_min' => array());
     $server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array('server_id' => $server_id), array('warning_threshold'));
     $cb_if_up = function ($uptime_record) use($server) {
         return $uptime_record['checks_failed'] < $server['warning_threshold'];
     };
     $records = $this->getRecords('history', $server_id, $start_time, $end_time);
     // dont add uptime for now because we have no way to calculate accurate uptimes for archived records
     $data = $this->generateGraphLines($records, $lines, $cb_if_up, 'latency_avg', $start_time, $end_time, false);
     $data['title'] = psm_get_lang('servers', 'chart_history');
     $data['plotmode'] = 'month';
     $data['buttons'] = array();
     $data['buttons'][] = array('mode' => 'week2', 'label' => psm_get_lang('servers', 'week'));
     $data['buttons'][] = array('mode' => 'month', 'label' => psm_get_lang('servers', 'month'), 'class_active' => 'btn-info');
     $data['buttons'][] = array('mode' => 'year', 'label' => psm_get_lang('servers', 'year'));
     // make sure to add chart id after buttons so its added to those tmeplates as well
     $data['chart_id'] = $server_id . '_history';
     return $data;
 }
コード例 #4
0
 /**
  * This function initializes the sending (text msg & email) and logging
  *
  * @param int $server_id
  * @param boolean $status_old
  * @param boolean $status_new
  * @return boolean
  */
 public function notify($server_id, $status_old, $status_new)
 {
     if (!$this->send_emails && !$this->send_sms && !$this->save_logs) {
         // seems like we have nothing to do. skip the rest
         return false;
     }
     $this->server_id = $server_id;
     $this->status_old = $status_old;
     $this->status_new = $status_new;
     // get server info from db
     $this->server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array('server_id' => $server_id), array('server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'error', 'active', 'email', 'sms', 'pushover'));
     if (empty($this->server)) {
         return false;
     }
     $notify = false;
     // check which type of alert the user wants
     switch (psm_get_conf('alert_type')) {
         case 'always':
             if ($status_new == false) {
                 // server is offline. we are in error state.
                 $notify = true;
             }
             break;
         case 'offline':
             // only send a notification if the server goes down for the first time!
             if ($status_new == false && $status_old == true) {
                 $notify = true;
             }
             break;
         case 'status':
             if ($status_new != $status_old) {
                 // status has been changed!
                 $notify = true;
             }
             break;
     }
     if (!$notify) {
         return false;
     }
     // first add to log (we use the same text as the SMS message because its short..)
     if ($this->save_logs) {
         psm_add_log($this->server_id, 'status', psm_parse_msg($status_new, 'sms', $this->server));
     }
     $users = $this->getUsers($this->server_id);
     if (empty($users)) {
         return $notify;
     }
     // check if email is enabled for this server
     if ($this->send_emails && $this->server['email'] == 'yes') {
         // send email
         $this->notifyByEmail($users);
     }
     // check if sms is enabled for this server
     if ($this->send_sms && $this->server['sms'] == 'yes') {
         // yay lets wake those nerds up!
         $this->notifyByTxtMsg($users);
     }
     // check if pushover is enabled for this server
     if ($this->send_pushover && $this->server['pushover'] == 'yes') {
         // yay lets wake those nerds up!
         $this->notifyByPushover($users);
     }
     return $notify;
 }