1-进程监控
1-1 在agent端编写脚本 取出需要监控资源的状态值
//创建目录
[root@localhost ~]# mkdir /scripts
//编写内容脚本
[root@localhost ~]# vim /scripts/check_process.sh
[root@localhost ~]# cat /scripts/check_process.sh
#!/bin/bash
status=$(ps -ef |grep $1|grep -Ev "$0|grep"|wc -l)
if [ $status -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 134 Sep 6 21:23 /scripts/check_process.sh
1-2 在agent端编写配置文件
//将下面两行取消注释作修改,或者直接添加这两行
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
# Mandatory: no
# Range: 0-1
# Default:
UnsafeUserParameters=1 //取消注释 改为1
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=,
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1 //取消注释写入内容
//重启agent,生效作更改的配置
[root@localhost ~]# pkill zabbix_agentd
[root@localhost ~]# zabbix_agentd
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//安装httpd服务
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
//在Server端测试刚刚所做的进程监控配置
[root@localhost ~]# zabbix_get -s 192.168.79.131 -k check_process[httpd]
0
[root@localhost ~]# zabbix_get -s 192.168.79.131 -k check_process[mysql]
1
1-3 在web界面添加监控项
1-4 在web界面添加触发器
1-5 手动触发警告
//在agent端中关闭服务器
[root@localhost ~]# systemctl stop httpd
2-日志监控
2-1在agent端编写脚本 取出需要监控资源的状态值
//下载python的编译器
[root@localhost ~]# yum -y install python3
//写python脚本。因shell脚本只能在Linux系统执行,如zabbix需要监控Windows系统
//则需要python脚本,py脚本在这两操作系统都能运行
[root@localhost ~]# vim /scripts/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 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() 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 ~]# chmod +x /scripts/log.py
[root@localhost ~]# ll /scripts/log.py
-rwxr-xr-x 1 root root 1937 Sep 6 22:15 /scripts/log.py
//测试一下该脚本是否可用,0代表没问题
[root@localhost scripts]# /scripts/log.py /var/log/httpd/error_log
0
执行脚本检查httpd的错误日志
[root@localhost scripts]# /scripts/log.py /var/log/httpd/error_log
0
//手动添加错误信息"Error"测试效果
[root@localhost scripts]# echo 'Error' >> /var/log/httpd/error_log
//可以看到输出了1,1代表日志文件里有报错
[root@localhost scripts]# /scripts/log.py /var/log/httpd/error_log
1
2-2在agent端编辑配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
# Mandatory: no
# Default:
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
UserParameter=check_log[*],/scripts/log.py $1 $2 $3 //添加这行
//重启agent,生效配置
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
[root@localhost ~]# pkill zabbix_agentd
[root@localhost ~]# zabbix_agentd
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//生效配置之后,去Server端测试
//测试之前先做些测试的准备工作
//设置文件权限。因为zabbix服务端访问客户端属于其他,如果
//文件的其他人没有读的权限,则无法监控该项
[root@localhost ~]# ll /var/log/httpd/error_log
-rw-r--r-- 1 root root 1020 Sep 6 22:25 /var/log/httpd/error_log
[root@localhost ~]# ll -d /var/log/httpd/
drwx------ 2 root root 41 Sep 6 21:33 /var/log/httpd/
[root@localhost ~]# chmod 755 /var/log/httpd/
[root@localhost ~]# ll -d /var/log/httpd/
drwxr-xr-x 2 root root 41 Sep 6 21:33 /var/log/httpd/
//因之前本地测试脚本生成了该文件,该文件的属组是本地的root用户,
//需本地测试完后删掉,在server执行脚本生成新的文件
[root@localhost ~]# ll /tmp/logseek
-rw-r--r-- 1 root root 4 Sep 6 22:25 /tmp/logseek
[root@localhost ~]# rm -f /tmp/logseek
//Server端测试
[root@localhost ~]# zabbix_get -s 192.168.79.131 -k check_log['/var/log/httpd/error_log']
1 //会输出1是因为之前测试往httpd的error_log文件追加了‘Error’,前面又把测试的logseek记录检查文件
//给删除了,这是执行脚本会生成新的logseek文件,该文件会从头开始,所以检测出‘Error’
//再次执行就不会输出1了,因为再次检测从上次检测的尾部之后开始,故当前error日志没有‘Error’信息
[root@localhost ~]# zabbix_get -s 192.168.79.131 -k check_log['/var/log/httpd/error_log']
0
//去到agent查看新生成的logseek记录检查文件,可以看到属主是zabbix
[root@localhost ~]# ll /tmp/logseek
-rw-rw-r-- 1 zabbix zabbix 4 Sep 6 22:32 /tmp/logseek
2-3 在web界面添加监控项
2-4 在web界面添加触发器
2-5 手动触发警告
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# echo 'Error' >> /var/log/httpd/error_log
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net