0

I am trying to find the best way to find the overall downtime of my internet connection over a month. I have an SLA with my ISP and they are not meeting it and I want to be able to hand them over the report. I have a probe running that writes a log file when it can't connect and when it can connect again. So the data looks like this hypothetically:

UP - T0
DOWN - T1
UP - T3
so on

I am trying to figure out a way, without writing too much code, to have a script to run over this data and figure the overall downtime by calculating the time it took to go from the DOWN state back to the UP state. If there is not a simple way to do this I think I can write something to do so. Does anyone know of a module that performs this?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Roughrider
  • 15
  • 2
  • 1
    See the `datetime` module – c2huc2hu Sep 18 '17 at 17:50
  • 2
    are T0, T1 etc... UTC timestamps (i.e. seconds from epoch) or are they date-time strings? Can you post a sample? – MrE Sep 18 '17 at 17:51
  • what are you asking? if its seconds then just use simple math `elapsed_seconds = time1-time0` ... with datetimes its just as easy ... except you will have to parse the string into an actual dattime – Joran Beasley Sep 18 '17 at 17:53
  • to get downtime, you'd want all line matching DOWN and the following one, then subtract 2 consecutive lines. `sed -n '/DOWN/,$p'` would get you the matching line that according to https://stackoverflow.com/questions/17908555/printing-with-sed-or-awk-a-line-following-a-matching-pattern – MrE Sep 18 '17 at 18:00
  • There are web services for this; https://www.pingdom.com – markwalker_ Sep 18 '17 at 18:27
  • Your ISP dumping the onus on you to demonstrate the SLA wasn't met, it's probably a sort of test to see if you have any proof, and if not, how you're possibly SOL for the last period. – Nick T Sep 18 '17 at 21:08

1 Answers1

0

Assuming you have timestamps, not dates, in bash you can do:

sed -n '/DOWN/,$p' test.txt 
| awk -F"-" '{print $2}' 
| paste - -  
| awk -F"\t" '{print $2-$1}'
| awk '{ sum+=$1} END {print sum}'
  • sed -n '/DOWN/,$p' test.txt will pull every line with DOWN and the next line
  • awk -F"-" '{print $2}' | paste - - will print the 2 numbers in a single line
  • | awk -F"\t" '{print $2-$1}' does the subtraction
  • awk '{ sum+=$1} END {print sum}' sums all the numbers

with test.txt

UP - 123
DOWN - 124
UP - 127
DOWN - 130
UP - 140

I get the list:

3
10

and then you just need to add the numbers

awk '{ sum+=$1} END {print sum}' which returns 13 as expected.

If you don't have a timestamp, you can get one from a date string with

date -j -f "%a %b %d %T %Z %Y" "$DATE" "+%s"

MrE
  • 19,584
  • 12
  • 87
  • 105