Like many of my classmates, I was confused shell programming before I learned shell programming, but I didn't know what it could do, but I felt I could use a different way to manage the system. Now I slowly understand the importance of the shell for a small room inside a small shell monitor enough, of course, large-scale computer room can not use the shell to monitor the system, because large computer room monitoring machine has a lot of, and their status is to report and graphical interface form to report problems.
Next I write an article about how to use the shell to monitor the system and send messages.
Create a file:
Vim monitor.sh
#/bin/bash ----Default Execution method # The following is the variable: HOST= ' hostname ' ---Output the machine name of the current system sys= ' uname -a ' ---Output current system Information date= ' date "+%f %h:%m:%s" ' ---Output current system time web=/var/www/html/$HOST .html --- Create a Web page for the current system's Web site (named after the script) cpu= ' cat /proc/cpuinfo |grep processor | wc -l ' ---Output the current system's CPU information (as a judgment information) id= ' vmstat | awk ' nr==3{print $15 } ' ----Output The current system CPU IP number wa= ' vmstat | awk ' nr==3{print $16 } ' ---The waiting percentage of the I/O for the output system load= ' w | awk ' nr==1{ print $8} ' |awk -F ', ' ' {print $1} ' -- -Output system Load value disk= ' df -h |awk ' nr==3{print $4} ' |awk -F '% ' ' {print $1} ' ---output hard drive value free= ' free -m | awk ' nr==2{print $4} ' ----Output Memory idle value cache= ' free -m | awk ' nr==2{print $7} ' ----Output Memory cache value tcp= ' NETSTAT&NBSP;-ATUNLP ' ----Output The current system's network link status if= ' iftop -i eth0 -p -nn -l 100 -s 1 -t >> $WEB ' --- The state of the Eth0 interface of the output system # The following is the output of the variables executed above # Here the number of threads on the CPU, the load of the CPU, the state of the system disk, the memory remaining. These four to do a mail alarm echo "Design for leoheng" echo "######---hostname---##########" echo hostname= " echo $HOST ' echo ' ######---system parameters---######## "echo system= ' echo $SYS ' echo "######---Number of CPU threads---#######" echo processor=' echo $CPU ' if [ $CPU -eq 4 ];then ' echo ' wa= $WA and id= $ID Cpu processor is ok " | mail -s " $HOST cpu " [email protected] ' Else ' echo "wa= $WA and id= $ID cpu processor have problem" | mail -s "$HOST cpu" [email protected] ' fiecho "######---CPU ID---#########" echo Cpu~id= ' echo $ID ' echo ' ######---CPU wa---######### ' echo cpu~wa= ' echo $WA ' echo ' ###### ---CPU load---####### "echo cpu-load= ' echo $LOAD ' if [ $LOAD > 0.05 ];then ' echo ' cpu-load is beyond the rate | mail -s ' cpu-load ' [ Email protected] ' Else ' echo "Cpu-load is ok" | mail -s "Cpu-load" [email protected] ' fiecho ' ######---system disk status---# # "echo disk-status= ' echo $DISK ' if [ $DISK &NBSP;> 80 ];then ' echo "system-disk is beyond the 80%" | mail -s "System-disk" [email protected] ' Else ' echo "System-disk is ok" | mail -s "System-disk" [email protected] ' fiecho "######---memory remaining---########" echo memory = ' echo ($FREE + $CACHE) kb ' memory=$ (($FREE + $CACHE)) echo $memoryif [ $memory < 1000 ];then ' echo "system-memory less than 1000m " | mail -s " System-memory " [email protected" else ' echo "System-memory is ok" | mail -s "System-memory" [email protected] ' fi echo "######--- TCP-related connection status----# "echo tcp-status= ' echo $TCP ' echo " ######---eth0 traffic---###### "echo $IFecho "END"
# # # # # # # # # # # # # # # # # #---##### #部分同学的邮件无法发出 after exiting the shell, probably for two reasons 1. MAILX package is not installed Yum install-y mailx sendmail2. System Setup does not do the following to Inet_ The interfaces option changes to all, as follows vim/etc/postfix/main.cfinet_interfaces = All
This article is from the "Leoheng" blog, make sure to keep this source http://leoheng.blog.51cto.com/12202141/1955773
"Shell scripting system monitoring-------Email alerts"