使用shell脚本获取系统运行状态

本文介绍了一个用于定期收集和报告系统状态信息的bash脚本。该脚本能够收集系统的运行时间、磁盘使用情况、内存使用率以及僵尸进程数量等关键数据,并将这些信息汇总成一份报告,通过邮件发送给指定用户。

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

#!/bin/bash
#
#snapshot_states - produces a report for system states
##################################################
# Set Scripts Variables
DATE=`date +%m%d%Y`
DISKS_TO_MONITOR="/dev/sda1 /dev/sda7"
MAIL=`which mail`
MAIL_TO=user
REPORT=/home/user/Documents/snapshot_states_$DATE.rpt
#
#####################################
# Create Report FIles
#
exec 3>&1  #Save file descriptor
#
exec 1 > $REPORT
echo
echo -e "\t\t Daily System Report"
echo
#
############################################
echo -e "Today is "`date +%m/%d/%Y`
echo
#####################################
#1) Gather System Uptime Statistics
#
echo -e "System Has Been \c"
uptime | sed -n '/,/s/,/ /gp'|\
gawk '{if($4=="days" || $4=="day")
		{print $2,$3,$4,$5}
		else
		{print $2,$3}
}'

######################################
# 2) Gather Disk Usage Statistics
#
echo
for DISK in $DISKS_TO_MONITOR
do
	echo -e "$DISK usage:\c"
	df -h $DISK |sed -n '/% \//p'|gawk '{print $5}'
done

#####################################
# 3) Gather Memory Usage Statistics
#
echo 
echo -e "Memory Usage:\c"
#
free |sed -n '2p'|gawk 'x=int(($3/$2)*100) {print x}'|\
	sed 's/$/%/'

##################################
# 4) Gather number of zombie processes

echo
ZOMBIE_CHECK=`ps -al | gawk '{print $2,$4}'|grep Z`

if [ "$ZOMBIE_CHECK" = "" ];then
	echo "No Zombie Process on System at this time"
else
	echo "Current System Zombie Processes"
	ps -al |gawk '{print $2,$4}'|grep Z
fi

echo
exec 1>&3
$MAIL -a $REPORT -s "System Statistics Report for $DATE"\
--$MAIL_TO </dev/null

rm -f $REPORT


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值