Author: jw Date: 2006-06-02 14:05:28 +0200 (Fri, 02 Jun 2006) New Revision: 1476 Modified: tools/scripts/check_urls Log: check_urls: things will never be the same (HTML reports, reminders, notification tracking) Modified: tools/scripts/check_urls =================================================================== --- tools/scripts/check_urls 2006-06-02 12:05:05 UTC (rev 1475) +++ tools/scripts/check_urls 2006-06-02 12:05:28 UTC (rev 1476) @@ -5,17 +5,78 @@ # # Redirect stdout to /dev/null to only see failures -checked=0 -failed=0 +STATE_DIR=~/.check_urls +LOGFILE=$STATE_DIR/state.log +HTMLLOG=$STATE_DIR/state.html +PORT_STATES=$STATE_DIR/ports/ -print_err() { - echo "$@" +NOTIFY_DELTA=10 # seconds +REMOVE_DELTA=20 + +notify() { + local port=$1 + local url=$2 + local cause=$3 + local state=$4 + + echo "==> Notify: $port, Url: $url Reason: $2, State: $3" } -print_msg() { - echo "$@" 1>&2 +log() { + local timestamp=$1 + local port=$2 + local url=$3 + local reason=$4 + local state=$5 + + echo "$1|$2|$3|$4|$5" >> $LOGFILE } + +check_notify() { + local portpath=$1 + local port=$(echo $portpath|sed -e 's|/|_|g') + local url=$2 + local cause=$3 + + if [ -f $PORT_STATES/$port.created ]; then +# debug "Known problem '$port', first detected on \ +# $(cat $PORT_STATES/$port.created|awk '{print strftime("%c",$1)}')" + + log "$(cat $PORT_STATES/$port.created)" \ + "$portpath" "$(cat $PORT_STATES/$port.cause)" \ + "KNOWN" + + notify_date=$(cat $PORT_STATES/$port.notified) + now=$(date +%s) + delta=$((now - notify_date)) + +# debug "Delta: $delta" + + if [ ! "$(cat $PORT_STATES/$port.cause)" = "$cause" ]; then + notify "$portpath" "$cause" "Changed reason" + echo $now > $PORT_STATES/$port.notified + echo $cause > $PORT_STATES/$port.cause + elif [ $delta -gt $NOTIFY_DELTA ]; then + notify "$portpath" "$cause" "Reminder" + echo $now > $PORT_STATES/$port.notified + fi + + date +%s > $PORT_STATES/$port.revisited + else + datestr=$(date +%s) + + notify "$portpath" "$cause" "New" + log $datestr $portpath "$cause" "NEW" + + echo $datestr > $PORT_STATES/$port.created + echo $datestr > $PORT_STATES/$port.revisited + echo $datestr > $PORT_STATES/$port.notified + echo $cause > $PORT_STATES/$port.cause + fi +} + + check_port() { if [ ! -d $f ]; then return 0 @@ -25,7 +86,6 @@ return -1 fi - errCount=0 . $f/Pkgfile for s in ${source[*]}; do local proto="$(echo $s | sed 's|://.*||g')" @@ -39,18 +99,55 @@ err=$(curl -S -s -I $s 2>&1) if [ $? -ne 0 ]; then err=$(echo $err|head -n 1|sed -e 's|\r||g') - print_err "Failed: $s" - print_err " Reason: $err" - failed=$((failed+1)) + check_notify "$f" "$s" "$err" fi - checked=$((checked+1)) fi done } +cleanup_states() { + for f in $(ls -1 $PORT_STATES/*.revisited 2> /dev/null); do + age=$(cat $f) + now=$(date +%s) + delta=$((now - age)) + base=$(basename $f .revisited) + name=$(echo $base|sed -e 's|_|/|g') + + if [ $delta -gt $REMOVE_DELTA ]; then + log $now $name "Solved" "RESOLVED" + rm -r $PORT_STATES/$base.* + fi + done +} + +htmlify() { + if [ ! -f $LOGFILE ]; then + return + fi + echo "<html><head><title>check_urls status</title><style type=\"text/css\">" > $HTMLLOG + echo "h3 { background: #ff8888; } " >> $HTMLLOG + echo "h3.NEW { background: #ffcccc; } " >> $HTMLLOG + echo "h3.RESOLVED { background: #77ff77; } " >> $HTMLLOG + echo "</style></head><body>" >> $HTMLLOG + cat $LOGFILE|sort --field-separator="|" -n|\ + awk -F\| '/.*/ { print "<p ><h3 class=\""$4"\">"$2"</h3>" \ + "<b>Status:</b> "$4"<br>" \ + "<b>Reason:</b> "$3"<br>" \ + "<b>Open since:</b>" strftime("%c",$1) "</p>";}' >> $HTMLLOG + echo "</body></html>" >> $HTMLLOG +} + +########## + + +mkdir -p $STATE_DIR +mkdir -p $PORT_STATES +:> $LOGFILE + for f in $@; do - print_msg "* Checking $f" check_port $f done -print_msg "$(basename $0): Checked $checked urls, $failed failures" +cleanup_states +htmlify +
participants (1)
-
crux@crux.nu