- Formalized cmdline arguments + defaults
authorZdeněk Šustr <sustr4@cesnet.cz>
Fri, 20 Jan 2012 15:51:34 +0000 (15:51 +0000)
committerZdeněk Šustr <sustr4@cesnet.cz>
Fri, 20 Jan 2012 15:51:34 +0000 (15:51 +0000)
- Timestamp checks (not considering local queue yet! <= TODO)

org.glite.lb.server/src/notif-keeper.sh

index c69a00f..b80144f 100644 (file)
@@ -31,12 +31,21 @@ while read line ; do
        if [ $? -ne 0 ]; then
                HANDLES[$TOTALNOTIFS]=`echo $cleanline | sed -r 's/\s+.*$//'`
                OPTIONS[$TOTALNOTIFS]=`echo $cleanline | sed -r 's/^\w+\s+//'`
+               TOPICS[$TOTALNOTIFS]=`echo ${OPTIONS[${TOTALNOTIFS}]} | grep -E -o '\-a[ ]+x-msg://[^ ]+' | sed -r 's/^.*msg:\/\///'`
                TOTALNOTIFS=$(($TOTALNOTIFS+1))
        fi
 done < $infile
 
 }
 
+function vecho() {
+       if [ $1 -le $Verbose ]; then
+               shift
+               printf "${HANDLES[${i}]}: \t"
+               echo $*
+       fi
+}
+
 function lookup_notifid() {
        retnotifid=""
        if [ -f $fname ]; then
@@ -46,23 +55,24 @@ function lookup_notifid() {
 
 function setup_new() {
        opts=${OPTIONS[$1]}
-       echo glite-lb-notify new ${opts}
+       vecho 2 glite-lb-notify new ${opts}
        retnotifid=`glite-lb-notify new ${opts} | grep -E "notification ID: " | sed 's/^notification ID: //'`
-       echo $retnotifid
+       vecho 2 $retnotifid
 }
 
 function drop() {
        notifid=${NOTIFID[${1}]}
-       echo glite-lb-notify drop $notifid
+       vecho 2 glite-lb-notify drop $notifid
        glite-lb-notify drop $notifid > /dev/null
+       NOTIFID[${1}]=""
 }
 
 function extend() {
        notifid=${NOTIFID[${1}]}
-       echo glite-lb-notify refresh $notifid
+       vecho 2 glite-lb-notify refresh $notifid
        glite-lb-notify refresh $notifid > /dev/null
-       if [ $? -ne 0 ]; then
-               echo Failed to refresh notification for handle ${HANDLES[${1}]}
+       if [ $? -gt 0 ]; then
+               vecho 1 Failed to refresh notification for handle ${HANDLES[${1}]}
                NOTIFID[${1}]=""
        fi
 }
@@ -74,7 +84,7 @@ function check_opts() {
        fi
        nospcopts=`echo $opts | sed -r 's/\s//g'`
        nospcstoredopts=`echo $storedopts | sed -r 's/\s//g'`
-       printf "Checking options for ${HANDLES[$1]}\n\t$opts\n\t$storedopts\n"
+       vecho 1 "Checking options for changes"
        if [ "$nospcopts" == "$nospcstoredopts" ]; then
                checkopts_ret=0
        else
@@ -82,6 +92,31 @@ function check_opts() {
        fi
 }
 
+function check_timestamp() {
+       tsfile=$FilePrefix.${TOPICS[${1}]}.stat
+       if [ ! -f "$tsfile" ]; then
+               vecho 0 WARNING: stat file $tsfile not found!
+       else
+               vecho 1 Parsing stat file $tsfile
+               storedlc=`grep -E "^last_connected=" $tsfile | sed 's/^last_connected=//'`
+               storedls=`grep -E "^last_sent=" $tsfile | sed 's/^last_sent=//'`
+               if [ $storedlc -gt $storedls ]; then
+                       stored=$storedlc
+               else
+                       stored=$storedls
+               fi
+               threshold=`expr $stored + $AGE`
+               vecho 1 "Comparing current time ($NOW) with stale threshold ($threshold)"
+               if [ $NOW -gt $threshold ]; then
+                       vecho 1 Age Stale
+                       age_check=1;
+               else
+                       vecho 2 Age OK
+                       age_check=0;
+               fi
+       fi
+}
+
 function load() {
        out="`cat \"$FILE\"`"
        nid=`echo "$out" | grep '^notification ID:' | cut -f3 -d' '`
@@ -99,6 +134,41 @@ function save_ttl() {
        cat "$FILE" | grep -v '^valid' > "$FILE"
 }
 
+function showHelp() {
+cat << EndHelpHeader
+Script for registering, checking and refreshing site-specific notifications.
+It is intended primarily to be run by cron, but can be run manually by site
+admins to avoid waiting for the next cron cycle.
+
+EndHelpHeader
+
+        echo "Usage: $progname [OPTIONS]"
+        echo "Options:"
+        echo " -h | --help            Show this help message."
+        echo " -f | --file-prefix     Notification files prefix (same value as for server and"
+       echo "                        notif-il)."
+        echo " -n | --site-notif      Location of the site-notif.conf (input definition) file."
+        echo " -a | --stale-age              Time in seconds since last read before the registration is"
+       echo "                        considered stale (default 345600 = 4 days)."
+        echo " -v | --verbose         Verbose cmdline output. (Repeat for higher verbosity)"
+}
+
+Verbose=0
+AGE=345600
+while test -n "$1"
+do
+        case "$1" in
+                "-h" | "--help") showHelp && exit 2 ;;
+                "-f" | "--file-prefix") shift ; FilePrefix=$1 ;;
+                "-n" | "--site-notif") shift ; SiteNotif=$1 ;;
+               "-a" | "--stale-age" ) shift ; AGE=$1 ;;
+                "-v" | "--verbose") Verbose=$(($Verbose+1)) ;;
+                "-vv" ) Verbose=$(($Verbose+2)) ;;
+               *) echo WARNING: unknown argument $1 ;;
+        esac
+        shift
+done
+
 
 # -- set up --
 
@@ -117,17 +187,47 @@ if [ -n "$GLITE_HOST_CERT" -a -n "$GLITE_HOST_KEY" ] ;then
         X509_USER_CERT="$GLITE_HOST_CERT"
         X509_USER_KEY="$GLITE_HOST_KEY"
 else
-       echo "WARNING: host certificate not specified"
+       vecho 0 "WARNING: host certificate not specified"
+fi
+
+if [ -z "$GLITE_LB_LOCATION_VAR" ]; then
+       export GLITE_LB_LOCATION_VAR=/var/glite
+       vecho 0 "WARNING: GLITE_LB_LOCATION_VAR not specified, using default"
+fi
+
+if [ -z "$FilePrefix" ]; then
+       export FilePrefix=/var/tmp/glite-lb-notif
+       vecho 0 "WARNING: Notif file prefix not specified, using default"
+fi
+
+if [ -z "$SiteNotif" ]; then
+       if [ -f "/etc/glite-lb/site-notif.conf" ]; then
+               vecho 1 Configuration file site-notif.conf not specified, using default
+               SiteNotif="/etc/glite-lb/site-notif.conf"
+       else
+               vecho 0 "ERROR: No configuration file (site-notif.conf)"
+               exit 1
+       fi
+fi
+
+touch $GLITE_LB_LOCATION_VAR/notif-keeper.rw.try.$$.tmp
+
+if [ -f $GLITE_LB_LOCATION_VAR/notif-keeper.rw.try.$$.tmp ]; then
+       rm $GLITE_LB_LOCATION_VAR/notif-keeper.rw.try.$$.tmp
+else
+       vecho 0 ERROR: $GLITE_LB_LOCATION_VAR not writable!
+       exit 1
 fi
 
 #export GLITE_WMS_NOTIF_SERVER="$SERVER"
 export X509_USER_CERT
 export X509_USER_KEY
 
-read_list $1
+read_list $SiteNotif
 
 for ((i=0 ; i < ${TOTALNOTIFS} ; i++))
 do
+       NOW=`date +%s`
        fname="$GLITE_LB_LOCATION_VAR/notif-keeper-${HANDLES[${i}]}.notif"
        lookup_notifid ${HANDLE[${i}]}
        NOTIFID[i]=$retnotifid
@@ -135,20 +235,31 @@ do
        if [ "${NOTIFID[${i}]}" == "" ]; then
                setup_new $i
                NOTIFID[i]=$retnotifid
+               vecho 1 "New registration ($retnotifid)"
        else 
                check_opts $i
                if [ $checkopts_ret -eq 0 ]; then
-                       extend $i
-                       if [ "${NOTIFID[${i}]}" == "" ]; then
-                               setup_new $i
-                               NOTIFID[i]=$retnotifid
+                       check_timestamp $i
+                       if [ $age_check -eq 0 ]; then
+                               extend $i
+                               if [ "${NOTIFID[${i}]}" == "" ]; then
+                                       setup_new $i
+                                       NOTIFID[i]=$retnotifid
+                                       vecho 1 "Failed to extend. Registration recreated ($retnotifid)."
+                               else
+                                       vecho 1 "Registration extended (${NOTIFID[${i}]})"
+                               fi
+                       else
+                               drop $i
+                               vecho 1 "Registration stale => dropped"
                        fi
                else
                        drop $i 
                        setup_new $i
                        NOTIFID[i]=$retnotifid
+                       vecho 1 "Options changed. Registration dropped and recreated ($retnotifid)."
                fi
        fi
-       echo Writing $fname
+       vecho 1 "Writing $fname"
        printf "#This file is maintained automatically by script $0 initiated by cron\nOptions: ${OPTIONS[${i}]}\nNotifid: ${NOTIFID[${i}]}\n" > $fname
 done