Technorati tag: Shell,df,sed,awk
-Monitor System statistics
Monitor disk free space
Disk Resource Usage
Monitor CPU and Memory usage
-Perform a backup
Archive data files
Offline Storage of Backup files
Monitoring System Statistics
· Monitor disk free space
This shell script monitors the available disk space on a specific volume, such as/, and sends an e-mail message when free disk space falls below the set threshold.
#!/bin/Bash# Monitor available disk space#注意格式 variable =' value 'Space= 'DF|sed-N'/\/$/p'|gawk '{print $}'|sed 's/%//'`if[$SPACE-ge - ] ThenEcho "Disk space on the root at $SPACE% used"|mail–s "Disk warning"ElseEcho "Disk space on root is OK"fi
Add a task Schedule
The frequency of running this script depends on how active the file server is. Small space, once a day;
#小型, every morning:0 * * */root/diskmon# Large, 4 times a day, three:8 :12:30 and PM 4:0,8, +, * **/ Root/diskmon
• User disk usage
#!/bin/bash# Calculate disk usage and report per usertemp= 'mktemp -t tmp. XXXXXX 'du -s/home/* |grep-v lost|sed ' s/\/home\///' |sort-g-r > $TEMPTOTAL = ' du-s/home | Gawk ' {print $} ' cat $TEMP |gawk-v n= "$TOTAL" ' BEGIN {print ' Total Disk useage by User ' print ' user\tspace\tpercent '}{pri ntf "%s\t%d\t%6.2f%\n", $2,$1, ($1/n) *100}end {print "---------------------------";p rintf "total\t%d\n", n} ' rm-f $TEMP
The results are as follows
[Email protected] ~]#./diskhogs. SH 370499.68%0.32%--------------------------- 3716
• Monitoring of CPU and memory usage
Capture Script
#!/bin/bash# script to capture system Statisticsoutfile=/root/capstats.csvdate=`Date+%m/%d/%Y ' time=`Date+%k:%m:%S ' TIMEOUT=`Uptime' Vmout= ' Vmstat1 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 $}'`Echo "$DATE, $TIME, $USERS, $LOAD, $FREE, $IDLE">> $OUTFILE
The results are as follows
[Email protected] ~]#CatCapstats.csv A/ One/ .,0: the: Geneva,2,0.04,183360, -output HTML format #!/bin/bash# parse capstats data into daily reportfile=/root/capstats.csvtemp=/root/Capstats.htmlmail=whichmuttdate=`Date+"%a,%b,%d,%y"`Echo "">$TEMPEcho "<table border=\ "1\" >">>$TEMPEcho "<tr><td>Date</td><td>Time</td><td>Users</td>">>$TEMPEcho "<td>load</td><td>free memory</td><td>%cpu idle</td></tr>">>$TEMPCat$FILE |awk-F,'{printf"<tr><td>%s</td><td>%s</td><td>%s</td>",$1,$2,$3;p rintf"<td>%s</td><td>%s</td><td>%s</td>\n</tr>\n",$4,$5,$6;}'>> $TEMPEcho "</table></body>">>$TEMP $mail-A $TEMP-s"Stat Report for $DATE"Root </dev/NULL#RM-F $TEMP
The results are as follows
650) this.width=650; "title=" clip_image002[4] "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image002[4] "src=" http://s3.51cto.com/wyfs02/M01/8B/76/ Wkiom1hoeztjpyduaasyduuzwvs834.png "width=" 718 "height=" 794 "/>
Perform a backup
Two different ways to back up data on your system using shell scripts.
· 1) Archive data files
Required function tar command
The tar command is used to archive an entire directory as a single file.
tar -cf archive. Tar 2>/dev/null
Create Daily archive files
#!/bin/bash# Archive a working diretorydate= 'date +%y%m%d ' FILE=Archive$datesource =/root/testdestination=/root/archive/$FILEtar2>/dev/null gzip $DESTINATION
Create an hourly archive script
#!/bin/bash# archive a working diretory hourlyday = ' date +%d ' MONTH = ' date +%m ' time = ' date +%k%m ' SOURCE =/root/testbasedest =/root/archive mkdir -P $BASEDEST/$MONTH/ $DAYDESTINATION = $BASEDEST/$MONTH/$DAY/archive$time tar -cf $DESTINATION $SOURCE 2 >/dev/null gzip $DESTINATION
650) this.width=650; "title=" clip_image004[4] "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image004[4] "src=" http://s3.51cto.com/wyfs02/M01/8B/76/ Wkiom1hoezxqdokcaae-dgpdp-y615.png "width=" 653 "height=" 178 "/>
Can be combined with cron
· 2) offline storage of backup files
Working directory archive can be sent as an email attachment
#!/bin/bash# Archive A working directory and E-Mail it outmail=`whichMutt ' DATE=`Date+%y%m%d ' FILE=Archive$datesource=/root/testdestination=/root/archive/$FILEZIPFILE= $DESTINATION.ZipTar-CF $DESTINATION $SOURCE2>/dev/NULLZip$ZIPFILE $DESTINATIONRM-F $DESTINATION $mail-A $ZIPFILE-s"Archive for $DATE"[Email protected] </dev/NULL
PS:DF sed awk
Administrator Shell Script