1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
2、判断web服务是否运行
1)、查看进程的方式判断该程序是否运行
2)、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务。
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
1)、服务端和客户端安装mailx和postfix
#服务端
[root@server ~]# yum install postfix -y
[root@server ~]# systemctl enable postfix --now
#客户端
[root@client ~]# yum install sendmail -y
2)、编写脚本
[root@server ~]# cat mail.sh
#!/bin/bash
##############################################################
# File Name: zuoye.sh
# Version: V1.0
# Author: xx
# Email: xx@163.com
# Organization: http://www.xx.com/xx/
# Created Time : 2023-05-23 19:39:44
# Description:
##############################################################
#1、判断当前磁盘剩余空间是否由20G
free_space1=`df -m | grep -w /boot | tr -s " " " "|cut -d " " -f 4`
free_space2=`df -m | grep -w / | tr -s " " " "|cut -d " " -f 4`
free_sum=$((free_space1+free_space2))
free=$((free_sum/1024))
if [ $free -lt 20 ]
then
echo 内存空间严重不足!| mail -s "内存报警!" redhat
fi
3)、运行测试
[root@server ~]# bash mail.sh
[root@server mailx]# systemctl restart postfix.service
[root@server mailx]# mail redhat
sadsa
EOT
[root@server mailx]# vim /var/spool/mail/redhat
by server.hostname.com (Postfix) with ESMTPS id 1984E31F5E9C
for <redhat@server.hostname.com>; Tue, 23 May 2023 19:59:50 +0800 (CST)
Received: (from root@localhost)
by server.hostname.com (8.16.1/8.16.1/Submit) id 34NBxoEH003618
for redhat; Tue, 23 May 2023 19:59:50 +0800
From: root <root@server.hostname.com>
Message-Id: <202305231159.34NBxoEH003618@server.hostname.com>
Date: Tue, 23 May 2023 19:59:50 +0800
To: redhat@server.hostname.com
Subject: =?utf-8?B?5YaF5a2Y5oql6K2mIQ==?=
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
内存空间严重不足!
4)、加入计划任务
[root@server day05]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
0 0 * * * root /root/mail.sh
2、判断web服务是否运行
1)、查看进程的方式判断该程序是否运行
2)、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务。
(1)、下载httpd包
[root@server ~]# yum install httpd -y
(2)、编写脚本
[root@server ~]# vim httpd.sh
#!/bin/bash
##############################################################
# File Name: httpd.sh
# Version: V1.0
# Author: xx
# Email: xx@163.com
# Organization: http://www.xx.com/xx/
# Created Time : 2023-05-21 16:20:15
# Description:
##############################################################
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)、运行检测
[root@server ~]# bash httpd.sh
httpd.server is runing
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
1)、编写脚本
[root@server ~]# vim web.sh
#!/bin/bash
##############################################################
# File Name: web.sh
# Version: V1.0
# Author: xx
# Email: xx@163.com
# Organization: http://www.xx.com/xx/
# Created Time : 2023-05-21 16:27:01
# Description:
##############################################################
curl -s 192.168.186.139 > /dev/null
if [[ $? = 0 ]];then
echo "web server is runing"
else
exit 12
fi
2)、运行测试
[root@server ~]# systemctl restart httpd
[root@server ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; ven>
Active: active (running) since Sun 2023-05-21 16:31:37 CST; 8s ago
Docs: man:httpd.service(8)
Main PID: 5895 (httpd)
Status: "Started, listening on: port 80"
Tasks: 213 (limit: 10756)
Memory: 47.9M
CPU: 82ms
CGroup: /system.slice/httpd.service
├─5895 /usr/sbin/httpd -DFOREGROUND
├─5896 /usr/sbin/httpd -DFOREGROUND
├─5897 /usr/sbin/httpd -DFOREGROUND
├─5898 /usr/sbin/httpd -DFOREGROUND
└─5899 /usr/sbin/httpd -DFOREGROUND
5月 21 16:31:35 server.hostname.com systemd[1]: Starting The Apache HTTP >
[root@server ~]# bash web.sh
web server is runing
[root@server ~]# systemctl stop httpd
[root@server ~]# bash web.sh
[root@server ~]# echo $?
12
[root@server ~]#