您好,登錄后才能下訂單哦!
因為忙著寫項目。所以沒有時間來回倒騰!因此開始討厭寫一些技術博客,所以從本節開始放棄,剛入門就要放棄了!可能我是一個不善于分享的人,也可能我是一個自私的人。不得不承認白羊座的人做事情確實是三分鐘熱度。昨天還想著好好的寫一篇系列文章,除了當是給自己做一些總結之外,也可以幫助到有需要的朋友。不過,確實是三分鐘熱度了,今天是拔涼拔涼的。
好了,下面是獲取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()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。