Technorati 标记: shell,df,sed,awk
-监视系统统计信息
监视磁盘空闲空间
磁盘资源占用情况
监视CPU和内存使用情况
-执行备份
归档数据文件
脱机存储备份文件
监视系统统计信息
· 监视磁盘空闲空间
此shell脚本将监视特定卷上(如/)的可用磁盘空间,并在可用磁盘空间低于设置的阈值时发送一则电子邮件信息。
#!/bin/bash # monitor available disk space #注意格式 变量=`值` SPACE=`df|sed -n ‘/\/$/p‘| gawk ‘{print $5}‘|sed ‘s/%//‘` if [ $SPACE -ge 20 ] then echo "Disk space on root at $SPACE% used"|mail –s “Disk warning” else echo "Disk space on root is OK" fi
添加任务计划
运行此脚本的频率取决于文件服务器的活跃程度。空间小的,一天一次;
#小型,每天上午12:30 30 0 * * * /root/diskmon #大型,每天4次,12:30,8:30,下午12:30和下午4:30 30 0,8,12,16 * * * /root/diskmon
#!/bin/bash # calculate disk usage and report per user TEMP=`mktemp -t tmp.XXXXXX` du -s /home/* |grep -v lost|sed ‘s/\/home\///‘|sort -g -r > $TEMP TOTAL=`du -s /home |gawk ‘{print $1}‘` cat $TEMP |gawk -v n="$TOTAL" ‘ BEGIN { print "Total Disk Useage by User" print "User\tSpace\tPercent" } { printf "%s\t%d\t%6.2f%\n",$2,$1,($1/n)*100 } END { print "---------------------------"; printf "Total\t%d\n", n }‘ rm -f $TEMP
[root@docker1 ~]# ./diskhogs.sh Total Disk Useage by User User Space Percent benny 3704 99.68% test 12 0.32% --------------------------- Total 3716
捕获脚本
#!/bin/bash # script to capture system statistics OUTFILE=/root/capstats.csv DATE=`date +%m/%d/%Y` TIME=`date +%k:%M:%S` TIMEOUT=`uptime` VMOUT=`vmstat 1 2` USERS=`echo $TIMEOUT | awk ‘{print $4}‘` LOAD=`echo $TIMEOUT | awk ‘{print $9}‘| sed ‘s/,//‘` FREE=`echo "$VMOUT"| sed -n ‘/[0-9]/p‘ | sed -n ‘2p‘ | awk ‘{print $4}‘` IDLE=`echo "$VMOUT" | sed -n ‘/[0-9]/p‘ | sed -n ‘2p‘ | awk ‘{print $15}‘` echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" >> $OUTFILE
[root@docker1 ~]# cat capstats.csv 12/11/2016, 0:09:02,2,0.04,183360,100 输出html格式 #!/bin/bash # parse capstats data into daily report FILE=/root/capstats.csv TEMP=/root/capstats.html MAIL=which mutt DATE=`date +"%A,%B,%d,%Y"` echo "<html><body><h2>Report for $DATE</h2>" > $TEMP echo "<table border=\"1\">" >> $TEMP echo "<tr><td>Date</td><td>Time</td><td>Users</td>" >> $TEMP echo "<td>Load</td><td>Free Memory</td><td>%CPU Idle</td></tr>" >> $TEMP cat $FILE |awk -F, ‘{ printf "<tr><td>%s</td><td>%s</td><td>%s</td>",$1,$2,$3; printf "<td>%s</td><td>%s</td><td>%s</td>\n</tr>\n",$4,$5,$6; }‘ >> $TEMP echo "</table></body></html>" >> $TEMP $MAIL -a $TEMP -s "Stat report for $DATE" root < /dev/null #rm -f $TEMP
执行备份
使用shell脚本备份系统上数据的两种不同的方法。
· 1)归档数据文件
必需函数 tar命令
tar命令用于将整个目录归档为单个文件。
tar -cf archive.tar /root/test 2> /dev/null
创建日常归档文件
#!/bin/bash # archive a working diretory DATE=`date +%y%m%d` FILE=archive$DATE SOURCE=/root/test DESTINATION=/root/archive/$FILE tar -cf $DESTINATION $SOURCE 2> /dev/null gzip $DESTINATION
创建每小时的归档脚本
#!/bin/bash # archive a working diretory hourly DAY=`date +%d` MONTH=`date +%m` TIME=`date +%k%M` SOURCE=/root/test BASEDEST=/root/archive mkdir -p $BASEDEST/$MONTH/$DAY DESTINATION=$BASEDEST/$MONTH/$DAY/archive$TIME tar -cf $DESTINATION $SOURCE 2> /dev/null gzip $DESTINATION
可结合cron
· 2)脱机存储备份文件
工作目录归档可以邮件附件的形式发送
#!/bin/bash # archive a working directory and e-mail it out MAIL=`which mutt` DATE=`date +%y%m%d` FILE=archive$DATE SOURCE=/root/test DESTINATION=/root/archive/$FILE ZIPFILE=$DESTINATION.zip tar -cf $DESTINATION $SOURCE 2> /dev/null zip $ZIPFILE $DESTINATION rm -f $DESTINATION $MAIL -a $ZIPFILE -s "Archive for $DATE" root@docker1.com < /dev/null
PS: df sed awk
原文地址:http://bennychen.blog.51cto.com/6323894/1882026