1.创建脚本存放目录,写脚本,给执行权限,改脚本存放目录的属主属组为zabbix
[root@cwt-client ~]# cd /opt/scripts/
[root@cwt-client scripts]# ls
check_mysql.sh check_process.sh log.py swap.sh
[root@cwt-client scripts]# chmod a+x log.py
[root@cwt-client scripts]# chown -R zabbix.zabbix log.py
[root@cwt-client scripts]# ll
总用量 16
-rwxr-xr-x. 1 zabbix zabbix 124 8月 24 15:46 check_mysql.sh
-rwxr-xr-x. 1 zabbix zabbix 127 8月 24 16:26 check_process.sh
-rwxr-xr-x. 1 zabbix zabbix 1854 8月 25 10:27 log.py
-rwxr-xr-x. 1 zabbix zabbix 175 8月 25 09:59 swap.sh
脚本内容
#!/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
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)
2.更改客户端配置文件agentd.conf
[root@cwt-client ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_log[*],/usr/bin/python /opt/scripts/log.py $1 $2 $3
[root@cwt-client ~]# which python
/usr/bin/python
[root@cwt-client scripts]# /usr/bin/python log.py /var/log/httpd/error_log
0
[root@cwt-client scripts]# cd /var/log/httpd
[root@cwt-client httpd]# ll -d
drwx------. 4 0 root 169 5月 9 2017 .
[root@cwt-client ~]# setfacl -m u:zabbix:r-x /var/log/httpd/
3.重启zabbix——agent
[root@cwt-client ~]# pkill zabbix
[root@cwt-client ~]# zabbix_agentd
4.在服务端手动测试
[root@zabbix ~]# zabbix_get -s 192.168.40.140 -k check_log['/var/log/httpd/error_log','/tmp/hello.seek','error']
0
5.配置网页界面,添加监控项以及触发器
添加监控项
配置——主机——监控项
[root@cwt-client ~]# echo ‘failed’ >> /tmp/zabbix_agentd.log
添加触发器
配置——主机——触发器
6.验证
在配置文件/tmp/zabbix_agentd.log打印字符
[root@cwt-client ~]# echo 'failed' >> /tmp/zabbix_agentd.log
1.创建脚本存放目录,写脚本,给执行权限,改脚本存放目录的属主属组为zabbix
[root@cwt-client ~]# cd /scripts
[root@cwt-client scripts]# vim /scripts/mysql_delay.sh
#!/bin/bash
name=$(mysql -uroot -pchen123 -e 'show slave status \G;' 2>/dev/null |grep Behind |awk '{print NF}')
if [ $num -ne 0 ];then
echo "1"
else
echo "0"
fi
[root@cwt-client scripts]# chmod +x /scripts/mysql_delay.sh
[root@cwt-client scripts]# chown -R zabbix.zabbix /scripts/
[root@cwt-client scripts]# chmod 750 /scripts/mysql_delay.sh
[root@cwt-client scripts]# ll /scripts/
总用量 4
-rw-r--r--. 1 zabbix zabbix 0 6月 5 09:15 httpd.sh
-rwxr-x---. 1 zabbix zabbix 171 8月 26 18:30 mysql_delay.sh
2.改客户端配置文件agentd.conf
[root@cwt-client ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_mysql_delay,/bin/bash /scripts/mysql_delay.sh
3.重启zabbix_agent
[root@cwt-client ~]# pkill zabbix
[root@cwt-client ~]# zabbix_agentd
4.在服务端手动测试
[root@zabbix ~]# zabbix_get -s 192.168.40.140 -k check_mysql_delay
0