Zabbix custom scripts and custom dynamic parameter monitoring


This paper mainly records the collection of Zabbix by custom scripts and the collection and monitoring by custom dynamic parameters

Collection and commissioning are performed using common user-defined parameters

For example, collect data on the number of online users of the monitoring system

  1. #### Script Preparation
cat > /home/testuser/shells/getusers.sh << EOF #! /bin/sh count=\`uptime |grep -v grep | awk'{print \$4}'\`
echo \$count
EOF

/bin/sh /home/testuser/shells/getusers.sh
Copy the code
  1. #### Add the configuration in zabbix_agentd.conf
# sed -i's/^\# Include=\/usr\/local\/etc\/zabbix_agentd.conf.d\/$/Include=\/home\/testuser\/zabbix\/etc\/zabbix_agentd.conf.d/'/ home/testuser with/zabbix/etc/zabbix_agentd. The way the conf # # 2 cat > > / home/testuser with/zabbix/etc/zabbix_agentd. Conf < < EOF Include = / home/testuser with/zabbix/etc/zabbix_agentd. Conf., EOF # # # # # d perform one of two waysCopy the code
  1. #### Create the parameter configuration file getusers.conf
cat >> /home/testuser/zabbix/etc/zabbix_agentd.conf.d/getusers.conf << EOF
UserParameter=getusers-key,/bin/sh /home/testuser/shells/getusers.sh
EOF
Copy the code
  1. #### Restart the Zabbix Agent

Sh and run the sh restart_zabbix_agentd.sh command to restart agentD. The content of restart_zabbix_agentd.sh is as follows:

#! /bin/sh pid=`ps -ef | grep"/home/testuser/zabbix/sbin/zabbix_agentd" | awk '{print $2}'`

kill 9 - $pid >/dev/null 2> &1
sleep 1

/home/testuser/zabbix/sbin/zabbix_agentd -c /home/testuser/zabbix/etc/zabbix_agentd.conf
Copy the code
  1. #### test on the Zabbix-Server host
/home/testuser/zabbix/bin/zabbix_get -s 192.168. 56101. -p 10050 -k "getusers-key"
Copy the code

Perform operations on the Zabbix- Web UI to configure user-defined monitoring items

After a minute, I see the data collected.

Collect and commission data based on dynamic parameters

There is a defect in the above common parameters, that is, the configuration supports only one indicator collection at a time. It is hoped that multiple indicators can be collected at a time. In this case, dynamic parameters can be used, as shown in the following table.

  1. #### An example script is as follows:
cat > /home/testuser/shells/cusparameters.sh << EOF #! /bin/sh function pids { /sbin/pidof zabbix_agentd | wc -l } function users { /bin/sh /home/testuser/shells/getusers.sh } \ $1
EOF
Copy the code

# # # to test the/bin/sh/home/testuser with shells/cusparameters. Sh users/bin/sh/home/testuser with shells/cusparameters. Sh pids

  1. #### Configure a script file for user-defined parameters

/ home/testuser with/zabbix/etc/zabbix_agentd. Conf UserParameter in fields, UserParameter=cus. Metrics [*],/etc/zabbix/scripts/ngx-status.sh $1

cat > /home/testuser/zabbix/etc/zabbix_agentd.conf.d/cusparameters.conf << EOF
UserParameter=cus.metrics[*],/bin/sh /home/testuser/shells/cusparameters.sh \$1
EOF
Copy the code
  1. # # # # restart agentd
/bin/sh /home/testuser/restart_zabbix_agentd.sh
Copy the code
  1. #### Acquisition test
/home/testuser/zabbix/bin/zabbix_get -s 192.168. 56101. -p 10050 -k "cus.metrics[users]"
/home/testuser/zabbix/bin/zabbix_get -s 192.168. 56101. -p 10050 -k "cus.metrics[pids]"
Copy the code
  1. #### Configure monitoring items and add monitoring items with parameters to Zabbix, as shown in the figure

After a minute or two, I see zabbix’s latest data.