[nagiosplug] check_users: Use utmpx(5) only if available

Nagios Plugin Development nagios-plugins at users.sourceforge.net
Tue Aug 20 22:10:26 CEST 2013


 Module: nagiosplug
 Branch: master
 Commit: 92849a1a87f2c74a3017b30fec90c46919761f79
 Author: Holger Weiss <holger at zedat.fu-berlin.de>
   Date: Tue Aug 20 21:13:25 2013 +0200
    URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=92849a1

check_users: Use utmpx(5) only if available

For systems that don't provide an utmpx(5) interface, restore the code
that was replaced in commit 3e622f3a47bc7d31f22513a79892c3c52febd2d3.

---

 configure.in          |    4 ++++
 plugins/Makefile.am   |    4 ++++
 plugins/check_users.c |   45 ++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 52 insertions(+), 1 deletions(-)

diff --git a/configure.in b/configure.in
index de75e53..529720e 100644
--- a/configure.in
+++ b/configure.in
@@ -316,6 +316,10 @@ AS_IF([test "x$with_ldap" != "xno"], [
   LIBS="$_SAVEDLIBS"
 ])
 
+dnl Check for headers used by check_users
+AC_CHECK_HEADERS(utmpx.h)
+AM_CONDITIONAL([HAVE_UTMPX], [test "$ac_cv_header_utmpx_h" = "yes"])
+
 dnl Check for headers used by check_ide_smart
 case $host in
   *linux*)
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 64969db..031dd25 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -112,6 +112,10 @@ check_ide_smart_LDADD = $(BASEOBJS)
 negate_LDADD = $(BASEOBJS)
 urlize_LDADD = $(BASEOBJS)
 
+if !HAVE_UTMPX
+check_users_LDADD += popen.o
+endif
+
 ##############################################################################
 # secondary dependencies
 
diff --git a/plugins/check_users.c b/plugins/check_users.c
index c581fb2..ff2aedd 100644
--- a/plugins/check_users.c
+++ b/plugins/check_users.c
@@ -36,7 +36,12 @@ const char *email = "nagiosplug-devel at lists.sourceforge.net";
 
 #include "common.h"
 #include "utils.h"
-#include <utmpx.h>
+
+#if HAVE_UTMPX_H
+# include <utmpx.h>
+#else
+# include "popen.h"
+#endif
 
 #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
 
@@ -53,7 +58,11 @@ main (int argc, char **argv)
 	int users = -1;
 	int result = STATE_UNKNOWN;
 	char *perf;
+#if HAVE_UTMPX_H
 	struct utmpx *putmpx;
+#else
+	char input_buffer[MAX_INPUT_BUFFER];
+#endif
 
 	setlocale (LC_ALL, "");
 	bindtextdomain (PACKAGE, LOCALEDIR);
@@ -69,6 +78,7 @@ main (int argc, char **argv)
 
 	users = 0;
 
+#if HAVE_UTMPX_H
 	/* get currently logged users from utmpx */
 	setutxent ();
 
@@ -77,6 +87,39 @@ main (int argc, char **argv)
 			users++;
 
 	endutxent ();
+#else
+	/* run the command */
+	child_process = spopen (WHO_COMMAND);
+	if (child_process == NULL) {
+		printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
+		return STATE_UNKNOWN;
+	}
+
+	child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
+	if (child_stderr == NULL)
+		printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
+
+	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
+		/* increment 'users' on all lines except total user count */
+		if (input_buffer[0] != '#') {
+			users++;
+			continue;
+		}
+
+		/* get total logged in users */
+		if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
+			break;
+	}
+
+	/* check STDERR */
+	if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
+		result = possibly_set (result, STATE_UNKNOWN);
+	(void) fclose (child_stderr);
+
+	/* close the pipe */
+	if (spclose (child_process))
+		result = possibly_set (result, STATE_UNKNOWN);
+#endif
 
 	/* check the user count against warning and critical thresholds */
 	if (users > cusers)





More information about the Commits mailing list