centos环境下使用zabbix自动发现配合python和shell脚本对supervisor中的进程运行状态进行监控

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_3.2.0/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_3.2.0/conf/zabbix_agentd/supervisor_agentd.conf

UserParameter=supervisor.discovery,/bin/bash /usr/local/zabbix_agents_3.2.0/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

posted @   reblue520  阅读(397)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2017-06-12 web缓存服务器varnish-4.1.6的部署及配置详解
点击右上角即可分享
微信分享提示