Shell script to monitor server performance under linux

  . 1 ! # / Bin / the bash
   2  
  . 3  # extract IP address information from the present server
   . 4 ENOl = ` the ifconfig | Sed -n ' 1,1p ' | awk -F '  '  ' {}. 1 Print $ ' `
   . 5 IP = ` the ifconfig $ ENOl} {| grep  " inet addr " | Cut -f 2 -d " : " | Cut -f . 1 -d "  " `
   . 6  echo  'ENO: ' $ {ENOl}
   . 7  echo  ' the IP: ' $ {the IP}
   . 8  
  . 9  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 10  # each monitoring warning value
 . 11 WARN_LOAD = 1.1 
12 is WARN_CPU = 0.95 
13 is WARN_MEM = 0.93 
14  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 15  
16  
. 17  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 18 is the CPU = 0.0 
. 19 LOAD1 = 0.0 
20 is LOAD5 = 0.0 
21 isLOAD15 = 0.0 
22 is MEM = 0.0 
23 is  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 24  
25  
26 is  the send_mail () {
 27      echo  ' Send mail '   " $ {}. 1 "  " $ {2} " 
28  }
 29  
30  
31 is # . 1 changes, the load monitoring system.
 32  system_load_func ()
 33 is  {
 34 is      cpu_num = ` grep -C ' Model name ' / proc / cpuinfo `
 35  
36     load=$(uptime | awk -F 'load average: ' '{print $2}')
 37     load_1=$(echo $load | awk -F ', ' '{print $1}')
 38     load_5=$(echo $load | awk -F ', ' '{print $2}')
 39     load_15=$(echo $load | awk -F ', ' '{print $3}' )
 40  
41 is      # a single core computing system average load current value, the result is less than 1.0 the foregoing digits 0s.
42 is      LOAD1 = ` echo  " Scale = 2; A = LOAD_1} {$ /} $ {cpu_num; IF (length (A) == Scale (A)) Print 0; A Print " | bc`
 43 is      LOAD5 =` echo  " = 2 Scale; load_5} A = {$ /} $ {cpu_num; IF (length (A) == Scale (A)) Print 0; A Print " | bc`
 44 is      LOAD15 =` echo  " Scale = 2; A = load_15} {$ /} $ {cpu_num; IF (length (A) == Scale (A)) Print 0; A Print " | bc`
 45  
46 is      IF [` echo  " $ {LOAD15}>} $ {WARN_LOAD" | Bc` -eq . 1 ]
 47      the then 
48          the send_mail " ! $ {} The IP system, the average load on the server 15 minutes to $ {LOAD15}, than the warning value $ {WARN_LOAD}, please immediately processed "  " $ alarm system load the IP server ! " 
49      Fi 
50  }
 51 is  
52 is  
53 is # 2 ., the cpu of the monitoring system
 54 is  cpu_func ()
 55  {
 56 is      # take the current percentage of idle cpu values (only take the integer part)
 57 is      cpu_idle` Top -b -d = 0.1 - n- 2 | grep Cpu | tail -n . 1 | awk  'Print $. 8} { ' | Cut -f . 1 -d " . " `
 58      the CPU = 0 ` echo  " Scale = 2; (100 - $ cpu_idle {}) / 100 " | bc`
 59  
60      IF [ ` echo  " $ { } CPU> WARN_CPU $ {} " | bc` -eq . 1 ]
 61 is      the then 
62 is          the send_mail " $ {cpu server using the IP} $ {CPU}, please timely treatment. "  " $ CPU alerting the IP server " 
63 is      Fi 
64  }
 65  
66 
67 # 3 . Wu_li nei_cun jian_kong
 68  memfunc ()
 69  {
 70      the mem = $ ( with the free -m | sed to -n ' 22p ' )
 71      memtotal = ` echo out the $ {} the mem | awk -F '  '  ' {print The $ 2} ' `
 72      memused =` echo out the $ {} the mem | awk -F '  '  ' {print The $ 3} ' `
 , with 73      # memfree =` echo out the $ {} the mem | awk -F'  '  ' {}. 4 Print $ ' `
 74  
75      IF [$ mem_used -ne 0 ]
 76      the then 
77          MEM = 0 ` echo  " Scale = 2; $ {mem_used} / {mem_total} $ " | bc`
 78          IF [ ` echo  " $ {MEM}>} $ {WARN_MEM " | bc` -eq . 1 ]
 79          the then 
80              the send_mail " $ the IP server physical memory used $ {MEM}, please timely treatment. "  " $ alert the IP server memory " 
81          Fi
 82     fi
 83 }
 84 
 85 
 86 CNT=1
 87 print_info() {
 88     echo "# # # # # # # # # # # [${CNT}] # # # # # # # # # # # # # #"
 89     echo cpu: ${CPU}
 90     echo load1: ${LOAD1}
 91     echo load5: ${LOAD5}
 92     echo load15: ${LOAD15}
 93     echo mem: ${MEM}
 94     echo "# # # # # # # # # # # # # # # # # # # # # # # # # # #"
 95     (( CNT += 1 ))
 96     # exit 0
 97 }
 98 
 99 
100 while true
101 do
102     system_load_func
103     cpu_func
104     mem_func
105     print_info
106     sleep 5
107 done

 

Note: You need to download bc command:

sudo apt-get install bc

 

Here I only wrote the script inside information to obtain various monitoring data, send messages, and data written to the database which I omitted. Send e-mail I use the python, python calls to send mail through the shell script (use linux because self I will find with a lot of spam, I do not have to study how to solve). I'll get the data into the database record, here for versatility, I have written to the database section omitted.

 

Guess you like

Origin www.cnblogs.com/changdingfang/p/11996211.html