shell第三天练习

该文描述了一套自动化监控系统,包括检查磁盘剩余空间,当低于20GB时发送报警邮件给管理员;判断Web服务状态,未运行时启动服务并调整防火墙规则;以及使用curl验证Web服务的可达性,根据返回码确认服务状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。

1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。

首先,确保安装了邮件服务器(服务端安装命令:yum install postfix -y ;客户端安装命令:yum install mailx -y;开启邮件服务并开机自启:systemctl enable postfix --now)并确保邮件服务和crond服务正常运行

1、编写脚本

[root@localhost shell]# vim df_mail_crond.sh

#!/bin/bash
  
free_MB=`df -m / | awk '/\//{print $4}'`
free_GB=$[$free_MB/1024]
if [[ $free < 20480 ]] ;then
        echo "Warning:The current disk space remaining is ${free_GB}G that less than 20G." | mail -s "Warning" root
else
        echo "The current disk space remaining is ${free_GB}G.It is enough to use."
fi

2、设置权限

[root@localhost shell]# chmod a+rx df_mail_crond.sh 

3、运行测试

[root@localhost shell]# ./df_mail_crond.sh
[root@localhost shell]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Mon Jan  2 23:59  18/669   "Warning"
& 1
Message  1:
From root@localhost.localdomain  Mon Jan  2 23:59:22 2023
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Date: Mon, 02 Jan 2023 23:59:22 +0800
To: root@localhost.localdomain
Subject: Warning
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root <root@localhost.localdomain>
Status: R

Warning:The current disk space remaining is 32G that less than 20G.

4、加入计划任务

[root@localhost shell]# vim /etc/crontab

0 0 * * * root /root/shell/df_mail_crond.sh

2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。

首先确保安装httpd包(yum install -y httpd)

1、编写脚本

[root@localhost shell]# vim apache_start.sh 

#!/bin/bash
 
ps_httpd=`ps -ef | grep httpd | grep -v grep | wc -l`
if [[ $ps_httpd > 0 ]];then
	echo "httpd.service is running."
else
	systemctl start httpd
	systemctl stop firewalld
fi

2、设置权限

[root@localhost shell]# chmod a+rx apache_start.sh

3、运行测试

[root@localhost shell]# systemctl stop httpd
[root@localhost shell]# ./apache_start.sh
[root@localhost shell]# ./apache_start.sh
httpd.service is running.

3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。

1、编写脚本

[root@localhost shell]# vim web_test.sh

#!/bin/bash

curl -s 192.168.210.128 > /dev/null
if [[ $? = 0 ]];then
        echo "web server is running."
else
        exit 12
fi

2、设置权限

[root@localhost shell]# chmod a+rx web_test.sh

3、运行测试

[root@localhost shell]# ./web_test.sh
web server is running.
[root@localhost shell]# systemctl stop httpd
[root@localhost shell]# ./web_test.sh
[root@localhost shell]# echo $?
12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值