1. 程式人生 > >自動註冊到zabbix的服務

自動註冊到zabbix的服務

使用說明:

1,該服務部署在zabbix server主機上,或者其他可以正常訪問zbabix server的主機上,服務需要呼叫zabbix server的API;

2,程式碼基於flask,可以監聽在指定的埠上等待呼叫介面,建議通過supervisor進行管理;

3,介面說明:

curl -L http://192.168.12.175:10052/regist/<hostCreate>/<visiblename>

引數:

        (1)hostCreate:Zabbix server端新增新host的介面,輸入的Host name值,該值必須與zabbix_agentd.conf檔案中配置的Hostname一致;

        (2)visiblename:Zabbix server端新增新host的介面,輸入的Visible name值;

       (3)傳遞的上述兩個引數的值如果已經存在於zabbix server端,則會因為重複而無法成功新增主機;

 

#-*- coding: utf-8 -*-

from flask import Flask, request,json
import requests,time

zabbixurl = "http://127.0.0.1/api_jsonrpc.php"

config = {
                "jsonrpc": "2.0",
                "method": "host.create",
                "params": {
                    "host": "2.2.3.1hh",
                    "name": "AAAAAAA-2.2.2.112hh",
                    "interfaces": [{
                        "type": 1,
                        "main": 1,
                        "useip": 1,
                        "ip": "ip",
                        "dns": "",
                        "port": "10050"
                    }],
                    "groups": [{
                        "groupid": "2"
                    }],
                    "templates": [{
                            "templateid": "10001"
                        },
                        {
                            "templateid": "10106"
                        },
                        {
                            "templateid": "10107"
                        }
                    ]
                },
                "auth": "authID",
                "id": 1
            }

#獲取登入authID,後續呼叫其他介面都要用到
def user_login():
    zabbixurl = "http://127.0.0.1/api_jsonrpc.php"
    data = {
        "jsonrpc": "2.0",
        "method": "user.login",
        "params": {
            "user": "autoregister",
            "password": "XXXXXXXXXX"
        },
        "id": 0
    }
    headers = {
        'Content-Type': 'application/json'
    }
    res = requests.post(zabbixurl, data=json.dumps(data), headers=headers)
    return json.loads(res.text)['result']

#新增新host到zabbix server
def host_create(hostCreate,visiblename,authID):
    config['auth'] = authID
    config['params']['host'] = hostCreate
    config['params']['name'] = visiblename
    config['params']['interfaces'][0]['ip'] = str(request.remote_addr)

    headers = {
        'Content-Type': 'application/json'
    }
    res = requests.post(zabbixurl, data=json.dumps(config), headers=headers)
    return res.text

#成功與否傳送告警資訊到釘釘群
def send_message(info):
    headers = {
        'Content-type':'application/json'
    }
    payload = {
        'from':'devops.registToZabbix',
        'system':'devops',
        'message':info,
        'level':'INFO',
        'title':'New host regist to zabbix ',
        'time':time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    }
    res = requests.post("http://YOURHOST:PORT/api/send", data=json.dumps(payload),headers=headers)

#初始化Flask
app = Flask(__name__)
@app.route('/regist/<hostCreate>/<visiblename>')
def register(hostCreate,visiblename):
    myAuthID = user_login()
    result = host_create(hostCreate,visiblename,myAuthID)
    resobj = json.loads(result)

    try:
        if 'result' in resobj.keys():
            send_message('add host %s to zabbix success' %request.remote_addr)
            return 'success'
        else:
            send_message('add host %s to zabbix failed' % request.remote_addr)
    except Exception,e:
        print e
        send_message('add host %s to zabbix failed' % request.remote_addr)
    return 'failed'

if __name__ == '__main__':
    app.run(host = '0.0.0.0', port = 10052)