1. 在zabbix agent上修改配置文件
vi /usr/local/zabbix/conf/zabbix.agentd.conf
有两种修改方式:
第一种:
找到 # UnsafeUserParameters=0 改为 UnsafeUserParameters=1
找到 # UserParameter= 在下面直接添加用户参数
UserParameter=<key>,<shell command>
key:服务器添加监控时需用到的key值
shell command:监控脚本的绝对路径
例:UserParameter=tomcat,/home/zabbix/monitor/tomcat.sh (注意用逗号隔开)
如果要添加多个监控项,就依次写多个UserParameter
第二种:
找到 # Include=/usr/local/etc/zabbix_agentd.conf.d/ 在下面添加读取用户参数的文件路径
Include=/usr/local/zabbix/conf/zabbix_agentd/
然后在/usr/local/zabbix/conf/zabbix_agentd/下创建userparameter.conf 文件,编辑此文件
vi /usr/local/zabbix/conf/zabbix_agentd/userparameter.conf
添加想要监控的项,比如UserParameter=tomcat,/home/zabbix/monitor/java.sh
多个自定义监控项都可写在这个文件里
配置完成后重启zabbix agent服务
2. 在zabbix server端
用zabbix_get命令获取添加的监控项检测是否生效
如:/usr/local/zabbix/bin/zabbix_get -s 192.168.179.128 -k tomcat
-s:添加自定义脚本的zabbix agent的主机ip
-k:key值
示例
zabbix自带的默认模版里包括了很多监控项,有时候为了满足业务需求,需要根据自己的监控项目自定义监控项,这里介绍一种自定义监控项的方式。
首先编写自定义监控脚本,本文以监控mysql进程是否存在为例。
脚本名字:check_mysql.sh
脚本目录(这个目录可以自定义):/etc/zabbix/zabbix_scripts
脚本内容:
mysql_master=$1
master_slave=$3
mysql_master_port=$2
master_slave_port=$4
user=$5
password=$6
master_logpos=`mysql -h ${mysql_master} -P ${mysql_master_port} -u ${user} -p${password} -e 'show master status;' |awk '{print $2}' |tail -1`
slave_logpos=`mysql -h ${master_slave} -P ${master_slave_port} -u ${user} -p${password} -e 'show slave status\G' |grep Exec_Master_Log_Pos |awk '{print $2}'`
Slave_IO_status=`mysql -h ${master_slave} -P ${master_slave_port} -u ${user} -p${password} -e 'show slave status\G' |grep -E 'Slave_IO_Running:' |awk '{print $2}'`
Slave_SQL_status=`mysql -h ${master_slave} -P ${master_slave_port} -u ${user} -p${password} -e 'show slave status\G' |grep -E 'Slave_SQL_Running:' |awk '{print $2}'`
print_info() {
echo ""
echo "`date`"
echo "${mysql_master} ${master_slave} checksum result:"
echo Slave_IO_status: ${Slave_IO_status}
echo Slave_SQL_status: ${Slave_SQL_status}
echo master_logpos: ${master_logpos}
echo slave_logpos: ${slave_logpos}
}
if [ ${Slave_IO_status} == 'Yes' ] && [ ${Slave_SQL_status} == 'Yes' ];then
if [ ${master_logpos} -eq ${slave_logpos} ];then
print_info
echo checkcums is ok
else
print_info
echo checkcums is failed
fi
else
print_info
echo checkcums is failed
fi
2,修改zabbix_agentd.conf配置文件
第一个:默认为0,此处我们将它改为1,改为1以后,表示用户自定义的脚本中可以包含特殊字符。
第二个:为我们自定义监控项,格式为:UserParameter=<键值>,<命令>。这里我们自定义键值为:check_mysql,要执行的命令为执行check_mysql.sh这个脚本。
说明:添加完成以后,重启zabbix_agentd才会生效。
### Option: UnsafeUserParameters
# Allow all characters to be passed in arguments to user-defined parameters.
# The following characters are not allowed:
# \ ' " ` * ? [ ] { } ~ $ ! & ; ( ) < > | # @
# Additionally, newline characters are not allowed.
# 0 - do not allow
# 1 - allow
#
# Mandatory: no
# Range: 0-1
# Default:
UnsafeUserParameters=1
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=<key>,<shell command>
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
UserParameter=mysql_check,sh /etc/zabbix/zabbix_scripts/check_mysql.sh
3,选择要增加监控项的主机
4,创建监控项
查看监控数据