一、自定义监控进程
//安装httpd
[root@node4 ~]# yum -y install httpd
//查看进程
[root@node4 ~]# ps -ef | grep httpd
root 1234 1 0 16:16 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1235 1234 0 16:16 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1237 1234 0 16:16 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1238 1234 0 16:16 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1239 1234 0 16:16 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 1433 896 0 16:16 pts/0 00:00:00 grep --color=auto httpd
[root@node4 ~]# ps -ef | grep httpd | grep -v grep | wc -l
5
[root@node4 ~]# ps -ef | grep -v grep | grep -c httpd
5
//编写脚本
[root@node4 ~]# mkdir /etc/zabbix/script
[root@node4 ~]# cd /etc/zabbix/script/
[root@node4 script]# vim check_httpd.sh
[root@node4 script]# chmod +x check_httpd.sh
[root@node4 script]# chown -R zabbix.zabbix /etc/zabbix/script/
//检查脚本
[root@node4 script]# systemctl stop httpd
[root@node4 script]# ./check_httpd.sh
1
[root@node4 script]# systemctl restart httpd
[root@node4 script]# ./check_httpd.sh
0
//添加监控项
[root@node4 script]# vim /etc/zabbix/zabbix_agentd.conf
UserParameter=check_httpd,/bin/bash /etc/zabbix/script/check_httpd.sh
[root@node4 script]# systemctl restart zabbix-agent.service
新建监控项
配置触发器
测试
二、 自定义监控日志
//检查日志里有无错误信息
[root@node4 ~]# cd /var/log/httpd/
[root@node4 httpd]# cat error_log | grep error
[root@node4 httpd]# cat error_log | grep Error
//插入python脚本,给它赋权
[root@node4 ~]# cd /etc/zabbix/script/
[root@node4 script]# rz -E
rz waiting to receive.
[root@node4 script]# ll
total 8
-rwxr-xr-x 1 zabbix zabbix 129 Aug 13 16:30 check_httpd.sh
-rw-r--r-- 1 root root 1854 Aug 15 09:11 log.py
[root@node4 script]# chmod +x log.py
[root@node4 script]# chown zabbix.zabbix log.py
[root@node4 script]# ll
total 8
-rwxr-xr-x 1 zabbix zabbix 129 Aug 13 16:30 check_httpd.sh
-rwxr-xr-x 1 zabbix zabbix 1854 Aug 15 09:11 log.py
[root@node4 script]# 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)
//安装python3
[root@node4 script]# yum -y install python3
//httpd服务的日志文件在/var/log/httpd/目录下,首先我们需要给这个目录设置一个ACL权限,让zabbix用户有权限去访问该目录
[root@node4 script]# cd /var/log/httpd/
[root@node4 httpd]# ls -ld .
drwx------ 2 root root 107 Aug 9 00:30 .
[root@node4 httpd]# setfacl -m u:zabbix:r-x /var/log/httpd/
//修改zabbix_agentd.conf文件,并重启服务
[root@node4 httpd]# vim /etc/zabbix/zabbix_agentd.conf
UserParameter=check_logs[*],/usr/bin/python3 /etc/zabbix/script/log.py $1 $2 $3
[root@node4 httpd]# systemctl restart zabbix-agent.service
//测试脚本
[root@node4 httpd]# cd /etc/zabbix/script/
[root@node4 script]# python3 log.py /var/log/httpd/error_log
0
[root@node4 script]# echo 'Error' >> /var/log/httpd/error_log
[root@node4 script]# python3 log.py /var/log/httpd/error_log
1
[root@node4 script]# vim /var/log/httpd/error_log
[root@node4 script]# python3 log.py /var/log/httpd/error_log
0
[root@node4 script]# rm -rf /tmp/logseek
测试
三、zabbix监控mysql主从(需提前搭建好主从环境)
//编写脚本
[root@slave ~]# cd /etc/zabbix/
[root@slave zabbix]# mkdir script
[root@slave zabbix]# cd script/
[root@slave script]# vim mysql_slave_status.sh
#!/bin/bash
USER="root"
PASSWD="redhat"
NAME=$1
function IO {
Slave_IO_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_IO_Running |awk '{print $2}'`
if [ $Slave_IO_Running == "Connecting" ];then
echo 0
else
echo 1
fi
}
function SQL {
Slave_SQL_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_SQL_Running: |awk '{print $2}'`
if [ $Slave_SQL_Running == "Yes" ];then
echo 0
else
echo 1
fi
}
case $NAME in
io)
IO
;;
sql)
SQL
;;
*)
echo -e "Usage: $0 [io | sql]"
esac
[root@slave script]# chmod +x mysql_slave_status.sh
[root@slave script]# chown -R zabbix.zabbix /etc/zabbix/script/
//测试脚本
[root@slave script]# ./mysql_slave_status.sh
Usage: ./mysql_slave_status.sh [io | sql]
[root@slave script]# ./mysql_slave_status.sh io
0
[root@slave script]# ./mysql_slave_status.sh sql
0
//编写一个自配置文件,里面指定上面编写的脚本的路径,然后重启服务
[root@slave script]# cd /etc/zabbix/
[root@slave zabbix]# ls
script zabbix_agentd.conf zabbix_agentd.d
[root@slave zabbix]# cd zabbix_agentd.d/
[root@slave zabbix_agentd.d]# vim userparameter_mysql_slave.conf
[root@slave zabbix_agentd.d]# chown -R zabbix.zabbix /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
[root@slave zabbix_agentd.d]# systemctl restart zabbix-agent.service
//在zabbix服务端进行验证
[root@zabbix ~]# zabbix_get -s 192.168.35.143 -k mysql.slave[io]
0
[root@zabbix ~]# zabbix_get -s 192.168.35.143 -k mysql.slave[sql]
0
//验证的结果如果是0,为正常,如果为1,则异常
测试
四、zabbix监控主从延迟
//编写脚本
[root@slave zabbix_agentd.d]# cd /etc/zabbix/script/
[root@slave script]# vim mysql_delay.sh
#!/bin/bash
delay=$(mysql -uroot -predhat -e 'show slave status\G' 2> /dev/null | grep 'Seconds_Behind_Master' | awk '{print $2}')
if [ $delay == "NULL" ];then
echo 0
elif [ $delay -ge 0 ] && [ $delay -le 200 ];then
echo 0
else
echo $delay
fi
chown -R zabbix.zabbix mysql_delay.sh
chmod +x mysql_delay.sh
[root@slave script]# chown -R zabbix.zabbix mysql_delay.sh
[root@slave script]# chmod +x mysql_delay.sh
//配置agentd文件,并重启服务
[root@slave script]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
UserParameter=check_mysql_delay,/bin/bash /etc/zabbix/script/mysql_delay.sh
[root@slave script]# systemctl restart zabbix-agent.service
//在zabbix主机端进行测试
[root@zabbix ~]# zabbix_get -s 192.168.35.143 -k check_mysql_delay
0
测试