启动进程httpd(80),mysql(3306),php(9000),zabbix_server(10051),zabbix_agentd(10050),mail(25)
#服务端192.168.31.129
[root@zabbix_server ~]# ss -antl
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:10051 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
#客户端192.168.31.131
[root@localhost ~]# ss -antl
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:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 128 [::1]:6010 [::]:*
1.监控httpd进程
//在客户端上安装httpd服务
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd.service //启动服务
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled;>
Active: active (running) since Sun 2022-07-10 19:51:34 CST; 9s a>
Docs: man:httpd.service(8)
Main PID: 52898 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11088)
Memory: 22.8M
CGroup: /system.slice/httpd.service
├─52898 /usr/sbin/httpd -DFOREGROUND
├─52899 /usr/sbin/httpd -DFOREGROUND
├─52900 /usr/sbin/httpd -DFOREGROUND
├─52901 /usr/sbin/httpd -DFOREGROUND
└─52902 /usr/sbin/httpd -DFOREGROUND
Jul 10 19:51:34 localhost.localdomain systemd[1]: Starting The Apac>
Jul 10 19:51:34 localhost.localdomain httpd[52898]: AH00558: httpd:>
Jul 10 19:51:34 localhost.localdomain systemd[1]: Started The Apach>
Jul 10 19:51:34 localhost.localdomain httpd[52898]: Server configur>
[root@localhost ~]#
//修改配置文件
[root@localhost ~]# cd /usr/local/etc
[root@localhost etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@localhost etc]# vim 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=<key>,<shell command> //复制这个UserParameter以及后面的>止,到文件的最后空白处
# See 'zabbix_agentd' directory for examples.
UserParameter=check_process_httpd,/bin/bash /scripts/check_process.sh
//文件的最后空白处写
//重启agentd服务,让配置文件生效
[root@localhost ~]# pkill zabbix_agentd //先杀死进程
[root@localhost ~]# zabbix_agentd //再启动
[root@localhost ~]# ss -antl
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:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6011 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 128 [::1]:6010 [::]:*
LISTEN 0 128 [::1]:6011 [::]:*
//查看httpd相关的进程
[root@localhost ~]# ps -ef | grep httpd
root 52898 1 0 19:51 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 52899 52898 0 19:51 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 52900 52898 0 19:51 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 52901 52898 0 19:51 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 52902 52898 0 19:51 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 53157 51875 0 19:53 pts/1 00:00:00 grep --color=auto httpd
//和httpd有关的服务条数为5
[root@localhost ~]# ps -ef | grep -v grep | grep -c httpd
5 //-c 显示过滤后的计数 -v 取反将grep httpd 的进程取消 不让其进入计数
编写脚本
//创建目录
[root@localhost etc]# mkdir /scripts/
//编写脚本
[root@localhost etc]# vim /scripts/check_process.sh
[root@localhost etc]# cat /scripts/check_process.sh
#!/bin/bash
count=$(ps -ef | grep -v grep | grep -c httpd) //定义变量count的值为httpd进程数
if [ $count -ne 5 ];then //如果count不等于5,那么
echo '1' //打印为1
fi
//给脚本执行权限
[root@localhost etc]# cd /scripts/
[root@localhost scripts]# ls
check_process.sh
[root@localhost scripts]# chmod +x check_process.sh
[root@localhost scripts]# ll
total 4
-rwxr-xr-x. 1 root root 101 Jul 10 19:58 check_process.sh
测试脚本
//先停掉httpd服务
[root@localhost ~]# systemctl stop httpd.service
[root@localhost ~]# ps -ef | grep -v grep | grep -c httpd
0 //此时进程数为0
//运行脚本
[root@localhost scripts]# bash check_process.sh
1 //进程数不等于5时打印为1
//查看zabbix_server服务端是否能打印
[root@zabbix_server ~]# zabbix_get -s 192.168.31.131 -k check_process_httpd
1
添加监控项
添加触发器
优化
//优化脚本
[root@localhost scripts]# vim check_process.sh
#!/bin/bash
//"grep|$0" 过滤脚本自身 $1脚本执行时后面的参数
count=$(ps -ef | grep -Ev "grep|$0" | grep -c $1)
if [ $count -eq 0 ];then //当count 等于0 时打印1 因为正常程序执行都会有进程 所以不可能计数为0
echo '1'
else
echo '0' //没问题打印0
fi
//此时httpd是关闭的,所以打印应该为1
[root@localhost scripts]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled;>
Active: inactive (dead)
Docs: man:httpd.service(8)
//执行脚本, 添加$1的参数 打印结果为1 说明没有httpd相关的进程
[root@localhost scripts]# bash check_process.sh httpd
1
//修改配置文件
[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@localhost etc]# vim zabbix_agentd.conf
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
//重启zabbix_agentd
[root@localhost etc]# pkill zabbix_agentd
[root@localhost etc]# zabbix_agentd
//在服务端测试
[root@zabbix_server ~]# zabbix_get -s 192.168.31.131 -k check_process[httpd]
1 //httpd是停止的
[root@zabbix_server ~]# zabbix_get -s 192.168.31.131 -k check_process[zabbix]
0 //zabbix是正常运行的
zabbix网页修改监控项
2.监控mysql进程
//先在客户端上安装mysql服务
[root@localhost ~]# yum -y install mysql mysql-server //安装mysql 和mysql服务
[root@localhost ~]# systemctl start mysqld.service
[root@localhost ~]# systemctl status mysqld.service
● mysqld.service - MySQL 8.0 database server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled>
Active: active (running) since Sun 2022-07-10 20:47:20 CST; 4min>
Process: 55335 ExecStartPost=/usr/libexec/mysql-check-upgrade (co>
Process: 55211 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mys>
Process: 55187 ExecStartPre=/usr/libexec/mysql-check-socket (code>
Main PID: 55291 (mysqld)
Status: "Server is operational"
Tasks: 37 (limit: 11088)
Memory: 444.8M
CGroup: /system.slice/mysqld.service
└─55291 /usr/libexec/mysqld --basedir=/usr
Jul 10 20:47:15 localhost.localdomain systemd[1]: Starting MySQL 8.>
Jul 10 20:47:15 localhost.localdomain mysql-prepare-db-dir[55211]: >
Jul 10 20:47:20 localhost.localdomain systemd[1]: Started MySQL 8.0>
zabbix网页添加监控项
添加触发器
测试
//关闭mysql服务
[root@localhost ~]# systemctl stop mysqld.service
自定义监控日志(httpd)
//把脚本传到/scripts目录下
[root@localhost scripts]# ls
log.py
脚本下载地址 https://github.com/chendao2015/pyscripts
[root@localhost scripts]# chmod +x log.py //给脚本log执行权限
[root@localhost scripts]# ll
总用量 8
-rwxr-xr-x. 1 root root 124 7月 9 11:17 check_process.sh
-rwxr-xr-x. 1 root root 1854 7月 9 16:15 log.py
[root@localhost ~]# setfacl -m u:zabbix:rx /var/log/httpd //让zabbix用户对httpd的日志目录有读权限
[root@localhost ~]# getfacl /var/log/httpd/
getfacl: Removing leading '/' from absolute path names
# file: var/log/httpd/
# owner: root
# group: root
user::rwx
user:zabbix:r-x
group::---
mask::r-x
other::---
//测试脚本
[root@localhost ~]# yum install python3 -y //安装python
[root@localhost scripts]# python3 log.py /var/log/httpd/error_log //正常执行结果为0
0
[root@localhost scripts]# echo 'Error' >> /var/log/httpd/error_log //向日志中添加Error
[root@localhost scripts]# python3 log.py /var/log/httpd/error_log //脚本检查到Error 打印1
1
[root@localhost scripts]# python3 log.py /var/log/httpd/error_log //再次执行 因为日志是实时刷新的所以这次 显示无错误 打印0
0
[root@localhost scripts]# cat /tmp/logseek //logseek记录查询的位置
3851 //此时是3851
[root@localhost scripts]# echo 'fshwehdcuh' >> /var/log/httpd/error_log
[root@localhost scripts]# python3 log.py /var/log/httpd/error_log //执行脚本
0
[root@localhost scripts]# cat /tmp/logseek //再次查看
3873 //记录变为了3873
[root@localhost scripts]# ll /tmp/logseek
-rw-r--r--. 1 root root 4 7月 9 17:07 /tmp/logseek //当网页使用时没有权限
[root@localhost scripts]# rm -rf /tmp/logseek //脚本测试结束后可以删掉 网页会自动创建
//配置zabbix_agentd.conf文件,添加如下内容
[root@localhost etc]# vim zabbix_agentd.conf
UserParameter=check_logs[*],/usr/bin/python3 /scripts/log.py $1 $2 $3 //可以传更多的参数
[root@localhost ~]# pkill zabbix_agentd
[root@localhost ~]# zabbix_agentd //重启让文件生效
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
zabbix网页添加监控项
添加触发器
测试
//添加Error 到httpd的错误日志里
[root@localhost scripts]# echo 'Error' >> /var/log/httpd/error_log