自定义监控进程
- 写脚本,脚本放在统一位置
- 修改被监控机zabbix_agentd.conf配置文件
UnxsafeParameters=1
UnserParameter=<key>,<command>
- 重启zabbix_agent
- 在web界面配置监控项和触发器
修改客户端zabbix_agentd文件zabbix_agentd.conf
[root@localhost etc]# vim zabbix_agentd.conf
UnsafeUserParameters=1 #默认为0。修改为1,打开自定义监控功能
······
UserParameter=check_process[*],/script/check_process.sh $1
#重启zabbix
[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd
#写自定义监控脚本
[root@localhost scripts]# cat 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@server ~]# zabbix_get -s 192.168.8.131 -k check_process[httpd]
配置监控项
添加触发器
自定义监控日志
- 写脚本,脚本放在统一位置
- 修改被监控机zabbix_agentd.conf配置文件
UnxsafeParameters=1
UnserParameter=<key>,<command>
- 重启zabbix_agent
- 在web界面配置监控项和触发器
#日志脚本
#!/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)
log.py作用:检查日志文件中是否有指定的关键字
第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件,相对路径、绝对路径均可)
第三个参数为搜索关键字,默认为Error
#执行脚本需安装python
[root@localhost scripts]# yum -y install python3
#修改文件
[root@localhost etc]# vim zabbix_agentd.conf
UserParameter=check_log[*],/scripts/log.py $1 $2 $3
[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd
#修改文件权限
[root@localhost ~]# chmod 755 /var/log/httpd/
配置监控项
添加触发器
#写入错误
[root@localhost ~]# echo "Error" >> /var/log/httpd/error_log
自定义监控mysql主从
- 写脚本,脚本放在统一位置
- 修改被监控机zabbix_agentd.conf配置文件
UnxsafeParameters=1
UnserParameter=<key>,<command>
- 重启zabbix_agent
- 在web界面配置监控项和触发器
#在家目录下写存放数据库密码文件
[root@localhost ] vim /scripts/.password
[client]
user=root
password=123
#编写脚本
[root@localhost scripts]# vim mysql_io.sh
#!/bin/bash
IO=$(mysql --defaults-file=/scripts/.password -e"show slave status\G;" | grep Slave_IO_Running: | awk '{print $2}')
if [ $IO == "Yes" ];then
echo "0"
else
echo "1"
fi
[root@localhost scripts]# cat mysql_sql.sh
#!/bin/bash
SQL=$(mysql --defaults-file=/scripts/.password -e"show slave status\G;" | grep Slave_SQL_Running: | awk '{print $2}')
if [ $SQL == 'Yes' ];then
echo "0"
else
echo "1"
fi
# 修改zabbix_agentd文件
[root@localhost scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_io,/scripts/mysql_io.sh
UserParameter=check_mysql_sql,/scripts/mysql_sql.sh
#重启服务
[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd
#在服务端上查看
[root@server ~]# zabbix_get -s 192.168.8.131 -k check_mysql_io
0
[root@server ~]# zabbix_get -s 192.168.8.131 -k check_mysql_sql
0
监控IO进程
监控SQL进程
#关闭主从同步
[root@localhost scripts]# mysql -uroot -p12
mysql> stop slave
-> ;
Query OK, 0 rows affected (0.00 sec)
mysql> reset slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.8.129
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4
Relay_Log_File: mysql_relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:
Slave_IO_Running: No
Slave_SQL_Running: No
mysql主从延迟
- 写脚本,脚本放在统一位置
- 修改被监控机zabbix_agentd.conf配置文件
UnxsafeParameters=1
UnserParameter=<key>,<command>
- 重启zabbix_agent
- 在web界面配置监控项和触发器
#脚本
[root@localhost scripts]# cat mysql_behind.sh
#!/bin/bash
delay=$(mysql --defaults-file=/scripts/.password -e"show slave status\G;" | grep Behind | awk '{print $2}')
if [ $delay -eq 0 ];then
echo '0'
else
echo '1'
fi
#
添加监控项
测试=0时,报警效果
用户和组权限设置
guest用户此时为禁用
在用户组里将guest用户踢出
此时guest用户启用
guest用户即可访问
设置guest用户不能访问
给guest用户读权限
声音报警