diff options
author | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-08-20 19:13:25 (GMT) |
---|---|---|
committer | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-08-20 19:13:25 (GMT) |
commit | 92849a1a87f2c74a3017b30fec90c46919761f79 (patch) | |
tree | 1f35e4ffd5e942ca7c25c751a46b7fa1f521887b /plugins/check_users.c | |
parent | 4083622f86e1fe0fef1e81f23a2ca7a4614ea481 (diff) | |
download | monitoring-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.
Diffstat (limited to 'plugins/check_users.c')
-rw-r--r-- | plugins/check_users.c | 45 |
1 files changed, 44 insertions, 1 deletions
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@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) |