使用zabbix监控各项服务

自定义监控进程

使用zabbix监控apache服务

配置客户端

在客户端主机安装一个apache

[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# ls
zabbix_agentd.conf  zabbix_agentd.conf.d
[root@localhost etc]# vim zabbix_agentd.conf
# Mandatory: no
# Range: 0-1
# Default:
UnsafeUserParameters=1
//找到这行取消注释=1

UserParameter=check_process_httpd,/bin/bash /scripts/check_process.sh
/在文件的最后一行加上,监控apache的脚本

[root@localhost etc]# mkdir scripts
[root@localhost etc]# cd scripts/
[root@localhost scripts]# vim /scripts/check_process.sh

#!/bin/bash

count=$(ps -ef|grep -v grep|grep -c httpd)
if [ $count -ne 5 ];then
    echo '1'
fi 
//写一个脚本执行

[root@localhost scripts]# chmod +x /scripts/check_process.sh
//给他执行权限


[root@localhost ~]# systemctl stop httpd
//停掉httpd


[root@localhost scripts]# bash /scripts/check_process.sh
1
//执行脚本=1即成功


重启一下客户端的zabbix

配置服务端

[root@node1 ~]# zabbix_get -s 192.168.80.20 -k check_process_httpd
1
//并且在服务端也可以取到1的值

创建一个监控项目

 

 配置他的触发器

 已报警httpd异常。因为关闭了

监控多项服务进程

[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# cd scripts/
[root@localhost scripts]# vim check_process.sh 
#!/bin/bash
  
count=$(ps -ef|grep -Ev "grep|$0"|grep -c $1)
if [ $count -eq 0 ];then
    echo '1'
else
    echo '0'
fi

//修改一下脚本
[root@localhost scripts]# ./check_process.sh httpd
1
[root@localhost scripts]# ./check_process.sh zabbix

//有进程运行为0 


[root@localhost etc]# vim zabbix_agentd.conf
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
//配置文件最后一行修改

[root@node1 ~]# zabbix_get -s 192.168.80.20 -k check_process[httpd]
1

 重新更改监控项

 触发器已自动改变

[root@localhost ~]# systemctl start httpd
//启动httpd

 检查监控mysql

 添加监控项

 报警mysql异常

 监控日志

python程序

[root@localhost scripts]# cat log.py 
#!/usr/bin/env python3
import sys
import re

def prePos(seekfile):
    global curpos
    try:
        cf = open(seekfile)
    except IOError:
        curpos = 0
        return curpos
    except FileNotFoundError:
        curpos = 0
        return curpos
    else:
        try:
            curpos = int(cf.readline().strip())
        except ValueError:
            curpos = 0
            cf.close()
            return curpos
        cf.close()
    return curpos

def lastPos(filename):
    with open(filename) as lfile:
        if lfile.readline():
            lfile.seek(0,2)
        else:
            return 0
        lastPos = lfile.tell()
    return lastPos

def getSeekFile():
    try:
        seekfile = sys.argv[2]
    except IndexError:
        seekfile = '/tmp/logseek'
    return seekfile

def getKey():
    try:
        tagKey = str(sys.argv[3])
    except IndexError:
        tagKey = 'Error'
    return tagKey

def getResult(filename,seekfile,tagkey):
    destPos = prePos(seekfile)
    curPos = lastPos(filename)

    if curPos < destPos:
        curpos = 0

    try:
        f = open(filename)
    except IOError:
        print('Could not open file: %s' % filename)
    except FileNotFoundError:
        print('Could not open file: %s' % filename)
    else:
        f.seek(destPos)

        while curPos != 0 and f.tell() < curPos:
            rresult = f.readline().strip()
            global result
            if re.search(tagkey, rresult):
                result = 1
                break
            else:
                result = 0

        with open(seekfile,'w') as sf:
            sf.write(str(curPos))
    finally:
        f.close()
    return result

if __name__ == "__main__":
    result = 0
    curpos = 0
    tagkey = getKey()
    seekfile = getSeekFile()
    result = getResult(sys.argv[1],seekfile,tagkey)
    print(result)
[root@localhost ~]# cd /var/log/httpd

[root@localhost httpd]# ls
access_log  error_log

//找到httpd的日志位置

[root@localhost httpd]# setfacl -m u:zabbix:rx /var/log/httpd
[root@localhost httpd]# ll
total 4
-rw-r--r-- 1 root root    0 Jul 10 18:16 access_log
-rw-r--r-- 1 root root 4032 Jul 10 20:00 error_log


[root@localhost scripts]# dnf install python3
//安装一个python
[root@localhost etc]# vim /usr/local/etc/zabbix_agentd.conf

UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
UserParameter=check_logs[*],/usr/bin/python3 /scripts/log.py $1 $2 $3



[root@localhost etc]# cd scripts/
[root@localhost scripts]# python3 log.py /var/log/httpd/error_log 
0
//执行一个python脚本

[root@localhost scripts]# echo 'Error' >> //var/log/httpd/error_log 
[root@localhost scripts]# python3 log.py /var/log/httpd/error_log 
1
//往日志文件添加错误信息。检查值为1

[root@localhost scripts]# python3 log.py /var/log/httpd/error_log 
0
//再检查日志一刷新了
[root@localhost scripts]# rm -f /tmp/logseek 

配置监控项 

 添加他的触发器

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值