Monitor host ping scripts

Source: Internet
Author: User
Monitor whether the host can ping the script-general Linux technology-Linux programming and kernel information. The following is a detailed description. :) Shell is a beginner. a script that just compiled to monitor whether the host can be pinged. If a host cannot be pinged, send an email and record it. If the host can be pinged again, send an email again, notifications, and audible alarms can be set.

#! /Bin/sh
# Set-x
######################################## ###############
PingConf = '/opt/monitor/iptest'
PingCantrech = '/opt/monitor/IpcannotReach'
# File where the log will be stored
LogDir = '/opt/monitor/Log'
# Define the interval time
INTERVAL = '10'
# Define the alarm process
User1 = 'xiaoli'
User2 = 'xiaozhang'
YouNeed = '/opt/monitor/youneed'
######################################## ################
Proc_alarm ()
{
While read IpAddress
Do
# Ingore any hash signs
Case $ IpAddress in
\#*)
;;
*)
Ping $ IpAddress-c 5> errtmp
If [$? = 0]; then
Rm errtmp
Else
Rm errtmp
ErrTime = 'date + % Y/% m/% d/% H: % M: % s'
Echo "$ ErrTime" ":" "$ IpAddress" "cann't be connected" >>$ LogDir
Echo "$ IpAddress"> tmp11
Sleep 1
Mail-s "$ IpAddress" _ Can_not_reach xiaoli@abced.comEcho "from $ User1" ":" "from $ User2"> $ YouNeed
Echo "$ ErrTime" ":" "$ IpAddress" "off" >>$ YouNeed
Audioplay-v 50-p speaker/opt/monitor/space. au
Fi
;;
Esac
Done <$ PingConf
}

######################################## ##################
Proc_unalarm ()
{
While read IpAddress
Do
# Ingore any hash signs
Case $ IpAddress in
\#*)
;;
*)
Ping $ IpAddress-c 5> errtmp
If [$? = 0]; then
Rm errtmp
ErrTime = 'date + % Y/% m/% d/% H: % M: % s'
Echo "$ ErrTime" ":" "$ IpAddress" "can be connected" >>$ LogDir
Sleep 1
Mail-s "$ IpAddress" _ OK _le xiaoli@abced.comEcho "from $ User1" ":" "from $ User2"> $ YouNeed
Echo "$ ErrTime" ":" "$ IpAddress" "on" >>> YouNeed
Echo $ IpAddress> tmp12
# Audioplay-v 50-p speaker/opt/monitor/space1.au
Else
Rm errtmp
Fi
;;
Esac
Done <$ PingCantrech
}
######################################## ##################
# Execute the proc_main function every the specified time INTERVAL
Uniq $ PingCantrech> tmp17
Cat tmp17> $ PingCantrech
Rm tmp17
If [-e $ PingCantrech]
Then
Proc_unalarm
Else
Echo "$ PingCantrech" is null
Fi
While ["1"-eq "1"]
Do
# Execute the proc_main function
##########################
Proc_alarm
##########################
If [-e tmp11]
Then
Echo "$ MYFILE" is nomal
If [-e "$ PingCantrech"]
Then
Echo "$ PingCantrech" is nomal
Cat tmp11> $ PingCantrech
Uniq $ PingCantrech> tmp18
Cat tmp18> $ PingCantrech
Rm tmp18
Proc_unalarm
Cat tmp11> $ PingCantrech
Else
Cat tmp11> $ PingCantrech
Proc_unalarm
Fi
Rm tmp11
MYFILE = 'tmp12'
If [-e "$ MYFILE"]
Then
Comm-23 $ PingCantrech tmp12> tmp13
Rm tmp12
Cat tmp13> $ PingCantrech
Rm tmp13
Else
Echo "$ MYFILE" is null
Cat tmp11> $ PingCantrech
Fi
Elif
Echo "$ MYFILE" is null
Then
Echo "$ MYFILE" is null
If [-e "$ PingCantrech"]
Then
Proc_unalarm
Else
Echo "$ MYFILE" is null
Fi
Fi
######################################
# Suspend execution for INTERVAL seconds
Sleep $ INTERVAL
Done
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.