- Moving the notif-keeper script from utils to server
authorZdeněk Šustr <sustr4@cesnet.cz>
Thu, 19 Jan 2012 15:39:25 +0000 (15:39 +0000)
committerZdeněk Šustr <sustr4@cesnet.cz>
Thu, 19 Jan 2012 15:39:25 +0000 (15:39 +0000)
- the script much extended, maintains notifications and reacts to certain changes.
- Does not deal with consumer reliability as of yet.

org.glite.lb.server/src/notif-keeper.sh [new file with mode: 0644]
org.glite.lb.utils/src/notif-keeper.sh [deleted file]

diff --git a/org.glite.lb.server/src/notif-keeper.sh b/org.glite.lb.server/src/notif-keeper.sh
new file mode 100644 (file)
index 0000000..c69a00f
--- /dev/null
@@ -0,0 +1,154 @@
+#!/bin/bash
+#
+# Copyright (c) Members of the EGEE Collaboration. 2004-2010.
+# See http://www.eu-egee.org/partners for details on the copyright holders.
+# 
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+# 
+#     http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+GLITE_LOCATION=${GLITE_LOCATION:-"/opt/glite"}
+NOTIFY=${GLITE_LB_NOTIFY:-"$GLITE_LOCATION/bin/glite-lb-notify"}
+
+# This function reads the full list of notifications to maintain into an array
+function read_list() {
+
+infile="$1"
+TOTALNOTIFS=0
+
+while read line ; do
+       cleanline=`echo $line | sed -r 's/^\s*//'`
+       echo "$line" | grep -E "^#" > /dev/null
+       if [ $? -ne 0 ]; then
+               HANDLES[$TOTALNOTIFS]=`echo $cleanline | sed -r 's/\s+.*$//'`
+               OPTIONS[$TOTALNOTIFS]=`echo $cleanline | sed -r 's/^\w+\s+//'`
+               TOTALNOTIFS=$(($TOTALNOTIFS+1))
+       fi
+done < $infile
+
+}
+
+function lookup_notifid() {
+       retnotifid=""
+       if [ -f $fname ]; then
+               retnotifid=`grep -E '^Notifid: ' $fname | sed -r 's/^Notifid: //'`
+       fi
+}
+
+function setup_new() {
+       opts=${OPTIONS[$1]}
+       echo glite-lb-notify new ${opts}
+       retnotifid=`glite-lb-notify new ${opts} | grep -E "notification ID: " | sed 's/^notification ID: //'`
+       echo $retnotifid
+}
+
+function drop() {
+       notifid=${NOTIFID[${1}]}
+       echo glite-lb-notify drop $notifid
+       glite-lb-notify drop $notifid > /dev/null
+}
+
+function extend() {
+       notifid=${NOTIFID[${1}]}
+       echo 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}]}
+               NOTIFID[${1}]=""
+       fi
+}
+
+function check_opts() {
+       opts=${OPTIONS[$1]}
+       if [ -f $fname ]; then
+               storedopts=`grep -E '^Options: ' $fname | sed -r 's/^Options: //'`
+       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"
+       if [ "$nospcopts" == "$nospcstoredopts" ]; then
+               checkopts_ret=0
+       else
+               checkopts_ret=1
+       fi
+}
+
+function load() {
+       out="`cat \"$FILE\"`"
+       nid=`echo "$out" | grep '^notification ID:' | cut -f3 -d' '`
+       val=`echo "$out" | grep '^valid' | sed 's/[^(]*(\([^)]*\)).*/\1/'`
+       ori=`$STAT "$FILE"`
+
+       if [ -z "$nid" -o -z "$val" ]; then
+               val=-1
+               return 1
+       fi
+}
+
+function save_ttl() {
+       mv "$FILE" "$FILE.1"
+       cat "$FILE" | grep -v '^valid' > "$FILE"
+}
+
+
+# -- set up --
+
+#if [ -z "$SERVER" ]; then
+#      echo "Usage: $0 LB_SERVER [ TTL NEW_NOTIF_ARGUMRNTS... ]"
+#      echo
+#      echo "Environment:"
+#      echo "  DROP: drop the notification"
+#      echo "  DEBUG: show progress"
+#      exit 1
+#fi
+#shift
+#shift
+
+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"
+fi
+
+#export GLITE_WMS_NOTIF_SERVER="$SERVER"
+export X509_USER_CERT
+export X509_USER_KEY
+
+read_list $1
+
+for ((i=0 ; i < ${TOTALNOTIFS} ; i++))
+do
+       fname="$GLITE_LB_LOCATION_VAR/notif-keeper-${HANDLES[${i}]}.notif"
+       lookup_notifid ${HANDLE[${i}]}
+       NOTIFID[i]=$retnotifid
+
+       if [ "${NOTIFID[${i}]}" == "" ]; then
+               setup_new $i
+               NOTIFID[i]=$retnotifid
+       else 
+               check_opts $i
+               if [ $checkopts_ret -eq 0 ]; then
+                       extend $i
+                       if [ "${NOTIFID[${i}]}" == "" ]; then
+                               setup_new $i
+                               NOTIFID[i]=$retnotifid
+                       fi
+               else
+                       drop $i 
+                       setup_new $i
+                       NOTIFID[i]=$retnotifid
+               fi
+       fi
+       echo Writing $fname
+       printf "#This file is maintained automatically by script $0 initiated by cron\nOptions: ${OPTIONS[${i}]}\nNotifid: ${NOTIFID[${i}]}\n" > $fname
+done
diff --git a/org.glite.lb.utils/src/notif-keeper.sh b/org.glite.lb.utils/src/notif-keeper.sh
deleted file mode 100755 (executable)
index 08e42d9..0000000
+++ /dev/null
@@ -1,112 +0,0 @@
-#! /bin/bash
-#
-# Copyright (c) Members of the EGEE Collaboration. 2004-2010.
-# See http://www.eu-egee.org/partners for details on the copyright holders.
-# 
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-# 
-#     http://www.apache.org/licenses/LICENSE-2.0
-# 
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-GLITE_LOCATION=${GLITE_LOCATION:-"/opt/glite"}
-NOTIFY=${GLITE_LB_NOTIFY:-"$GLITE_LOCATION/bin/glite-lb-notify"}
-FILE='/var/tmp/msg-notif.txt'
-SERVER="$1"
-TTL=${2:-"7200"}
-STAT="stat -c %Y"
-#STAT="stat -f %m"
-
-
-function load() {
-       out="`cat \"$FILE\"`"
-       nid=`echo "$out" | grep '^notification ID:' | cut -f3 -d' '`
-       val=`echo "$out" | grep '^valid' | sed 's/[^(]*(\([^)]*\)).*/\1/'`
-       ori=`$STAT "$FILE"`
-
-       if [ -z "$nid" -o -z "$val" ]; then
-               val=-1
-               return 1
-       fi
-}
-
-function save_ttl() {
-       mv "$FILE" "$FILE.1"
-       cat "$FILE" | grep -v '^valid' > "$FILE"
-}
-
-
-# -- set up --
-
-if [ -z "$SERVER" ]; then
-       echo "Usage: $0 LB_SERVER [ TTL NEW_NOTIF_ARGUMRNTS... ]"
-       echo
-       echo "Environment:"
-       echo "  DROP: drop the notification"
-       echo "  DEBUG: show progress"
-       exit 1
-fi
-shift
-shift
-
-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"
-fi
-
-export GLITE_WMS_NOTIF_SERVER="$SERVER"
-export X509_USER_CERT
-export X509_USER_KEY
-
-now=`date '+%s'`
-val=-1
-ori=$now
-
-# -- load previous/create new --
-
-if [ -s "$FILE" ]; then load; fi
-if [ -z "$nid" -o $val -le $now ]; then
-       $NOTIFY new -t $TTL "$@" > "$FILE"
-       load
-       if [ $? != 0 ]; then
-               echo "Can't create notification"
-               exit 1
-       fi
-       [ -z "$DEBUG" ] || echo "`date '+%Y-%m-%d %H:%M:%S'`: notification '$nid' created"
-fi
-
-# -- refresh --
-
-let ref="($ori+$val)/2"
-if [ $ref -le $now ]; then
-       $NOTIFY refresh -t $TTL $nid > "$FILE"-new
-       if grep '^valid' "$FILE"-new >/dev/null 2>&1; then
-               mv "$FILE" "$FILE".1
-               grep '^notification ID:' "$FILE".1 > "$FILE"
-               cat "$FILE"-new >> "$FILE"
-               rm -f "$FILE"-new
-               [ -z "$DEBUG" ] || echo "`date '+%Y-%m-%d %H:%M:%S'`: notification '$nid' refreshed"
-       else
-               echo "Can't refresh notification '$nid'"
-               # next attempt in half of the remaining time
-               touch "$FILE"
-               exit 1
-       fi
-fi
-
-# -- drop --
-
-if [ ! -z "$DROP" ]; then
-       $NOTIFY drop $nid
-       rm -f "$FILE"
-       [ -z "$DEBUG" ] || echo "`date '+%Y-%m-%d %H:%M:%S'`: notification '$nid' dropped"
-fi