• zabbix api 设置维护模式


    1. 通过zabbix提供的api进行维护模式的设置

    #!/usr/bin/env python
    # -*-coding:utf-8-*-
    import urllib
    import urllib2
    import json
    import sys
    import platform
    import time
     
     
    def auth(uid, username, password, api_url):
        """
        zabbix认证
        :param uid:
        :param username:
        :param password:
        :return:
        """
        dict_data = {}
        dict_data['method'] = 'user.login'  # 方法
        dict_data['id'] = uid  # 用户id
        dict_data['jsonrpc'] = "2.0"  # api版本
        dict_data['params'] = {"user": username, "password": password}  # 用户账号密码
        jdata = json.dumps(dict_data)  # 格式化json数据
        content = post_data(jdata, api_url)  # post json到接口
        return content  # 返回信息
     
     
    def post_data(jdata, url):
        """
        POST方法
        :param jdata:
        :param url:
        :return:
        """
        req = urllib2.Request(url, jdata, {'Content-Type': 'application/json'})
        response = urllib2.urlopen(req)
        # content = response.read()
        content = json.load(response)
        return content
     
     
    def create_maintenance(name, hostid, active_since, active_till, period, auth_code, api_url):
        """
        create maintenance
        :return:
        """
        dict_data = {}
        dict_data['method'] = 'maintenance.create'  # 方法
        dict_data['id'] = uid  # 用户id
        dict_data['jsonrpc'] = "2.0"  # api版本
        dict_data['auth'] = auth_code  # api版本
        dict_data['description'] = "UPDATE" + hostid  # api版本
        # host
        hostids = [hostid]
        # timeperiods
        timeperiods = [{"timeperiod_type": 0, "start_time": 64800, "period": period}]
        dict_data['params'] = {"name": name, "active_since": active_since, "timeperiods": timeperiods,
                               "active_till": active_till, "hostids": hostids}  # 用户账号密码
        jdata = json.dumps(dict_data)  # 格式化json数据
        content = post_data(jdata, api_url)  # post json到接口
        print content
        return content  # 返回信息
     
     
    def get_hostid(hostname, auth_code, uid, api_url):
        """
        use hostname get hostid
        :param hostname:
        :param auth:
        :param uid:
        :return:
        """
        dict_data = {}
        dict_data['method'] = 'host.getobjects'  # 方法
        dict_data['id'] = uid  # 用户id
        dict_data['jsonrpc'] = "2.0"  # api版本
        dict_data['params'] = {"name": hostname}  # 主机名
        dict_data['auth'] = auth_code  # auth串
        jdata = json.dumps(dict_data)  # 格式化json数据
        content = post_data(jdata, api_url)  # post json到接口
        print content
        return content  # 返回信息
     
     
    def logout(uid, auth_code, api_url):
        """
        退出
        :param uid:
        :param auth_code:
        :return:
        """
        dict_data = {}
        dict_data['method'] = 'user.logout'  # 方法
        dict_data['id'] = uid  # 用户id
        dict_data['jsonrpc'] = "2.0"  # api版本
        dict_data['params'] = []
        dict_data['auth'] = auth_code  # auth串
        jdata = json.dumps(dict_data)  # 格式化json数据
        content = post_data(jdata, api_url)  # post json到接口
        return content  # 返回信息
     
     
    if __name__ == '__main__':
        # user info
        uid = 1 # 用户ID
        username = 'admin'
        password = '123456'
        api_url = "http://192.168.1.100/api_jsonrpc.php"
        res = auth(1, username, password, api_url)  # 认证
        if res['result']:
            auth_code = res['result']  # 认证串
            hostname = platform.node()  # 主机名
            res = get_hostid(hostname, auth_code, uid, api_url)
            if res['result']:
                period = 600  # 维护时长
                active_since = int(time.time())  # 开始时间
                active_till = int(time.time()) + period  # 结束时间
                hostid = res['result'][0]['hostid']  # 主机
                res = create_maintenance('AutoMaintenance_' + hostname + '_' + str(active_since), hostid, active_since, active_till, period,
                                         auth_code, api_url)  # 创建维护
                logout(uid, auth_code, api_url)  # 退出登录
                print res
        else:
            pass
    

    备注:以上脚本适用于zabbix2.4及以下版本

    需要修改uid、username、password、api_url以及res中的auth函数的uid值,通过修改period的值来修改维护时长。

    该脚本通过platform模块获取到agent的主机名,确保server端的主机名与客户端一致,且存在,否则设置不成功。

    设置完成后可在Configuration->Maintenance页面中查看设置的维护模板。

    可以通过在Configuration->Hosts页面中查看是否生效。

  • 相关阅读:
    DevExpress之Treelist应用
    空间参考及坐标转换
    ArcGIS API for JavaScript开发环境搭建及第一个实例demo
    SQL中字符串拼接
    Json.net实现方便的Json转C#(dynamic动态类型)对象
    第四章:C#如何生成JSON字符串提交给接口(服务器)
    第三章:C#如何生成JSON字符串?(序列化对象)
    第二章:C#如何解析JSON数据?(反序列化对象)
    Eucalyptus4.0 管理页面介绍
    Eucalyptus常用查询命令
  • 原文地址:https://www.cnblogs.com/aslongas/p/6016822.html
Copyright © 2020-2023  润新知