PDA

View Full Version : Bash script to meter server downtime in seconds?



Fli
03-29-2015, 02:43 PM
What is the easiest way to discover some server downtime in seconds?

Im not sure, but there is one script that can help to discover that



remoteserverip=1.2.3.4
sshport=22
# ssh access key should be setup in order to connect without password prompts...

# empty countfile
> count

echo "
This script will be connecting $serverip each second. Each successfull connection = 1 line in file named $pwd/count. You can run this script in "screen" and durng the runtime, see number of lines (1 second successfull connections) by command: wc -l $pwd/count"

while true ;do
echo "$(ssh -p $sshport $remoteserverip hostname)" >> count;sleep 1
done

#
# this script connects to remote server each second and add remote server hostname to local file named "count".
# this way we get hostnames one per second, if host is unconnectable, no hostnames will be added
#
# run this script like this: timeout 3600s /path/to/script
# so we run it for one hour and then it is automatically killed
# then we count number of lines in file "count", so we have rough number of seconds server was connectable
# the rest number of seconds in 3600 is the approximate downtime. Example after script is interupted after hour, we have 3500 lines in file, that means that approximate downtime during that hour was 100 seconds..
#
# need to check downtime of server, in bigger time range like 72 hours? 3600x72=259200s (timeout 259200s)

please kindly share Your experience or other better ways to get seconds downtime of an server, thx

RuskinF
08-05-2020, 01:33 PM
You can implement this by overwriting a line. Use \r to go back to the beginning of the line without writing \n to the terminal.

Write \n when you're done to advance the line.

Use echo -ne to:

not print \n and
to recognize escape sequences like \r.
Here's a demo:

echo -ne '##### (33%)\r'
sleep 1
echo -ne '############# (66%)\r'
sleep 1
echo -ne '####################### (100%)\r'
echo -ne '\n'