+

Last commit for check_apt_cacher_ng: 6601dd9c2f91d17d144031c38f74f9ba9f173f13

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
#!/bin/bash
#
# Nagios plugin to check Apt-cacher-ng status
#
# This plugin check apt-cacher-ng status by asking report page.
# It's able to parse result to retreive performance data.
#
# Copyright (c) 2015 Benjamin Renard <brenard@zionetrix.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

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] [-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:a:Pdh" OPTION
do
	case $OPTION in
		H)
			HOST=$OPTARG
		;;
		p)
			PORT=$OPTARG
		;;
		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
		;;
		d)
			DEBUG=1
		;;
		h)
			usage
			exit 0
		;;
		\?)
			echo "Invalid option: -$OPTARG" >&2
			usage
			exit 1
		;;
		:)
			echo "Option -$OPTARG requires an argument." >&2
			usage
			exit 1
		;;
	esac
done

function debug() {
	[ $DEBUG -eq 1 ] && echo "[DEBUG] $1"
}

html2text -version > /dev/null 2>&1
if [ $? -ne 0 ]
then
	echo "UNKNOWN : html2text command not found"
	exit 3
fi

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

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
	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

stats_line=$( echo -e "$stats"|html2text -width 300|grep '^ *Period' -A 2|tail -n 1 )
if [ -z "$stats_line" ]
then
	echo "WARNING : Fail to parse apt-cacher-ng stats page"
	exit 1
fi

if [ $DO_PERFDATA -eq 1 ]
then
	cache_req_hits_count=$( echo -e "$stats_line"|awk '{print $6}'|sed 's/^[^0-9]*//' )
	cache_req_hits_per=$( echo -e "$stats_line"|cut -d '(' -f 2|cut -d')' -f 1|sed 's/^[^0-9]*//' )
	cache_req_misses_count=$( echo -e "$stats_line"|awk '{print $8}'|sed 's/^[^0-9]*//' )
	cache_req_misses_per=$( echo -e "$stats_line"|cut -d '(' -f 3|cut -d')' -f 1|sed 's/^[^0-9]*//' )
	cache_req_total_count=$( echo -e "$stats_line"|awk '{print $10}'|sed 's/^[^0-9]*//' )

	cache_data_hits_count=$( echo -e "$stats_line"|awk '{print $11$12}'|sed 's/^[^0-9]*//' )
	cache_data_hits_per=$( echo -e "$stats_line"|cut -d '(' -f 4|cut -d')' -f 1|sed 's/^[^0-9]*//' )
	cache_data_misses_count=$( echo -e "$stats_line"|awk '{print $14$15}'|sed 's/^[^0-9]*//' )
	cache_data_misses_per=$( echo -e "$stats_line"|cut -d '(' -f 4|cut -d')' -f 1|sed 's/^[^0-9]*//' )
	cache_data_total_count=$( echo -e "$stats_line"|awk '{print $17$18}'|sed 's/^[^0-9]*//' )

	PERF_DATA="|cache_req_hits_count=$cache_req_hits_count"
	PERF_DATA="$PERF_DATA,cache_req_hits_per=$cache_req_hits_per"
	PERF_DATA="$PERF_DATA,cache_req_misses_count=$cache_req_misses_count"
	PERF_DATA="$PERF_DATA,cache_req_misses_per=$cache_req_misses_per"
	PERF_DATA="$PERF_DATA,cache_req_total_count=$cache_req_total_count"
	PERF_DATA="$PERF_DATA,cache_data_hits_count=$cache_data_hits_count"
	PERF_DATA="$PERF_DATA,cache_data_hits_per=$cache_data_hits_per"
	PERF_DATA="$PERF_DATA,cache_data_misses_count=$cache_data_misses_count"
	PERF_DATA="$PERF_DATA,cache_data_misses_per=$cache_data_misses_per"
	PERF_DATA="$PERF_DATA,cache_data_total_count=$cache_data_total_count"
else
	PERF_DATA=""
fi

echo "OK - Apt-cacher-ng is running$PERF_DATA"
exit 0
ViewGit