作用:

根据提交过来的groupid值,更新group监控组名称。

代码:

class Zabbix_UpdateGroup():
    def __init__(self,group_id,group_name):
        self.group_id = group_id
        self.group_name = group_name

    def update(self):
        '''
        更新监控群组的信息
        :return: 当创建成功,返回创建群组的ID值以前群组名,如创建失败,返回None
        '''
        ZABBIX_URL = 'http://10.3.153.14:9000'

        url = "{}/api_jsonrpc.php".format(ZABBIX_URL)
        header = {"Content-Type": "application/json"}

        # 使用hostgroup.update方法
        data = {
            "jsonrpc": "2.0",
            "method": "hostgroup.update",
            "params": {
                "groupid": self.group_id,
                "name": self.group_name
            },
            "id": 2,
            "auth": "924ba62dfdc27ceec1b80b4eef1e1fb7"
        }
        # 由于API接收的是json字符串,故需要转化一下
        value = json.dumps(data).encode('utf-8')

        # 对请求进行包装
        req = request.Request(url, headers=header, data=value)

        # 验证并获取Auth ID
        try:
            # 打开包装过的url
            result = request.urlopen(req)
        except Exception as e:
            print("异常数据:", e)
        else:
            response = result.read()
            # 上面获取的是bytes类型数据,故需要decode转化成字符串
            page = response.decode('utf-8')
            # 将此json字符串转化为python字典
            page = json.loads(page)
            # print(page.get('result'))
            ret = page.get('result')
            # result.close()
            return ret

结果:

成功返回监控群组ID