iTop文档
https://www.itophub.io/wiki/page?id=3_0_0:advancedtopics:rest_json
代码
[root@localhost ~]# cat alert_itop.py
#!/usr/bin/env python
#coding: utf-8
import requests
import json
from flask import Flask, request, jsonify
def itop(data, employee_number):
itopurl = "http://192.168.233.128:8000/webservices/rest.php"
for key in data['alerts']:
print(key)
message = {
"operation": "core/create",
"comment": "Synchronization from Alertmanager...",
"class": "UserRequest",
"output_fields": "id, friendlyname",
"fields": {
"org_id": "SELECT Organization WHERE name = \"IT Department\"",
"caller_id": { "employee_number": employee_number },
"title": key['labels']['alertname'],
"description": """【监控告警信息】
告警名称:%s
告警级别:%s
实例地址:%s
告警详情:%s
告警时间:%s""" % (key['labels']['alertname'], key['labels']['severity'], key['labels']['instance'], key['annotations']['description'], key['startsAt'])
}
}
opr = json.dumps(message)
payload={
"version":"1.0",
"json_data":opr
}
header = {
# echo -n '账户:密码'|base64
# 通过base64生成 Y3VpaGFvOmdvbmduaXVKU0AyMDVC
"Authorization": "Basic Y3VpaGFvOmdvbmduaXVKU0AyMDVC"
}
r = requests.post(url=itopurl, data=payload, headers=header)
dict_r = json.loads(r.content)
print(dict_r)
app = Flask(__name__)
app.debug = True
@app.route('/alert/itop/',methods=['post', 'get'])
def post_http():
employee_number = request.args.get('employee_number')
print(employee_number)
params= request.data.decode('utf-8')
#print(params)
prams = json.loads(params)
itop(prams, employee_number)
return jsonify(prams)
if __name__ == '__main__':
app.run(host='192.168.233.128',port=1234)
alertmanager配置
receivers:
- name: 'web.hook'
webhook_configs:
- url: 'http://192.168.233.128:1234/alert/itop/?employee_number=10010'