一、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间
1、安装邮件服务并配置邮件ssl证书
yum install mailx -y #安装邮件服务
mkdir -p /root/.certs
cd /root/.certs
echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt #配置QQ的ssl证书
vim /etc/mail.rc #进入并配置文件
#在最后一行添加内容
2、编写脚本
vim disk1.sh #进入脚本并开始配置
#!/bin/bash
#功能:检查磁盘剩余容量
disk=$(df -m | grep -w | tr -s " " | cut -d " " -f4)
if (($disk < 20000))
then
echo "warning : disk space less than 20g !" | mail -s " 警告邮件 " QQ邮箱号@qq.com
fi
#配置脚本内容
bash disk1.sh #检查脚本

3、配置每天检查时间
vim /etc/crontab #进入并开始配置文件
* * * * * root /bin/bash #配置时间在每天任意时刻

二、判断web服务是否执行,如果没有运行,则启动并配置防火墙规则
1、开启防火墙并配置开机启动
systemctl enable --now firewalld
2、配置脚本
vim disk2.sh #进入并配置
#!/bin/bash
ps_num=$(ps -ef | grep -c [n]ginx)
if (($ps_num>0))
then
echo "nginx is already running"
else
echo "nginx not started ,waiting......"
yum install nginx -y &> /dev/null
systemctl start nginx
firewall-cmd --permanent --zone=pulic --add-server=http &> /dev/null
firewall-cmd --permanent --zone=pulic --add-port=80/tcp &> /dev/null
firewall-cmd --reload &> /dev/null
echo "nginx is already running"
fi
#配置内容

三、使用curl命令访问第二题的web服务,看能否正常访问,如果可以,则返回web server is running; 如果不行,返回12状态
vim disk3.sh #进入并配置
#!/bin/bash
ip=$(ip a | grep ens32 | grep inet | cut -d / -fi | cut -d " " -f6)
curl $ip &> /dev/null
if (($?==0))
then
echo "web servers is running"
else
echo "web not accessible"
exit 12
fi
#配置内容


372

被折叠的 条评论
为什么被折叠?



