因为忙着写项目。所以没有时间来回倒腾!因此开始讨厌写一些技术博客,所以从本节开始放弃,刚入门就要放弃了!可能我是一个不善于分享的人,也可能我是一个自私的人。不得不承认白羊座的人做事情确实是三分钟热度。昨天还想着好好的写一篇系列文章,除了当是给自己做一些总结之外,也可以帮助到有需要的朋友。不过,确实是三分钟热度了,今天是拔凉拔凉的。
好了,下面是获取IP,HOSTS,HOSTID,GROUPID,ITEMID的Python代码,随便贴点,希望起到抛砖引玉的效果。当然,也是不完整的。
# -*- coding:utf-8 -*- class Token: """Request zabbix server token""" __REQ = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": ZBXUSER, "password": ZBXPASS }, "id": 0 } @classmethod def request(cls): try: try: return ZBXHttp.request(Token.__REQ)['result'] except Exception: raise MonAlarmException('Invalid username or password, unable to request zabbix token') except MonAlarmException as e: e.error() return Response.format(errorDesc=e, errorCode=MonAlarmErrCode.UNABLE_REQ_TOKEN) class BasicData: __REQ = { "jsonrpc": "2.0", "method": "host.get", "params": { "output": ['host'], 'selectInterfaces': ['ip'], 'selectGroups': '' }, "auth": Token.request(), "id": 1 } @classmethod def __request(cls): result = ZBXHttp.request(BasicData.__REQ) err = result.get('error', None) if err: try: raise MonAlarmException('Unable to request basic data, Session terminated, re-login, please.') except MonAlarmException as e: e.error() return Response.format(errorDesc=e, errorCode=MonAlarmErrCode.UNABLE_REQ_BASICDATA) else: return result @classmethod def get_summary(cls): ip = [] host = [] hostid = [] groupid = [] result = cls.__request()['result'] for item in result: ip.append(item['interfaces'][0]['ip']) host.append(item['host']) hostid.append(item['hostid']) groupid.append(item['groups'][0]['groupid']) return [ip, host, hostid, groupid] class ItemsID: @classmethod def request(cls,host_id, item_name): __REQ = { "jsonrpc": "2.0", "method": "item.get", "params": { "output": "itemid", "hostids": host_id, "search": {"key_": item_name} }, "auth": Token.request(), "id": 2 } try: return ZBXHttp.request(__REQ)['result'][0]['itemid'] except Exception: return 0
# -*- coding:utf-8 -*- class Action: @classmethod def get_cpu_data(cls): summary = BasicData.get_summary() ip = summary[0] host = summary[1] hostid = summary[2] groupid = summary[3] count = len(summary[0]) for index in range(count): itemid = ItemsID.request(hostid[index], CpuItem.item_cpu_used) print({ 'ip': ip[index], 'host': host[index], 'hostid': hostid[index], 'groupid': groupid[index], 'itemname': CpuItem.item_cpu_load, 'itemid': itemid }) if __name__ == '__main__': Action.get_cpu_data()
原文地址:http://blog.51cto.com/freshair/2133274
时间: 2024-11-07 06:15:36