#!/bin/bash

function fping {
  timeout 1s ping -n -c 1 $1 2>/dev/null | awk -F '/' '/rtt/ {printf "%0.f", $5}'
}

function https {
  timeout 10s httperf --ssl --server $1 --uri $2 2>/dev/null | awk -F ' ' '/Total:.*replies 1/ {print $9*1000}'
}

function printLabels {
  # Print a label every new hour
  thishour=`date +'%H'`
  if [[ $lasthour != $thishour ]]; then
    lasthour=$thishour
    #                                  |          |          |          |          |          |          |          |
    echo "                    |------Ping ms------| |-------------------------HTTPS ms------------------------------|"
    echo "Date                     Modem    Gateway Düsseldorf       Köln       Bern     Madrid   New York      Tokyo"
  fi
}

while true; do
  printLabels

  # Measure performance
  date=`date +'%d.%m.%Y %H:%M:%S'`
  res1=`fping 192.168.0.1`
  res2=`fping 109.91.12.1`
  res3=`https www.uni-duesseldorf.de /robots.txt`
  res4=`https www.uni-koeln.de /robots.txt`
  res5=`https www.unibe.ch /robots.txt`
  res6=`https www.aeropuertomadrid-barajas.com /robots.txt`
  res7=`https www.nytimes.com /robots.txt`
  res8=`https www.seijo.ac.jp /common/css/reset.css`

  # Output the results
  printf "%s %10s %10s %10s %10s %10s %10s %10s %10s\n" "$date" "$res1" "$res2" "$res3" "$res4" "$res5" "$res6" "$res7" "$res8"

  # Wait until next minute
  sleep $(( 60 - 10#$(date +%S) ))
done 
