国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網管 > 服務器 > 正文

Zabbix與RRDtool繪圖篇之用ZabbixAPI取監控數據

2020-05-27 13:09:16
字體:
來源:轉載
供稿:網友

  一起來看一個Zabbix與RRDtool繪圖篇之用ZabbixAPI取監控數據技巧文章,希望下文可以幫助到各位。

  經過一個星期的死磕,Zabbix取數據和RRDtool繪圖都弄清楚了,做第一運維平臺的時候繪圖取數據是直接從Zabbix的數據庫取的,顯得有點笨拙,不過借此也了解了Zabbix數據庫結構還是有不少的收獲。

  學習Zabbix的API官方文檔少不了,官方文檔地址鏈接https://www.zabbix.com/documentation/ 大家選擇對應的版本就好了,不過2.0版本的API手冊位置有點特別開始我還以為沒有,后來找到了如下https://www.zabbix.com/documentation/2.0/manual/appendix/api/api 用ZabbixAPI取監控數據的思路大致是這樣的,先獲取所有監控的主機,再遍歷每臺主機獲取每臺主機的所有圖形,最后獲取每張圖形每個監控對象(item)的最新的監控數據或者一定時間范圍的數據。 下面按照上面的思路就一段一段的貼出我的程序代碼:

  1、登錄Zabbix獲取通信token

  #!/usr/bin/env python

  #coding=utf-8

  import json

  import urllib2

  import sys

  ##########################

  class Zabbix:

  def __init__(self):

  self.url = "http://xxx.xxx.xxx.xxx:xxxxx/api_jsonrpc.php"

  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": "用戶名", "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

  2、獲取所有主機

  #####################################################################

  #獲取所有主機和對應的hostid

  def hostsid_get(self):

  data = json.dumps({

  "jsonrpc": "2.0",

  "method": "host.get",

  "params": { "output":["hostid","status","host"]},

  "auth": self.authID,

  "id": 1})

  res = self.get_data(data)[result]

  #可以返回完整信息

  #return res

  hostsid = []

  if (res != 0) and (len(res) != 0):

  for host in res:

  if host[status] == 1:

  hostsid.append({host[host]:host[hostid]})

  elif host[status] == 0:

  hostsid.append({host[host]:host[hostid]})

  else:

  pass

  return hostsid

  返回的結果是一個列表,每個元素是一個字典,字典的key代表主機名,value代表hostid

Zabbix與RRDtool繪圖篇之用ZabbixAPI取監控數據 武林網

  3、獲取每臺主機的每張圖形

  ###################################################################

  #查找一臺主機的所有圖形和圖形id

  def hostgraph_get(self, hostname):

  data = json.dumps({

  "jsonrpc": "2.0",

  "method": "host.get",

  "params": { "selectGraphs": ["graphid","name"],

  "filter": {"host": hostname}},

  "auth": self.authID,

  "id": 1})

  res = self.get_data(data)[result]

  #可以返回完整信息rs,含有hostid

  return res[0][graphs]

  注意傳入的參數是主機名而不是主機id,結果也是由字典組成的列表。

zabbixget02

  4、獲取每張圖的監控對象item

  #可以返回完整信息rs,含有hostid

  tmp = res[0][items]

  items = []

  for value in tmp:

  if $ in value[name]:

  name0 = value[key_].split([)[1].split(])[0].replace(,, )

  name1 = value[name].split()

  if CPU in name1:

  name1.pop(-2)

  name1.insert(1,name0)

  else:

  name1.pop()

  name1.append(name0)

  name = .join(name1)

  tmpitems = {itemid:value[itemid],delay:value[delay],units:value[units],name:name,value_type:value[value_type],

lastclock:value[lastclock],lastvalue:value[lastvalue]}

  else:

  tmpitems = {itemid:value[itemid],delay:value[delay],units:value[units],name:value[name],value_type:value[value_type],

lastclock:value[lastclock],lastvalue:value[lastvalue]}

  items.append(tmpitems)

  return items

  返回的數據已經包含了最新的一次監控數據的值和取值的時間戳,如果只需要取最新的監控數據,到這里其實就可以了,記得這次傳入的參數是graphid。

zabbixget03

  5、根據itemid取得更多的監控數據

  下面是取10條監控數據,可以任意更改參數獲取更多的數據,全憑自己所需了。

  ################################################################

  #獲取歷史數據,history必須賦值正確的類型0,1,2,3,4 float,string,log,integer,text

  def history_get(self, itemid, i):

  data = json.dumps({

  "jsonrpc": "2.0",

  "method": "history.get",

  "params": { "output": "extend",

  "history": i,

  "itemids": itemid,

  "sortfield": "clock",

  "sortorder": "DESC",

  "limit": 10},

  "auth": self.authID,

  "id": 1})

  res = self.get_data(data)[result]

  return res

zabbixget04

 

  上面的所有代碼加起來就是一個Zabbix取數據的類。取出來的數據可以用RRDtool繪圖或做其它用途了

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南丹县| 郁南县| 临城县| 新丰县| 黄山市| 湟中县| 个旧市| 罗源县| 双柏县| 武义县| 高雄县| 杂多县| 南丰县| 长垣县| 运城市| 历史| 都江堰市| 延津县| 莱阳市| 黄陵县| 名山县| 乌审旗| 临夏县| 凤台县| 息烽县| 遵化市| 马关县| 延安市| 麻栗坡县| 扎鲁特旗| 上犹县| 白玉县| 罗江县| 博白县| 温泉县| 鄂州市| 隆德县| 阿荣旗| 肇州县| 同江市| 阿勒泰市|