设为首页 收藏本站
查看: 1394|回复: 0

[经验分享] Zabbix二次开发_02获取数据

[复制链接]

尚未签到

发表于 2017-12-31 11:33:28 | 显示全部楼层 |阅读模式
  最近准备写一个zabbix二次页面的呈现。打算调用zabbix api接口来进行展示。
  具体流程以及获取的数据。
  1、  获得认证密钥
  2、  获取zabbix所有的主机组
  3、  获取单个组下的所有主机
  4、  获取某个主机下的所有监控项
  5、  获取某个监控项的历史数据
  6、  获取某个监控项的最新数据
认证获取
  user.login方法获取zabbix server的认证结果官方地址:https://www.zabbix.com/documentation/2.2/manual/api/reference/user/login
  python脚本:
1  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
[iyunv@yang python]# cat auth.py  #!/usr/bin/env python2.7
  #coding=utf-8
  import json
  import urllib2
  # based url and required header
  url = "http://1.1.1.1/zabbix/api_jsonrpc.php"
  header = {"Content-Type":"application/json"}
  # auth user and password
  data = json.dumps(
  {
  "jsonrpc": "2.0",
  "method": "user.login",
  "params": {
  "user": "Admin",
  "password": "zabbix"
  },
  "id": 0
  })
  # create request object
  request = urllib2.Request(url,data)
  for key in header:
  request.add_header(key,header[key])
  # auth and get authid
  try:
  result = urllib2.urlopen(request)
  except URLError as e:
  print "Auth Failed, Please Check Your Name AndPassword:",e.code
  else:
  response = json.loads(result.read())
  result.close()

  print"Auth Successful. The Auth>  python脚本运行结果:
1  2
[iyunv@yang python]# python auth.py
  Auth Successful. The Auth>  curl命令:
1  2
  3
curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc":  "2.0","method":"user.login","params":{"user":"admin","password":"zabbix"},"auth":
  null,"id":0}' http://1.1.1.1/zabbix/api_jsonrpc.php
  curl命令运行结果:
1{"jsonrpc":"2.0","result":"b895ce91ba84fe247e444817c6773cc3","id":0}主机ID获取
  2.     hostgroup.get方法获取所有主机组ID把认证密钥放到脚本中,每次获取数据时都需要认证。此处是获取zabbix server上的所有主机组名称与ID号。
  官方地址:https://www.zabbix.com/documentation/2.2/manual/api/reference/hostgroup/get
  python脚本:
1  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
[iyunv@yang python]# catget_hostgroup_list.py  #!/usr/bin/env python2.7
  #coding=utf-8
  import json
  import urllib2
  # based url and required header
  url = "http://1.1.1.1/zabbix/api_jsonrpc.php"
  header = {"Content-Type":"application/json"}
  # request json
  data = json.dumps(
  {
  "jsonrpc":"2.0",
  "method":"hostgroup.get",
  "params":{
  "output":["groupid","name"],
  },

  "auth":"3c0e88885a8cf8af9502b5c850b992bd", # theauth>  "id":1,
  })
  # create request object
  request = urllib2.Request(url,data)
  for key in header:
  request.add_header(key,header[key])
  # get host list
  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
  else:
  response = json.loads(result.read())
  result.close()
  print "Number Of Hosts: ", len(response['result'])
  #print response
  for group in response['result']:

  print "Group>  python脚本执行结果:
1  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
[iyunv@yang python]# pythonget_hostgroup_list.py  Number Of Hosts:  12

  Group>
  Group>
  Group>
  Group>
  Group>
  Group>
  Group>
  Group>
  Group>
  Group>
  Group>
  Group>  curl命令:
1curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc": "2.0","method":"hostgroup.get","params":{"output":["groupid","name"]},"auth":"11d2b45415d5de6770ce196879dbfcf1","id":0}' http://1.1.1.1/zabbix/api_jsonrpc.php  curl执行结果:
1{"jsonrpc":"2.0","result":[{"groupid":"11","name":"DBSchedule"},{"groupid":"14","name":"DG-WY-KD-Server"},{"groupid":"5","name":"Discoveredhosts"},{"groupid":"7","name":"Hypervisors"},{"groupid":"2","name":"Linuxservers"},{"groupid":"8","name":"monitored_linux"},{"groupid":"9","name":"qsmind"},{"groupid":"12","name":"qssec"},{"groupid":"13","name":"switch"},{"groupid":"1","name":"Templates"},{"groupid":"6","name":"Virtualmachines"},{"groupid":"4","name":"Zabbixservers"}],"id":0}  host.get方法获取单个主机组下所有的主机ID。根据标题2中获取到的主机组id,把主机组id填入到下边脚本中,就可以获得该主机组下所有的主机id。
  官方地址:https://www.zabbix.com/documentation/2.2/manual/api/reference/host/get
  python脚本:
1  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
[iyunv@yang python]# cat get_group_one.py  #!/usr/bin/env python2.7
  #coding=utf-8
  import json
  import urllib2
  # based url and required header
  url = "http://1.1.1.1/zabbix/api_jsonrpc.php"
  header = {"Content-Type":"application/json"}
  # request json
  data = json.dumps(
  {
  "jsonrpc":"2.0",
  "method":"host.get",
  "params":{
  "output":["hostid","name"],
  "groupids":"14",
  },

  "auth":"3c0e88885a8cf8af9502b5c850b992bd", # theauth>  "id":1,
  })
  # create request object
  request = urllib2.Request(url,data)
  for key in header:
  request.add_header(key,header[key])
  # get host list
  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
  else:
  response = json.loads(result.read())
  result.close()
  print "Number Of Hosts: ", len(response['result'])
  for host in response['result']:

  print "Host>  python脚本执行结果:
1  2
  3
  4
  5
  6
[iyunv@yang python]# pythonget_group_one.py  Number Of Hosts:  4

  Host>
  Host>
  Host>
  Host>  curl命令:
1  2
curl -i -X POST -H'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"host.get","params":{"output":["hostid","name"],"groupids":"14"},"auth":"11d2b45415d5de6770ce196879dbfcf1","id":0}'  http://1.1.1.1/zabbix/api_jsonrpc.php
  curl命令执行结果:
1{"jsonrpc":"2.0","result":[{"hostid":"10146","name":"DG-WY-KD-3F3B-00"},{"hostid":"10147","name":"DG-WY-KD-3F3B-01"},{"hostid":"10148","name":"DG-WY-KD-3F3B-02"},{"hostid":"10149","name":"DG-WY-KD-3F3B-03"}],"id":0}获取主机数据
  itemsid.get方法获取单个主机下所有的监控项ID根据标题3中获取到的所有主机id与名称,找到你想要获取的主机id,获取它下面的所有items。
  官方地址:https://www.zabbix.com/documentation/2.2/manual/api/reference/item
  python脚本:
1  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
[iyunv@yang python]# cat get_items.py  #!/usr/bin/env python2.7
  #coding=utf-8
  import json
  import urllib2
  # based url and required header
  url = "http://1.1.1.1/zabbix/api_jsonrpc.php"
  header = {"Content-Type":"application/json"}
  # request json
  data = json.dumps(
  {
  "jsonrpc":"2.0",
  "method":"item.get",
  "params":{
  "output":["itemids","key_"],
  "hostids":"10146",
  },

  "auth":"3c0e88885a8cf8af9502b5c850b992bd", # theauth>  "id":1,
  })
  # create request object
  request = urllib2.Request(url,data)
  for key in header:
  request.add_header(key,header[key])
  # get host list
  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
  else:
  response = json.loads(result.read())
  result.close()
  print "Number Of Hosts: ", len(response['result'])
  for host in response['result']:
  print host

  #print "Host>  python脚本运行结果:
1  2
  3
  4
  5
  6
  7
  8
  9
  10
[iyunv@yang python]# python get_items.py  Number Of Hosts:  54
  {u'itemid': u'24986', u'key_':u'agent.hostname'}
  {u'itemid': u'24987', u'key_':u'agent.ping'}
  {u'itemid': u'24988', u'key_':u'agent.version'}
  {u'itemid': u'24989', u'key_':u'kernel.maxfiles'}
  {u'itemid': u'24990', u'key_':u'kernel.maxproc'}
  {u'itemid': u'25157', u'key_':u'net.if.in[eth0]'}
  {u'itemid': u'25158', u'key_':u'net.if.in[eth1]'}
  … …
  curl命令:
1  2
curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc":"2.0","method":"item.get","params":{"output":"itemids","hostids":"10146","search":{"key_":"net.if.out[eth2]"}},"auth":"11d2b45415d5de6770ce196879dbfcf1","id":0}' http://1.1.1.1/zabbix/api_jsonrpc.php  #此处加上了单个key的名称
  curl命令执行结果:
1{"jsonrpc":"2.0","result":[{"itemid":"25154"}],"id":0}
  history.get方法获取单个监控项的历史数据根据第4项的获取到的所有items>  官方地址:https://www.zabbix.com/documentation/2.2/manual/api/reference/history/get
  python脚本:
1  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
[iyunv@yang python]# catget_items_history.py  #!/usr/bin/env python2.7
  #coding=utf-8
  import json
  import urllib2
  # based url and required header
  url = "http://1.1.1.1/zabbix/api_jsonrpc.php"
  header = {"Content-Type":"application/json"}
  # request json
  data = json.dumps(
  {
  "jsonrpc":"2.0",
  "method":"history.get",
  "params":{
  "output":"extend",
  "history":3,
  "itemids":"25159",
  "limit":10
  },

  "auth":"3c0e88885a8cf8af9502b5c850b992bd", # theauth>  "id":1,
  })
  # create request object
  request = urllib2.Request(url,data)
  for key in header:
  request.add_header(key,header[key])
  # get host list
  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
  else:
  response = json.loads(result.read())
  result.close()
  print "Number Of Hosts: ", len(response['result'])
  for host in response['result']:
  print host

  #print "Host>  python脚本执行结果:
1  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
[iyunv@yang python]# pythonget_items_history.py  Number Of Hosts:  10
  {u'itemid': u'25159', u'ns': u'420722133',u'value': u'3008', u'clock': u'1410744079'}
  {u'itemid': u'25159', u'ns': u'480606614',u'value': u'5720', u'clock': u'1410744139'}
  {u'itemid': u'25159', u'ns': u'40905600',u'value': u'6144', u'clock': u'1410744200'}
  {u'itemid': u'25159', u'ns': u'175337062',u'value': u'2960', u'clock': u'1410744259'}
  {u'itemid': u'25159', u'ns': u'202705084',u'value': u'3032', u'clock': u'1410744319'}
  {u'itemid': u'25159', u'ns': u'263158421',u'value': u'2864', u'clock': u'1410744379'}
  {u'itemid': u'25159', u'ns': u'702285081',u'value': u'7600', u'clock': u'1410744439'}
  {u'itemid': u'25159', u'ns': u'231191890',u'value': u'3864', u'clock': u'1410744499'}
  {u'itemid': u'25159', u'ns': u'468566742',u'value': u'3112', u'clock': u'1410744559'}
  {u'itemid': u'25159', u'ns': u'421679098',u'value': u'2952', u'clock': u'1410744619'}
  curl命令:
1curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc":"2.0","method":"history.get","params":{"history":3,"itemids":"25154","output":"extend","limit":10},"auth":"11d2b45415d5de6770ce196879dbfcf1","id":0}' http://1.1.1.1/zabbix/api_jsonrpc.php  curl命令运行结果:
1{"jsonrpc":"2.0","result":[{"itemid":"25154","clock":"1410744134","value":"4840","ns":"375754276"},{"itemid":"25154","clock":"1410744314","value":"5408","ns":"839852515"},{"itemid":"25154","clock":"1410744374","value":"7040","ns":"964558609"},{"itemid":"25154","clock":"1410744554","value":"4072","ns":"943177771"},{"itemid":"25154","clock":"1410744614","value":"8696","ns":"995289716"},{"itemid":"25154","clock":"1410744674","value":"6144","ns":"992462863"},{"itemid":"25154","clock":"1410744734","value":"6472","ns":"152634327"},{"itemid":"25154","clock":"1410744794","value":"4312","ns":"479599424"},{"itemid":"25154","clock":"1410744854","value":"4456","ns":"263314898"},{"itemid":"25154","clock":"1410744914","value":"8656","ns":"840460009"}],"id":0}  history.get方法获取单个监控项最后的值只需把上个脚本中或curl中的limit参数改为1就可。
  这样的话需要监控的数据就拿到了,下一步的就是前端页面的展示了。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-430233-1-1.html 上篇帖子: zabbix监控的基础概念、工作原理及架构 下篇帖子: Zabbix基本配置及监控主机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表