实验效果:实现httpd停止服务,zabbix执行远程命令进行恢复,1分钟之后如果恢复失败,发邮件给运行,1分钟之后,问题没解决发邮件给领导
ip地址 | 主机 |
---|---|
192.168.100.11 | zabbix-server,anasible |
192.168.100.12 | zabbix-agent,httpd |
略
略
xxxxxxxxxx
11yum install httpd -y
xxxxxxxxxx
41vim /etc/zabbix/zabbix_agentd.conf
2UserParameter=httpd,ps -ef|grep -v grep|grep -c '/usr/sbin/httpd'
3
4systemctl restart zabbix-agent
a:创建发件人 b:创建两个收件人
boss用户需要超级管理权限
xxxxxxxxxx
71yum install ansible -y
2
3vim /etc/ansible/hosts
4[web]
5192.168.100.12 ansible_ssh_user='root' ansible_ssh_pass='123456'
6
7ansible web -m shell -a 'systemctl restart httpd.service'
客户端效果
xxxxxxxxxx
321[root@oldboy ~]# systemctl status httpd
2● httpd.service - The Apache HTTP Server
3 Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
4 Active: activating (start) since Tue 2019-08-06 22:20:08 CST; 4s ago
5 Docs: man:httpd(8)
6 man:apachectl(8)
7 Process: 2839 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
8 Main PID: 2894 (httpd)
9 CGroup: /system.slice/httpd.service
10 └─2894 /usr/sbin/httpd -DFOREGROUND
11
12Aug 06 22:20:08 oldboy systemd[1]: Starting The Apache HTTP Server...
13Aug 06 22:20:08 oldboy httpd[2894]: AH00558: httpd: Could not reliably determine the server's fully qua...ssage
14Hint: Some lines were ellipsized, use -l to show in full.
15
16[root@oldboy ~]# systemctl status httpd
17● httpd.service - The Apache HTTP Server
18 Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
19 Active: active (running) since Tue 2019-08-06 22:20:13 CST; 295ms ago
20 Docs: man:httpd(8)
21 man:apachectl(8)
22 Process: 2839 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
23 Main PID: 2894 (httpd)
24 Status: "Processing requests..."
25 CGroup: /system.slice/httpd.service
26 ├─2894 /usr/sbin/httpd -DFOREGROUND
27 ├─2898 /usr/sbin/httpd -DFOREGROUND
28 ├─2899 /usr/sbin/httpd -DFOREGROUND
29 ├─2900 /usr/sbin/httpd -DFOREGROUND
30 ├─2901 /usr/sbin/httpd -DFOREGROUND
31 └─2902 /usr/sbin/httpd -DFOREGROUND
32
xxxxxxxxxx
51vim /etc/zabbix/zabbix_agentd.conf
2EnableRemoteCommands=1
3AllowRoot=1
4
5systemctl restart zabbix-agent.service
整体效果如下:
远程命令细节:
xxxxxxxxxx
11命令:ansible web -m shell -a 'systemctl restart httpd.service'
停止httpd服务
xxxxxxxxxx
101[root@oldboy ~]# systemctl status httpd
2● httpd.service - The Apache HTTP Server
3 Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
4 Active: activating (start) since Tue 2019-08-06 22:20:08 CST; 4s ago
5 Docs: man:httpd(8)
6 man:apachectl(8)
7 Process: 2839 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
8 Main PID: 2894 (httpd)
9 CGroup: /system.slice/httpd.service
10 └─2894 /usr/sbin/httpd -DFOREGROUND
查看httpd服务状态
xxxxxxxxxx
161[root@oldboy ~]# systemctl status httpd
2● httpd.service - The Apache HTTP Server
3 Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
4 Active: active (running) since Tue 2019-08-06 22:20:13 CST; 295ms ago
5 Docs: man:httpd(8)
6 man:apachectl(8)
7 Process: 2839 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
8 Main PID: 2894 (httpd)
9 Status: "Processing requests..."
10 CGroup: /system.slice/httpd.service
11 ├─2894 /usr/sbin/httpd -DFOREGROUND
12 ├─2898 /usr/sbin/httpd -DFOREGROUND
13 ├─2899 /usr/sbin/httpd -DFOREGROUND
14 ├─2900 /usr/sbin/httpd -DFOREGROUND
15 ├─2901 /usr/sbin/httpd -DFOREGROUND
16 └─2902 /usr/sbin/httpd -DFOREGROUND
停止httpd服务
xxxxxxxxxx
141[root@oldboy ~]# systemctl status httpd
2● httpd.service - The Apache HTTP Server
3 Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
4 Active: activating (start) since Tue 2019-08-06 22:20:08 CST; 4s ago
5 Docs: man:httpd(8)
6 man:apachectl(8)
7 Process: 2839 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
8 Main PID: 2894 (httpd)
9 CGroup: /system.slice/httpd.service
10 └─2894 /usr/sbin/httpd -DFOREGROUND
11
12#使用nc监听80端口,模拟httpd重启失败
13yum install nc -y
14nc -l 80
至此,已经实现httpd停止服务,zabbix执行远程命令进行恢复,1分钟之后如果恢复失败,发邮件给运行,1分钟之后,问题没解决发邮件给领导