SAE域名綁定之后,一般是用CNAME方式將域名綁定到應(yīng)用中。但是有時(shí)候我們需要用到A記錄(比如說根域名,雖然在DNSPOD上可以設(shè)置CNAME記錄,但很可能會(huì)影響到MX記錄),而SAE的IP地址經(jīng)常改變,ping應(yīng)用二級(jí)域名得到的IP沒多久就失效了(前些天網(wǎng)站因此幾天打不開都沒發(fā)現(xiàn),我用的是教育網(wǎng),自己能打開,但是電信線路變了)。還好DNSPOD有個(gè)功能叫D監(jiān)控,可以幫你監(jiān)控網(wǎng)站能否正常打開。如果發(fā)現(xiàn)宕機(jī),DNSPOD會(huì)用郵件、短信、微信等方式提醒你。這里用到的是它的另一個(gè)通知方式,那就是URL回調(diào)“通過DNSPod 提供的D監(jiān)控 URL 回調(diào)功能,您可以讓宕機(jī)或恢復(fù)信息提交到您指定的 URL 上,從而更加靈活地處理各種通知信息。”
我們可以通過宕機(jī)之后的URL回調(diào)取得相關(guān)參數(shù),并通過DNSPOD API實(shí)現(xiàn)自動(dòng)修改記錄的功能,再通過飛信發(fā)送宕機(jī)通知。
代碼在后面,先說設(shè)置方法:
1.點(diǎn)此下載代碼,修改其中的參數(shù)為你自己的。
2.將代碼上傳到網(wǎng)站。
3.在DNSPOD開啟D監(jiān)控,在通知設(shè)置中回調(diào)URL一欄填入monitorCallback.php的地址,如http://blog.gimhoy.com/monitorCallback.php?rHost=hipic.sinaapp.com.其中rHost是SAE的二級(jí)域名。并設(shè)置回調(diào)密鑰。
4.Enjoy~ dnspod-monitor-callback
monitorCallback.php,代碼如下:
- /*
- * Copyright 2007-2014 Gimhoy Studio.
- *
- * @author Gimhoy
- * @email contact@gimhoy.com
- * @version 1.0.0
- */
- $rHost = $_GET['rHost']; // SAE二級(jí)域名
- if(emptyempty($rHost)) $rHost = 'sinaapp.com';
- $logName = 'monitorLog.txt'; //log 文件名
- $logStorDomain = 'log'; // Storage Domain
- $FetionNum = ''; //飛信登陸號(hào)碼
- $FetionPwd = ''; //飛信登陸密碼
- $MobileNum = ''; //接收通知短信的號(hào)碼
- $callback_key = 'MYKEY'; // 添加監(jiān)控時(shí)設(shè)置的密鑰
- $monitor_id = $_POST['monitor_id']; // 監(jiān)控編號(hào)
- $domain_id = $_POST['domain_id']; // 域名編號(hào)
- $domain = $_POST['domain']; // 域名名稱
- $record_id = $_POST['record_id']; // 記錄編號(hào)
- $sub_domain = $_POST['sub_domain']; // 主機(jī)名稱
- $record_line = $_POST['record_line']; // 記錄線路
- $ip = $_POST['ip']; // 記錄IP
- $status = $_POST['status']; // 當(dāng)前狀態(tài)
- $status_code = $_POST['status_code']; // 狀態(tài)代碼
- $reason = $_POST['reason']; // 宕機(jī)原因
- $created_at = $_POST['created_at']; // 發(fā)生時(shí)間
- $checksum = $_POST['checksum']; // 校檢代碼
- if (md5($monitor_id. $domain_id. $record_id. $callback_key. $created_at) != $checksum) {
- // 非法請(qǐng)求
- echo 'BAD REQUEST';
- } else {
- // 開始處理
- if ($status == 'Warn' || $status == 'Ok') {
- // 宕機(jī)恢復(fù)
- $msg = date("Y-m-d H:i:s").' '.$sub_domain.'.'.$domain."(".$record_line." ".$ip.")宕機(jī)恢復(fù)";
- } elseif ($status == 'Down') {
- // 宕機(jī)
- $msg = date("Y-m-d H:i:s").' '.$sub_domain.'.'.$domain."(".$record_line." ".$ip.")在".$created_at."宕機(jī)。宕機(jī)原因:".$reason."可用IP:";
- $ips = @gethostbyname($rHost);
- include_once 'dnspod.class.php';
- $newIP = $ips;
- $data = array(
- 'domain_id' => $domain_id,
- 'record_id' => $record_id,
- 'sub_domain' => $sub_domain,
- 'record_type' => 'A',
- 'record_line' => $record_line,
- 'ttl' => '600',
- 'value' => $newIP
- );
- $dnspod = new dnspod();
- $response = $dnspod->api_call('Record.Modify', $data);
- if(isset($response['status']['code']) && $response['status']['code'] == 1) {
- $msg = $msg.$newIP.'(已切換)';
- }
- else {
- $msg = $msg.$newIP.'(切換失敗,錯(cuò)誤代碼'.$response['status']['code'].')';
- }
- }
- //飛信通知
- require_once 'Fetion.class.php';
- $fetion = new PHPFetion($FetionNum, $FetionPwd);
- $result = $fetion->send($MobileNum, $msg);
- if(strpos($result, '短信發(fā)送成功!') || strpos($result, '發(fā)送消息成功!')) { $r = "成功。";}else{$r = "失敗。";}
- $s = new SaeStorage();
- $content = $s -> read($logStorDomain, $logName);
- $content = $content.$msg.'。飛信通知'.$r.'
- ';//開源代碼Vevb.com
- $s -> write($logStorDomain, $logName, $content);
- // 處理完成
- echo 'DONE';
- }
dnspod.class.php
- /*
- * DNSPod API PHP Web 示例
- * http://m.survivalescaperooms.com/
- *
- * Copyright 2011, Kexian Li
- * Released under the MIT, BSD, and GPL Licenses.
- *
- */
- class dnspod {
- public function api_call($api, $data) {
- if ($api == '' || !is_array($data)) {
- exit('內(nèi)部錯(cuò)誤:參數(shù)錯(cuò)誤');
- }
- $api = 'https://dnsapi.cn/' . $api;
- $data = array_merge($data, array('login_email' => 'DNSPOD登陸賬號(hào)', 'login_password' => 'DNSPOD登陸密碼', 'format' => 'json', 'lang' => 'cn', 'error_on_empty' => 'yes'));
- $result = $this->post_data($api, $data);
- if (!$result) {
- exit('內(nèi)部錯(cuò)誤:調(diào)用失敗');
- }
- $results = @json_decode($result, 1);
- if (!is_array($results)) {
- exit('內(nèi)部錯(cuò)誤:返回錯(cuò)誤');
- }
- if ($results['status']['code'] != 1) {
- exit($results['status']['message']);
- }
- return $results;
- }
- private function post_data($url, $data) {
- if ($url == '' || !is_array($data)) {
- return false;
- }
- $ch = @curl_init();
- if (!$ch) {
- exit('內(nèi)部錯(cuò)誤:服務(wù)器不支持CURL');
- }
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
- curl_setopt($ch, CURLOPT_USERAGENT, 'Gimhoy Monitor/1.0 (contact@gimhoy.com)');
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- }
Fetion.class.php,代碼如下:
- header('Content-Type: text/html; charset=utf-8');
- /**
- * PHP飛信發(fā)送類
- * @author quanhengzhuang
- * @version 1.5.0
- */
- class PHPFetion {
- /**
- * 發(fā)送者手機(jī)號(hào)
- * @var string
- */
- protected $_mobile;
- /**
- * 飛信密碼
- * @param string
- */
- protected $_password;
- /**
- * Cookie字符串
- * @param string
- */
- protected $_cookie = '';
- /**
- * Uid緩存
- * @var array
- */
- protected $_uids = array();
- /**
- * csrfToken
- * @param string
- */
- protected $_csrfToten = null;
- /**
- * 構(gòu)造函數(shù)
- * @param string $mobile 手機(jī)號(hào)(登錄者)
- * @param string $password 飛信密碼
- */
- public function __construct($mobile, $password)
- {
- if ($mobile === '' || $password === '')
- {
- return;
- }
- $this->_mobile = $mobile;
- $this->_password = $password;
- $this->_login();
- }
- /**
- * 析構(gòu)函數(shù)
- */
- public function __destruct() {
- $this->_logout();
- }
- /**
- * 登錄
- * @return string
- */
- protected function _login()
- {
- $uri = '/huc/user/space/login.do?m=submit&fr=space';
- $data = 'mobilenum='.$this->_mobile.'&password='.urlencode($this->_password);
- $result = $this->_postWithCookie($uri, $data);
- //解析Cookie
- preg_match_all('/.*?rnSet-Cookie: (.*?);.*?/si', $result, $matches);
- if (isset($matches[1]))
- {
- $this->_cookie = implode('; ', $matches[1]);
- }
- $result = $this->_postWithCookie('/im/login/cklogin.action', '');
- return $result;
- }
- /**
- * 獲取csrfToken,給好友發(fā)飛信時(shí)需要這個(gè)字段
- * @param string $uid 飛信ID
- * @return string
- */
- protected function _getCsrfToken($uid)
- {
- if ($this->_csrfToten === null)
- {
- $uri = '/im/chat/toinputMsg.action?touserid='.$uid;
- $result = $this->_postWithCookie($uri, '');
- preg_match('/name="csrfToken".*?value="(.*?)"/', $result, $matches);
- $this->_csrfToten = isset($matches[1]) ? $matches[1] : '';
- }
- return $this->_csrfToten;
- }
- /**
- * 向指定的手機(jī)號(hào)發(fā)送飛信
- * @param string $mobile 手機(jī)號(hào)(接收者)
- * @param string $message 短信內(nèi)容
- * @return string
- */
- public function send($mobile, $message) {
- if($message === '') {
- return '';
- }
- // 判斷是給自己發(fā)還是給好友發(fā)
- if($mobile === $this->_mobile) {
- return $this->_toMyself($message);
- }
- else if(strlen($mobile)===11){
- $uid = $this->_getUid($mobile);
- }
- else {
- $uid=$mobile;
- }
- return $uid === '' ? $this->_addFriend($mobile) : $this->_toUid($uid, $message);
- }
- protected function _getname() {
- $uri = '/im/index/index.action';
- $result = $this->_postWithCookie($uri, '#');
- // 匹配
- preg_match('/(.*?)</a>/si', $result, $matches);
- return $matches[2];
- }
- /*
- * 通過手機(jī)號(hào)增加好友
- * @param string $number 手機(jī)號(hào)(要加的好友手機(jī))
- * @param string $nickname 你的名字,出現(xiàn)在對(duì)方的驗(yàn)證短信里
- * @param string $buddylist 分組,默認(rèn)為空
- * @param string $localName 好友屏顯名
- * @return string
- */
- protected function _addFriend($number) {
- $uri = '/im/user/insertfriendsubmit.action';
- $data = 'nickname='. urlencode($this->_getname()).'&buddylist=1&localName=&number='. $number .'&type=0';
- $result = $this->_postWithCookie($uri, $data);
- return $result;
- }
- /**
- * 獲取飛信ID
- * @param string $mobile 手機(jī)號(hào)
- * @return string
- */
- protected function _getUid($mobile)
- {
- if (emptyempty($this->_uids[$mobile]))
- {
- $uri = '/im/index/searchOtherInfoList.action';
- $data = 'searchText='.$mobile;
- $result = $this->_postWithCookie($uri, $data);
- //匹配
- preg_match('/toinputMsg.action?touserid=(d+)/si', $result, $matches);
- $this->_uids[$mobile] = isset($matches[1]) ? $matches[1] : '';
- }
- return $this->_uids[$mobile];
- }
- /**
- * 向好友發(fā)送飛信
- * @param string $uid 飛信ID
- * @param string $message 短信內(nèi)容
- * @return string
- */
- protected function _toUid($uid, $message) {
- $uri = '/im/chat/sendMsg.action?touserid='.$uid;
- $csrfToken = $this->_getCsrfToken($uid);
- $data = 'msg='.urlencode($message).'&csrfToken='.$csrfToken;
- $result = $this->_postWithCookie($uri, $data);
- return $result;
- }
- /**
- * 給自己發(fā)飛信
- * @param string $message
- * @return string
- */
- protected function _toMyself($message) {
- $uri = '/im/user/sendMsgToMyselfs.action';
- $result = $this->_postWithCookie($uri, 'msg='.urlencode($message));
- return $result;
- }
- /**
- * 退出飛信
- * @return string
- */
- protected function _logout() {
- $uri = '/im/index/logoutsubmit.action';
- $result = $this->_postWithCookie($uri, '');
- return $result;
- }
- protected function getgroup() {
- $uri = '/im/index/index.action';
- $data = 'type=group';
- $result = $this->_postWithCookie($uri, $data);
- // 匹配
- preg_match_all('/contactlistView.action?idContactList=(d+)/si', $result, $matches);
- foreach($matches[1] as $k=>$v){
- if( $k== 0 ){
- $min = $v;
- $max = $v;
- }else if($v!=9998&&$v!=9999){
- $min = min($min,$v);
- $max = max($max,$v);
- }
- }
- return $max;
- }
- public function getyou1() {
- $list=$this->getgroup();
- for($i=0;$i<=$list;$i++){
- $uri = '/im/index/contactlistView.action';
- $data = 'idContactList='.$i.'&type=group';
- $result = $this->_postWithCookie($uri, $data);
- preg_match('/(.*?)|(.*?)((.*?)/(.*?))/si', $result, $listn);
- if(!$listn[2]){continue;}
- $shuchu.=str_replace(" ","",$listn[2])."(".$listn[4].")n";
- preg_match('/共(d+)頁/si', $result, $zpage);
- preg_match('/共(d+)</a>頁/si', $result, $dpage);
- isset($zpage[1]) ? $page=$zpage[1] : $page=$dpage[4];
- for($j=1;$j<=$page;$j++){
- $uri = '/im/index/contactlistView.action';
- $data = 'idContactList='.$i.'&page='.$j;
- $result = $this->_postWithCookie($uri, $data);
- preg_match_all('/(.*?)</a>/si', $result, $matches);
- if(!$matches[1][0]){break;}
- for($x=0;$x<=9;$x++){
- if(!$matches[1][$x]){continue;}
- $shuchu.=$matches[1][$x]." ".str_replace(" ","",$matches[3][$x])."n";
- }
- }
- }
- return $shuchu;
- }
- public function getyou() {
- $list=$this->getgroup();
- for($i=0;$i<=$list;$i++){
- $uri = '/im/index/contactlistView.action';
- $data = 'idContactList='.$i.'&type=group';
- $result = $this->_postWithCookie($uri, $data);
- preg_match('/(.*?)|(.*?)((.*?)/(.*?))/si', $result, $listn);
- if(!$listn[2]){continue;}
- $shuchu.=str_replace(" ","",$listn[2])."(".$listn[4].")n";
- preg_match('/共(d+)頁/si', $result, $zpage);
- preg_match('/共(d+)</a>頁/si', $result, $dpage);
- isset($zpage[1]) ? $page=$zpage[1] : $page=$dpage[4];
- for($j=1;$j<=$page;$j++){
- $uri = '/im/index/contactlistView.action';
- $data = 'idContactList='.$i.'&page='.$j;
- $result = $this->_postWithCookie($uri, $data);
- preg_match_all('/(.*?)</a>/si', $result, $matches);
- if(!$matches[1][0]){break;}
- for($x=0;$x<=9;$x++){
- if(!$matches[1][$x]){continue;}
- $shuchu.=$matches[1][$x]." ".str_replace(" ","",$matches[3][$x])."n";
- }
- }
- }
- return $shuchu;
- }
- /**
- * 攜帶Cookie向f.10086.cn發(fā)送POST請(qǐng)求
- * @param string $uri
- * @param string $data
- */
- protected function _postWithCookie($uri, $data)
- {
- $fp = fsockopen('f.10086.cn', 80);
- fputs($fp, "POST $uri HTTP/1.1rn");
- fputs($fp, "Host: f.10086.cnrn");
- fputs($fp, "Cookie: {$this->_cookie}rn");
- fputs($fp, "Content-Type: application/x-www-form-urlencodedrn");
- fputs($fp, "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1rn");
- fputs($fp, "Content-Length: ".strlen($data)."rn");
- fputs($fp, "Connection: closernrn");
- fputs($fp, $data);
- $result = '';
- while (!feof($fp))
- {
- $result .= fgets($fp);
- }
- fclose($fp);
- return $result;
- }
- }
新聞熱點(diǎn)
疑難解答