summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Weiss <holger@zedat.fu-berlin.de>2013-08-20 21:13:25 +0200
committerHolger Weiss <holger@zedat.fu-berlin.de>2013-08-20 21:13:25 +0200
commit92849a1a87f2c74a3017b30fec90c46919761f79 (patch)
tree1f35e4ffd5e942ca7c25c751a46b7fa1f521887b
parent4083622f86e1fe0fef1e81f23a2ca7a4614ea481 (diff)
downloadmonitoring-plugins-92849a1a87f2c74a3017b30fec90c46919761f79.tar.gz
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.
-rw-r--r--configure.in4
-rw-r--r--plugins/Makefile.am4
-rw-r--r--plugins/check_users.c45
3 files changed, 52 insertions, 1 deletions
diff --git a/configure.in b/configure.in
index de75e532..529720e7 100644
--- a/configure.in
+++ b/configure.in
@@ -316,6 +316,10 @@ AS_IF([test "x$with_ldap" != "xno"], [
316 LIBS="$_SAVEDLIBS" 316 LIBS="$_SAVEDLIBS"
317]) 317])
318 318
319dnl Check for headers used by check_users
320AC_CHECK_HEADERS(utmpx.h)
321AM_CONDITIONAL([HAVE_UTMPX], [test "$ac_cv_header_utmpx_h" = "yes"])
322
319dnl Check for headers used by check_ide_smart 323dnl Check for headers used by check_ide_smart
320case $host in 324case $host in
321 *linux*) 325 *linux*)
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 64969dbf..031dd251 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -112,6 +112,10 @@ check_ide_smart_LDADD = $(BASEOBJS)
112negate_LDADD = $(BASEOBJS) 112negate_LDADD = $(BASEOBJS)
113urlize_LDADD = $(BASEOBJS) 113urlize_LDADD = $(BASEOBJS)
114 114
115if !HAVE_UTMPX
116check_users_LDADD += popen.o
117endif
118
115############################################################################## 119##############################################################################
116# secondary dependencies 120# secondary dependencies
117 121
diff --git a/plugins/check_users.c b/plugins/check_users.c
index c581fb29..ff2aedd2 100644
--- a/plugins/check_users.c
+++ b/plugins/check_users.c
@@ -36,7 +36,12 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
36 36
37#include "common.h" 37#include "common.h"
38#include "utils.h" 38#include "utils.h"
39#include <utmpx.h> 39
40#if HAVE_UTMPX_H
41# include <utmpx.h>
42#else
43# include "popen.h"
44#endif
40 45
41#define possibly_set(a,b) ((a) == 0 ? (b) : 0) 46#define possibly_set(a,b) ((a) == 0 ? (b) : 0)
42 47
@@ -53,7 +58,11 @@ main (int argc, char **argv)
53 int users = -1; 58 int users = -1;
54 int result = STATE_UNKNOWN; 59 int result = STATE_UNKNOWN;
55 char *perf; 60 char *perf;
61#if HAVE_UTMPX_H
56 struct utmpx *putmpx; 62 struct utmpx *putmpx;
63#else
64 char input_buffer[MAX_INPUT_BUFFER];
65#endif
57 66
58 setlocale (LC_ALL, ""); 67 setlocale (LC_ALL, "");
59 bindtextdomain (PACKAGE, LOCALEDIR); 68 bindtextdomain (PACKAGE, LOCALEDIR);
@@ -69,6 +78,7 @@ main (int argc, char **argv)
69 78
70 users = 0; 79 users = 0;
71 80
81#if HAVE_UTMPX_H
72 /* get currently logged users from utmpx */ 82 /* get currently logged users from utmpx */
73 setutxent (); 83 setutxent ();
74 84
@@ -77,6 +87,39 @@ main (int argc, char **argv)
77 users++; 87 users++;
78 88
79 endutxent (); 89 endutxent ();
90#else
91 /* run the command */
92 child_process = spopen (WHO_COMMAND);
93 if (child_process == NULL) {
94 printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
95 return STATE_UNKNOWN;
96 }
97
98 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
99 if (child_stderr == NULL)
100 printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
101
102 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
103 /* increment 'users' on all lines except total user count */
104 if (input_buffer[0] != '#') {
105 users++;
106 continue;
107 }
108
109 /* get total logged in users */
110 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
111 break;
112 }
113
114 /* check STDERR */
115 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
116 result = possibly_set (result, STATE_UNKNOWN);
117 (void) fclose (child_stderr);
118
119 /* close the pipe */
120 if (spclose (child_process))
121 result = possibly_set (result, STATE_UNKNOWN);
122#endif
80 123
81 /* check the user count against warning and critical thresholds */ 124 /* check the user count against warning and critical thresholds */
82 if (users > cusers) 125 if (users > cusers)