实验效果:实现httpd停止服务,zabbix执行远程命令进行恢复,1分钟之后如果恢复失败,发邮件给运行,1分钟之后,问题没解决发邮件给领导
| ip地址 | 主机 |
|---|---|
| 192.168.100.11 | zabbix-server,anasible |
| 192.168.100.12 | zabbix-agent,httpd |
略
略
xxxxxxxxxx11yum install httpd -yxxxxxxxxxx41vim /etc/zabbix/zabbix_agentd.conf2UserParameter=httpd,ps -ef|grep -v grep|grep -c '/usr/sbin/httpd'34systemctl restart zabbix-agent

a:创建发件人
b:创建两个收件人


boss用户需要超级管理权限

xxxxxxxxxx71yum install ansible -y23vim /etc/ansible/hosts4[web]5192.168.100.12 ansible_ssh_user='root' ansible_ssh_pass='123456'67ansible web -m shell -a 'systemctl restart httpd.service'客户端效果
xxxxxxxxxx321[root@oldboy ~]# systemctl status httpd2● httpd.service - The Apache HTTP Server3 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 ago5 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.service10 └─2894 /usr/sbin/httpd -DFOREGROUND1112Aug 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...ssage14Hint: Some lines were ellipsized, use -l to show in full.1516[root@oldboy ~]# systemctl status httpd17● httpd.service - The Apache HTTP Server18 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 ago20 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.service26 ├─2894 /usr/sbin/httpd -DFOREGROUND27 ├─2898 /usr/sbin/httpd -DFOREGROUND28 ├─2899 /usr/sbin/httpd -DFOREGROUND29 ├─2900 /usr/sbin/httpd -DFOREGROUND30 ├─2901 /usr/sbin/httpd -DFOREGROUND31 └─2902 /usr/sbin/httpd -DFOREGROUND32xxxxxxxxxx51vim /etc/zabbix/zabbix_agentd.conf 2EnableRemoteCommands=13AllowRoot=145systemctl restart zabbix-agent.service整体效果如下:

远程命令细节:

xxxxxxxxxx11命令:ansible web -m shell -a 'systemctl restart httpd.service'停止httpd服务
xxxxxxxxxx101[root@oldboy ~]# systemctl status httpd2● httpd.service - The Apache HTTP Server3 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 ago5 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.service10 └─2894 /usr/sbin/httpd -DFOREGROUND
查看httpd服务状态
xxxxxxxxxx161[root@oldboy ~]# systemctl status httpd2● httpd.service - The Apache HTTP Server3 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 ago5 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.service11 ├─2894 /usr/sbin/httpd -DFOREGROUND12 ├─2898 /usr/sbin/httpd -DFOREGROUND13 ├─2899 /usr/sbin/httpd -DFOREGROUND14 ├─2900 /usr/sbin/httpd -DFOREGROUND15 ├─2901 /usr/sbin/httpd -DFOREGROUND16 └─2902 /usr/sbin/httpd -DFOREGROUND
停止httpd服务
xxxxxxxxxx141[root@oldboy ~]# systemctl status httpd2● httpd.service - The Apache HTTP Server3 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 ago5 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.service10 └─2894 /usr/sbin/httpd -DFOREGROUND1112#使用nc监听80端口,模拟httpd重启失败13yum install nc -y14nc -l 80
至此,已经实现httpd停止服务,zabbix执行远程命令进行恢复,1分钟之后如果恢复失败,发邮件给运行,1分钟之后,问题没解决发邮件给领导