From 290e1cbb516ee54249038551d7bc9795c004c061 Mon Sep 17 00:00:00 2001 From: Kevin Siml Date: Fri, 7 Apr 2017 10:08:47 +0200 Subject: [PATCH] Add files via upload --- .../Util/Server/Updater/StatusNotifier.php | 171 +++++++++++++++++- 1 file changed, 169 insertions(+), 2 deletions(-) diff --git a/src/psm/Util/Server/Updater/StatusNotifier.php b/src/psm/Util/Server/Updater/StatusNotifier.php index 0edfde98..331f14e4 100755 --- a/src/psm/Util/Server/Updater/StatusNotifier.php +++ b/src/psm/Util/Server/Updater/StatusNotifier.php @@ -59,6 +59,12 @@ class StatusNotifier { * @var boolean $send_pushover */ protected $send_pushover = false; + + /** + * Send pushsafer? + * @var boolean $send_pushsafer + */ + protected $send_pushsafer = false; /** * Save log records? @@ -96,6 +102,7 @@ class StatusNotifier { $this->send_emails = psm_get_conf('email_status'); $this->send_sms = psm_get_conf('sms_status'); $this->send_pushover = psm_get_conf('pushover_status'); + $this->send_pushsafer = psm_get_conf('pushsafer_status'); $this->save_logs = psm_get_conf('log_status'); } @@ -121,7 +128,7 @@ class StatusNotifier { $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', + 'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'error', 'active', 'email', 'sms', 'pushover', 'pushsafer', )); if(empty($this->server)) { return false; @@ -187,6 +194,12 @@ class StatusNotifier { // yay lets wake those nerds up! $this->notifyByPushover($users); } + + // check if pushsafer is enabled for this server + if($this->send_pushsafer && $this->server['pushsafer'] == 'yes') { + // yay lets wake those nerds up! + $this->notifyByPushsafer($users); + } return $notify; } @@ -278,6 +291,160 @@ class StatusNotifier { } } + /** + * This functions performs the pushsafer chart generation + */ + protected function PushsaferChart($values) { + $img_width=900; + $img_height=600; + $margins=30; + + # ---- Find the size of graph by substracting the size of borders + $graph_width=$img_width - $margins * 2; + $graph_height=$img_height - $margins * 2; + $img=imagecreate($img_width,$img_height); + + $bar_width=20; + $total_bars=count($values); + $gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1); + + # ------- Define Colors ---------------- + $bar_color=imagecolorallocate($img,0,64,128); + $background_color=imagecolorallocate($img,240,240,255); + $border_color=imagecolorallocate($img,200,200,200); + $line_color=imagecolorallocate($img,220,220,220); + + # ------ Create the border around the graph ------ + + imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color); + imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color); + + + # ------- Max value is required to adjust the scale ------- + $max_value=max($values); + $ratio= $graph_height/$max_value; + + + # -------- Create scale and draw horizontal lines -------- + $horizontal_lines=20; + $horizontal_gap=$graph_height/$horizontal_lines; + + for($i=1;$i<=$horizontal_lines;$i++){ + if ($i % 2 != 0) { + $y=$img_height - $margins - $horizontal_gap * $i ; + imageline($img,$margins,$y,$img_width-$margins,$y,$line_color); + $v=number_format($horizontal_gap * $i /$ratio,2); + imagestring($img,1,5,$y-5,$v,$bar_color); + } + } + + # ----------- Draw the bars here ------ + imagestring($img,3,5,10,"LATENCY",$bar_color); + for($i=0;$i< $total_bars; $i++){ + # ------ Extract key and value pair from the current pointer position + list($key,$value)=each($values); + $x1= $margins + $gap + $i * ($gap+$bar_width) ; + $x2= $x1 + $bar_width; + $y1=$margins +$graph_height- intval($value * $ratio) ; + $y2=$img_height-$margins; + $value=number_format($value,2); + $key=explode(' ',$key); + imagestring($img,0,$x1+1,$y1-10,$value,$bar_color); + imagestring($img,0,$x1-15,$img_height-25,$key[0],$bar_color); + imagestring($img,0,$x1-11,$img_height-15,$key[1],$bar_color); + imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); + } + + ob_start(); + imagejpeg($img); + $contents = ob_get_contents(); + ob_end_clean(); + return "data:image/jpeg;base64," . base64_encode($contents); + } + + protected function notifyByPushsafer($users) { + $userlist = array(); + + $message = str_replace('
', "\n", psm_parse_msg($this->status_new, 'pushsafer_message', $this->server)); + $title = psm_parse_msg($this->status_new, 'pushover_title', $this->server); + $url = psm_build_url(); + $urltitle = psm_get_lang('system', 'title'); + + if($this->status_new === true) { + $icon = psm_get_conf('pushsafer_icon_on'); + $sound = psm_get_conf('pushsafer_sound_on'); + $vibration = psm_get_conf('pushsafer_vibration_on'); + } else { + $icon = psm_get_conf('pushsafer_icon_off'); + $sound = psm_get_conf('pushsafer_sound_off'); + $vibration = psm_get_conf('pushsafer_vibration_off'); + } + + // GET Latency and generte Chart as png image + if (extension_loaded('gd') && function_exists('gd_info')) { + $now = new \DateTime(); + $last_week = new \DateTime('-1 week 0:0:0'); + $records = $this->db->execute( + 'SELECT * + FROM `' . PSM_DB_PREFIX . 'servers_uptime` + WHERE `server_id` = :server_id AND `date` BETWEEN :start_time AND :end_time ORDER BY `date` ASC LIMIT 15', + array( + 'server_id' => $this->server_id, + 'start_time' => $last_week->format('Y-m-d H:i:s'), + 'end_time' => $now->format('Y-m-d H:i:s'), + ) + ); + + $chartvalues = array(); + foreach ($records as $uptime) { + $chartvalues[$uptime['date']] = $uptime['latency']; + } + $picture = $this->PushsaferChart($chartvalues); + } + + foreach($users as $user) { + $userlist[] = $user['user_id']; + if(empty($user['pushsafer_key'])) { + $this->addMessage(psm_get_lang('config', 'pushsafer_error_nokey'), 'error'); + psm_add_log($this->server_id, 'pushsafer', psm_get_lang('config', 'pushsafer_error_nokey'), $user['user_id']); + } else { + $apiurl = 'https://www.pushsafer.com/api'; + $data = array( + 't' => urldecode($title), + 'm' => urldecode($message), + 's' => $sound, + 'i' => $icon, + 'v' => $vibration, + 'l' => psm_get_conf('pushsafer_time2live'), + 'd' => $user['pushsafer_device'], + 'u' => urldecode($url), + 'ut' => urldecode($urltitle), + 'p' => $picture, + 'k' => $user['pushsafer_key'] + ); + $options = array( + 'http' => array( + 'header' => "Content-type: application/x-www-form-urlencoded\r\n", + 'method' => 'POST', + 'content' => http_build_query($data) + ) + ); + $context = stream_context_create($options); + $result = json_decode(file_get_contents($apiurl, false, $context), true); + + if(isset($result['status']) && $result['status'] == 1) { + psm_add_log($this->server_id, 'pushsafer', psm_get_lang('config', 'pushsafer_sent'), $user['user_id']); + } else { + psm_add_log($this->server_id, 'pushsafer', psm_get_lang('config', 'pushsafer_error'), $user['user_id']); + } + } + } + + if(psm_get_conf('log_pushsafer')) { + psm_add_log($this->server_id, 'pushsafer', $message, implode(',', $userlist)); + } + } + /** * This functions performs the text message notifications * @@ -321,7 +488,7 @@ class StatusNotifier { public function getUsers($server_id) { // find all the users with this server listed $users = $this->db->query(" - SELECT `u`.`user_id`, `u`.`name`,`u`.`email`, `u`.`mobile`, `u`.`pushover_key`, `u`.`pushover_device` + SELECT `u`.`user_id`, `u`.`name`,`u`.`email`, `u`.`mobile`, `u`.`pushover_key`, `u`.`pushover_device`, `u`.`pushsafer_key`, `u`.`pushsafer_device` FROM `".PSM_DB_PREFIX."users` AS `u` JOIN `".PSM_DB_PREFIX."users_servers` AS `us` ON ( `us`.`user_id`=`u`.`user_id`