php专区

 首页 > php专区 > PHP应用 > 常用功能 > SAE域名绑定设置服务器宕机时自动修改A记录并飞

SAE域名绑定设置服务器宕机时自动修改A记录并飞

分享到:
【字体:大 中 小】
导读:
          SAE域名绑定之后,一般是用CNAME方式将域名绑定到应用中。但是有时候我们需要用到A记录(比如说根域名,虽然在DNSPOD上可以设置CNAME记录,但很可能会影响到MX记录),而SAE的IP地址经...

SAE域名绑定设置服务器宕机时自动修改A记录并飞信通知

SAE域名绑定之后,一般是用CNAME方式将域名绑定到应用中。但是有时候我们需要用到A记录(比如说根域名,虽然在DNSPOD上可以设置CNAME记录,但很可能会影响到MX记录),而SAE的IP地址经常改变,ping应用二级域名得到的IP没多久就失效了(前些天网站因此几天打不开都没发现,我用的是教育网,自己能打开,但是电信线路变了)。还好DNSPOD有个功能叫D监控,可以帮你监控网站能否正常打开。如果发现宕机,DNSPOD会用邮件、短信、微信等方式提醒你。这里用到的是它的另一个通知方式,那就是URL回调“通过DNSPod 提供的D监控 URL 回调功能,您可以让宕机或恢复信息提交到您指定的 URL 上,从而更加灵活地处理各种通知信息。”

我们可以通过宕机之后的URL回调取得相关参数,并通过DNSPOD API实现自动修改记录的功能,再通过飞信发送宕机通知。

代码在后面,先说设置方法:

1.点此下载代码,修改其中的参数为你自己的。

2.将代码上传到网站。

3.在DNSPOD开启D监控,在通知设置中回调URL一栏填入monitorCallback.php的地址,如http://blog.gimhoy.com/monitorCallback.php?rHost=hipic.sinaapp.com.其中rHost是SAE的二级域名。并设置回调密钥。

4.Enjoy~ dnspod-monitor-callback

monitorCallback.php,代码如下:

  1. /* 
  2. * Copyright 2007-2014 Gimhoy Studio. 
  3. * 
  4. * @author Gimhoy 
  5. * @email contact@gimhoy.com 
  6. * @version 1.0.0 
  7. */ 
  8. $rHost = $_GET['rHost']; // SAE二级域名 
  9. if(emptyempty($rHost)) $rHost = 'sinaapp.com'; 
  10. $logName = 'monitorLog.txt'; //log 文件名 
  11. $logStorDomain = 'log'; // Storage Domain 
  12. $FetionNum = ''; //飞信登陆号码 
  13. $FetionPwd = ''; //飞信登陆密码 
  14. $MobileNum = ''; //接收通知短信的号码 
  15. $callback_key = 'MYKEY'; // 添加监控时设置的密钥 
  16.  
  17. $monitor_id = $_POST['monitor_id']; // 监控编号 
  18. $domain_id = $_POST['domain_id']; // 域名编号 
  19. $domain = $_POST['domain']; // 域名名称 
  20. $record_id = $_POST['record_id']; // 记录编号 
  21. $sub_domain = $_POST['sub_domain']; // 主机名称 
  22. $record_line = $_POST['record_line']; // 记录线路 
  23. $ip = $_POST['ip']; // 记录IP 
  24. $status = $_POST['status']; // 当前状态 
  25. $status_code = $_POST['status_code']; // 状态代码 
  26. $reason = $_POST['reason']; // 宕机原因 
  27. $created_at = $_POST['created_at']; // 发生时间 
  28. $checksum = $_POST['checksum']; // 校检代码 
  29.  
  30. if (md5($monitor_id. $domain_id. $record_id. $callback_key. $created_at) != $checksum) { 
  31. // 非法请求 
  32. echo 'BAD REQUEST'; 
  33. } else { 
  34. // 开始处理 
  35. if ($status == 'Warn' || $status == 'Ok') { 
  36. // 宕机恢复 
  37. $msg = date("Y-m-d H:i:s").' '.$sub_domain.'.'.$domain."(".$record_line." ".$ip.")宕机恢复"; 
  38. } elseif ($status == 'Down') { 
  39. // 宕机 
  40. $msg = date("Y-m-d H:i:s").' '.$sub_domain.'.'.$domain."(".$record_line." ".$ip.")在".$created_at."宕机。宕机原因:".$reason."可用IP:"; 
  41. $ips = @gethostbyname($rHost); 
  42. include_once 'dnspod.class.php'; 
  43. $newIP = $ips; 
  44. $data = array( 
  45. 'domain_id' => $domain_id, 
  46. 'record_id' => $record_id, 
  47. 'sub_domain' => $sub_domain, 
  48. 'record_type' => 'A', 
  49. 'record_line' => $record_line, 
  50. 'ttl' => '600', 
  51. 'value' => $newIP 
  52. ); 
  53. $dnspod = new dnspod(); 
  54. $response = $dnspod->api_call('Record.Modify', $data); 
  55. if(isset($response['status']['code']) && $response['status']['code'] == 1) { 
  56. $msg = $msg.$newIP.'(已切换)'; 
  57. } 
  58. else { 
  59. $msg = $msg.$newIP.'(切换失败,错误代码'.$response['status']['code'].')'; 
  60. } 
  61. } 
  62. //飞信通知 
  63. require_once 'Fetion.class.php'; 
  64. $fetion = new PHPFetion($FetionNum, $FetionPwd); 
  65. $result = $fetion->send($MobileNum, $msg); 
  66. if(strpos($result, '短信发送成功!') || strpos($result, '发送消息成功!')) { $r = "成功。";}else{$r = "失败。";} 
  67. $s = new SaeStorage(); 
  68. $content = $s -> read($logStorDomain, $logName); 
  69. $content = $content.$msg.'。飞信通知'.$r.' 
  70. ';//开源代码phpfensi.com 
  71. $s -> write($logStorDomain, $logName, $content); 
  72.  
  73. // 处理完成 
  74. echo 'DONE'; 
  75. } 

dnspod.class.php

  1. /* 
  2. * DNSPod API PHP Web 示例 
  3. * http://www.phpfensi.com/ 
  4. * 
  5. * Copyright 2011, Kexian Li 
  6. * Released under the MIT, BSD, and GPL Licenses. 
  7. * 
  8. */ 
  9.  
  10. class dnspod { 
  11. public function api_call($api, $data) { 
  12. if ($api == '' || !is_array($data)) { 
  13. exit('内部错误:参数错误'); 
  14. } 
  15. $api = 'https://dnsapi.cn/' . $api; 
  16. $data = array_merge($data, array('login_email' => 'DNSPOD登陆账号', 'login_password' => 'DNSPOD登陆密码', 'format' => 'json', 'lang' => 'cn', 'error_on_empty' => 'yes')); 
  17.  
  18. $result = $this->post_data($api, $data); 
  19.  
  20. if (!$result) { 
  21. exit('内部错误:调用失败'); 
  22. } 
  23. $results = @json_decode($result, 1); 
  24. if (!is_array($results)) { 
  25. exit('内部错误:返回错误'); 
  26. } 
  27. if ($results['status']['code'] != 1) { 
  28. exit($results['status']['message']); 
  29. } 
  30. return $results; 
  31. } 
  32. private function post_data($url, $data) { 
  33. if ($url == '' || !is_array($data)) { 
  34. return false; 
  35. } 
  36. $ch = @curl_init(); 
  37. if (!$ch) { 
  38. exit('内部错误:服务器不支持CURL'); 
  39. } 
  40. curl_setopt($ch, CURLOPT_URL, $url); 
  41. curl_setopt($ch, CURLOPT_POST, 1); 
  42. curl_setopt($ch, CURLOPT_HEADER, 0); 
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  44. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
  45. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
  46. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); 
  47. curl_setopt($ch, CURLOPT_USERAGENT, 'Gimhoy Monitor/1.0 (contact@gimhoy.com)'); 
  48. $result = curl_exec($ch); 
  49. curl_close($ch); 
  50. return $result; 
  51. } 
  52. } 

Fetion.class.php,代码如下:

  1. header('Content-Type: text/html; charset=utf-8'); 
  2. /** 
  3. * PHP飞信发送类 
  4. * @author quanhengzhuang 
  5. * @version 1.5.0 
  6. */ 
  7. class PHPFetion { 
  8. /** 
  9. * 发送者手机号 
  10. * @var string 
  11. */ 
  12. protected $_mobile; 
  13. /** 
  14. * 飞信密码 
  15. * @param string 
  16. */ 
  17. protected $_password; 
  18. /** 
  19. * Cookie字符串 
  20. * @param string 
  21. */ 
  22. protected $_cookie = ''; 
  23. /** 
  24. * Uid缓存 
  25. * @var array 
  26. */ 
  27. protected $_uids = array(); 
  28. /** 
  29. * csrfToken 
  30. * @param string 
  31. */ 
  32. protected $_csrfToten = null; 
  33. /** 
  34. * 构造函数 
  35. * @param string $mobile 手机号(登录者) 
  36. * @param string $password 飞信密码 
  37. */ 
  38. public function __construct($mobile, $password) 
  39. { 
  40. if ($mobile === '' || $password === '') 
  41. { 
  42. return; 
  43. } 
  44. $this->_mobile = $mobile; 
  45. $this->_password = $password; 
  46. $this->_login(); 
  47. } 
  48. /** 
  49. * 析构函数 
  50. */ 
  51. public function __destruct() { 
  52. $this->_logout(); 
  53. } 
  54. /** 
  55. * 登录 
  56. * @return string 
  57. */ 
  58. protected function _login() 
  59. { 
  60. $uri = '/huc/user/space/login.do?m=submit&fr=space'; 
  61. $data = 'mobilenum='.$this->_mobile.'&password='.urlencode($this->_password); 
  62. $result = $this->_postWithCookie($uri, $data); 
  63.  
  64. //解析Cookie 
  65. preg_match_all('/.*?rnSet-Cookie: (.*?);.*?/si', $result, $matches); 
  66. if (isset($matches[1])) 
  67. { 
  68. $this->_cookie = implode('; ', $matches[1]); 
  69. } 
  70. $result = $this->_postWithCookie('/im/login/cklogin.action', ''); 
  71.  
  72. return $result; 
  73. } 
  74. /** 
  75. * 获取csrfToken,给好友发飞信时需要这个字段 
  76. * @param string $uid 飞信ID 
  77. * @return string 
  78. */ 
  79. protected function _getCsrfToken($uid) 
  80. { 
  81. if ($this->_csrfToten === null) 
  82. { 
  83. $uri = '/im/chat/toinputMsg.action?touserid='.$uid; 
  84. $result = $this->_postWithCookie($uri, ''); 
  85. preg_match('/name="csrfToken".*?value="(.*?)"/', $result, $matches); 
  86.  
  87. $this->_csrfToten = isset($matches[1]) ? $matches[1] : ''; 
  88. } 
  89.  
  90. return $this->_csrfToten; 
  91. } 
  92.  
  93. /** 
  94. * 向指定的手机号发送飞信 
  95. * @param string $mobile 手机号(接收者) 
  96. * @param string $message 短信内容 
  97. * @return string 
  98. */ 
  99. public function send($mobile, $message) { 
  100. if($message === '') { 
  101. return ''; 
  102. } 
  103. // 判断是给自己发还是给好友发 
  104. if($mobile === $this->_mobile) { 
  105. return $this->_toMyself($message); 
  106. } 
  107. else if(strlen($mobile)===11){ 
  108. $uid = $this->_getUid($mobile); 
  109. } 
  110. else { 
  111. $uid=$mobile; 
  112. } 
  113. return $uid === '' ? $this->_addFriend($mobile) : $this->_toUid($uid, $message); 
  114. } 
  115. protected function _getname() { 
  116. $uri = '/im/index/index.action'; 
  117. $result = $this->_postWithCookie($uri, '#'); 
  118. // 匹配 
  119. preg_match('/(.*?)/si', $result, $matches); 
  120. return $matches[2]; 
  121. } 
  122. /* 
  123. * 通过手机号增加好友 
  124. * @param string $number 手机号(要加的好友手机) 
  125. * @param string $nickname 你的名字,出现在对方的验证短信里 
  126. * @param string $buddylist 分组,默认为空 
  127. * @param string $localName 好友屏显名 
  128. * @return string 
  129. */ 
  130. protected function _addFriend($number) { 
  131. $uri = '/im/user/insertfriendsubmit.action'; 
  132. $data = 'nickname='. urlencode($this->_getname()).'&buddylist=1&localName=&number='. $number .'&type=0'; 
  133. $result = $this->_postWithCookie($uri, $data); 
  134. return $result; 
  135. } 
  136. /** 
  137. * 获取飞信ID 
  138. * @param string $mobile 手机号 
  139. * @return string 
  140. */ 
  141. protected function _getUid($mobile) 
  142. { 
  143. if (emptyempty($this->_uids[$mobile])) 
  144. { 
  145. $uri = '/im/index/searchOtherInfoList.action'; 
  146. $data = 'searchText='.$mobile; 
  147. $result = $this->_postWithCookie($uri, $data); 
  148. //匹配 
  149. preg_match('/toinputMsg.action?touserid=(d+)/si', $result, $matches); 
  150.  
  151. $this->_uids[$mobile] = isset($matches[1]) ? $matches[1] : ''; 
  152. } 
  153. return $this->_uids[$mobile]; 
  154. } 
  155. /** 
  156. * 向好友发送飞信 
  157. * @param string $uid 飞信ID 
  158. * @param string $message 短信内容 
  159. * @return string 
  160. */ 
  161. protected function _toUid($uid, $message) { 
  162. $uri = '/im/chat/sendMsg.action?touserid='.$uid; 
  163. $csrfToken = $this->_getCsrfToken($uid); 
  164. $data = 'msg='.urlencode($message).'&csrfToken='.$csrfToken; 
  165. $result = $this->_postWithCookie($uri, $data); 
  166. return $result; 
  167. } 
  168. /** 
  169. * 给自己发飞信 
  170. * @param string $message 
  171. * @return string 
  172. */ 
  173. protected function _toMyself($message) { 
  174. $uri = '/im/user/sendMsgToMyselfs.action'; 
  175. $result = $this->_postWithCookie($uri, 'msg='.urlencode($message)); 
  176. return $result; 
  177. } 
  178. /** 
  179. * 退出飞信 
  180. * @return string 
  181. */ 
  182. protected function _logout() { 
  183. $uri = '/im/index/logoutsubmit.action'; 
  184. $result = $this->_postWithCookie($uri, ''); 
  185. return $result; 
  186. } 
  187. protected function getgroup() { 
  188. $uri = '/im/index/index.action'; 
  189. $data = 'type=group'; 
  190. $result = $this->_postWithCookie($uri, $data); 
  191. // 匹配 
  192. preg_match_all('/contactlistView.action?idContactList=(d+)/si', $result, $matches); 
  193. foreach($matches[1] as $k=>$v){ 
  194. if( $k== 0 ){ 
  195. $min = $v; 
  196. $max = $v; 
  197. }else if($v!=9998&&$v!=9999){ 
  198. $min = min($min,$v); 
  199. $max = max($max,$v); 
  200. } 
  201. } 
  202. return $max; 
  203. } 
  204. public function getyou1() { 
  205. $list=$this->getgroup(); 
  206. for($i=0;$i<=$list;$i++){ 
  207. $uri = '/im/index/contactlistView.action'; 
  208. $data = 'idContactList='.$i.'&type=group'; 
  209. $result = $this->_postWithCookie($uri, $data); 
  210. preg_match('/(.*?)|(.*?)((.*?)/(.*?))/si', $result, $listn); 
  211. if(!$listn[2]){continue;} 
  212. $shuchu.=str_replace(" ","",$listn[2])."(".$listn[4].")n"; 
  213. preg_match('/共(d+)页/si', $result, $zpage); 
  214. preg_match('/共(d+)页/si', $result, $dpage); 
  215. isset($zpage[1]) ? $page=$zpage[1] : $page=$dpage[4]; 
  216. for($j=1;$j<=$page;$j++){ 
  217. $uri = '/im/index/contactlistView.action'; 
  218. $data = 'idContactList='.$i.'&page='.$j; 
  219. $result = $this->_postWithCookie($uri, $data); 
  220. preg_match_all('/(.*?)/si', $result, $matches); 
  221. if(!$matches[1][0]){break;} 
  222. for($x=0;$x<=9;$x++){ 
  223. if(!$matches[1][$x]){continue;} 
  224. $shuchu.=$matches[1][$x]." ".str_replace(" ","",$matches[3][$x])."n"; 
  225. } 
  226. } 
  227. } 
  228. return $shuchu; 
  229. } 
  230. public function getyou() { 
  231. $list=$this->getgroup(); 
  232. for($i=0;$i<=$list;$i++){ 
  233. $uri = '/im/index/contactlistView.action'; 
  234. $data = 'idContactList='.$i.'&type=group'; 
  235. $result = $this->_postWithCookie($uri, $data); 
  236. preg_match('/(.*?)|(.*?)((.*?)/(.*?))/si', $result, $listn); 
  237. if(!$listn[2]){continue;} 
  238. $shuchu.=str_replace(" ","",$listn[2])."(".$listn[4].")n"; 
  239. preg_match('/共(d+)页/si', $result, $zpage); 
  240. preg_match('/共(d+)页/si', $result, $dpage); 
  241. isset($zpage[1]) ? $page=$zpage[1] : $page=$dpage[4]; 
  242. for($j=1;$j<=$page;$j++){ 
  243. $uri = '/im/index/contactlistView.action'; 
  244. $data = 'idContactList='.$i.'&page='.$j; 
  245. $result = $this->_postWithCookie($uri, $data); 
  246. preg_match_all('/(.*?)/si', $result, $matches); 
  247. if(!$matches[1][0]){break;} 
  248. for($x=0;$x<=9;$x++){ 
  249. if(!$matches[1][$x]){continue;} 
  250. $shuchu.=$matches[1][$x]." ".str_replace(" ","",$matches[3][$x])."n"; 
  251. } 
  252. } 
  253. } 
  254. return $shuchu; 
  255. } 
  256. /** 
  257. * 携带Cookie向f.10086.cn发送POST请求 
  258. * @param string $uri 
  259. * @param string $data 
  260. */ 
  261. protected function _postWithCookie($uri, $data) 
  262. { 
  263. $fp = fsockopen('f.10086.cn', 80); 
  264. fputs($fp, "POST $uri HTTP/1.1rn"); 
  265. fputs($fp, "Host: f.10086.cnrn"); 
  266. fputs($fp, "Cookie: {$this->_cookie}rn"); 
  267. fputs($fp, "Content-Type: application/x-www-form-urlencodedrn"); 
  268. fputs($fp, "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1rn"); 
  269. fputs($fp, "Content-Length: ".strlen($data)."rn"); 
  270. fputs($fp, "Connection: closernrn"); 
  271. fputs($fp, $data); 
  272.  
  273. $result = ''; 
  274. while (!feof($fp)) 
  275. { 
  276. $result .= fgets($fp); 
  277. } 
  278.  
  279. fclose($fp); 
  280.  
  281. return $result; 
  282. } 
  283. } 
分享到:
php中操作xml文档程序代码 - php高级应用
php中操作xml文档程序代码 下面我先是介绍一个朋友自己写的一个操作xml文档程序,然后再介绍了php中一个自带的解析xml文档的函数功能,有需要的朋友可参考,代码如下: /*        孙悟空  孙行者  猴精猴精        白骨精  140  幻化万千      ...
解决php curl_multi批处理造成CPU负载过...
解决php curl_multi批处理造成CPU负载过高问题 今天在利用curl_multi函数来获取一些外网内容时发现只要一运行curl_multi函数我的cpu就占得非常的高,后来看一站长分享了此问题解决方法我也整理一下与各位分享一下,希望对大家有帮助. 简单的cURL处理如下,代码如下: $ch = curl_init();...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……