zabbix自定义监控
1.在客户端打开自定义监控
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
...
UnsafeUserParameters=1 //此行取消注释,0改为1
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=<key>,<shell command> //复制此行粘贴到文件最后
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
####### LOADABLE MODULES #######
...
2.自定义监控进程
//进程监控脚本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# vim check_process.sh
#!/bin/bash
status=$(ps -ef|grep -Ev "grep|$0"|grep -w "$1"|wc -l)
if [ $status -eq 0 ];then
echo '1'
else
echo '0'
fi
[root@localhost scripts]# chmod +x check_process.sh
[root@localhost scripts]# bash check_process.sh httpd
1
[root@localhost scripts]# bash check_process.sh mysqld
0
//改属主
[root@localhost scripts]# chown -R zabbix.zabbix /scripts/
[root@localhost scripts]# ll /scripts/
总用量 4
-rwxr-xr-x. 1 zabbix zabbix 129 12月 25 10:05 check_process.sh
//修改配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
...
### Option: TLSPSKFile
# Full pathname of a file containing the pre-shared key.
#
# Mandatory: no
# Default:
# TLSPSKFile=
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
//重启服务
[root@localhost ~]# pkill zabbix_agentd
[root@localhost ~]# zabbix_agentd
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 25 *:514 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 25 :::514 :::*
LISTEN 0 80 :::3306
//配置zabbix网页
//关闭mysqld服务
[root@localhost ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
3.自定义监控日志
[root@localhost pyscripts-master]# ls
log.py mail_send.py README.md 定时发微信群消息.zip
[root@localhost pyscripts-master]# cp log.py /scripts/
[root@localhost pyscripts-master]# cd /scripts/
[root@localhost scripts]# ls
check_process.sh log.py
[root@localhost scripts]# chmod +x log.py
[root@localhost scripts]# chown zabbix.zabbix log.py
[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 scripts]# vim /usr/local/etc/zabbix_agentd.conf
...
### Option: TLSPSKFile
# Full pathname of a file containing the pre-shared key.
#
# Mandatory: no
# Default:
# TLSPSKFile=
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
UserParameter=check_log[*],/usr/bin/python /scripts/log.py $1 $2 $3
[root@localhost scripts]# pkill zabbix_agentd
[root@localhost scripts]# zabbix_agentd
[root@localhost scripts]# setfacl -m u:zabbix:r-x /var/log/httpd/
//在服务端手动执行
[root@liping ~]# zabbix_get -s 192.168.136.145 -k check_log[/var/log/httpd/error_log]
0
//配置zabbix网页
//在客户端改监控文件
[root@localhost ~]# echo 'Error' >>/var/log/httpd/error_log
4.自定义监控zabbix主从
//监控处从脚本
[root@localhost scripts]# vim mysql_repl.sh
#!/bin/bash
status=$(mysql -uroot -p123456 -e 'show slave status\G' 2>/dev/null |grep -E 'Slave_IO_Running|Slave_SQL_Running:'|grep -c 'Yes')
if [ $status -ne 2 ];then
echo '1'
else
echo '0'
fi
[root@localhost scripts]# chmod +x mysql_repl.sh
[root@localhost scripts]# bash mysql_repl.sh
0
//编辑配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
...
### Option: TLSPSKFile
# Full pathname of a file containing the pre-shared key.
#
# Mandatory: no
# Default:
# TLSPSKFile=
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
UserParameter=check_log[*],/usr/bin/python /scripts/log.py $1 $2 $3
UserParameter=check_mr,/bin/bash /scripts/mysql_repl.sh
[root@localhost ~]# chown zabbix.zabbix /scripts/mysql_repl.sh
[root@localhost ~]# ll /scripts/
总用量 12
-rwxr-xr-x. 1 zabbix zabbix 129 12月 25 10:05 check_process.sh
-rwxr-xr-x. 1 zabbix zabbix 1854 12月 25 10:47 log.py
-rwxr-xr-x. 1 zabbix zabbix 204 12月 25 11:43 mysql_repl.sh
//重启服务
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd
//配置zabbix网页
//关闭一个mysqld
[root@localhost ~]# service mysqld stop
Shutting down MySQL............ SUCCESS!
5.自定义监控zabbix主从延迟
\\监控延迟脚本
[root@localhost scripts]# vim mysql_delay.sh
#!/bin/bash
delay=$(mysql -uroot -p123456 -e 'show slave status\G' 2>/dev/null|grep 'Seconds_Behind_Master'|awk '{print $2}')
echo $delay
[root@localhost scripts]# vim mysql_delay.sh
[root@localhost scripts]# chown -R zabbix.zabbix mysql_delay.sh
[root@localhost scripts]# chmod +x mysql_delay.sh
[root@localhost scripts]# ll mysql_delay.sh
-rwxr-xr-x. 1 zabbix zabbix 140 12月 25 18:16 mysql_delay.sh
\\编辑配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
...
### Option: TLSPSKFile
# Full pathname of a file containing the pre-shared key.
#
# Mandatory: no
# Default:
# TLSPSKFile=
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
UserParameter=check_log[*],/usr/bin/python /scripts/log.py $1 $2 $3
UserParameter=check_mr,/bin/bash /scripts/mysql_repl.sh
UserParameter=mysql_delay,/bin/bash /scripts/mysql_delay.sh
\\重启服务
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd
\配置zabbix网页
版权声明:本文为liping0826原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。