zabbix自定義監控 自定義監控進程 測試監控httpd,需要在監控端部署httpd,以方便監控 配置監控腳本 #在監控端 [root@localhost ~]# dnf -y install httpd [root@localhost ~]# systemctl start httpd [roo ...
zabbix自定義監控
自定義監控進程
測試監控httpd,需要在監控端部署httpd,以方便監控
配置監控腳本
#在監控端
[root@localhost ~]# dnf -y install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# ss -anlt | grep 80
LISTEN 0 128 *:80 *:*
#編寫腳本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# vim /scripts/check_process.sh
#!/bin/bash
count=$(ps -ef|grep -Ev "grep|$0" | grep $1 | wc -l)
if [ $count -eq 0 ];then
echo '1'
else
echo '0'
fi
[root@localhost ~]# chmod +x /scripts/check_process.sh
[root@localhost ~]# ll /scripts/check_process.sh
-rwxr-xr-x. 1 root root 119 Sep 6 21:09 /scripts/check_process.sh
#在監控端,修改配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UnsafeUserParameters=check_process[*],/bin/bash /scripts/check_process.sh $1
#服務端檢查key是否可用
[root@zabbix ~]# zabbix_get -s 192.168.111.137 -k check_process[httpd]
0
[root@zabbix ~]# zabbix_get -s 192.168.111.137 -k check_process[mysql]
1
[root@zabbix ~]# zabbix_get -s 192.168.111.137 -k check_process[zabbix]
0
添加監控項
查看監控數據
添加觸發器
手動關閉httpd,進行測試觸發報警
[root@localhost ~]# systemctl stop httpd
查看報錯
發送郵件
查看郵件
自定義監控日誌
初學者複製的腳本
忘情的log腳本日誌
#由於是python的腳本,需要在python環境中
[root@localhost ~]# dnf -y install python36
#編寫腳本
[root@localhost ~]# cd /etc/scripts/
[root@localhost scripts]# vim log.py
#!/usr/bin/env python3
import sys
import re
def prePos(seekfile):
global curpos
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]# chmod +x log.py
[root@localhost scripts]# ll
total 12
-rwxr-xr-x. 1 root root 128 Sep 6 21:53 check_process.sh
-rwxr-xr-x. 1 root root 1890 Sep 6 22:49 log.py
#修改配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_logs[*],/etc/scripts/log.py $1 $2 $3
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd
#需要給/var/log/httpd目錄添加許可權
[root@localhost ~]# chmod 755 /var/log/httpd/
[root@localhost ~]# ll /var/log/httpd/
total 8
-rw-r--r--. 1 root root 704 Sep 6 21:42 access_log
-rw-r--r--. 1 root root 2635 Sep 6 22:12 error_log
#給httpd的error日誌,添加一個錯誤信息
[root@localhost ~]# echo "Error" >> /var/log/httpd/error_log
#服務端測試
[root@zabbix ~]# zabbix_get -s 192.168.111.137 -k check_logs['/var/log/httpd/error_log']
1
[root@zabbix ~]# zabbix_get -s 192.168.111.137 -k check_logs['/var/log/httpd/error_log']
0
添加監控項
添加觸發器
手動添加錯誤日誌,觸發報警
[root@localhost httpd]# echo "Error123" >> /var/log/httpd/error_log