43 lines
		
	
	
		
			834 B
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			834 B
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
| #!/bin/bash
 | |
| 
 | |
| export LANG=en_US.UTF-8
 | |
| PING_FILE="$(dirname $0)/ping.list"
 | |
| LOG_PATH="/var/log/monitor"
 | |
| LOG_NAME="ping"
 | |
| INTERVAL=120
 | |
| 
 | |
| function Init {
 | |
|     local self_count=$(pgrep -cx "$(basename $0)")
 | |
|     [ 0 -eq $? ] || exit 1
 | |
|     [ 1 -eq $self_count ] || exit 1
 | |
|     type fping > /dev/null || exit 1
 | |
|     mkdir -p $LOG_PATH || exit 1
 | |
| }
 | |
| 
 | |
| function Log {
 | |
|     local msg="$1"
 | |
|     local log_time="$(date +'%F %T')"
 | |
|     local log_file="$LOG_PATH/$LOG_NAME-${log_time% *}.log"
 | |
|     echo "$log_time $msg" >> $log_file
 | |
|     cd $LOG_PATH && ls ${LOG_NAME}-* 2>/dev/null \
 | |
|         | head -n -7 | xargs rm -f
 | |
| }
 | |
| 
 | |
| function GetPingInfo {
 | |
|     fping -A -f $PING_FILE \
 | |
|         | awk '{print $1,$3,"alive"==$3?1:0}'
 | |
| }
 | |
| 
 | |
| function Main {
 | |
|     local line=
 | |
|     sleep $INTERVAL
 | |
|     GetPingInfo|while read line; do
 | |
|         Log "$line"
 | |
|     done
 | |
| }
 | |
| 
 | |
| # start
 | |
| Init
 | |
| Main
 | |
| 
 |