packages: nagios-plugin-check_domain/check_domain - fix bashism, simplify

glen glen at pld-linux.org
Sun Aug 30 10:46:58 CEST 2009


Author: glen                         Date: Sun Aug 30 08:46:58 2009 GMT
Module: packages                      Tag: HEAD
---- Log message:
- fix  bashism, simplify

---- Files affected:
packages/nagios-plugin-check_domain:
   check_domain (1.2 -> 1.3) 

---- Diffs:

================================================================
Index: packages/nagios-plugin-check_domain/check_domain
diff -u packages/nagios-plugin-check_domain/check_domain:1.2 packages/nagios-plugin-check_domain/check_domain:1.3
--- packages/nagios-plugin-check_domain/check_domain:1.2	Sun Aug 30 10:28:00 2009
+++ packages/nagios-plugin-check_domain/check_domain	Sun Aug 30 10:46:53 2009
@@ -1,7 +1,5 @@
-#!/bin/bash
-
-PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
-
+#!/bin/sh
+PROGPATH=${0%/*}
 . $PROGPATH/utils.sh
 
 # Default values (days):
@@ -9,78 +7,87 @@
 warning=30
 
 # Parse arguments
-args=`getopt -o hd:w:c:P: --long help,domain:,warning:,critical:,path: -u -n $0 -- "$@"` 
-[ $? != 0 ] && echo "$0: Could not parse arguments" && echo "Usage: $0 -h | -d <domain> [-c <critical>] [-w <warning>]" && exit
+args=$(getopt -o hd:w:c:P: --long help,domain:,warning:,critical:,path: -u -n $0 -- "$@")
+if [ $? != 0 ]; then
+	echo "$0: Could not parse arguments"
+	echo "Usage: $0 -h | -d <domain> [-c <critical>] [-w <warning>]"
+	exit 1
+fi
 set -- $args
 
-while true ; do
-        case "$1" in
-                -c|--critical) critical=$2;shift 2;;
-                -w|--warning)  warning=$2;shift 2;;
-            -d|--domain)   domain=$2;shift 2;;
-            -P|--path)     whoispath=$2;shift 2;;
-            -h|--help)     echo "check_domain - v1.01"
-                               echo "Copyright (c) 2005 Tomàs Núñez Lirola <tnunez at criptos.com> under GPL License"
-                               echo "This plugin checks the expiration date of a domain name." 
-                               echo ""
-                               echo "Usage: $0 -h | -d <domain> [-c <critical>] [-w <warning>]"
-                               echo "NOTE: -d must be specified"
-                               echo ""
-                               echo "Options:"
-                               echo "-h"
-                               echo "     Print detailed help"
-                               echo "-d"
-                               echo "     Domain name to check"
-                               echo "-w"
-                               echo "     Response time to result in warning status (days)"
-                               echo "-c"
-                               echo "     Response time to result in critical status (days)"
-                               echo ""
-                               echo "This plugin will use whois service to get the expiration date for the domain name. "
-                               echo "Example:"
-                               echo "     $0 -d domain.tld -w 30 -c 10"
-                               echo ""
-                               exit;;
-             --) shift; break;;
-                *)  echo "Internal error!" ; exit 1 ;;
-        esac
+die() {
+	local rc=$1
+	local msg=$2
+	echo $msg
+	exit $rc
+}
+
+while :; do
+	case "$1" in
+		-c|--critical) critical=$2; shift 2;;
+		-w|--warning)  warning=$2; shift 2;;
+		-d|--domain)   domain=$2; shift 2;;
+		-P|--path)     whoispath=$2; shift 2;;
+		-h|--help)     echo "check_domain - v1.2"
+					   echo "Copyright (c) 2005 Tomàs Núñez Lirola <tnunez at criptos.com>, 2009 Elan Ruusamäe <glen at delfi.ee>"
+					   echo "under GPL License"
+					   echo ""
+					   echo "This plugin checks the expiration date of a domain name."
+					   echo ""
+					   echo "Usage: $0 -h | -d <domain> [-c <critical>] [-w <warning>]"
+					   echo "NOTE: -d must be specified"
+					   echo ""
+					   echo "Options:"
+					   echo "-h"
+					   echo "     Print detailed help"
+					   echo "-d"
+					   echo "     Domain name to check"
+					   echo "-w"
+					   echo "     Response time to result in warning status (days)"
+					   echo "-c"
+					   echo "     Response time to result in critical status (days)"
+					   echo ""
+					   echo "This plugin will use whois service to get the expiration date for the domain name. "
+					   echo "Example:"
+					   echo "     $0 -d domain.tld -w 30 -c 10"
+					   echo ""
+					   exit;;
+		--) shift; break;;
+		*)  die $STATE_UNKNOWN "Internal error!";;
+	esac
 done
 
-[ -z $domain ] && echo "UNKNOWN - There is no domain name to check" && exit $STATE_UNKNOWN
+if [ -z $domain ]; then
+	die $STATE_UNKNOWN "UNKNOWN - There is no domain name to check"
+fi
 
 # Looking for whois binary
 if [ -z $whoispath ]; then
-      type whois &> /dev/null || error="yes"
-      [ ! -z $error ] && echo "UNKNOWN - Unable to find whois binary in your path. Is it installed? Please specify path." && exit $STATE_UNKNOWN
+	type whois > /dev/null 2>&1 || error="yes"
+	[ -z "$error" ] || die $STATE_UNKNOWN "UNKNOWN - Unable to find whois binary in your path. Is it installed? Please specify path."
 else
-      [ ! -x "$whoispath/whois" ] && echo "UNKNOWN - Unable to find whois binary, you specified an incorrect path" && exit $STATE_UNKNOWN
+	[ -x "$whoispath/whois" ] || die $STATE_UNKNOWN "UNKNOWN - Unable to find whois binary, you specified an incorrect path"
 fi
 
 # Calculate days until expiration
-country=${domain: -3}
+country=$(awk -vd="$domain" 'BEGIN{print substr(d, length(d) - 2); exit}')
+
+if [ "$country" = ".ru" ]; then
+	expiration=$(whois $domain | awk '/paid-till:/{split($2, a, "."); printf("%s-%s-%s\n", a[1], a[2], a[3])}')
+else
+	expiration=$(whois $domain | awk '/Expiration Date:/{print $3}')
+fi
 
-if [ "$country" = ".ru" ]
-	then
-	ruexpiration=`whois $domain |grep "paid-till:"| awk -F"paid-till:" '{print $2}'|cut -f 1`
-		year=${ruexpiration: 2:4}
-		month=${ruexpiration: 7:2}
-		day=${ruexpiration: -2}
-		month_string=`date +%b -d"$year$month$day"`
-		expiration="$day-$month_string-$year"
-	else
-		expiration=`whois $domain |grep "Expiration Date:"| awk -F"Date:" '{print $2}'|cut -f 1`
-	fi
-
-expseconds=`date +%s --date="$expiration"`
-nowseconds=`date +%s`
-((diffseconds=expseconds-nowseconds))
+expseconds=$(date +%s --date="$expiration")
+nowseconds=$(date +%s)
+diffseconds=$((expseconds-nowseconds))
 expdays=$((diffseconds/86400))
 
 # Trigger alarms if applicable
-[ -z "$expiration" ] && echo "UNKNOWN - Domain doesn't exist or no WHOIS server available." && exit $STATE_UNKNOWN
-[ $expdays -lt 0 ] && echo "CRITICAL - Domain expired on $expiration" && exit $STATE_CRITICAL
-[ $expdays -lt $critical ] && echo "CRITICAL - Domain will expire in $expdays days" && exit $STATE_CRITICAL
-[ $expdays -lt $warning ]&& echo "WARNING - Domain will expire in $expdays days" && exit $STATE_WARNING
+[ -z "$expiration" ] && die $STATE_UNKNOWN "UNKNOWN - Domain doesn't exist or no WHOIS server available."
+[ $expdays -lt 0 ] && die $STATE_CRITICAL "CRITICAL - Domain expired on $expiration"
+[ $expdays -lt $critical ] && die $STATE_CRITICAL "CRITICAL - Domain will expire in $expdays days"
+[ $expdays -lt $warning ]&& die $STATE_WARNING "WARNING - Domain will expire in $expdays days"
 
 # No alarms? Ok, everything is right.
 echo "OK - Domain will expire in $expdays days"
================================================================

---- CVS-web:
    http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/packages/nagios-plugin-check_domain/check_domain?r1=1.2&r2=1.3&f=u



More information about the pld-cvs-commit mailing list