使用Zabbix Api功能,跨互联网监控Zabbix-Proxy状态

时间:2021-08-06 12:37:45

使用Zabbix Api功能,跨互联网监控Zabbix-Proxy状态

背景:

项目需要通过zabbix-proxy 来获取监控数据【网络是单向的,zabbix-proxy 通过公网主动连接 zabbix-server ,上报监控数据,已达到监控的目的】

需求:

因为是单向网络,proxy 挂了后,zabbix master 都无法获取到 zabbix-proxy 和 下面的agent 机器的离线的状态,更不会触发告警;现需要对 zabbix-proxy 的状态实行监控,利用 zabbix-proxy 主动上传到 zabbix 页面上的 agent 代理程序的 状态 来判断 zabbix-proxy 的存活

使用Zabbix Api功能,跨互联网监控Zabbix-Proxy状态

思路:

通过获取 zabbix api 中 Proxy 代理 的 信息 来实现 监控

官方api说明:

版本:zabbix4.0

  1. 获取token:https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/user/login
  2. 获取proxy 代理信息: https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/proxy/get

#获取token:

  1. #入参:
  2. curl -s -X POST -H 'Content-Type: application/json' -d '
  3. {
  4. "jsonrpc": "2.0",
  5. "method": "user.login",
  6. "params": {
  7. "user": "Admin",
  8. "password": "PASSWORD"
  9. },
  10. "id": 1
  11. }' http://172.16.10.37:8888/api_jsonrpc.php;
  12. #回参:
  13. {"jsonrpc":"2.0","result":"为0qwewerwrsdfdsfdsafsd","id":1}
  14. #得到token 为0qwewerwrsdfdsfdsafsd

#获取proxy 代理信息

  1. #利用上面获取的token,来获取api 中proxy 的代理信息
  2. #入参:
  3. curl -s -X POST -H 'Content-Type: application/json' -d '
  4. {
  5. "jsonrpc": "2.0",
  6. "method": "proxy.get",
  7. "params": {
  8. "output": "extend",
  9. "selectInterface": "extend"
  10. },
  11. "auth": "0qwewerwrsdfdsfdsafsd",
  12. "id": 1
  13. }' http://172.16.10.37:8888/api_jsonrpc.php
  14. #回参
  15. {
  16. "jsonrpc": "2.0",
  17. "result": [
  18. {
  19. "proxy_hostid": "0",
  20. "host": "a-proxy",
  21. "status": "5",
  22. "disable_until": "0",
  23. "error": "",
  24. "available": "0",
  25. "errors_from": "0",
  26. "lastaccess": "1637806905",
  27. "ipmi_authtype": "-1",
  28. "ipmi_privilege": "2",
  29. "ipmi_username": "",
  30. "ipmi_password": "",
  31. "ipmi_disable_until": "0",
  32. "ipmi_available": "0",
  33. "snmp_disable_until": "0",
  34. "snmp_available": "0",
  35. "maintenanceid": "0",
  36. "maintenance_status": "0",
  37. "maintenance_type": "0",
  38. "maintenance_from": "0",
  39. "ipmi_errors_from": "0",
  40. "snmp_errors_from": "0",
  41. "ipmi_error": "",
  42. "snmp_error": "",
  43. "jmx_disable_until": "0",
  44. "jmx_available": "0",
  45. "jmx_errors_from": "0",
  46. "jmx_error": "",
  47. "name": "",
  48. "flags": "0",
  49. "templateid": "0",
  50. "description": "a-proxy",
  51. "tls_connect": "1",
  52. "tls_accept": "1",
  53. "tls_issuer": "",
  54. "tls_subject": "",
  55. "tls_psk_identity": "",
  56. "tls_psk": "",
  57. "proxy_address": "1.1.1.1",
  58. "auto_compress": "1",
  59. "discover": "0",
  60. "proxyid": "10385",
  61. "interface": []
  62. },
  63. {
  64. "proxy_hostid": "0",
  65. "host": "b-proxy",
  66. "status": "5",
  67. "disable_until": "0",
  68. "error": "",
  69. "available": "0",
  70. "errors_from": "0",
  71. "lastaccess": "1637806906",
  72. "ipmi_authtype": "-1",
  73. "ipmi_privilege": "2",
  74. "ipmi_username": "",
  75. "ipmi_password": "",
  76. "ipmi_disable_until": "0",
  77. "ipmi_available": "0",
  78. "snmp_disable_until": "0",
  79. "snmp_available": "0",
  80. "maintenanceid": "0",
  81. "maintenance_status": "0",
  82. "maintenance_type": "0",
  83. "maintenance_from": "0",
  84. "ipmi_errors_from": "0",
  85. "snmp_errors_from": "0",
  86. "ipmi_error": "",
  87. "snmp_error": "",
  88. "jmx_disable_until": "0",
  89. "jmx_available": "0",
  90. "jmx_errors_from": "0",
  91. "jmx_error": "",
  92. "name": "",
  93. "flags": "0",
  94. "templateid": "0",
  95. "description": "b-proxy",
  96. "tls_connect": "1",
  97. "tls_accept": "1",
  98. "tls_issuer": "",
  99. "tls_subject": "",
  100. "tls_psk_identity": "",
  101. "tls_psk": "",
  102. "proxy_address": "1.1.1.1",
  103. "auto_compress": "1",
  104. "discover": "0",
  105. "proxyid": "10402",
  106. "interface": []
  107. },
  108. {
  109. "proxy_hostid": "0",
  110. "host": "c_proxy",
  111. "status": "5",
  112. "disable_until": "0",
  113. "error": "",
  114. "available": "0",
  115. "errors_from": "0",
  116. "lastaccess": "1637806905",
  117. "ipmi_authtype": "-1",
  118. "ipmi_privilege": "2",
  119. "ipmi_username": "",
  120. "ipmi_password": "",
  121. "ipmi_disable_until": "0",
  122. "ipmi_available": "0",
  123. "snmp_disable_until": "0",
  124. "snmp_available": "0",
  125. "maintenanceid": "0",
  126. "maintenance_status": "0",
  127. "maintenance_type": "0",
  128. "maintenance_from": "0",
  129. "ipmi_errors_from": "0",
  130. "snmp_errors_from": "0",
  131. "ipmi_error": "",
  132. "snmp_error": "",
  133. "jmx_disable_until": "0",
  134. "jmx_available": "0",
  135. "jmx_errors_from": "0",
  136. "jmx_error": "",
  137. "name": "",
  138. "flags": "0",
  139. "templateid": "0",
  140. "description": "c_proxy",
  141. "tls_connect": "1",
  142. "tls_accept": "1",
  143. "tls_issuer": "",
  144. "tls_subject": "",
  145. "tls_psk_identity": "",
  146. "tls_psk": "",
  147. "proxy_address": "1.1.1.1",
  148. "auto_compress": "1",
  149. "discover": "0",
  150. "proxyid": "10445",
  151. "interface": []
  152. }
  153. ],
  154. "id": 1
  155. }

再次过滤,找到lastaccess字段,该字段的值表示 proxy 当前的 时间戳,每5秒 内会发生变化;通过对比该字段的值 和 zabbix-server 当前时间戳的 时间差,来判断 proxy 的状态是否正常

添加监控项:

获取 lastaccess字段 的值

创建监控脚本:

  1. [root@sre ~]# cd /etc/zabbix/zabbix_agentd.d
  2. [root@sre zabbix_agentd.d]# vim a-proxy-check.sh
  3. #!/bin/bash
  4. curl -s -X POST -H 'Content-Type: application/json' -d '
  5. {
  6. "jsonrpc": "2.0",
  7. "method": "proxy.get",
  8. "params": {
  9. "output": "extend",
  10. "selectInterface": "extend"
  11. },
  12. "auth": "0qwewerwrsdfdsfdsafsd",
  13. "id": 1
  14. }' http://172.16.10.37:8888/api_jsonrpc.php | awk -F '{"' '{print $3}' | awk -F ',' '{print $8}' | awk -F '"' '{print $4}'
  15. [root@sre zabbix_agentd.d]# chmod +x /etc/zabbix/zabbix_agentd.d/a-proxy-check.sh

修改 zabbix_agentd.conf 配置文件,创建KEY ,指定脚本路径

  1. [root@sre ~]# vim /etc/zabbix/zabbix_agentd.conf
  2. ........
  3. UnsafeUserParameters=1
  4. #自定义一个key,监控a_proxy的状态
  5. UserParameter=a_proxy_status,/bin/bash /etc/zabbix/zabbix_agentd.d/a-proxy-check.sh
  6. ........

重启 zabbix-agent

  1. systemctl restart zabbix-agent

登录到zabbix-server 使用zabbix-get 测试监控项

  1. [root@sre zabbix]# zabbix_get -s 172.16.10.37 -p 10050 -k "a_proxy_status"
  2. 1637923240

登录到zabbix web 控制台,

#添加监控项

使用Zabbix Api功能,跨互联网监控Zabbix-Proxy状态

#添加触发器

触发器表达式表示: 当a-proxy 时间戳的 值 和 zabbix 当前时间戳的值 ,相差60 秒 ,触发告警

使用Zabbix Api功能,跨互联网监控Zabbix-Proxy状态

修改触发器值,模拟触发告警

使用Zabbix Api功能,跨互联网监控Zabbix-Proxy状态

总结: 本篇文能写出来也是被逼无奈,单向网络,跨互联网监控,也是生产上一个案例吧.

原文链接:https://www.toutiao.com/a7034827437355614750/