packages: nagios-plugin-check_ipmi/check_ipmi - setup sudo rules with -S

glen glen at pld-linux.org
Thu Dec 9 23:30:18 CET 2010


Author: glen                         Date: Thu Dec  9 22:30:18 2010 GMT
Module: packages                      Tag: HEAD
---- Log message:
- setup sudo rules with -S

---- Files affected:
packages/nagios-plugin-check_ipmi:
   check_ipmi (1.1 -> 1.2) 

---- Diffs:

================================================================
Index: packages/nagios-plugin-check_ipmi/check_ipmi
diff -u packages/nagios-plugin-check_ipmi/check_ipmi:1.1 packages/nagios-plugin-check_ipmi/check_ipmi:1.2
--- packages/nagios-plugin-check_ipmi/check_ipmi:1.1	Thu Dec  9 23:16:46 2010
+++ packages/nagios-plugin-check_ipmi/check_ipmi	Thu Dec  9 23:30:13 2010
@@ -29,24 +29,31 @@
 # filename points to a file which is used as a cache for speeding up the check.
 
 PROGRAM=${0##*/}
+REVISION=$(echo '$Revision$' | sed -e 's/[^0-9.]//g')
 PROGPATH=${0%/*}
+ARGS="$*"
 . $PROGPATH/utils.sh
 
-IPMITOOL=ipmitool
+# this needs to be full path for sudo
+ipmitool=/usr/bin/ipmitool
+sudo=/usr/bin/sudo
 
 die() {
-	eval local rc=\$STATEs_$1
+	eval local rc=\$STATE_$1
 	[ "$rc" ] || rc=$STATE_UNKNOWN
 	echo "$2"
 	exit $rc
 }
 
 usage() {
-	cat <<EOF
+	cat >&2 <<EOF
 Usage: check_ipmi <filename>
-	
-  	<filename> indicates the cache file for speeding up sensor readings.
+       check_ipmi -S <filename>
 
+     <filename> indicates the cache file for speeding up sensor readings.
+
+    -S
+       Install sudo rules
 EOF
 }
 
@@ -55,6 +62,26 @@
 	echo "$*" | sed -e 's/^ *\| *$//g'
 }
 
+setup_sudoers() {
+	new=/etc/sudoers.$$.new
+	umask 0227
+	cat /etc/sudoers > $new
+	cat >> $new <<-EOF
+
+	# Lines matching CHECK_IPMI added by $0 $ARGS on $(date)
+	User_Alias CHECK_IPMI=nagios
+	CHECK_IPMI ALL=(root) NOPASSWD: $ipmitool sdr dump $cache_filename
+	CHECK_IPMI ALL=(root) NOPASSWD: $ipmitool -S $cache_filename sdr
+	EOF
+
+	if visudo -c -f $new; then
+		mv -f $new /etc/sudoers
+		exit 0
+	fi
+	rm -f $new
+	exit 1
+}
+
 create_sdr_cache_file() {
 	local filename="$1"
 
@@ -63,15 +90,38 @@
 
 	touch "$filename"
 	# we run the dump in background
-	$IPMITOOL sdr dump $filename >/dev/null &
+	$sudo $ipmitool sdr dump $filename >/dev/null &
 	return 0
 }
 
 ## Start of main program ##
-if [ -z "$1" ]; then
+while [ $# -gt 0 ]; do
+	case "$1" in
+	-h|--help)
+		usage
+		exit 0
+		;;
+	-V|--version)
+		echo $PROGRAM $VERSION
+		exit 0
+		;;
+	-S)
+		setup_sudo=1
+		;;
+	*)
+		cache_filename="$1"
+		;;
+	esac
+	shift
+done
+
+if [ -z "$cache_filename" ]; then
 	die UNKNOWN "No databasename given."
 fi
-cache_filename="$1"
+
+if [ "$setup_sudo" = 1 ]; then
+	setup_sudoers
+fi
 
 if [ ! -s "$cache_filename" ]; then
 	if create_sdr_cache_file $cache_filename; then
@@ -82,7 +132,7 @@
 fi
 
 t=$(mktemp) || die CRITICAL "Can't create tempfile"
-$IPMITOOL -S $cache_filename sdr > $t || die CRITICAL "Can't run ipmitool sdr"
+$sudo $ipmitool -S $cache_filename sdr > $t || die CRITICAL "Can't run ipmitool sdr"
 # VRD 1 Temp       | 34 degrees C      | ok
 # CMOS Battery     | 3.12 Volts        | ok
 # VCORE            | 0x01              | ok
================================================================

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



More information about the pld-cvs-commit mailing list