1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
第一步服务端和客户端安装mailx和postfix
yum install postfix -y
systemctl enable postfix --now
yum install sendmail -y
第二步编写脚本
set from=xx@qq.com
set smtp=smtp.qq.com
set smtp-quth-user=xx@qq.com
set smtp-auth-password=docmjcv
set smtp-auth=login
第三步判断当前磁盘剩余空间是否由20G
a=`df -m | grep -w /boot | tr -s " " " "|cut -d " " -f 4`
b=`df -m | grep -w / | tr -s " " " "|cut -d " " -f 4`
c=$((a+b))
free=$((c/1024))
if [ $free -lt 20 ]
then
echo 空间不足!| mail -s "内存报警!" redhat
fi
编写脚本
第四步设置计划任务
[root@server ~]# vim /etc/crontab
0 0 * * * root /bin/bash /root/diskl.sh 在最后一行添加
2、判断web服务是否运行 (1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
第一步下载服务
yum install httpd -y
第二步编写脚本
ps_httpd=`ps -ef | grep httpd | grep -v grep | wc -l`
if [[ $ps_httpd > 0 ]];then
echo "httpd.server is runing"
else
systemctl start httpd
systemctl stop firewalld
fi
测试
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web serveris running;如果不能正常访问,返回12状态码。
编写脚本
curl -s 192.168.19.135 > /dev/null
if [[ $? = 0 ]];then
echo "web server is runing"
else
exit 12
fi
测试