1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
(1)安装s-nail
[root@server ~]# yum install s-nail -y
(2)编辑s-nail配置文件
[root@server ~]# vim /etc/s-nail.rc
在文件末尾添加配置
set from=XXXXXX@qq.com #qq邮箱
set smtp=smtp.qq.com #smtp协议
set smtp-auth-user=xxxxxxx@qq.com
set smtp-auth-password=XXXXXXXXXXX
set smtp-auth=login
最后wq!强制保存退出
(3)制作脚本文件判断磁盘空间
[root@server ~]# vim disk1.sh
#!/bin/bash
#
disk=`df -m | grep -w " /" | tr -s " " | cut -d " " -f4`
str1="warning disk spece less than 20G !"
if [ "$disk" -lt 20000 ]
then
echo "$str1" | mail -s "$str1" xxxxxx@qq.com
fi
wq保存退出
(4)在系统周期化文件/etc/crontab中编写周期化执行脚本disk1.sh
在文件末尾添加配置
0 0 * * * root /bin/bash /root/disk1.sh
wq保存退出
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
(1)创建脚本web1.sh并编辑
[root@server ~]# vim web1.sh
#!/bin/bash
ps=`ps -ef | grep ”httpd“ | grep -v "grep" | wc -l`
if [ "$ps" -gt 0 ]
then
echo "httpd is already running"
else
echo "httpd not start ,waiting……"
yum install httpd -y &> /dev/null
systemctl start httpd
systemctl start firewalld
firewall-cmd --permanent --zone=public --add-service=http > /dev/null
firewall-cmd --permanent --zone=public --add-port=80/tcp > /dev/null
firewall-cmd --reload > /dev/null
echo "httpd is already running!"
fi
wq保存退出
(2)执行脚本
[root@server ~]# bash web1.sh
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
(1)创建脚本web2.sh并编辑
[root@server ~]# vim web2.sh
#!/bin/bash
curl -s 192.168.110.128 > /dev/null
if (($?==0))
then
echo "web server is running"
else
echo "web not accessible"
exit 12
fi
wq保存退出
(2)执行脚本
[root@server ~]# bash web2.sh