zabbix自定义监控
自定义监控进程
// 修改被监控机的配置文件
[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# vim zabbix_agentd.conf
322 UnsafeUserParameters=1
525 UserParameter=check_process[*],/scripts/check_process.sh $1
[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd
// 注意在被监控机去写脚本获取数据
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# ls
[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]# chmod +x check_process.sh
//返回服务端手动获取数据
[root@server ~]# zabbix_get -s 192.168.200.158 -k check_process[httpd]
0
[root@localhost ~]# systemctl stop httpd.service //关闭httpd
[root@server ~]# zabbix_get -s 192.168.200.158 -k check_process[httpd] //显示进程有问题
1
在web界面配置监控项和触发器
查看最新数据,因为关闭了httpd所以数值为1
选择主机添加触发器
可以看到httpd服务报警了,存在故障,因为关闭了httpd服务输出的结果为1就有问题触发报警
自定义监控日志
实例:现在需要监控httpd的日志文件
写脚本,脚本放到统一的位置
注意!!!
下面使用的是python编写的脚本
作用:检查日志文件中是否有指定的关键字
第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)
第三个参数为搜索关键字,默认为Error
[root@localhost scripts]# ls
check_httpd.sh log.py
[root@localhost scripts]#
[root@localhost scripts]# chmod +x 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]# yum -y install python3 // 下载python3
// 手动触发验证一下脚本
[root@localhost scripts]# ./log.py /var/log/httpd/error_log
0
[root@localhost scripts]# echo "Error" >> /var/log/httpd/error_log
[root@localhost scripts]# ./log.py /var/log/httpd/error_log
1
[root@localhost scripts]# rm -f /tmp/logseek
修改被监控机的配置文件
[root@localhost scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_log[*],/scripts/log.py $1 $2 $3
[root@localhost scripts]# pkill zabbix
[root@localhost scripts]# zabbix_agentd
[root@localhost scripts]# chmod 775 /var/log/httpd/
// 返回服务端是否能获取数据
[root@server ~]# zabbix_get -s 192.168.200.158 -k check_log[/var/log/httpd/error_log]
0
[root@localhost scripts]# echo "Error" >> /var/log/httpd/error_log
[root@server ~]# zabbix_get -s 192.168.200.158 -k check_log[/var/log/httpd/error_log]
1
在web界面配置监控项和触发器
在主机里面添加监控项
在主机里面添加触发器
查看最新数据
手动触发验证
[root@localhost scripts]# echo "Error" >> /var/log/httpd/error_log
[root@localhost scripts]#
在监控项里面继续添加其他值
手动触发验证
[root@localhost scripts]# echo "error" >> /var/log/httpd/error_log
[root@localhost scripts]#
自定义监控MySQL主从
- 写脚本,脚本放在统一位置
- 修改被监控机zabbix_agentd.conf配置文件
UnxsafeParameters=1
UnserParameter=<key>,<command>
- 重启zabbix_agent
- 在web界面配置监控项和触发器
[root@slave scripts]# vim check_replication.sh
#!/bin/bash
user=zabbix
pass=zabbix123!
count=$(mysql -u$user -p"$pass" -e 'show slave status\G' |grep '_Running:' |grep -c 'Yes')
if [ $count -ne 2 ];then
echo '1'
else
echo '0'
fi
[root@slave scripts]# chmod +x check_replication.sh
[root@slave ~]# vim .my.cnf
[client]
user=root
password=syb123
[root@slave scripts]# ./check_replication.sh
0
[root@slave ~]# cd /usr/local/etc/
[root@slave etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@slave etc]# vim zabbix_agentd.conf
UserParameter=check_replication,/scripts/check_replication.sh
[root@slave etc]# pkill zabbix
[root@slave etc]# zabbix_agentd
MariaDB [(none)]> grant SUPER, REPLICATION CLIENT on *.* to 'zabbix'@'localhost' identified by 'zabbix123!';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> quit
Bye
[root@slave ~]# mysql -uzabbix -pzabbix123!
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 22
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)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.161
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 781
Relay_Log_File: my_lay.000002
Relay_Log_Pos: 1008
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 781
Relay_Log_Space: 1308
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 10
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Slave_DDL_Groups: 3
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
[root@server ~]# zabbix_get -s 192.168.200.158 -k check_replication
0
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.002 sec)
[root@server ~]# zabbix_get -s 192.168.200.158 -k check_replication
1
添加监控项
添加触发器
结果
mysql主从延迟
- 写脚本,脚本放在统一位置
- 修改被监控机zabbix_agentd.conf配置文件
UnxsafeParameters=1
UnserParameter=, - 重启zabbix_agent
- 在web界面配置监控项和触发器
// 脚本
[root@slave scripts]# vim check_replication_delay.sh
#!/bin/bash
delay_count=$(mysql -uzabbix -pzabbix123! -e 'show slave status\G' | grep 'Behind' | awk '{print $2}')
if [ $delay_count != NULL ];then
echo $delay_count
else
echo '0'
fi
[root@slave scripts]# chmod +x check_replication_delay.sh
[root@slave scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_replication_delay,/scripts/check_replication_delay.sh
[root@slave scripts]# pkill zabbix
[root@slave scripts]# zabbix_agentd
[root@server ~]# zabbix_get -s 192.168.200.158 -k check_replication_delay
0
添加监控项
添加触发器
结果
用户和组权限设置
guest用户此时为禁用
在用户组里将guest用户踢出
此时guest用户启用
guest用户即可访问
设置guest用户不能访问
给guest用户读权限
声音报警