这边拿supervisor端口监控为案例,zabbix版本Zabbix 3.4.2
首先授权,就是脚本中需要用到的命令加上sudo的权限,zabbix是以zabbix用户执行的,需要命令是需要root权限来执行,zabbix用户执行必须要用zabbix用户
vim /etc/sudoers #加如下的行zabbix ALL=(root) NOPASSWD:/usr/bin/supervisorctl
然后就是自动发现脚本的部分
#cat Supervisor_low_discovery.sh#!/bin/bashsupervisor() { #list=`less -r /etc/supervisord/*.conf |grep program|awk -F: '{print $2}'|sed 's/]//g'` list=`sudo /usr/bin/supervisorctl status|awk '{print $1}'` tail_line=`echo $list|awk '{print $NF}'` printf '{n' printf 't"data":[n' for key in $list do printf 't {n' if [ "$key" != "$tail_line" ];then printf "ttt"{#SUPERVISOR_PROGRAM}":"${key}"},n" else printf "ttt"{#SUPERVISOR_PROGRAM}":"${key}"}n" fi done printf 't ]n' printf '}n'}$1
接着是zabbix的项目的监控脚本,如果是端口监控可以直接用系统的模板,系统没有的就需要自定义
#cat supervisor_program.sh#!/bin/bashstatus=`sudo /usr/bin/supervisorctl status "$1"|awk '{print $2}'`if [ a"$status" == a"FATAL" ];then echo 0;else echo 1;fi
接着是zabbix配置文件的部分
#cat /etc/zabbix/etc/zabbix_agentd.conf.d/userparameter_supervisor.confUserParameter=supervisor_program.discovery[*], sh /etc/zabbix/zabbix_scripts/Supervisor_low_discovery.sh $1UserParameter=supervisor_program.status[*], /etc/zabbix/zabbix_scripts/supervisor_program.sh $1
然后是zabbix的模板设置
<?xml version="1.0" encoding="UTF-8"?>