Zabbix 监控服务配置自定义监控
文章目录
1.自定义监控进程
2.自定义监控日志
3.自定义监控mysql主从状态
4.自定义监控mysql主从延迟
准备环境:
ip | 虚拟机 | 服务 |
---|---|---|
192.168.136.140 | centos8(server) | zabbix server 、zabbix agentd 、postfix |
192.168.136.139 | centos8(client) | postfix 、mysql |
192.168.136.142 | centos8(c3) | mysql |
1.监控进程
在被监控的主机
[root@client ~]# cd /usr/local/etc/
[root@client etc]# ll
total 20
-rw-r--r--. 1 root root 17032 Sep 23 10:01 zabbix_agentd.conf
drwxr-xr-x. 2 root root 6 Sep 23 04:55 zabbix_agentd.conf.d
[root@client etc]# vim zabbix_agentd.conf
# Default:
UnsafeUserParameters=1 (添加这行)
监控postfix,httpd
通过脚本
[root@client ~]# mkdir /script
[root@client ~]# cd script/
[root@client script]# vim showprocess.sh
[root@client script]# cat showprocess.sh
#!/bin/bash
process=$(ps -ef|grep "$1" | grep -Ev "grep|$0" | wc -l)
if [ $process -eq 0 ];then
echo "1"
else
echo "0"
fi
[root@client script]# chmod +x showprocess.sh
[root@client script]# ll
total 4
-rwxr-xr-x 1 root root 132 Sep 26 02:23 showprocess.sh
[root@client script]# ./showprocess.sh
0 (值为0代表没有问题)
[root@client script]# ./showprocess.sh postfix
0
[root@client script]# ./showprocess.sh httpd
1 (因为这台主机没有安装httpd)
[root@client script]# ./showprocess.sh mysql
0
[root@client script]# systemctl stop postfix
[root@client script]# ./showprocess.sh postfix
1
[root@client ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=show_process_postfix,/bin/bash /script/showprocess.sh postfix(末尾添加)
[root@client ~]# systemctl restart zabbix_agentd
服务端
[root@server ~]# zabbix_get -s 192.168.136.139 -k show_process_postfix
0
[root@client ~]# systemctl stop postfix
[root@server ~]# zabbix_get -s 192.168.136.139 -k show_process_postfix
1
为该脚本配置监控项和触发器
创建触发器
[root@client ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=show_process[*],/bin/bash /script/showprocess.sh $1
[root@server ~]# zabbix_get -s 192.168.136.139 -k show_process['postfix']
0 (值为0代表这台虚拟机安装postfix服务)
[root@server ~]# zabbix_get -s 192.168.136.139 -k show_process['httpd']
1
httpd
2.自定义监控日志
注意:
也是要利用脚本,不过这里使用的时python脚本
可以去github中拉去该包,或者码云
我这里是在github中:
https://github.com/chendao2015
[root@client ~]# cd /script/
[root@client script]# ll
total 4
-rwxr-xr-x 1 root root 131 Sep 26 03:06 showprocess.sh
[root@client script]# vim 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)
wq(保存退出)
# 给脚本执行权限
[root@client script]# chmod +x log.py
[root@client script]#
[root@client ~]# ll
total 42732
-rw-------. 1 root root 1246 Jul 27 09:41 anaconda-ks.cfg
测试
[root@client ~]# echo 'Error' >> anaconda-ks.cfg
[root@client ~]# echo 'jjyy' >> anaconda-ks.cfg
[root@client ~]# grep 'Error' anaconda-ks.cfg
Error
[root@client ~]# grep 'Error' anaconda-ks.cfg
Error
[root@client ~]# grep 'Error' anaconda-ks.cfg
Error
# 下载python3
[root@client ~]# yum -y install python3
[root@client script]# vim /tmp/test.log
[root@client script]# cat /tmp/test.log
hsdknckedc
ciscn,ms djas
sfdsmclkfmld
# Error
calkdmcskje
cnkacm slked
[root@client ~]# python3 /script/log.py /tmp/test.log
1
[root@client ~]# python3 /script/log.py /tmp/test.log
0
[root@client ~]# ls /tmp/
jjyy vmware-root_805-4257200540
logseek vmware-root_807-4248746014
pear vmware-root_808-2965972425
systemd-private-411b60e55f2c4770a1a3494478b36a70-chronyd.service-mMvvUR vmware-root_811-4290756501
systemd-private-411b60e55f2c4770a1a3494478b36a70-mariadb.service-vQAcse vmware-root_818-2957124693
test.log vmware-root_820-2956993618
vmware-root_801-4248614937 zabbix_agentd.log
vmware-root_803-4257069467 zabbix_agentd.pid
[root@client ~]# cat /tmp/logseek
70[root@client ~]# echo -e "this a test\this a playgame" >> /tmp/test.log
[root@client ~]# cat /tmp/logseek
70[root@client ~]python3 /script/log.py /tmp/test.log
0
[root@client ~]# cat /tmp/logseek
[root@client ~]echo "Error" >> /tmp/test.logog
[root@client ~]# python3 /script/log.py /tmp/test.log
1
[root@client ~]# rm -rf /tmp/logseek
[root@client ~]# ls /tmp
jjyy vmware-root_807-4248746014
pear vmware-root_808-2965972425
systemd-private-411b60e55f2c4770a1a3494478b36a70-chronyd.service-mMvvUR vmware-root_811-4290756501
systemd-private-411b60e55f2c4770a1a3494478b36a70-mariadb.service-vQAcse vmware-root_818-2957124693
test.log vmware-root_820-2956993618
vmware-root_801-4248614937 zabbix_agentd.log
vmware-root_803-4257069467 zabbix_agentd.pid
vmware-root_805-4257200540
[root@client ~]# python3 /script/log.py /tmp/test.log /tmp/logseek failed
0
[root@client ~]# cat /tmp/logseek
103[root@client ~
[root@client ~]# echo "failed" >> /tmp/test.log
[root@client ~]# python3 /script/log.py /tmp/test.log /tmp/logseek failed
1
[root@client script]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=show_process[*],/bin/bash /script/showprocess.sh $1
UserParameter=check_logs[*],python3 /script/log.py $1 $2 $3 //添加这行
[root@client script]# systemctl restart zabbix_agentd
[root@client ~]# rm -f /tmp/logseek
[root@client ~]# ll /tmp/
[root@server ~]# zabbix_get -s 192.168.136.139 -k check_logs['/tmp/test.log']
1
手动触发
[root@client ~]# echo 'Error' >> /tmp/test.log
3.自定义监控mysql主从状态
主库
[root@c3 ~]# systemctl stop --now firewalld
[root@c3 ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@c3 ~]# setenforce 0
[root@c3 ~]# yum -y install mariadb mariadb-server
[root@c3 ~]# setenforce 0
[root@c3 ~]# systemctl start mariadb
[root@c3 ~]# systemctl enable mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@c3 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@c3 ~]# mysql -uroot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> set password = password('123456');
Query OK, 0 rows affected (0.006 sec)
ariaDB [(none)]> grant replication slave on *.* to 'repl'@'%' identified by '123456';
Query OK, 0 rows affected (0.011 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.006 sec)
MariaDB [(none)]> quit
[root@c3 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
log-bin=mysql_bin
server-id=10
MariaDB [(none)]> create database school;
Query OK, 1 row affected (0.000 sec)
[root@c3 ~]# systemctl restart mariadb
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql_bin.000001 | 328 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
配置从库
[root@client ~]# yum -y install mariadb mariadb-server
[root@client ~]# systemctl stop --now firewalld
[root@client ~]# systemctl disable firewalld
[root@client ~]# setenforce 0
[root@client ~]# systemctl start mariadb
[root@client ~]# systemctl enable mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@client ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
[root@client ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> set password=password('123456');
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)
[root@client ~]# vim /etc/my.cnf.d/mariadb-server.cnf
relay-log=myrelay
server-id=20
[root@client ~]# systemctl restart mariadb
[root@client ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> change master to
-> master_host='192.168.136.142',
-> master_user='repl',
-> master_password='123456',
-> master_log_file='mysql_bin.000001',
-> master_log_pos=328;
Query OK, 0 rows affected (0.014 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.136.142
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000002
Read_Master_Log_Pos: 371
Relay_Log_File: myrelay.000004
Relay_Log_Pos: 670
Relay_Master_Log_File: mysql_bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
+--------------------+
4 rows in set (0.010 sec)
编写mysql主从状态脚本
[root@client script]# cat mysql_status.sh
#!/bin/bash
count=$(mysql -uroot -p'123456' -e 'show slave status\G' | grep -i 'running:' | grep -c 'Yes')
if [ $count -ne 2 ];then
echo '1'
else
echo '0'
fi
[root@client script]# chmod +x mysql_status.sh
[root@client script]# ./mysql_status.sh
0
# 创建自定义监控任务
[root@client script]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_status,/bin/bash /script/mysql_status.sh
[root@client script]# systemctl restart zabbix_agentd
在server主机中获取
[root@server ~]# zabbix_get -s 192.168.136.139 -k check_mysql_status
0
创建监控项和触发器
创建监控项
创建触发器
测试
触发
开启防火墙使主从状态断开联系,模拟告警
[root@c3 ~]# systemctl stop firewalld
[root@c3 ~]# systemctl start firewalld
root@client script]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 28
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.008 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.005 sec)
MariaDB [(none)]>
4.自定义监控mysql主从延迟
是查看的是show slave status\G;中的Seconds_Behind_M
aster:的值
[root@client script]# vim mysql_delay.sh
[root@client script]# cat mysql_delay.sh
#!/bin/bash
mysql -uroot -p'123456' -e 'show slave status\G' | grep 'Seconds_Behind_Master' | awk '{print $NF}'
[root@client script]# chmod +x mysql_delay.sh
[root@client script]# ./mysql_delay.sh
0
[root@client script]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_status,/bin/bash /script/mysql_status.sh
UserParameter=check_mysql_delay,/bin/bash /script/mysql_delay.sh
[root@client script]# systemctl restart zabbix_agentd
[root@server ~]# zabbix_get -s 192.168.136.139 -k check_mysql_delay
0