#!/usr/bin/env bash
# $Id$
#
# Thomas Dreibholz's PlanetLab Script Collection
# Copyright (C) 2005-2022 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: dreibh@iem.uni-due.de


# ###### Settings for "location" ############################################
NorthAmerica=1
Europe=2
Australasia=3
SouthAmerica=4


# ###### Definitions ########################################################
DNSWorking="(dns1udp<80 || dns2udp<80)"
DNSFine="(dns1udp<20 && dns2udp<80)"

DiskOkay="(gbfree>=5)"
MemOkay="((freemem >= 100)  && (swapused <= 10))"

TimeOkay="(drift <= 1000ms)"
TimeFine="(drift <= 1ms)"

LoadOkay="(5minload < 5)"
LoadFine="(5minload < 1)"

AliveHost="((resptime > 0) && (sshstatus < 1h) && $DNSWorking)"
GoodHost="($AliveHost && $DiskOkay && $MemOkay && $LoadOkay)"
GreatHost="($GoodHost && $DNSFine && $LoadFine)"

CandidateForPU="($GoodHost && $TimeOkay)"
CandidateForPE="($GoodHost && $TimeOkay)"
CandidateForPR="($GreatHost && $TimeFine)"


# ###### Query CoMon service ################################################
do_query ()
# $1 = Output prefix
# $2 = Filter rule
# $3 = Title
{
   local outputPrefix=$1
   local filterRule=$2
   local title=$3

   ./comon-query $outputPrefix.hosts "$filterRule" name 5minload nameonly quiet
   ./comon-query $outputPrefix.addresses "$filterRule" address address "" quiet
   grep "^[0-9a-f]*$" $outputPrefix.addresses | xargs ./comon-print-address >$outputPrefix.ip
   rm -f $outputPrefix.addresses

   echo "$title`wc --lines $outputPrefix.hosts | awk '{ print $1 }'`"
}



# ###### Main program #######################################################
do_query alive-planetlab "$AliveHost" "Alive Hosts: "

# ------ Remove old keys in background ------
(
   cat alive-planetlab.hosts | xargs -n1 ssh-keygen -R  >/dev/null 2>&1
   cat alive-planetlab.ip | xargs -n1 ssh-keygen -R  >/dev/null 2>&1
) &

do_query good-planetlab  "$GoodHost"  "Good Hosts:  "
do_query great-planetlab "$GreatHost" "Great Hosts: "


do_query pr-europe       "$CandidateForPR && location==$Europe"                                    "PR Europe:   "
do_query pr-asia         "$CandidateForPR && location==$Australasia"                               "PR Asia:     "
do_query pr-america      "$CandidateForPR && (location==$NorthAmerica || location==$SouthAmerica)" "PR America:  "

do_query pe-europe       "$CandidateForPE && location==$Europe"                                    "PE Europe:   "
do_query pe-asia         "$CandidateForPE && location==$Australasia"                               "PE Asia:     "
do_query pe-america      "$CandidateForPE && (location==$NorthAmerica || location==$SouthAmerica)" "PE America:  "

do_query pu-europe       "$CandidateForPU && location==$Europe"                                    "PU Europe:   "
do_query pu-asia         "$CandidateForPU && location==$Australasia"                               "PU Asia:     "
do_query pu-america      "$CandidateForPU && (location==$NorthAmerica || location==$SouthAmerica)" "PU America:  "


echo "Waiting for old keys to be removed ..."
wait
