Share a VPS System monitoring resources script "dry Goods"

Source: Internet
Author: User
Tags vps

A few months ago began to use the VPS, the monthly limit of 300GB traffic, traffic convenience basic enough, but sometimes due to some malicious access, resulting in CPU, memory and other resource consumption, resulting in a VPS blog site response time is too slow even sometimes can not open the Web page. So, I simply made a script to monitor and email the alarm.

Because it is not to do a very professional operation, temporarily do not want to have a very professional monitoring tools, such as: Nagios, Puppet, Cacti, Zabbix, etc., so I manually a shell script to monitor my concern CPU, Load, Memory, network transmission, blog site can open and so on. Share this monitoring script as follows:

https://github.com/smilejay/shell/blob/master/sh2013/vps_monitor.sh

The code is as follows Copy Code
#!/bin/bash
#set-X
# The script to monitor my VPS
# It'll alert when memory, load, cpu%, networking, httpd/mysqld or Home-page
# is in a abnormal state.
# Author:jay
# date:2013-10-16

Email= "Smile665@gmail.com"
Warn_msg= ""

# Alert when free memory is less than MB
function Mem_free ()
{
Threshold=50 MB free Memory
free_mem=$ (free-m | grep "Buffers/cache" | awk ' {print $} ')
If [$free _mem-lt $threshold]; Then
warn_msg= $WARN _msg "Free memeory is less than $threshold MB.N"
Return 1
Fi
return 0
}

# Alert when load (5min) is larger than 4
function Load_avg ()
{
Threshold=4 # Load is 4
load_5min=$ (Cat/proc/loadavg | awk ' {print $} ')
If [$ (echo "$load _5min > $threshold" | bc)-EQ 1]; Then
warn_msg= $WARN _msg "5min average load is larger than $threshold. N"
Return 1
Fi
return 0
}

# Alert when CPU idle% is less than 20%
function Cpu_idle ()
{
THRESHOLD=20 # CPU Idle% 20%
Cpu_idle=$ (SAR 1 5 | grep-i ' Average ' | awk ' {print $NF} ')
If [$ (echo "$cpu _idle < $threshold" | BC)-EQ 1]; Then
# in printf cmd, percent% represents a single%
warn_msg= $WARN _msg "CPU idle%% is less than $threshold%%.N"
Return 1
Fi
return 0
}

# Alert when networking tx speed is larger than kb/s
function Network_tx ()
{
THRESHOLD=80 # TX Speed kb/s
Interface=eth0
tx_speed=$ (sar-n DEV 5 | grep "Average" | grep "$interface" | awk ' {print $} ')
If [$ (echo "$tx _speed > $threshold" | bc)-EQ 1]; Then
warn_msg= $WARN _msg "Networking TX speed is larger than $threshold KB/S.N"
Return 1
Fi
return 0
}

# Alert when httpd or mysqld process doesn ' t exist
function Httpd_mysqld ()
{
Ps-ef | grep ' httpd ' | Grep-v ' grep '
If [$?-ne 0]; Then
warn_msg= $WARN _msg "httpd process doesn ' t EXIST.N"
Return 1
Else
Ps-ef | grep ' mysqld ' | Grep-v ' grep '
If [$?-ne 0]; Then
warn_msg= $WARN _msg "mysqld process doesn ' t EXIST.N"
Return 1
Fi
Fi
return 0
}

# Alert when ' Stay hungry ' doesn ' t exist in the home page http://smilejay.com/
function Home_Page ()
{
url=http://smilejay.com/
Keywords= "Stay Hungry"
CURL-SL $url | Grep-i "$keywords"
If [$?-ne 0]; Then
warn_msg= $WARN _msg "keywords ' $keywords ' doesn ' t exist on link $url. N"
Return 1
Fi
return 0
}

# Use a array to store ' return value ' of each monitoring function
Mem_free
Chk[1]=$?
Load_avg
Chk[2]=$?
Cpu_idle
Chk[3]=$?
Network_tx
Chk[4]=$?
Httpd_mysqld
Chk[5]=$?
Home_Page
Chk[6]=$?

# Send warning email to the web master if any of the check fails.
For I in $ (SEQ 1 6)
Todo
If [${chk[i]}-ne 0]; Then
printf "$WARN _msg" | Mailx-s "Warning from smilejay.com" $EMAIL
Exit 1
Fi
Done

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.