Zabbix结合Python监控服务

python监控服务

脚本

#!/usr/bin/python
# coding:utf-8
import socket, os, re, sys, sendmail, psutil, time

with open('/home/ctchat/python/error.log', 'a+') as f:

    class Myserver():

        fuwu_ip = str(re.findall(":(\d.*)\s{2}B", os.popen("ifconfig").read())[0])
        port_lst = []
        def __init__(self, service, number):
            self.service = service
            self.number = number
            self.shell_service = "netstat -tnlp|grep " + " " + service
            self.file_name_set1 = "/home/ctchat/python/" + self.service + "set1.txt"
            self.file_name_set2 = "/home/ctchat/python/" + self.service + "set2.txt"


        def run(self):
            result = os.popen(self.shell_service).read()
            return result

        def Guo_lu(self):
            post = list(set(re.findall(".*:(\d+)", self.run())))

            return post

        def jiance_all(self):
            try:
                res = 0
                f.write(' --- ' + self.service + ':' + str(self.Guo_lu()))
                if len(self.Guo_lu()) >= int(self.number):
                    for i in range(len(self.Guo_lu())):
                        if int(self.Guo_lu()[i]) in [1805, 1905, 8015, 19966]:
                            continue
                        else:
                            res += Myserver.get(self.fuwu_ip, int(self.Guo_lu()[i]))
                    f.write(' --- socket.port.num:' + str(res) + ' --- socket.port:' + str(Myserver.port_lst))
                    if res > 0:
                        return 1
                    else:
                        return 0
                else:
                    f.write('\n********************\n'+self.run() + '\n********************\n')
                    return 0
            except IndexError:
                return 1


        @staticmethod
        def get(ip, port):
                try:
                    Myserver.port_lst.append(port)
                    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    client.connect((ip, port))
                    return 1
                except:
                    return 0
                finally:
                    client.close()

    class Process():
        all = psutil.pids()
        def __init__(self, name, number):
            self.name = name
            self.number = number

        def process_num(self):
            try:
                n = 0
                for i in range(len(Process.all)):
                    all_class = psutil.Process(Process.all[i])

                    if all_class.name() == self.name:
                        n += 1
                f.write(str(time.strftime('[%Y-%m-%d %H:%M:%S]',
                                          time.localtime(time.time()))) + ' --- ' + self.name + '.process.num:' + str(n))
                if n >= int(self.number):
                    return 1
                else:
                    return 0
            except psutil.NoSuchProcess:
                f.write(str(time.strftime('[%Y-%m-%d %H:%M:%S]',
                                          time.localtime(time.time()))) + ' --- ' + self.name + ':psutil.NoSuchProcess' )
                return 1


    res1 = Process(sys.argv[3], sys.argv[4]).process_num()
    res2 = Myserver(sys.argv[1], sys.argv[2]).jiance_all()

    f.write(' --- port.result:'+ str(res2) +' --- porcess.result:'+ str(res1) + '\n')
    if res1 == 1 and res2 == 1:
        print(1)
    else:
        print(0)

agent端操作

[root@server02 ctchat]# mkdir /home/ctchat/python
[root@server02 ctchat]# cd /home/ctchat/python

将脚本上传到服务器python

[root@server02 python]# chmod +x get_port.py 
[root@server02 python]# chmod 777 error.log 
# 返回1代表成功
[root@server02 python]# ./get_port.py mysqld 1 mysqld 1
1

查看当前目录生成有error.log文件,日志如果跑时间太长可以使用 echo > error.log 清空

[root@server02 python]# ls
error.log  get_port.py

查看内容

[root@server02 python]# cat error.log 
[2020-03-05 17:03:25] --- mysqld.process.num:1 --- mysqld:['1342'] --- socket.port.num:1 --- socket.port:[1342] --- port.result:1 --- porcess.result:1

解析:

  • mysqld.process.num:1  # 进程数量为1个
  • mysqld:['1342']         # 最主要的,脚本过滤出来的端口号
  • socket.port:[1342]   # 监测通过的端口号
  • port.result:1        # 端口检查结果1为正常,当为0时告警
  • porcess.result:1    # 进程监测的结果,同上

配置agent

[root@server02 python]# vi /etc/zabbix/zabbix_agentd.d/userparameter_service.conf 
UserParameter=service.status[*], /usr/bin/python /home/ctchat/python/get_port.py $1 $2 $3 $4
[root@server02 python]# /etc/init.d/zabbix-agent restart

zabbix_server执行

[root@server01 ~]# zabbix_get -s 10.4.7.6 -p 10050 -k "service.status[mysqld 1 mysqld 1]"
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
0

 如果出现上面情况,需要在新部署的agent端执行chmod +s /bin/netstat

[root@server01 ~]#  zabbix_get -s 10.4.7.6 -p 10050 -k "service.status[mysqld 1 mysqld 1]"
1

页面配置

 创建模板

 

 创建应用集

 

 固定格式,只需要更改服务名称

{mysql:service.status[mysqld 1 mysqld 1].last(0)}=0

last(0)代表收到为0的值之后立即报警

 可以添加一个监控图看看是否配置成功

 

猜你喜欢

转载自www.cnblogs.com/Wshile/p/12890238.html
今日推荐