centos环境下使用zabbix配合python脚本对supervisor中的进程运行状态进行监控
# 需要监控的supervisor相关的进程如下
# supervisorctl status
maxapi-laravel-convert:maxapi-laravel-convert_01 RUNNING pid 24584, uptime 0:46:18
maxapi-laravel-convert:maxapi-laravel-convert_02 RUNNING pid 24695, uptime 0:45:23
maxapi-laravel-listeners:maxapi-laravel-listeners_01 RUNNING pid 4538, uptime 2 days, 6:38:05
maxapi-laravel-listeners:maxapi-laravel-listeners_02 RUNNING pid 4536, uptime 2 days, 6:38:05
maxapi-laravel-listeners:maxapi-laravel-listeners_03 RUNNING pid 4537, uptime 2 days, 6:38:05
maxapi-laravel-listeners:maxapi-laravel-listeners_04 RUNNING pid 4539, uptime 2 days, 6:38:05
maxapi-laravel-worker:maxapi-laravel-worker_01 RUNNING pid 4532, uptime 2 days, 6:38:05
maxapi-laravel-worker:maxapi-laravel-worker_02 RUNNING pid 4533, uptime 2 days, 6:38:05
maxapi-laravel-worker:maxapi-laravel-worker_03 RUNNING pid 4534, uptime 2 days, 6:38:05
maxapi-laravel-worker:maxapi-laravel-worker_04 RUNNING pid 4535, uptime 2 days, 6:38:05
# 直接监控进程名可能会有误差,如下,监控 queue:work 进程如果报警可能不知道是哪个程序宕机引发的
apache 4532 4530 0 Jun10 ? 00:00:40 /usr/local/bin/php /data/www/vhosts/maxapi.chinasoft.cn/artisan queue:work --daemon
apache 4536 4530 0 Jun10 ? 00:00:40 /usr/local/bin/php /data/www/vhosts/maxapi.chinasoft.cn/artisan queue:work --queue=listeners --daemon
# 使用python对 supervisorctl status 的输出结果进行判断比较合适
# cat supervisor_status_monitor.py
#_*_ coding:utf-8 _*_
import sys
import os
res = {}
file1 =os.popen('sudo /usr/bin/supervisorctl status')
for line in file1.readlines():
#获取服务名&服务状态
#print 'line = %s' % line
ser_name = line.split()[0].strip().split(":")[0]
#print 'ser_name = %s' % ser_name
ser_res = line.split()[1].strip()
#判断服务运行状态,1为成功,0为失败
if ser_res == 'RUNNING':
ser_res = '1'
else:
ser_res = '0'
res[ser_name] = ser_res
#print res
user_input = str(sys.argv[1].strip())
print res[user_input]
# 监控的配置
UserParameter=supervisorctl.status[*],/usr/bin/python /usr/local/zabbix_agents_3.2.0/scripts/supervisor_status_monitor.py $1
# 具体的监控项目
supervisorctl.status[maxapi-laravel-convert]
supervisorctl.status[maxapi-laravel-worker]
supervisorctl.status[maxapi-laravel-listeners]
# shell版监控
#!/bin/bash
supervisorctl status > /usr/local/zabbix/etc/zabbix_agentd.conf.d/supervisorctl.txt
file=/usr/local/zabbix/etc/zabbix_agentd.conf.d/supervisorctl.txt
case $1 in
redis)
REDIS=grep "redis" $file |awk '{print $2}'
if [ "$REDIS" == "RUNNING" ];then
echo "2"
else
echo "1"
fi
;;
*)
echo "USAGE: $0 [ redis | motorroom ]"
esac
# shell脚本结合zabbix自动发现对supervisor监控
1.脚本
# vim /usr/local/zabbix_agents/scripts/zbx_discovery_supervisor.sh
#!/bin/bash
# For auto discovery of supervisor
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# list all processes
PROCESSES=`sudo /usr/bin/supervisorctl -c /usr/local/supervisor/supervisord.conf status 2>/dev/null| grep -v 'supervisor.sock'|awk '{print $1}'`
# get processes array
INDEX=0
for PROCESS_NAME in $PROCESSES
do
PROCESS_NAME_NUM[$INDEX]=$PROCESS_NAME
let INDEX=INDEX+1
done
num=$(echo ${#PROCESS_NAME_NUM[@]})
# echo json
printf '{\n'
printf '\t"data":[\n'
for((i=0;i<${#PROCESS_NAME_NUM[@]};++i))
{
NUM=$(echo $((${#PROCESS_NAME_NUM[@]}-1)))
if [ "$i" != ${NUM} ];then
printf "\t\t{ \n"
printf "\t\t\t\"{#PROCESS_NAME}\":\"${PROCESS_NAME_NUM[$i]}\"},\n"
else
printf "\t\t{ \n"
printf "\t\t\t\"{#PROCESS_NAME}\":\"${PROCESS_NAME_NUM[$NUM]}\"}\n"
fi
}
printf ']}'
2.监控项
vim /usr/local/zabbix_agents/conf/zabbix_agentd/supervisor_agentd.conf
UserParameter=supervisor.discovery,/bin/bash /usr/local/zabbix_agents/scripts/zbx_discovery_supervisor.sh
UserParameter=supervisor.status[*],sudo /usr/bin/supervisorctl -c /usr/local/supervisor/supervisord.conf status $1 2>/dev/null| awk '{print $$2}'
3.添加具体的监控
item prototypes
Status of {#PROCESS_NAME}
supervisor.status[{#PROCESS_NAME}]
trigger
Supervisor {#PROCESS_NAME} not running
{Template Supervisor autodiscovery:supervisor.status[{#PROCESS_NAME}].str(RUNNING)}=0