站点可用性监测实验
利用Crontab来判断Web服务器是否可用,如果不可用,则自动启动,并且将过程的操作写入到日志文件中。
一个站点如果无法访问,可能的原因有两个:
(1)Web服务器没有正常启动:如何使用Shell进行判断?
(2)防火墙没有允许80端口通过:让shell执行firewall-cmd添加端口通过的命令即可。
将web服务关闭
[root@localhost yt]# /opt/lampp/lampp stop
Stopping XAMPP for Linux 8.2.12-0...
XAMPP: Stopping Apache...ok.
XAMPP: Stopping MySQL...ok.
XAMPP: Stopping ProFTPD...ok.
[root@localhost yt]# curl http://192.168.102.131/dashboard/phpinfo.php
curl: (7) Failed connect to 192.168.102.131:80; Connection refusedecho $? 执行结果非0,则上一条命令执行失败,
[root@localhost yt]# echo $?
7
[root@localhost yt]# /opt/lampp/lampp start
[root@localhost yt]# curl http://192.168.102.131/dashboard/phpinfo.phpecho $? 执行结果0,则上一条命令执行成功
[root@localhost yt]# echo $?
0
防火墙正常启动,关闭防火墙的80端口,curl还是能正常访问,所以还要进一步判断80端口
[root@localhost yt]# firewall-cmd --remove-port=80/tcp
[root@localhost yt]# curl http://192.168.102.131/dashboard/phpinfo.php
[root@localhost yt]# echo $?
0
(2)防火墙没有允许80端口通过:让shell执行firewall-cmd添加端口通过的命令即可。
通过查找具体的端口号,grep 80之后有内容执行命令成功,grep 80 之后没有内容则执行命令失败,可以通过该方式来判断是否正常启用80端口。[root@localhost yt]# firewall-cmd --list-all
public (active)target: defaulticmp-block-inversion: nointerfaces: ens33sources: services: ssh dhcpv6-clientports: protocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules: [root@localhost yt]# firewall-cmd --list-port |grep 80
[root@localhost yt]# echo $?
1
[root@localhost yt]# firewall-cmd --add-port=80/tcp
success
[root@localhost yt]# firewall-cmd --list-port |grep 80
80/tcp
[root@localhost yt]# echo $?
0
明确检测的特征,使用netstat -ant | grep :80 进行查询,结合echo $? 判断是否成功访问[root@localhost yt]# netstat -ant | grep :80
tcp6 0 0 :::80 :::* LISTEN
tcp6 0 0 192.168.102.131:80 192.168.102.1:61292 TIME_WAIT
tcp6 0 0 192.168.102.131:80 192.168.102.1:55504 ESTABLISHED
[root@localhost yt]# echo $?
0
[root@localhost yt]# netstat -ant | grep :8080
[root@localhost yt]# echo $?
1
完成上述特征处理,在shell中编写脚本,并让cron定时开启。
启动crond服务,确定是否有cron在运行
[root@localhost yt]# systemctl start crond
[root@localhost yt]# systemctl status crond[root@localhost yt]# crontab -l
no crontab for root
接下来编写脚本
curl http://192.168.102.131/dashboard/phpinfo.php >/dev/null
if [ $? -ne 0 ]; then/opt/lampp/lampp startecho "检测到lampp没有启动,已经完成启动---,时间:"`date "+%Y-%m-%d %H:%M:%S"` > /home/yt/site_check.log
fifirewall-cmd --list-port | grep 80
if [ $? -ne 0 ]; thenfirewall-cmd --add-port=80/tcpecho "检测到防火墙没有启动80端口,已经完成启动---,时间:"`date "+%Y-%m-%d %H:%M:%S"` > /home/yt/site_check.log
fi
测试:将lampp服务和防火墙80端口关闭,需要root权限[root@localhost test]# /opt/lampp/lampp stop
Stopping XAMPP for Linux 8.2.12-0...
XAMPP: Stopping Apache...ok.
XAMPP: Stopping MySQL...ok.
XAMPP: Stopping ProFTPD...ok.
[root@localhost test]# /opt/lampp/lampp status
Version: XAMPP for Linux 8.2.12-0
Apache is not running.
MySQL is not running.
ProFTPD is not running.[root@localhost test]$ systemctl restart firewalld
[root@localhost test]$ systemctl status firewalld
执行脚本
[root@localhost test]$ chmod u+x site_check.sh [root@localhost test]# sh site_check.sh % Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 86584 0 86584 0 0 15.6M 0 --:--:-- --:--:-- --:--:-- 20.6M
success
接下来编写定时任务
[root@localhost test]# pwd
/home/yt/test
[root@localhost test]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab[root@localhost test]# crontab -l
* * * * * sh /home/yt/test/site_check.sh每分钟执行一次定时任务
测试情况如下图: