⛓️ Как определить, как долго указанный удаленный сервер или устройство находились в автономном режиме

Создайте скрипт оболочки, чтобы определить, как долго указанный удаленный сервер или устройство находились в автономном режиме.

Он не предназначен для замены решения для мониторинга, но для определения того, сколько времени потребуется для перезагрузки сервера или устройства.

Он использует преобразование секунд в читаемый человеком временную метку для отображения результатов.

#!/bin/bash# Determine how long remote server was offline# initial sleep timesleep_time=1# ping timeouttimeout=5if [ "$#" -ne "1" ]; thenecho "Provide server/device address as a parameter"exit 1fi# server/device address is provided as a first parameteraddress="$1"# function to pretty print timefunction pretty_time() {# calculate seconds for each time periodseconds_in_year=$(echo "(365.2425* 24 * 60 * 60)/1" | bc)seconds_in_month=$( echo "(30.436875 * 24 * 60 * 60)/1" | bc)seconds_in_day=$( echo "(24 * 60 * 60)/1" | bc)seconds_in_hour=$(echo "( 60 * 60)/1" | bc)seconds_in_minute=60seconds_param="$1"time_string=""# take care of yearsif [ "$seconds_param" -ge "$seconds_in_year" ]; thenyears=$(expr $seconds_param \/ $seconds_in_year)seconds_param=$(expr $seconds_param - $years \* $seconds_in_year)if [ "$years" -gt "1" ]; thentime_string="$years years"elif [ "$years" -eq "1" ]; thentime_string="$years year"fifi# take care of monthsif [ "$seconds_param" -ge "$seconds_in_month" ]; thenmonths=$(expr $seconds_param \/ $seconds_in_month)seconds_param=$(expr $seconds_param - $months \* $seconds_in_month)if [ "$months" -gt "1" ]; thentime_string="$time_string $months months"elif [ "$months" -eq "1" ]; thentime_string="$time_string $months month"fifi# take care of daysif [ "$seconds_param" -ge "$seconds_in_day" ]; thendays=$(expr $seconds_param \/ $seconds_in_day)seconds_param=$(expr $seconds_param - $days \* $seconds_in_day)if [ "$days" -gt "1" ]; thentime_string="$time_string $days days"elif [ "$days" -eq "1" ]; thentime_string="$time_string $days day"fifi# take care of hoursif [ "$seconds_param" -ge "$seconds_in_hour" ]; thenhours=$(expr $seconds_param \/ $seconds_in_hour)seconds_param=$(expr $seconds_param - $hours \* $seconds_in_hour)if [ "$hours" -gt "1" ]; thentime_string="$time_string $hours hours"elif [ "$hours" -eq "1" ]; thentime_string="$time_string $hours hour"fifi# take care of minutesif [ "$seconds_param" -ge "$seconds_in_minute" ]; thenminutes=$(expr $seconds_param \/ $seconds_in_minute)seconds_param=$(expr $seconds_param - $minutes \* $seconds_in_minute)if [ "$minutes" -gt "1" ]; thentime_string="$time_string $minutes minutes"elif [ "$minutes" -eq "1" ]; thentime_string="$time_string $minutes minute"fifi# take care of secondsseconds=$seconds_paramif [ "$seconds" -gt "1" ]; thentime_string="$time_string $seconds seconds"elif [ "$seconds" -eq "1" ]; thentime_string="$time_string $seconds second"fiecho "$time_string" | sed "s/^ //"}# initial statestate=0# server/device was offline when script startedping -c 1 -W $timeout -q $address 2>&1 1>/dev/nullexit_code="$?"if [ "$exit_code" -gt "0" ]; thenstate_in="$(date +%s)"state=2echo "Server/device [${address}] was not responding at [$(date --date="@${state_in}" -R)] when script was executed"elseecho "Server/device [${address}] was responding at [$(date -R)] when script was executed"fiwhile true; doping -c 1 -W $timeout -q $address 2>&1 1>/dev/nullexit_code="$?"# server/device was offline when script started, but it is online nowif [ "$exit_code" -eq "0" ] && [ "$state" -eq "2" ]; thenstate_out="$(date +%s)"state_diff="$(expr $state_out- $state_in)"state=0echo "Server/device [${address}] started responding at [$(date --date="@${state_in}" -R)]"echo "Server/device [${address}] was offline for at least [$(pretty_time $state_diff)]"# server/device was online, but it is offline nowelif [ "$exit_code" -gt "0" ] && [ "$state" -eq "0" ]; thenstate_in="$(date +%s)"state=1echo "Server/device [${address}] stopped responding at [$(date --date="@${state_in}" -R)]"# server/device was offline, but it is online nowelif [ "$exit_code" -eq "0" ] && [ "$state" -eq "1" ]; thenstate_out="$(date +%s)"state_diff="$(expr $state_out- $state_in)"state=0;echo "Server/device [${address}] started responding at [$(date --date="@${state_out}" -R)]"echo "Server/device [${address}] was offline for [$(pretty_time $state_diff)]"fisleep $sleep_timedone

Пример вывода, когда сервер отвечал во время запуска скрипта оболочки.

$ bash offline.sh 192.0.2.11
Server/device [192.0.2.11] was responding at [Fri, 06 Apr 2018 19:37:49 +0000] when script was executedServer/device [192.0.2.11] stopped responding at [Fri, 06 Apr 2018 19:38:08 +0000]Server/device [192.0.2.11] started responding at [Fri, 06 Apr 2018 19:50:22 +0000]Server/device [192.0.2.11] was offline for [12 minutes 14 seconds] secondsServer/device [192.0.2.11] stopped responding at [Fri, 06 Apr 2018 19:53:46 +0000]Server/device [192.0.2.11] started responding at [Fri, 06 Apr 2018 19:55:59 +0000]Server/device [192.0.2.11] was offline for [2 minutes 13 seconds] seconds

Пример вывода, когда сервер не отвечал во время запуска скрипта оболочки.

$ bash offline.sh 192.0.2.11
Server/device [192.0.2.11] was not responding at [Fri, 06 Apr 2018 19:38:32 +0000] when script was executedServer/device [192.0.2.11] started responding at [Fri, 06 Apr 2018 19:38:32 +0000]Server/device [192.0.2.11] was offline for at least [11 minutes 48 seconds] secondsServer/device [192.0.2.11] stopped responding at [Fri, 06 Apr 2018 19:53:45 +0000]Server/device [192.0.2.11] started responding at [Fri, 06 Apr 2018 19:55:58 +0000]Server/device [192.0.2.11] was offline for [2 minutes 13 seconds] seconds

Отправка комментария

0 Комментарии