分享

自己架设一个监控宝

 看见就非常 2015-04-19
本帖最后由 lazypeople 于 2012-12-5 09:58 编辑

众所周知,在做互联网这块,需要一个报警系统,例如你在外面K歌的时候网站挂了,你可能还在玩的很high,那也不能总是一直坐在电脑前盯着。所以需要架设一个监控,监控报警应该分两个方面,首先需要搭建一个发送邮件,发送短信的接口,根据故障的级别选择是发短信还是发邮件。邮件这块相对比较简单,在sae上就可以自己创建一个发送邮件的接口。本文主要讲述一个如何在“省钱”的前提下给自己发送短信。
前提就是你是中国移动用户,且使用了飞信业务,当然这个业务也是不要钱的。直接上代码(我集中到自己的系统中了,项目是勇敢的lazyphp框架 http:///lazyphp/):
调用飞信的基类: 这个文件放在(/function/下)
PHP代码
  1. <?php
  2. /**
  3. * PHP飞信发送类
  4. *
  5. * @author quanhengzhuang <blog.quanhz.com>
  6. * @version 1.5.0
  7. */
  8. class PHPFetion
  9. {

  10.   /**
  11.   * 发送者手机号
  12.   * @var string
  13.   */
  14.   protected $_mobile;

  15.   /**
  16.   * 飞信密码
  17.   * @var string
  18.   */
  19.   protected $_password;

  20.   /**
  21.   * Cookie字符串
  22.   * @var string
  23.   */
  24.   protected $_cookie = '';

  25.   /**
  26.   * Uid缓存
  27.   * @var array
  28.   */
  29.   protected $_uids = array();

  30.   /**
  31.   * csrfToken
  32.   * @var string
  33.   */
  34.   protected $_csrfToten = null;

  35.   /**
  36.   * 构造函数
  37.   * @param string $mobile 手机号(登录者)
  38.   * @param string $password 飞信密码
  39.   */
  40.   public function __construct($mobile, $password)
  41.   {
  42.     if ($mobile === '' || $password === '')
  43.     {
  44.     return;
  45.     }
  46.     
  47.     $this->_mobile = $mobile;
  48.     $this->_password = $password;
  49.     
  50.     $this->_login();
  51.   }

  52.   /**
  53.   * 析构函数
  54.   */
  55.   public function __destruct()
  56.   {
  57.     $this->_logout();
  58.   }

  59.   /**
  60.   * 登录
  61.   * @return string
  62.   */
  63.   protected function _login()
  64.   {
  65.     $uri = '/huc/user/space/login.do?m=submit&fr=space';
  66.     $data = 'mobilenum='.$this->_mobile.'&password='.urlencode($this->_password);
  67.     
  68.     $result = $this->_postWithCookie($uri, $data);

  69.     //解析Cookie
  70.     preg_match_all('/.*?\r\nSet-Cookie: (.*?);.*?/si', $result, $matches);
  71.     if (isset($matches[1]))
  72.     {
  73.     $this->_cookie = implode('; ', $matches[1]);
  74.     }
  75.     
  76.     $result = $this->_postWithCookie('/im/login/cklogin.action', '');

  77.     return $result;
  78.   }

  79.   /**
  80.   * 向指定的手机号发送飞信
  81.   * @param string $mobile 手机号(接收者)
  82.   * @param string $message 短信内容
  83.   * @return string
  84.   */
  85.   public function send($mobile, $message)
  86.   {
  87.     if ($message === '')
  88.     {
  89.     return '';
  90.     }

  91.     //判断是给自己发还是给好友发
  92.     if ($mobile == $this->_mobile)
  93.     {
  94.     return $this->_toMyself($message);
  95.     }
  96.     else
  97.     {
  98.     $uid = $this->_getUid($mobile);

  99.     return $uid === '' ? '' : $this->_toUid($uid, $message);
  100.     }
  101.   }

  102.   /**
  103.   * 获取飞信ID
  104.   * @param string $mobile 手机号
  105.   * @return string
  106.   */
  107.   protected function _getUid($mobile)
  108.   {
  109.     if (emptyempty($this->_uids[$mobile]))
  110.     {
  111.     $uri = '/im/index/searchOtherInfoList.action';
  112.     $data = 'searchText='.$mobile;
  113.     
  114.     $result = $this->_postWithCookie($uri, $data);
  115.     
  116.     //匹配
  117.     preg_match('/toinputMsg\.action\?touserid=(\d+)/si', $result, $matches);

  118.     $this->_uids[$mobile] = isset($matches[1]) ? $matches[1] : '';
  119.     }
  120.     
  121.     return $this->_uids[$mobile];
  122.   }

  123.   /**
  124.   * 获取csrfToken,给好友发飞信时需要这个字段
  125.   * @param string $uid 飞信ID
  126.   * @return string
  127.   */
  128.   protected function _getCsrfToken($uid)
  129.   {
  130.     if ($this->_csrfToten === null)
  131.     {
  132.     $uri = '/im/chat/toinputMsg.action?touserid='.$uid;
  133.     
  134.     $result = $this->_postWithCookie($uri, '');
  135.     
  136.     preg_match('/name="csrfToken".*?value="(.*?)"/', $result, $matches);

  137.     $this->_csrfToten = isset($matches[1]) ? $matches[1] : '';
  138.     }

  139.     return $this->_csrfToten;
  140.   }

  141.   /**
  142.   * 向好友发送飞信
  143.   * @param string $uid 飞信ID
  144.   * @param string $message 短信内容
  145.   * @return string
  146.   */
  147.   protected function _toUid($uid, $message)
  148.   {
  149.     $uri = '/im/chat/sendMsg.action?touserid='.$uid;
  150.     $csrfToken = $this->_getCsrfToken($uid);
  151.     $data = 'msg='.urlencode($message).'&csrfToken='.$csrfToken;
  152.     
  153.     $result = $this->_postWithCookie($uri, $data);
  154.     
  155.     return $result;
  156.   }

  157.   /**
  158.   * 给自己发飞信
  159.   * @param string $message
  160.   * @return string
  161.   */
  162.   protected function _toMyself($message)
  163.   {
  164.     $uri = '/im/user/sendMsgToMyselfs.action';
  165.     $result = $this->_postWithCookie($uri, 'msg='.urlencode($message));

  166.     return $result;
  167.   }

  168.   /**
  169.   * 退出飞信
  170.   * @return string
  171.   */
  172.   protected function _logout()
  173.   {
  174.     $uri = '/im/index/logoutsubmit.action';
  175.     $result = $this->_postWithCookie($uri, '');
  176.     
  177.     return $result;
  178.   }

  179.   /**
  180.   * 携带Cookie向f.10086.cn发送POST请求
  181.   * @param string $uri
  182.   * @param string $data
  183.   */
  184.   protected function _postWithCookie($uri, $data)
  185.   {
  186.     $fp = fsockopen('f.10086.cn', 80);
  187.     fputs($fp, "\"static/image/smiley/default/titter.gif\"" smilieid="\"9\"" alt="\"\"" border="\"0\"">OST $uri HTTP/1.1\r\n");
  188.     fputs($fp, "Host: f.10086.cn\r\n");
  189.     fputs($fp, "Cookie: {$this->_cookie}\r\n");
  190.     fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
  191.     fputs($fp, "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1\r\n");
  192.     fputs($fp, "Content-Length: ".strlen($data)."\r\n");
  193.     fputs($fp, "Connection: close\r\n\r\n");
  194.     fputs($fp, $data);

  195.     $result = '';
  196.     while (!feof($fp))
  197.     {
  198.     $result .= fgets($fp);
  199.     }

  200.     fclose($fp);

  201.     return $result;
  202.   }

  203. }
自定义封装接口部分:
PHP代码
  1. <?php
  2. if( !defined('IN') ) die('bad request');
  3. include_once( AROOT . 'controller'.DS.'app.class.php' );
  4. include_once( AROOT . 'controller'.DS.'function.class.php' );
  5. include_once( AROOT . 'function'.DS.'phpfetion.php' );
  6. @session_start();
  7. class messageController extends appController
  8. {
  9.     function __construct()
  10.     {
  11.       parent::__construct();
  12.       $this->username = '手机号';
  13.       $this->password = '飞信密码';
  14.       $this->fetion = new PHPFetion($this->username, $this->password);
  15.     }
  16.     
  17.     function index()
  18.     {
  19.       $message = v('message');
  20.       if($message == null) {
  21.         die('fobbiden!');
  22.       }
  23.       $token = '81e6ec089e7690638bd074f0b977cf38';//定义一个token防止被滥用
  24.       if(v('token') != $token) {
  25.         die('fobbiden!');
  26.       }
  27.       $tel = v('phone');
  28.       if($tel == null) {
  29.         $tel = '你的手机号';
  30.       }
  31.       $result = $this->fetion->send($tel, $message);
  32.     }
  33. }
哈,这时候就自己封装好了一个短信接口了,例如上述文件你部署在http://xxxx.sinaapp.com,那么在任意地方你想调用的时候只需要按照如下的格式发送一个post请求就可以了。封装一个函数
PHP代码
  1. private function send_message($data)
  2.   {
  3.     $url = 'http://xxxx.sinaapp.com/?c=message&token=81e6ec089e7690638bd074f0b977cf38';
  4.       $post_data = $data;
  5.       $ch = curl_init();
  6.       curl_setopt($ch, CURLOPT_URL, $url);
  7.       curl_setopt($ch, CURLOPT_HEADER, 0);
  8.       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  9.       curl_setopt($ch, CURLOPT_POST, 1);
  10.       curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  11.       $output = curl_exec($ch);
  12.       if ($output === FALSE) {
  13.         echo "cURL Error: " . curl_error($ch);
  14.       }
  15.       curl_close($ch);
  16.   }
调用:
PHP代码
  1. $tel = '你的手机号';
  2. $message = '晚安。';
  3.         $post_data = array(
  4.         'message' => $message,
  5.         'phone' => $tel,
  6.         );
  7.         $this->send_message($post_data);
以上的部分就是一个免费的短信发送接口了,可以做很多事情,例如你定时的给你的女友发发晚安,或者给你的朋友定时发发天气预报,可以结合sae的cron服务定时的触发你想要干的任意事情。

下面给出一个监控部分的代码,当然这个部分现在需要一个可以执行shell的环境下才能执行,可以用sws实现,当然用php也可以模拟实现,例如设置一个curl超时时间,如果超时就触发~
要监控的网站按照如下的格式写到一个txt文件:(文件名 check.txt)
PHP代码
  1. www.changes.com.cn,独立域名绑定测试
  2. lazypeople.sinaapp.com,sae app monitor
  3. jp.cname.,sae海外域名绑定节点
扫描代码:
PHP代码
  1. <?php
  2. $fp_in = fopen('check.txt', "r");
  3. while (!feof($fp_in)) {
  4.   $line = fgets($fp_in);
  5.   $array = explode(",", $line);
  6.   $host = $array[0];
  7.   if($host != '') {
  8.     $intro = $array[1];
  9.     $shell = "ping -c 2 -W 1 ".$host;
  10.       $out = shell_exec($shell);
  11.       $out = trim($out);
  12.       $out_array = explode(",", $out);
  13.       $recieve = trim($out_array[1]);
  14.       $num = explode(" ", $recieve);
  15.       if($num[0] < 2) {
  16.         //alert
  17.         $message = $intro.' Status:down!Ping info:'.$out;
  18.         $post_data = array(
  19.             'message' => $message,
  20.             'phone' => '你的手机号',
  21.             );
  22.         if($out != '')
  23.         {
  24.             send_message($post_data);
  25.           echo $host.' status:bad!<br/>';
  26.         }
  27.       } else {
  28.         echo $host.' status\"\"k!<br/>';
  29.       }
  30.   }
  31. }

  32. function send_message($data)
  33. {
  34.     $url = 'http://xxxx.sinaapp.com/?c=message&token=81e6ec089e7690638bd074f0b977cf38';
  35.     $post_data = $data;
  36.     $ch = curl_init();
  37.     curl_setopt($ch, CURLOPT_URL, $url);
  38.     curl_setopt($ch, CURLOPT_HEADER, 0);
  39.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  40.     curl_setopt($ch, CURLOPT_POST, 1);
  41.     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  42.     $output = curl_exec($ch);
  43.     if ($output === FALSE) {
  44.     echo "cURL Error: " . curl_error($ch);
  45.     }
  46.     curl_close($ch);
  47. }
命名为check.php,下面只需要加一条cron定时触发就行了。
PHP代码
  1. */2 * * * * root /usr/bin/curl -m 500 "http://你的地址/check.php" >> /var/lazy/monitor.txt
希望对大家有帮助~
ps:打个广告!如果不是移动用户怎么办,那就是sae的短信服务了,没有运营商限制,只要一毛一条,高效高到达~http://sae.sina.com.cn/?m=apistore&a=detail&service_code=AF&type=0



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约