zabbix自定义监控
在zabbix客户端上将zabbix_agent.conf文件中将自定义监控的功能打开
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
在两台被监控的主机上创建脚本存放目录
[root@localhost ~]# mkdir /scripts
编写判断进程是否开启的脚本
[root@localhost ~]# vim /scripts/check_process.sh
!/bin/bash
process_status=$(ps -elf | grep -Ev "grep|$0" | grep -c $1)
if [ $process_status != 0 ];then
echo '1'
else
echo '0'
fi
给脚本加执行权限
[root@localhost ~]# chmod +x /scripts/check_process.sh
测试zabbix用户是否能使用此脚本
[root@localhost ~]# su - zabbix -s /bin/bash
[zabbix@localhost root]$ /scripts/check_process.sh mysql
1
在zabbix客户端端写入此条自定义监控并且在服务端测试是否可用
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
……
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
……
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
服务端
[root@zabbix-noad1 ~]# zabbix_get -s 192.168.136.133 -k check_process['mysql']
1
在zabbix页面上创建监控项
创建触发器
测试验证
[root@localhost ~]# service mysqld stop
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
自定义监控日志文件
[root@localhost ~]# cat /scripts/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 ~]# chmod +x /scripts/log.py
测试zabbix用户是否能使用此脚本
[root@localhost /]# dnf -y module install python38
[root@localhost /]# setfacl -m u:zabbix:rx /var/log/httpd/
[root@localhost /]# mkdir /zabbix_item_log
[root@localhost /]# chown -R zabbix.zabbix /zabbix_item_log/
[root@localhost /]# su - zabbix -s /bin/bash
[zabbix@localhost /]$ python3 /scripts/log.py /var/log/httpd/error_log /zabbix_item_log/logseek Failed
0
在zabbix客户端写入此条自定义监控并且在服务端测试是否可用
[root@localhost /]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_logs[*],/scripts/log.py $1 $2 $3
……
[root@localhost /]# pkill zabbix_agent
[root@localhost /]# zabbix_agentd
服务端:
[root@zabbix-noad1 ~]# zabbix_get -s 192.168.136.133 -k check_logs['/var/log/httpd/error_log','zabbix_item_log/logseek','Failed']
0
客户端:
[root@localhost /]# echo 'Failed' >> /var/log/httpd/error_log
服务端:
[root@zabbix-noad1 ~]# zabbix_get -s 192.168.136.133 -k check_logs['/var/log/httpd/error_log','zabbix_item_log/logseek','Failed']
1
在zabbix页面上创建监控项
创建触发器
测试验证
配置主数据库的root账号在从数据库可以远程登录且不需要密码
mysql> create user 'root'@'192.168.136.133' identified by 'tengjia' ;
Query OK, 0 rows affected (0.07 sec)
mysql> grant all on *.* to 'root'@'192.168.136.133';
Query OK, 0 rows affected (0.10 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.12 sec)
[root@localhost /]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mysql_data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysql_data/mysql.pid
user = mysql
skip-name-resolve
server-id=2
relay-log=slave_relay_bin
[client]
user=root
password=tengjia
编写判断主从状态的脚本
[root@localhost /]# vim /scripts/check_mysql_replistatus.sh
#!/bin/bash
status=$(mysql -e 'show slave status\G' | grep "Running: Yes" |awk '{print $2}' | grep -c 'Yes')
if [ $status == 2 ];then
echo '0'
else
echo '1'
fi
给脚本加执行权限
[root@localhost /]# chmod +x /scripts/check_mysql_replistatus.sh
测试zabbix用户是否能使用此脚本
[root@localhost /]# su - zabbix -s /bin/bash
[zabbix@localhost /]$ /scripts/check_mysql_replistatus.sh
1
在zabbix客户端写入此条自定义监控并且在服务端测试是否可用
[root@localhost /]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_replication,/scripts/check_mysql_replistatus.sh
[root@localhost /]# pkill zabbix_agent
[root@localhost /]# zabbix_agentd
[root@zabbix-noad1 ~]# zabbix_get -s 192.168.136.133 -k check_mysql_replication
1
在zabbix页面上创建监控项
创建触发器
测试验证
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql主从延迟的监控
[root@localhost /]# vim /scripts/check_mysql_delay.sh
\#!/bin/bash delay=$(mysql -e 'show slave status\G' | grep "Seconds_Behind_Master" |awk '{print $2}') echo $delay
给脚本加执行权限
[root@localhost /]# chmod +x /scripts/check_mysql_delay.sh
测试zabbix用户是否能使用此脚本
[root@localhost /]# su - zabbix -s /bin/bash
[zabbix@localhost /]$ /scripts/check_mysql_delay.sh
NULL
在zabbix客户端写入此条自定义监控并且在服务端测试是否可用
[root@localhost /]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_delay,/scripts/check_mysql_delay.sh
[root@localhost /]# pkill zabbix_agent
[root@localhost /]# zabbix_agentd
[root@zabbix-noad1 ~]# zabbix_get -s 192.168.136.133 -k check_mysql_delay
0
在zabbix页面上创建监控项
创建触发器