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

Benjamin Renard commited on 2015-06-30 15:41:43
Showing 2 changed files, with 56 additions and 6 deletions.

... ...
@@ -4,10 +4,12 @@ Nagios plugin to check Apt-cacher-NG status
4 4
 Usage
5 5
 -----
6 6
 
7
-  Usage : check_apt_cacher_ng [-H hostaddress] [-p port] [-Pdh]
7
+  Usage : check_apt_cacher_ng [-H hostaddress] [-p port] [-r reportpage] [-a user:pass] [-Pdh]
8 8
   
9 9
     -H hostaddress	Specify apt-cacher-ng host address (default=127.0.0.1)
10 10
     -p post		Specify apt-cacher-ng TCP port (default=3142)
11
+    -r reportpage		Specify apt-cacher-ng ReportPage parameter value (default=acng-report.html)
12
+    -a user:pass		Specify authentication informations with format 'user:pass'
11 13
     -P			Include performance data
12 14
     -d			Enable debug mode
13 15
     -h			Show this help message
... ...
@@ -24,23 +24,27 @@
24 24
 HOST=127.0.0.1
25 25
 PORT=3142
26 26
 REPORTPAGE=acng-report.html
27
+AUTHINFO=""
28
+USER=""
29
+PASS=""
27 30
 DO_PERFDATA=0
28 31
 DEBUG=0
29 32
 
30 33
 function usage() {
31 34
 cat << EOF
32
-Usage : check_apt_cacher_ng [-H hostaddress] [-p port] [-Pdh]
35
+Usage : check_apt_cacher_ng [-H hostaddress] [-p port] [-r reportpage] [-a user:pass] [-Pdh]
33 36
 
34 37
   -H hostaddress	Specify apt-cacher-ng host address (default=127.0.0.1)
35 38
   -p post		Specify apt-cacher-ng TCP port (default=3142)
36 39
   -r reportpage		Specify apt-cacher-ng ReportPage parameter value (default=acng-report.html)
40
+  -a user:pass		Specify authentication informations with format 'user:pass'
37 41
   -P			Include performance data
38 42
   -d			Enable debug mode
39 43
   -h			Show this help message
40 44
 EOF
41 45
 }
42 46
 
43
-while getopts "H:p:r:Pdh" OPTION
47
+while getopts "H:p:r:a:Pdh" OPTION
44 48
 do
45 49
 	case $OPTION in
46 50
 		H)
... ...
@@ -52,6 +56,17 @@ do
52 56
 		r)
53 57
 			REPORTPAGE=$OPTARG
54 58
 		;;
59
+		a)
60
+			AUTHINFO=$OPTARG
61
+			USER=$( echo "$AUTHINFO"|cut -d':' -f 1 )
62
+			PASS=$( echo "$AUTHINFO"|cut -d':' -f 2 )
63
+			if [ -z "$USER" -o -z "$PASS" -o $( echo "$AUTHINFO"|grep -c ':' ) -ne 1 ]
64
+			then
65
+				echo "Authentification informations invalid. You must specify it using format : 'user:password'"
66
+				usage
67
+				exit 1
68
+			fi
69
+		;;
55 70
 		P)
56 71
 			DO_PERFDATA=1
57 72
 		;;
... ...
@@ -88,11 +103,44 @@ fi
88 103
 
89 104
 stats_url="http://$HOST:$PORT/$REPORTPAGE?doCount=Count+Data#top"
90 105
 debug "Get apt-cacher-ng stats from $HOST:$PORT (URL : $stats_url)"
91
-stats=$( wget -q -O - $stats_url )
106
+wget_cmd="wget -O - $stats_url"
92 107
 
93
-if [ $? -ne 0 ]
108
+if [ -n "$AUTHINFO" ]
94 109
 then
95
-	echo "CRITICAL : Fail to connect to apt-cacher-ng"
110
+	debug "Using auth info $USER:*****"
111
+	wget_cmd="$wget_cmd --user=$USER --password=$PASS"
112
+fi
113
+
114
+[ $DEBUG -ne 1 ] && wget_cmd="$wget_cmd -q"
115
+
116
+stats=$( $wget_cmd )
117
+res=$?
118
+debug "Wget return code : $res"
119
+
120
+if [ $res -ne 0 ]
121
+then
122
+	case $res in
123
+		4)
124
+			comment="(Network failure)"
125
+		;;
126
+		5)
127
+			comment="(SSL verification failure)"
128
+		;;
129
+		6)
130
+			comment="(Auth required)"
131
+		;;
132
+		7)
133
+			comment="(Protocol errors)"
134
+		;;
135
+		8)
136
+			comment="(Server issued an error response)"
137
+		;;
138
+		*)
139
+			comment=""
140
+		;;
141
+	esac
142
+
143
+	echo "CRITICAL : Fail to connect to apt-cacher-ng $comment"
96 144
 	exit 2
97 145
 fi
98 146
 
99 147