+

Permit to specify auth info by using -a parameter and improve wget call

Benjamin Renard [2015-06-30 15:39:49]
Permit to specify auth info by using -a parameter and improve wget call
Filename
README
check_apt_cacher_ng
diff --git a/README b/README
index ae43c36..a9bc230 100644
--- a/README
+++ b/README
@@ -4,10 +4,12 @@ Nagios plugin to check Apt-cacher-NG status
 Usage
 -----

-  Usage : check_apt_cacher_ng [-H hostaddress] [-p port] [-Pdh]
+  Usage : check_apt_cacher_ng [-H hostaddress] [-p port] [-r reportpage] [-a user:pass] [-Pdh]

     -H hostaddress	Specify apt-cacher-ng host address (default=127.0.0.1)
     -p post		Specify apt-cacher-ng TCP port (default=3142)
+    -r reportpage		Specify apt-cacher-ng ReportPage parameter value (default=acng-report.html)
+    -a user:pass		Specify authentication informations with format 'user:pass'
     -P			Include performance data
     -d			Enable debug mode
     -h			Show this help message
diff --git a/check_apt_cacher_ng b/check_apt_cacher_ng
index 3d1ae12..7b2c788 100755
--- a/check_apt_cacher_ng
+++ b/check_apt_cacher_ng
@@ -24,23 +24,27 @@
 HOST=127.0.0.1
 PORT=3142
 REPORTPAGE=acng-report.html
+AUTHINFO=""
+USER=""
+PASS=""
 DO_PERFDATA=0
 DEBUG=0

 function usage() {
 cat << EOF
-Usage : check_apt_cacher_ng [-H hostaddress] [-p port] [-Pdh]
+Usage : check_apt_cacher_ng [-H hostaddress] [-p port] [-r reportpage] [-a user:pass] [-Pdh]

   -H hostaddress	Specify apt-cacher-ng host address (default=127.0.0.1)
   -p post		Specify apt-cacher-ng TCP port (default=3142)
   -r reportpage		Specify apt-cacher-ng ReportPage parameter value (default=acng-report.html)
+  -a user:pass		Specify authentication informations with format 'user:pass'
   -P			Include performance data
   -d			Enable debug mode
   -h			Show this help message
 EOF
 }

-while getopts "H:p:r:Pdh" OPTION
+while getopts "H:p:r:a:Pdh" OPTION
 do
 	case $OPTION in
 		H)
@@ -52,6 +56,17 @@ do
 		r)
 			REPORTPAGE=$OPTARG
 		;;
+		a)
+			AUTHINFO=$OPTARG
+			USER=$( echo "$AUTHINFO"|cut -d':' -f 1 )
+			PASS=$( echo "$AUTHINFO"|cut -d':' -f 2 )
+			if [ -z "$USER" -o -z "$PASS" -o $( echo "$AUTHINFO"|grep -c ':' ) -ne 1 ]
+			then
+				echo "Authentification informations invalid. You must specify it using format : 'user:password'"
+				usage
+				exit 1
+			fi
+		;;
 		P)
 			DO_PERFDATA=1
 		;;
@@ -88,11 +103,44 @@ fi

 stats_url="http://$HOST:$PORT/$REPORTPAGE?doCount=Count+Data#top"
 debug "Get apt-cacher-ng stats from $HOST:$PORT (URL : $stats_url)"
-stats=$( wget -q -O - $stats_url )
+wget_cmd="wget -O - $stats_url"

-if [ $? -ne 0 ]
+if [ -n "$AUTHINFO" ]
+then
+	debug "Using auth info $USER:*****"
+	wget_cmd="$wget_cmd --user=$USER --password=$PASS"
+fi
+
+[ $DEBUG -ne 1 ] && wget_cmd="$wget_cmd -q"
+
+stats=$( $wget_cmd )
+res=$?
+debug "Wget return code : $res"
+
+if [ $res -ne 0 ]
 then
-	echo "CRITICAL : Fail to connect to apt-cacher-ng"
+	case $res in
+		4)
+			comment="(Network failure)"
+		;;
+		5)
+			comment="(SSL verification failure)"
+		;;
+		6)
+			comment="(Auth required)"
+		;;
+		7)
+			comment="(Protocol errors)"
+		;;
+		8)
+			comment="(Server issued an error response)"
+		;;
+		*)
+			comment=""
+		;;
+	esac
+
+	echo "CRITICAL : Fail to connect to apt-cacher-ng $comment"
 	exit 2
 fi
ViewGit