这台agent已经和zabbix_server已经是关联好的
一、zabbix_agent增加监控项
root@patch1:/etc/zabbix/zabbix_agentd.conf.d# vim userparameter_timezone.conf
root@patch1:/etc/zabbix/zabbix_agentd.conf.d# cat userparameter_timezone.conf
UserParameter=timezone[*],/opt/check_timezone.sh $1
二、时区监控脚本编写如下
脚本先通过 date +%Z%z 获取当前服务器的时区,如果传入的参数是China,则将获取的时区和 CST+0800 比较,相同则返回1,否则返回0,HongKong是我后来加上去的和大陆时间其实是一样的不加也可以,调整服务器时区为Shanghai也行
root@patch1:/etc/zabbix/zabbix_agentd.conf.d# cd
root@patch1:~# vim /opt/check_timezone.sh
root@patch1:~# cat /opt/check_timezone.sh
#!/bin/bash
timezone(){
curzone=`date +%Z%z`
# echo $curzone
if [ $curzone == $zone ]
then
echo 1
else
echo 0
fi
}
city=$1
case $city in
China)
zone="CST+0800"
timezone
;;
America)
zone="EDT-0400"
timezone
;;
HongKong)
zone="HKT+0800"
timezone
;;
esac
关于%z与%Z的解释如下:
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
三、重启zabbix_agent
root@patch1:~# systemctl restart zabbix-agent.service
root@patch1:~# systemctl status zabbix-agent.service
● zabbix-agent.service - Zabbix Agent
Loaded: loaded (/lib/systemd/system/zabbix-agent.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-11-15 13:31:56 HKT; 7s ago
Docs: man:zabbix_agentd
Process: 27896 ExecStart=/usr/sbin/zabbix_agentd (code=exited, status=0/SUCCESS)
Tasks: 6
Memory: 1.6M
CPU: 14ms
CGroup: /system.slice/zabbix-agent.service
├─27902 /usr/sbin/zabbix_agentd
├─27903 /usr/sbin/zabbix_agentd: collector [idle 1 sec
├─27904 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection
├─27905 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection
├─27906 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection
└─27907 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec
Nov 15 13:31:56 patch1 systemd[1]: Stopped Zabbix Agent.
Nov 15 13:31:56 patch1 systemd[1]: Starting Zabbix Agent...
Nov 15 13:31:56 patch1 systemd[1]: Started Zabbix Agent.
四、zabbix_server端使用zabbix_get测试
提示权限拒绝
ops@zabbix2:~$ zabbix_get -s xx.xx.xx.99 -p 10050 -k timezone[HongKong]
sh: 1: /opt/check_timezone.sh: Permission denied
给脚本赋予执行权限
root@patch1:~# ll /opt/check_timezone.sh
-rw-r--r-- 1 root root 417 Oct 31 16:13 /opt/check_timezone.sh
root@patch1:~# chmod +x /opt/check_timezone.sh
root@patch1:~# ll /opt/check_timezone.sh
-rwxr-xr-x 1 root root 417 Oct 31 16:13 /opt/check_timezone.sh*
再次测试:
ops@zabbix2:~$ zabbix_get -s xx.xx.xx.99 -p 10050 -k timezone[HongKong]
1