Shell script monitoring-linux operating system

Source: Internet
Author: User
Tags bz2 memory usage cpu usage server memory



Https://www.jb51.net/article/58799.htm



In the absence of nagios monitoring software, as long as the server can be on the Internet, you can send e-mail to remind the Administrator of the system load and CPU usage.



One, install Linux under a mail client msmtp software (similar to a foxmail tool)






1. Download and install:
Http://downloads.sourceforge.net/msmtp/msmtp-1.4.16.tar.bz2?modtime=1217206451&big_mirror=0


Copy CodeThe code is as follows:
# tar JXVF msmtp-1.4.16.tar.bz2
# CD msmtp-1.4.16
#./configure--PREFIX=/USR/LOCAL/MSMTP
# Make && make install





2, create MSMTP configuration files and log files (host for mail domain name, mail user name Fuquanjun, password Fuquanjun)





Copy CodeThe code is as follows:
# VIM/ROOT/.MSMTPRC





Account Default
Host Xxxxx.com
From [email protected]
Auth Login
User Fuquanjun
Password Fuquanjun
LogFile ~/.msmtp.log
# chmod 600/ROOT/.MSMTPRC
# Touch ~/.msmtp.log






3, Mutt Installation configuration: (General Linux under the default installation mutt)



If not installed, use Yum to install





Copy CodeThe code is as follows:
Yum-y Install Mutt
# VIM/ROOT/.MUTTRC
Set sendmail= "/usr/local/msmtp/bin/msmtp"
Set Use_from=yes
Set Realname= "Moniter"
Set [email protected]
Set Envelope_from=yes
Set Rfc2047_parameters=yes
Set charset= "Utf-8





4. Mail sending test (-S message header,-a table plus attachments)


Copy CodeThe code is as follows:
# echo "Mail content 123456" | Mutt-s "Mail title test mail"-a/scripts/test.txt [email protected]



The following error message appears:


Copy CodeThe code is as follows:
Msmtp:account Default not found:no configuration file available





Error sending letter, child process exited 78 ().
This letter cannot be sent.
Workaround:



Use MSMTP to send tests separately:/usr/local/msmtp/bin/msmtp-s discovery is a profile not found


Copy CodeThe code is as follows:
Msmtp:account Default not found:no configuration file available



To view the current configuration file path:/usr/local/msmtp/bin/msmtp-p


Copy CodeThe code is as follows:
Ignoring system configuration File/work/target/etc/msmtprc:no such file or directory
Ignoring user Configuration file/root/.msmtprc:no such file ordirectory
Falling back to the default account
Msmtp:account Default not found:no configuration file available



So copy the/USR/LOCAL/ETC/MSMTPRC to/ROOT/.MSMTPRC
Check the installation directory of the mutt file


Copy CodeThe code is as follows:
RPM-QL Mutt



So copy the/ETC/MUTTRC to/ROOT/.MUTTRC to send the message.









Second, monitoring server system load situation:






1, use the uptime command to view the current load situation (1 minutes, 5 minutes, 15 minutes average load situation) on Apple's Mac computer also applies





Copy CodeThe code is as follows:
# uptime
15:43:59 up 186 days, 20:04, 1 user, load average:0.01, 0.02, 0.00



"Load average" means the average load of the system in 1 minutes, 5 minutes, 15 minutes, respectively.
(1) The main observation "15 minutes system load", it as the normal operation of the computer indicators.
(2) If within 15 minutes (the system load divided by the number of CPU cores) The average load is greater than 1.0, indicating that the problem persists, not a temporary phenomenon.
(3) When the system load continues to be greater than 0.7, you must start investigating where the problem is and prevent the situation from deteriorating.
(4) When the system load continues to be greater than 1.0, you must find a solution to this value down.
(5) When the system load reaches 5.0, it indicates that your system has a serious problem, long time no response, or close to the freezing.
Suppose your computer has only 1 CPUs. If your computer has 2 CPUs, it means that the computer's processing power has doubled, and the number of processes that can be processed simultaneously has doubled.
2 CPUs indicate that the system load can reach 2.0, at which time each CPU reaches 100% of the workload. Spread out, n CPU computer, acceptable system load maximum is n.0.






2. View the total number of cores of the server CPU


Copy CodeThe code is as follows:
# grep-c ' model name '/proc/cpuinfo or Cat/proc/cpuinfo



3, intercept the server 1 minutes, 5 minutes, 15 minutes of the load situation


Copy CodeThe code is as follows:
# Uptime | awk ' {print $8,$9,$10,$11,$12} '
Load average:0.01, 0.02, 0.00





4. View the average load of 15 minutes





Copy CodeThe code is as follows:
# Uptime | awk ' {print $} ' (with ' {print $} ' is not accurate enough to get a 12th field with awk, the result is likely to be empty.) and use the $NF table to output the last paragraph of the content)
# Uptime | awk ' {print $NF} '



5, write the system load monitoring script file:


Copy CodeThe code is as follows:
# vim/scripts/load-check.sh
[Code]
#!/bin/bash

#Using the uptime command to monitor Linux system load changes

#Take system current time (write file in append >>)
Date >> / scripts / datetime-load.txt

#Extract server 1 minute, 5-minute, 15-minute load conditions
Uptime | awk '{print $ 8, $ 9, $ 10, $ 11, $ 12}' >> / scripts / load.txt

#Connect the above time and load related row data line by line (re-write file> each time)
Paste / scripts / datetime-load.txt / scripts / load.txt> /scripts/load_day.txt
# chmod a + x / scripts / load-check.sh
6, write the system load result file mail Send script:

Copy CodeThe code is as follows:
# vim / scripts / sendmail-load.sh
#! / bin / bash
#Load_day. Txt files are sent to the user by mail

#Extract the IP address information of this server
ip = 'Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d ""'

#Extract the current date
today = 'date-d "0 Day" +% y year% m month% d "

#Send system load monitoring result email
echo "This is the system load monitoring report for the $ IP server $ today, please download the attachment." | Mutt-s "System load monitoring report for the $ IP server $ today" -a / scripts / load_day.txt [email protected]
# chmod a + x / scripts / sendmail-load.sh

7, write the system load monitoring script file:

Copy CodeThe code is as follows:
# vim / scripts / load-warning.sh
#! / bin / bash
#Using the uptime command to monitor Linux system load changes

#Extract the IP address information of this server
ip = 'Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d ""'

#Grab the total number of CPUs
cpu_num = 'grep-c' model name '/ proc / cpuinfo'

#Grab the 15-minute average load of the current system
load_15 = 'Uptime | awk' {print $ NF} '

#Calculate the 15-minute average load of a single core of the current system, the result is less than 1.0 when the front single digit is 0.
Average_load = 'echo' scale = 2; a = $ load _15 / $ cpu _num; if (Length (a) == scale (a)) print 0; print a "| BC '

#Take the single-digit integer of the above average load value
Average_int = 'echo $ average _load | Cut-f 1-d "." `

#Set the 15-minute average load alarm of a single core of the system to 0.70 (i.e. alarm when using more than 70%).
load_warn = 0.70

#When the average load of a single core for 15 minutes is greater than or equal to 1.0 (that is, single-digit integer greater than 0), direct email alarm, if less than 1.0 two times comparison
if (($ average _int> 0)); Then
echo "$ IP Server 15-minute system average load of $ average_load, exceeding the alert value of 1.0, please immediately handle !!!" | Mutt-s "$ IP Server system load critical alarm !!!" [Email protected]
Else
#The current system's 15-minute average load value is compared with the alarm value (1 is returned when the alarm value is greater than 0.70, and 0 is returned if it is less than)
load_now = 'expr $ average _load \> $ load _warn'

#If the 15-minute average load of a single core of the system is greater than the alarm value of 0.70 (return value is 1), send an email to the administrator
if (($ load _now = = 1)); Then
echo "$ IP server 15 minutes of system average load reached $ average _load, exceeding the alert value of 0.70, please timely processing." | Mutt-s "$ IP Server system load Alarm" [email protected]
Fi
Fi
# chmod a + x / scripts / load-warning.sh

Third, monitoring server system CPU Usage:

1. Use the top command to view the CPU usage of the Linux system:

Copy CodeThe code is as follows:
# top-b-N 1 | grep Cpu (-b-n 1 table only requires 1 output results)
Cpu (s): 0.0% us, 0.0% sy, 0.0% ni, 99.9% id, 0.0% wa, 0.0% hi, 0.0% si, 0.0% st

2. View the Percent value command to intercept the idle CPU (take only the integer part):

Copy CodeThe code is as follows:
# top-b-N 1 | grep Cpu | awk '{print $}' | Cut-f 1-d "."

3, write the CPU monitoring script file:

Copy CodeThe code is as follows:
# vim / scripts / cpu-check.sh
#! / bin / bash
#Using the top command to monitor cpu changes in the Linux system

#Take system current time (write file in append >>)
Date >> / scripts / datetime-cpu.txt

#Grab the current cpu value (write file in append >>)
Top-b-N 1 | grep Cpu >> / scripts / cpu-now.txt

#Connect the above time and CPU related line data line by line (re-write file> each time)
Paste / scripts / datetime-cpu.txt / scripts / cpu-now.txt> /scripts/cpu.txt
# chmod a + x / scripts / cpu-check.sh

4. View the result files of CPU monitoring:

Copy CodeThe code is as follows:
# Cat / scripts / cpu.txt

5, write the CPU result file mail Send script:

Copy CodeThe code is as follows:
# vim / scripts / sendmail-cpu.sh
#! / bin / bash
# 把 generated cpu. Txt files are sent to the user by mail

#Extract the IP address information of this server
ip = 'Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d ""'

#Extract the current date
today = 'date-d "0 Day" +% y year% m month% d "

#Send cpu monitoring results email
echo "This is the CPU monitoring report of the $ IP server $ today, please download the attachment." | Mutt-s "CPU monitoring report for the $ IP server $ today" -a / scripts / cpu.txt [email protected]
# chmod a + x / scripts / sendmail-cpu.sh
Iv. Monitoring the CPU of the system, when using more than 80% of the time to send alarm messages:

Copy CodeThe code is as follows:
# vim / scripts / cpu-warning.sh
#! / bin / bash
#Monitor system cpu situation script

#Extract the IP address information of this server
ip = 'Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d ""'

#Take current idle CPU percentage (take only integer parts)
Cpu_idle = 'Top-b-N 1 | grep Cpu | awk' {print $} '| Cut-f 1-d "." `

#Set the alert value of idle cpu to 20%, send an email alert immediately if the current CPU is using more than 80% (that is, less than 20% remaining)
if (($ cpu _idle <20)); Then
echo "$ IP Server CPU remaining $ cpu_idle%, the usage rate has exceeded 80%, please handle in time." | Mutt-s "$ IP Server CPU Alarm" [email protected]
Fi
# chmod a + x / scripts / cpu-warning.sh
V. Use the free command to monitor system memory:

1. Use the free command to view the memory usage of the Linux system: (in M)

Copy Code
The code is as follows:
# free-m
Total used free shared buffers Cached
mem: 3952 3414 538 0 168 484
-/ + buffers / cache: 2760 1191
SWAP: 8191 86 8105
2. View the value command to intercept the remaining memory free:

(1) Physical Memory Free Value: # Free-m | grep Mem | awk '{print $ 4}'
(2) Free value of buffer: # Free-m | grep- | awk '{print $ 4}'
(3) Swap partition Free value: # Free-m | grep Swap | awk '{print $ 4}'

3, write the Memory monitoring script file:

Copy CodeThe code is as follows:
# vim / scripts / free-mem.sh
#! / bin / bash
#Monitor Linux system memory changes with the free command

#Take system current time (write file in append >>)
Date >> / scripts / date-time.txt

#Grab physical memory free value (write file in append >>)
echo mem-free: 'Free-m | grep Mem | awk' {print $ 4} 'M >> / scripts / mem-free.txt

#Grab the free value of the buffer (write file in append >>)
echo buffers / cache-free: 'Free-m | grep- | awk' {print $ 4} 'M >> / scripts / buffers-free.txt

#Grab Swap partition free value (write file in append >>)
echo swap-free: 'Free-m | grep Swap | awk' {print $ 4} 'M >> / scripts / swap-free.txt

#Concatenate the above time and memory related row data line by line (re-write file> each time)
Paste / scripts / date-time.txt / scripts / mem-free.txt / scripts / buffers-free.txt / scripts / swap-free.txt> / scripts / fr Eemem.txt
# chmod a + x / scripts / free-mem.sh

4. View the memory monitoring results file:

Copy CodeThe code is as follows:
# Cat / scripts / freemem.txt

5. Write the free result file mail Send script:

Copy CodeThe code is as follows:
# vim / scripts / sendmail-mem.sh
#! / bin / bash
# 把 Generated freemem. Txt files are sent to the user by mail

#Extract the IP address information of this server
ip = 'Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d ""'

#Extract the current date and time
today = 'date-d "0 Day" +% y year% m month% d "

#Send memory monitoring results email
echo "This is the memory monitoring report for the $ IP server $ today, please download the attachment." | Mutt-s "$ IP Server $ today Memory Monitoring Report" -a / scripts / freemem.txt [email protected]
# chmod a + x / scripts / sendmail-mem.sh
Six, monitoring system swap partition swap situation, when the use of more than 80% when the alarm e-mail:

Copy CodeThe code is as follows:
# vim / scripts / swap-warning.sh
#! / bin / bash

#Extract the IP address information of this server
ip = 'Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d ""'

#Total swap partition allocated by the system
Swap_total = 'Free-m | grep Swap | awk' {print $} '

#Current remaining swap partition free size
Swap_free = 'Free-m | grep Swap | awk' {print $ 4} '

#Currently used swap partition used size
Swap_used = 'Free-m | grep Swap | awk' {print $} '


if (($ swap _used! = 0)); Then

#If the swap partition has been used, calculates the percentage of the total of the current remaining swap partition free, in decimal notation, to complement an integer digit before the decimal point 0
Swap_per = 0 'echo' scale = 2, $ swap _free / $ swap _total "| BC '

#Set the alarm value of the swap partition to 20% (i.e. alarm when using more than 80%).
swap_warn = 0.20

#The current remaining swap partition percentage is compared with the alarm value (1 is returned when it is greater than the alarm value (i.e. more than 20% remaining), and 0 is returned when it is less than (that is, less than 20%)
swap_now = 'expr $ swap _per \> $ swap _warn'

#If the current swap partition uses more than 80% (i.e. the remainder is less than 20%, the above return value equals 0), send an email alert immediately
if (($ swap _now = = 0)); Then
echo "$ IP server swap partition only left $ swap _free M unused, the remaining less than 20%, the utilization rate has exceeded 80%, please timely processing." | Mutt-s "$ IP server memory Alarm" [email protected]
Fi
Fi
# chmod a + x / scripts / swap-warning.sh
Vii. Join the task plan: the system load and CPU usage is detected every 10 minutes, and alerts are sent immediately (10 minutes), load and CPU test results messages are sent 8 O 'Day in the morning.

Copy CodeThe code looks like this: # CRONTAB-E
* / 10 * * * * / scripts / load-check.sh> / dev / null 2> & 1
* / 10 * * * * / scripts / load-warning.sh
0 8 * * * / scripts / sendmail-load.sh

* / 10 * * * * / scripts / cpu-check.sh
* / 10 * * * * / scripts / cpu-warning.sh
0 8 * * * / scripts / sendmail-cpu.sh

* / 10 * * * * / scripts / free-mem.sh
* / 10 * * * * / scripts / swap-warning.sh
0 8 * * * / scripts / sendmail-mem.sh
# Service Crond Restart
Shell script monitoring-linux operating system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.