码迷,mamicode.com
首页 > Windows程序 > 详细

zabbix通过api批量添加web监控

时间:2016-02-27 01:11:49      阅读:1226      评论:0      收藏:0      [点我收藏+]

标签:zabbix web监控

zabbix 添加web监控始终是大问题,不能自动发现只能手动添加


写了个py脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import urllib2
import sys
class zabbixtools:
    def __init__(self):
        self.url = "Zabbixserver"
        self.header = {"Content-Type": "application/json"}
        self.authID = self.user_login()
    def user_login(self):
        data = json.dumps(
                {   
                    "jsonrpc": "2.0",
                    "method": "user.login",
                    "params": { 
                        "user": "USER",
                        "password": "PASSWORD"
                        },
                    "id": 0
                    })
        request = urllib2.Request(self.url,data)
        for key in self.header:
            request.add_header(key,self.header[key])
        try:
            result = urllib2.urlopen(request)
        except URLError as e:
            print "Auth Failed, Please Check Your Name And Password:",e.code
        else:
            response = json.loads(result.read())
            result.close()
            authID = response[‘result‘]
            return authID
    def get_data(self,data,hostip=""):
        request = urllib2.Request(self.url,data)
        for key in self.header:
            request.add_header(key,self.header[key])
        try:
            result = urllib2.urlopen(request)
        except URLError as e:
            if hasattr(e, ‘reason‘):
                print ‘We failed to reach a server.‘
                print ‘Reason: ‘, e.reason
            elif hasattr(e, ‘code‘):
                print ‘The server could not fulfill the request.‘
                print ‘Error code: ‘, e.code
            return 0
        else:
            response = json.loads(result.read())
            result.close()
            return response
    def host_create(self):
        data = json.dumps(
                    {
                        "jsonrpc": "2.0",
                        "method": "httptest.create",
                        "params": {
                            "hostid": "10788",
                            "name": "Homepage",
                            "steps": [
                                {
                                    "name": "1",
                                    "url": "http://mycompany.com",
                                    "no": 1
                                }
                            ]
                    },
                        "auth": self.authID,
                        "id": 1,
                        })
        res = self.get_data(data)
        print res

def main():
    test = zabbixtools()
    test.host_create()
if __name__ == "__main__":
    main()


zabbix通过api批量添加web监控

标签:zabbix web监控

原文地址:http://superbigsea.blog.51cto.com/6862263/1745399

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!