<?php set_time_limit(0); //ping一個IP地址,能不能通 function ping($ip) { $ip_port = explode(':', $ip); // var_dump($ip_port); if (filter_var($ip_port[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { //IPv6 $socket = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP); } elseif (filter_var($ip_port[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { //IPv4 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); } else { return false; } if (!isset($ip_port[1])) { //沒有寫端口則指定爲80 $ip_port[1] = '80'; } //var_dump($socket); @$ok = socket_connect($socket, $ip_port[0], $ip_port[1]); //var_dump(socket_strerror(socket_last_error($socket))); //var_dump($ok); socket_close($socket); if ($ok) { //echo "鏈接OK\n"; return true; } else { //echo "socket_connect() failed.\nReason: ($ok) " . socket_strerror($ok) . "\n"; return false; } } function sendMessage($data){ $postUrl = 'http://*****.shtml';//釘釘接口地址 $postData = $data; //$postData = http_build_query($postData); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $postUrl); curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15'); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); //curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($postData) )); curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); $r = curl_exec($curl); curl_close($curl); return $r; } $ipList=array('192.168.2.50');//被監測ip列表 $mobileList=array('1380000000','1381111111');//異常通知釘釘手機號碼 $sendData['mobiles']=$mobileList; $sendData['type']=1; foreach($ipList as $k=>$v){ if(!ping($v)){ $sendData['content']="親,IP爲".$v."的服務器宕機了,快去查看一下吧 ".date('Y-m-d H:i:s'); sendMessage(json_encode($sendData)); } }