예제 #1
0
 def initialize(self):
     super(SendTextHandler, self).initialize()
     self.wxModel = WxMsgSendDetailModel()
     self.p_jmx = re.compile("(.+?) JMX is not reachable")
     self.p_agent = re.compile(
         "Zabbix agent on (.+?) is unreachable for 5 minutes")
     self.p_time = re.compile("(.+?) Host local time error")
예제 #2
0
파일: wechat.py 프로젝트: MinjunHuang/wx2p
class SendTextHandler(handler.base.BaseHandler):
    def initialize(self):
        super(SendTextHandler, self).initialize()
        self.wxModel = WxMsgSendDetailModel()
        self.p_jmx = re.compile("(.+?) JMX is not reachable")
        self.p_agent = re.compile(
            "Zabbix agent on (.+?) is unreachable for 5 minutes")

    @asynchronous
    @gen.coroutine
    def get(self):
        args = self.request.arguments
        logging.info('arguments: %s' % json.dumps(args))

        user_str = self.get_argument("to_user", None)
        content = self.get_argument("content", None)

        is_match = self.p_jmx.search(content) or self.p_agent.search(content)
        if is_match:
            self.write(json.dumps(dict(errCode=0, errMsg='pass')))
            self.finish()
            return

        if None in [user_str, content]:
            ret = dict(errCode=10001,
                       errMsg='Missing parameter to_user/content')
            self.write(json.dumps(ret))
            self.finish()
            return

        try:
            self.wxModel.content = content
            self.wxModel.send_to = user_str
            self.wxModel.clock = int(time.time())
            self.wxModel.uptime = int(time.time())
            self.wxModel.save()
            issue_id = self.wxModel.id
        except Exception, e:
            logging.error('Message content saved failed:%s' % str(e))

        users = user_str.split(',') if not isinstance(user_str,
                                                      list) else user_str

        if self._redis and self._redis.get('link'):
            link = "<a href='http://%s/issue/%s'>点我</a>" % (
                self.request.headers.get('Host'), issue_id)
            content = '%s %s' % (content, link)

        status, resp = self.wcep.send_msg2user(self.access_token,
                                               content,
                                               to_user=users,
                                               to_ptmt=None)
        if not status:
            logging.error('Response from wx: ' + json.dumps(resp))
            ret = dict(errCode=10002, errMsg=resp)
        else:
            ret = dict(errCode=0, errMsg='')

        self.write(json.dumps(ret))
        self.finish()
예제 #3
0
파일: test.py 프로젝트: zhangalbert/wx2p
class ModelTestHandler(handler.base.BaseHandler):
    def initialize(self):
        super(ModelTestHandler, self).initialize()
        self.wxModel = WxMsgSendDetailModel()

    def get(self):
        content = self.get_argument('content', 'xxx')
        send_to = 'heruihong'
        event_id = 110
        self.wxModel.content = content
        self.wxModel.send_to = send_to
        self.wxModel.event_id = event_id

        self.wxModel.save()
예제 #4
0
파일: test.py 프로젝트: zhangalbert/wx2p
 def initialize(self):
     super(ModelTestHandler, self).initialize()
     self.wxModel = WxMsgSendDetailModel()
예제 #5
0
class SendTextHandler(handler.base.BaseHandler):
    def initialize(self):
        super(SendTextHandler, self).initialize()
        self.wxModel = WxMsgSendDetailModel()
        self.p_jmx = re.compile("(.+?) JMX is not reachable")
        self.p_agent = re.compile(
            "Zabbix agent on (.+?) is unreachable for 5 minutes")
        self.p_time = re.compile("(.+?) Host local time error")

    @asynchronous
    @gen.coroutine
    def get(self):
        args = self.request.arguments
        logging.info('arguments sync: %s' % json.dumps(args))

        user_str = self.get_argument("to_user", None)
        content = self.get_argument("content", None)
        content = json.loads(content)
        event_id = content['eventid']
        status = content['status']
        trigger_name = content['trigger_name']

        is_match = self.p_jmx.search(trigger_name) or self.p_agent.search(
            trigger_name) or self.p_time.search(trigger_name)
        if is_match:
            self.write(json.dumps(dict(errCode=0, errMsg='pass')))
            self.finish()
            return

        if None in [user_str, trigger_name]:
            ret = dict(errCode=10001,
                       errMsg='Missing parameter to_user/content')
            self.write(json.dumps(ret))
            self.finish()
            return

        try:
            self.wxModel.content = trigger_name
            self.wxModel.send_to = user_str
            self.wxModel.clock = int(time.time())
            self.wxModel.uptime = int(time.time())
            self.wxModel.save()
            issue_id = self.wxModel.id
        except Exception, e:
            logging.error('Message content saved failed:%s' % str(e))

        users = user_str.split(',') if not isinstance(user_str,
                                                      list) else user_str

        if self._redis and self._redis.get('add_link') and status in [
                0, '0'
        ] and 'heruihong' in users:
            link_str = "<a style=\"color:red;\">Click</a>"
            trigger_name = '%s %s' % (trigger_name, link_str)

        if status in [1, '1']:
            trigger_name = '%s %s' % (trigger_name, '<已恢复>')

        status, resp = self.wcep.send_msg2user(self.access_token,
                                               trigger_name,
                                               to_user=users,
                                               to_ptmt=None)
        if not status:
            logging.error('Response from wx: ' + json.dumps(resp))
            ret = dict(errCode=10002, errMsg=resp)
        else:
            ret = dict(errCode=0, errMsg='')

        self.write(json.dumps(ret))
        self.finish()