习题1:监控磁盘使用率

要求:写一个shell脚本,检测所有磁盘分区使用率和inode使用率并记录到以当天日期为命名的日志文件里,当发现某个分区容量或者inode使用量大于85%时,发邮件通知你自己。

思路:就是先df -h 然后过滤出已使用的那一列,然后再想办法过滤出百分比的整数部分,然后和85去比较,同理,inode也是一样的思路。

参考答案:

#!/bin/bash
# date: 2018年2月26日
log=/var/log/disk/`date +%F`.log
date +'%F %T' > $log
df -h >> $log
echo >> $log
df -i >> $log
email="your_email@163.com"
# 磁盘分区使用率
for i in `df -h|awk '{print $5}'|sed 's/%//'`
do
    expr $i + 1 &>/dev/null
    if [ $? -eq 0 ];then
	if [ $i -gt 85 ];then
	   python /root/shell/mail.py "$email" "disk test" "`date +'%F %T'`  disk warning..."
    	fi
    fi
done
# inode使用率
for i in `df -i|awk '{print $5}'|sed 's/%//'`
do
    expr $i + 1 &>/dev/null
    if [ $? -eq 0 ];then
        if [ $i -gt 85 ];then
           python /root/shell/mail.py "$email" "disk test" "`date +'%F %T'`  disk inode warning..."
        fi
    fi
done

邮件脚本mail.py:https://blog.51cto.com/11924224/2069732 


习题2:统计普通用户

要求:写个shell,看看你的Linux系统中是否有自定义用户(普通用户),若是有,一共有几个?(假设所有普通用户都是uid大于1000的

参考答案:

#!/bin/bash
# date:2018年2月26日
total=`awk -F ':' '$3 > 1000' /etc/passwd |wc -l`
if [ $total -gt 0 ];then
   echo "There are $total common user."
else
   echo "No common user!"
fi