Home » Categories » Server Monitoring

Hướng dẫn gửi message từ Server Monitoring tới Slack

Bước 1: Add API Alert

Bước 2: Add API Alert vào Notification Lists

Bước 3: Trên Web Server chứa link trinhtrongson.xyz tạo file slack.php với nội dung như sau

<?php

    $results = json_decode($_POST['webhook_data']);
	$fp = @fopen('logs.txt', "a");
	/////Tao web hook URL tai link https://your.slack.com/services/new/incoming-webhook
	$slack_url="https://hooks.slack.com/services/xxxxxxxx/xxxxxxxxxx/xxxxxxxx/xxxxxxxxx";
	$username="Server Monitoring";
	//////Ten channel da tao web hook truoc do
	$channel="#xxxxxxxx";
	$text="";
		
	if ($results->key == "test"){

		$Status=$results->results;
		$downtime=$results->downtime;
		$hostname=$results->hostname;
		$alert_date_time=$results->alert_date_time;
		$check_type=$results->check_type;
		$first_down_time=$results->first_down_time;
		$recover_message=$results->recover_message;
		$failure_message=$results->failure_message;
        $location=$results->location;	

		if($Status == 'true'){
			 $text=$alert_date_time."-"." ----> *UP* <----- "."-".$check_type." ".$hostname;
                         
		}else{
		     $text=$alert_date_time."-"." ----> *DOWN* <---- "."-".$check_type." ".$hostname."-".$location; 
        }

		$payload_arr = array(
			"channel" => $channel,
			"username" => $username,
			"text" => $text
		);

	$payload = json_encode($payload_arr);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$slack_url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	$server_output = curl_exec ($ch);
    fwrite($fp,"-----------".PHP_EOL.$alert_date_time.PHP_EOL."Curl ".$server_output.PHP_EOL.$_POST['webhook_data'].PHP_EOL);
	curl_close ($ch);
	}


?>

Email: support@vhost.vn

Hotline: 19006806

Website: http://vhost.vn/

       
      Attachments Attachments
      There are no attachments for this article.
      Comments Comments
      There are no comments for this article. Be the first to post a comment.
      Related Articles RSS Feed
      Hướng dẫn sử dụng PHP Script monitor disk space usage
      Viewed 4349 times since Fri, Dec 18, 2015
      Hướng dẫn sử dụng Monitor Add Check
      Viewed 1549 times since Tue, Jun 20, 2017
      Hướng dẫn tạo một Check để monitor
      Viewed 6356 times since Thu, Dec 17, 2015
      Hướng dẫn tạo một Notification List
      Viewed 3189 times since Thu, Dec 17, 2015
      Hướng dẫn tạo contact đối với dịch vụ Monitor
      Viewed 8596 times since Thu, Dec 17, 2015
      Hướng dẫn sử dụng Add API Alert
      Viewed 1731 times since Tue, Jun 20, 2017
      Hướng dẫn sử dụng Add Notification List
      Viewed 1434 times since Tue, Jun 20, 2017
      Hướng dẫn sử dụng API Alerts
      Viewed 1676 times since Tue, Jun 20, 2017
      Hướng dẫn sử dụng API Alerts và Notification Lists
      Viewed 1967 times since Tue, Jun 20, 2017
      Hướng dẫn sử dụng Monitor View Check
      Viewed 3508 times since Tue, Jun 20, 2017