美文网首页我爱编程
Python 调用 Zabbix API 自动创建主机,关联主机

Python 调用 Zabbix API 自动创建主机,关联主机

作者: awker | 来源:发表于2018-06-21 21:36 被阅读0次

    1、编写 Python 脚本调用 Zabbix API 自动创建 Host,关联 Host Group 和 Linked template

    # coding: utf-8
    import requests
    import json
    import uuid
    import sys
    
    
    class ZabbixApi(object):
        def __init__(self, zabbix_url):
            self.zabbix_url = zabbix_url
            self.headers = {"Content-Type": "application/json-rpc"}
            self.data = {
                "jsonrpc": "2.0",
                "id": str(uuid.uuid1()),
                "auth": None
            }
    
        def post_action(self, data):
            req = requests.post(self.zabbix_url, data=json.dumps(data), headers=self.headers)
            dict_data = json.loads(req.text)
            return dict_data['result']
    
        def get_user_token(self, username, password):
            self.data['method'] = "user.login"
            self.data['params'] = {
                "user": username,
                "password": password
            }
            return self.post_action(self.data)
    
        def zabbix_action(self, method, params, auth):
            self.data['method'] = method
            self.data['params'] = params
            self.data['auth'] = auth
            self.data['id'] = str(uuid.uuid1())
            return self.post_action(self.data)
    
    if __name__ == "__main__":
        if len(sys.argv) == 5:
            host_name = sys.argv[1]
            host_ip = sys.argv[2]
            group_name = sys.argv[3]
            template_name = sys.argv[4]
            url = 'http://192.168.1.254/api_jsonrpc.php'
            zabbix_api = ZabbixApi(url)
            auth = zabbix_api.get_user_token('Admin', 'zabbix')
            # 获取 template_id
            method = "template.get"
            params = {
                  "output": "templateid",
                  "filter": {
                      "host": [
                          template_name,
                      ]
                  }
                }
            template_id = zabbix_api.zabbix_action(method, params, auth)
            # 获取 groupid
            method = "hostgroup.get"
            params = {
                "output": "groupid",
                "filter": {
                    "name": [
                        group_name
                    ]
                }
            }
            groupid = zabbix_api.zabbix_action(method, params, auth)
            # 添加 host
            method = "host.create"
            params = {
                "host": host_name,
                "interfaces": [
                    {
                        "type": 1,
                        "main": 1,
                        "useip": 1,
                        "ip": host_ip,
                        "dns": "",
                        "port": "10050"
                    }
                ],
                "groups": groupid,
                "templates": template_id
            }
            result = zabbix_api.zabbix_action(method, params, auth)
            print result
        else:
            print "Usage: " + sys.argv[0] + " host_name host_ip group_name template_name"
            print "Example: " + sys.argv[0] + ' realserver-03 192.168.1.125 "Linux servers" "Template OS Linux"'
    
    

    2、脚本执行结果

    # python zabbix_api.py 
    Usage: zabbix_api.py host_name host_ip group_name template_name
    Example: zabbix_api.py realserver-03 192.168.1.125 "Linux servers" "Template OS Linux"
    
    # python zabbix_api.py realserver-03 192.168.1.125 "Linux servers" "Template OS Linux"
    {u'hostids': [u'10263']}
    
    

    3、查看 Zabbix Web 端


    相关文章

      网友评论

        本文标题:Python 调用 Zabbix API 自动创建主机,关联主机

        本文链接:https://www.haomeiwen.com/subject/vszcyftx.html