コード例 #1
0
def main(send_to, subject, content, value):
    try:
        global sendstatus
        global senderr
        data = ''
        messages = {}
        body = {}
        config_file_path = get_path()
        CorpID = read_config(config_file_path, 'wei', "CorpID")
        CorpSecret = read_config(config_file_path, 'wei', "CorpSecret")
        agentid = read_config(config_file_path, 'wei', "agentid")
        web = read_config(config_file_path, 'wei', "web")
        content = json.loads(content)
        messages["message_url"] = web
        itemid = content[u'监控ID']
        itemvalue = value
        s = re.match(r'^[0-9\.]+$', itemvalue)
        if s:
            body[
                "url"] = web + "history.php?action=showgraph&itemids[]=" + itemid
        else:
            body[
                "url"] = web + "history.php?action=showvalues&itemids[]=" + itemid
        warn_message = ''
        if content[u'当前状态'] == 'PROBLEM':
            body["title"] = "服务器故障"
            warn_message += subject + '\n'
            warn_message += '详情:\n'
            warn_message += '告警等级:' + content[u'告警等级'] + '\n'
            warn_message += '告警时间:' + content[u'告警时间'] + '\n'
            warn_message += '告警地址:' + content[u'告警地址'] + '\n'
            warn_message += '持续时间:' + content[u'持续时间'] + '\n'
            warn_message += '监控项目:' + content[u'监控项目'] + '\n'
            warn_message += content[u'告警主机'] + '故障(' + content[u'事件ID'] + ')'
        else:
            body["title"] = "服务器恢复"
            warn_message += subject + '\n'
            warn_message += '详情:\n'
            warn_message += '告警等级:' + content[u'告警等级'] + '\n'
            warn_message += '恢复时间:' + content[u'恢复时间'] + '\n'
            warn_message += '告警地址:' + content[u'告警地址'] + '\n'
            warn_message += '持续时间:' + content[u'持续时间'] + '\n'
            warn_message += '监控项目:' + content[u'监控项目'] + '\n'
            warn_message += content[u'告警主机'] + '恢复(' + content[u'事件ID'] + ')'
        body["title"] = "服务器故障"
        body['description'] = warn_message
        wx = WeiXin(CorpID, CorpSecret)
        pic_path = getpic(itemid, s)
        picurl = wx.get_imaging(pic_path)
        body['picurl'] = picurl
        data = []
        data.append(body)
        messages['articles'] = data
        data = wx.send_message(toparty=send_to,
                               agentid=agentid,
                               messages=messages)
        sendstatus = True
    except Exception, e:
        senderr = str(e)
        sendstatus = False
コード例 #2
0
def getpic(item_id):
    try:
        config_file_path = get_path()
        user = read_config(config_file_path, 'zabbix', "user")
        passwd = read_config(config_file_path, 'zabbix', "passwd")
        url = read_config(config_file_path, 'wei', "web")
        ppath = get_item_pic(url, user=user, passwd=passwd, itemid=item_id)
        global sendstatus
        sendstatus = True
        return ppath
    except Exception, e:
        global senderr
        senderr = str(e)
        sendstatus = False
コード例 #3
0
def main(send_to, subject, content):
    try:
        global sendstatus
        global senderr
        data = ''
        messages = {}
        body = {}
        config_file_path = get_path()
        CorpID = read_config(config_file_path, 'ding', "CorpID")
        CorpSecret = read_config(config_file_path, 'ding', "CorpSecret")
        agentid = read_config(config_file_path, 'ding', "agentid")
        web = read_config(config_file_path, 'ding', "web")
        toparty = read_config(config_file_path, 'ding', "toparty")
        content = json.loads(content)
        messages["message_url"] = web
        form = []

        body["content"] = subject
        body["rich"] = {"num": content[u'监控取值']}

        if content[u'当前状态'] == 'PROBLEM':
            messages["head"] = {
                "bgcolor": "DBE97659",  #前两位表示透明度
                "text": "服务器故障"
            }
            body["title"] = "服务器故障"
            form.append({'key': '告警等级:', 'value': content[u'告警等级']})
            form.append({'key': '告警时间:', 'value': content[u'告警时间']})
            form.append({'key': '告警地址:', 'value': content[u'告警地址']})
            form.append({'key': '持续时间:', 'value': content[u'持续时间']})
            form.append({'key': '监控项目:', 'value': content[u'监控项目']})
            body['form'] = form
            body["author"] = content[u'告警主机'] + '故障(' + content[u'事件ID'] + ')'
        else:
            messages["head"] = {
                "bgcolor": "DB00AA00",  #前两位表示透明度
                "text": "服务器恢复"
            }
            body["title"] = "服务器恢复"
            form.append({'key': '告警等级:', 'value': content[u'告警等级']})
            form.append({'key': '恢复时间:', 'value': content[u'恢复时间']})
            form.append({'key': '告警地址:', 'value': content[u'告警地址']})
            form.append({'key': '持续时间:', 'value': content[u'持续时间']})
            form.append({'key': '监控项目:', 'value': content[u'监控项目']})
            body['form'] = form
            body["author"] = content[u'告警主机'] + '恢复(' + content[u'事件ID'] + ')'
        messages['body'] = body
        ding = Dingtalk(CorpID, CorpSecret)
        if toparty == '':
            res = ding.get_dept_list()
            # topartname = read_config(config_file_path, 'ding', "topartname")
            for item in res:
                if item['name'] == send_to:
                    toparty = item['id']
                    write_config(config_file_path, 'ding', "toparty", toparty)
        # print ding.get_dept_list()[1]   #65875499钉邮 66029515 自定义 65875504钉盘
        data = ding.send_message(toparty=toparty,
                                 agentid=agentid,
                                 messages=messages)
        sendstatus = True
    except Exception, e:
        senderr = str(e)
        sendstatus = False
コード例 #4
0
def main(send_to, subject, content):
    try:
        global sendstatus
        global senderr
        data = ''
        messages = {}
        body = {}
        config_file_path = get_path()
        CorpID = read_config(config_file_path, 'wei', "CorpID")
        CorpSecret = read_config(config_file_path, 'wei', "CorpSecret")
        agentid = read_config(config_file_path, 'wei', "agentid")
        web = read_config(config_file_path, 'wei', "web")
        author = read_config(config_file_path, 'wei', "author")
        # content = json.loads(content)
        messages["message_url"] = web
        root = etree.fromstring(content)
        itemid = root.xpath(u"监控ID")[0].text
        itemvalue = root.xpath(u"监控取值")[0].text
        s = re.match(r'^[0-9\.]+(\s\w{0,3})?$', itemvalue)
        # s = re.match(r'^[0-9\.]+$', itemvalue.split(' ')[0])
        if s:
            body[
                "content_source_url"] = web + "history.php?action=showgraph&itemids[]=" + itemid
        else:
            body[
                "content_source_url"] = web + "history.php?action=showvalues&itemids[]=" + itemid
        warn_message = ''
        if root.xpath(u"当前状态")[0].text == 'PROBLEM':
            body["title"] = "服务器故障"
            warn_message += subject + '\n'
            warn_message += '详情:\n'
            warn_message += '告警等级:' + root.xpath(u"告警等级")[0].text + '\n'
            warn_message += '告警时间:' + root.xpath(u"告警时间")[0].text + '\n'
            warn_message += '告警地址:' + root.xpath(u"告警地址")[0].text + '\n'
            warn_message += '持续时间:' + root.xpath(u"持续时间")[0].text + '\n'
            warn_message += '监控项目:' + root.xpath(u"监控项目")[0].text + '\n'
            warn_message += root.xpath(u"告警主机")[0].text + '故障(' + root.xpath(
                u"事件ID")[0].text + ')'
        else:
            body["title"] = "服务器恢复"
            warn_message += subject + '\n'
            warn_message += '详情:\n'
            warn_message += '告警等级:' + root.xpath(u"告警等级")[0].text + '\n'
            warn_message += '恢复时间:' + root.xpath(u"恢复时间")[0].text + '\n'
            warn_message += '告警地址:' + root.xpath(u"告警地址")[0].text + '\n'
            warn_message += '持续时间:' + root.xpath(u"持续时间")[0].text + '\n'
            warn_message += '监控项目:' + root.xpath(u"监控项目")[0].text + '\n'
            warn_message += root.xpath(u"告警主机")[0].text + '恢复(' + root.xpath(
                u"事件ID")[0].text + ')'
        body['digest'] = warn_message
        warn_message = ''
        if root.xpath(u"当前状态")[0].text == 'PROBLEM':
            body["title"] = "服务器故障"
            warn_message += '<p><span style="color:#E53333;"><strong>' + subject + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#E56600;"><strong>' + '详情:' + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#4C33E5;"><strong>' + '告警等级:' + root.xpath(
                u"告警等级")[0].text + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#4C33E5;"><strong>' + '告警时间:' + root.xpath(
                u"告警时间")[0].text + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#4C33E5;"><strong>' + '告警地址:' + root.xpath(
                u"告警地址")[0].text + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#4C33E5;"><strong>' + '持续时间:' + root.xpath(
                u"持续时间")[0].text + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#4C33E5;"><strong>' + '监控项目:' + root.xpath(
                u"监控项目")[0].text + '</strong></span></p>' + '\n'
            warn_message += '<p><span><em><span style="color:#E53333;">' + root.xpath(
                u"告警主机")[0].text + '故障(' + root.xpath(
                    u"事件ID")[0].text + ')' + '</span></em><br/></span></p>'
        else:
            body["title"] = "服务器恢复"
            warn_message += '<p><span style="color:#009900;"><strong>' + subject + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#E56600;"><strong>' + '详情:' + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#4C33E5;"><strong>' + '告警等级:' + root.xpath(
                u"告警等级")[0].text + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#4C33E5;"><strong>' + '恢复时间:' + root.xpath(
                u"恢复时间")[0].text + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#4C33E5;"><strong>' + '告警地址:' + root.xpath(
                u"告警地址")[0].text + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#4C33E5;"><strong>' + '持续时间:' + root.xpath(
                u"持续时间")[0].text + '</strong></span></p>' + '\n'
            warn_message += '<p><span style="color:#4C33E5;"><strong>' + '监控项目:' + root.xpath(
                u"监控项目")[0].text + '</strong></span></p>' + '\n'
            warn_message += '<p><span><em><span style="color:#009900;">' + root.xpath(
                u"告警主机")[0].text + '恢复(' + root.xpath(
                    u"事件ID")[0].text + ')' + '</span></em><br/></span></p>'
        body['content'] = warn_message
        body['author'] = author
        wx = WeiXin(CorpID, CorpSecret)
        pic_path = getpic(itemid, s)
        picurl = wx.get_media_ID(pic_path)
        body['thumb_media_id'] = picurl
        data = []
        data.append(body)
        messages['articles'] = data
        data = wx.send_message_mediaid(toparty=send_to,
                                       agentid=agentid,
                                       messages=messages)
        sendstatus = True
    except Exception, e:
        senderr = str(e)
        sendstatus = False
コード例 #5
0
def main(send_to, subject, content):
    try:
        global sendstatus
        global senderr
        data = ''
        messages = {}
        body = {}
        config_file_path = get_path()
        CorpID = read_config(config_file_path, 'wei', "CorpID")
        CorpSecret = read_config(config_file_path, 'wei', "CorpSecret")
        agentid = read_config(config_file_path, 'wei', "agentid")
        web = read_config(config_file_path, 'wei', "web")
        # content = json.loads(content)
        messages["message_url"] = web
        root = etree.fromstring(content)
        itemid = root.xpath(u"监控ID")[0].text
        itemvalue = root.xpath(u"监控取值")[0].text
        s = re.match(r'^[0-9\.]+(\s\w{0,3})?$', itemvalue)
        # s = re.match(r'^[0-9\.]+$', itemvalue.split(' ')[0])
        if s:
            body[
                "url"] = web + "history.php?action=showgraph&itemids[]=" + itemid
        else:
            body[
                "url"] = web + "history.php?action=showvalues&itemids[]=" + itemid
        warn_message = ''
        if root.xpath(u"当前状态")[0].text == 'PROBLEM':
            body["title"] = "服务器故障"
            warn_message += subject + '\n'
            warn_message += '详情:\n'
            warn_message += '告警等级:' + root.xpath(u"告警等级")[0].text + '\n'
            warn_message += '告警时间:' + root.xpath(u"告警时间")[0].text + '\n'
            warn_message += '告警地址:' + root.xpath(u"告警地址")[0].text + '\n'
            warn_message += '持续时间:' + root.xpath(u"持续时间")[0].text + '\n'
            warn_message += '监控项目:' + root.xpath(u"监控项目")[0].text + '\n'
            warn_message += root.xpath(u"告警主机")[0].text + '故障(' + root.xpath(
                u"事件ID")[0].text + ')'
        else:
            body["title"] = "服务器恢复"
            warn_message += subject + '\n'
            warn_message += '详情:\n'
            warn_message += '告警等级:' + root.xpath(u"告警等级")[0].text + '\n'
            warn_message += '恢复时间:' + root.xpath(u"恢复时间")[0].text + '\n'
            warn_message += '告警地址:' + root.xpath(u"告警地址")[0].text + '\n'
            warn_message += '持续时间:' + root.xpath(u"持续时间")[0].text + '\n'
            warn_message += '监控项目:' + root.xpath(u"监控项目")[0].text + '\n'
            warn_message += root.xpath(u"告警主机")[0].text + '恢复(' + root.xpath(
                u"事件ID")[0].text + ')'
        body["title"] = "服务器故障"
        body['description'] = warn_message
        wx = WeiXin(CorpID, CorpSecret)
        pic_path = getpic(itemid, s)
        picurl = wx.get_imaging(pic_path)
        body['picurl'] = picurl
        data = []
        data.append(body)
        messages['articles'] = data
        data = wx.send_message(toparty=send_to,
                               agentid=agentid,
                               messages=messages)
        sendstatus = True
    except Exception, e:
        senderr = str(e)
        sendstatus = False
コード例 #6
0
    # test = zabbixtools()
    # test.template_get()               #模板数
    print test.hostgroup_get()
    # test.host_get()                         ##主机数量
    # test.get_item()                  ## 监控项数量
    # test.get_value("25345", 3)
    # test.host_del()
    #test.host_create()


if __name__ == "__main__":
    # main()
    if len(sys.argv) < 1:
        sys.exit(1)
    config_file_path = get_path()
    web = read_config(config_file_path, 'api', "web")
    user = read_config(config_file_path, 'api', "user")
    passwd = read_config(config_file_path, 'api', "passwd")
    tool = zabbixtools(web, user=user, passwd=passwd)
    for case in switch(sys.argv[1]):
        if case('groups'):
            print tool.hostgroup_get()
            break
        if case('templates'):
            print tool.template_get()
            break
        if case('hosts'):
            print tool.host_get()
            break
        if case('items'):
            print tool.get_item()