Technology Sharing

Zabbix and Grafana connection

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Install grafana

1. Initialization operation

  1. 初始化操作
  2. systemctl disable --now firewalld
  3. setenforce 0
  4. vim /etc/selinux/config
  5. SELINUX=disabled

2. Upload the data package and install it

  1. cd /opt
  2. grafana-enterprise-9.4.7-1.x86_64.rpm #上传软件包
  3. yum localinstall -y grafana-enterprise-9.4.7-1.x86_64.rpm #安装
  4. systemctl enable --now grafana-server.service #开启服务
  5. netstat -lntp | grep 3000 #查询是否已经开启服务

2. Browser access

1. Log in to Grafana

Enter http://192.168.10.50:3000 in your browser

Username/password:admin/admin

2. Change password and log in

3. Add zabbix to grafana

Then return to the Plugins interface, zabbix has been installed

4. Import zabbix data and connectgrafana

3. Monitoring Linux system status

1. Select a monitoring template

We only monitor the CPU and load average states here.

2. Monitoring Operations

3. Save operation

4. Monitoring of Linux system service status (second template)

1. Select a template or import a template

2. Modify data

3. Modify the status of specific monitoring

To modify the number of CPU cores, you cannot modify it directly. You need to do the following operations first.

This way you can monitor the number of CPUs

The subsequent operations are basically the same. First find the keyword above the icon and then re-enter it.

The final result is like this

Compared with zabbix, it is easier to observe the specific status

5. Monitoring nginx

To monitor nginx, you must first have this configuration--with-http stub status module

1. Modify nginx configuration

  1. vim /etc/nginx/conf.d/default.conf
  2. location / {
  3. root /var/www/zbx;
  4. index index.php index.html index.htm;
  5. }

  1. systemctl reload nginx.service
  2. curl -s http://192.168.10.20/nginx_state

2. Write a script to view the status of nginx

  1. vim nginx_status.sh
  2. chmod +x nginx_status.sh
  3. ./nginx_status.sh waiting

Script to detect nginx status

  1. #!/bin/bash
  2. IP=192.168.10.20
  3. PORT=80
  4. STUB_URI=nginx_state
  5. active() {
  6. curl -s http://$IP:$PORT/$STUB_URI | awk 'NR==1 {print $3}'
  7. }
  8. accepts() {
  9. curl -s http://$IP:$PORT/$STUB_URI | awk 'NR==3 {print $1}'
  10. }
  11. handled() {
  12. curl -s http://$IP:$PORT/$STUB_URI | awk 'NR==3 {print $2}'
  13. }
  14. requests() {
  15. curl -s http://$IP:$PORT/$STUB_URI | awk 'NR==3 {print $3}'
  16. }
  17. Reading() {
  18. curl -s http://$IP:$PORT/$STUB_URI | awk 'NR==4 {print $2}'
  19. }
  20. writing() {
  21. curl -s http://$IP:$PORT/$STUB_URI | awk 'NR==4 {print $4}'
  22. }
  23. waiting() {
  24. curl -s http://$IP:$PORT/$STUB_URI | awk 'NR==4 {print $6}'
  25. }
  26. process() {
  27. pidof nginx | wc -w
  28. }
  29. case $1 in
  30. active)
  31. active
  32. ;;
  33. accepts)
  34. accepts
  35. ;;
  36. handled)
  37. handled
  38. ;;
  39. requests)
  40. requests
  41. ;;
  42. Reading)
  43. Reading
  44. ;;
  45. Writing)
  46. writing
  47. ;;
  48. Waiting)
  49. waiting
  50. ;;
  51. process)
  52. process
  53. ;;
  54. *)
  55. echo "Usage: $0 {active|accepts|handled|requests|Reading|writing|waiting|process}" exit 1
  56. esac
  57. exit 0

Verify that you can view the status

3. Set key value

  1. vim nginx_state.conf
  2. UserParameter=nginx.status[*],/etc/zabbix/zabbix_agent2.d/nginx_status.sh $1

4. Complete custom monitoring items on the zabbix web client

(1) Create a template

(2) Create monitoring items

(3) Create a trigger

4. Use grafana to monitor nginx