aboutsummaryrefslogtreecommitdiff
path: root/plugins/check_users.c
diff options
context:
space:
mode:
authorGravatar M. Remy <mremy@gmx.ch> 2012-04-17 22:15:15 +0200
committerGravatar Holger Weiss <holger@zedat.fu-berlin.de> 2012-06-07 10:14:16 +0200
commit3e622f3a47bc7d31f22513a79892c3c52febd2d3 (patch)
tree75df841b5d21b36cd869a574e7857ec6a49fb6ae /plugins/check_users.c
parent679a2296065893fd428891d610499c04a50aaa29 (diff)
downloadmonitoring-plugins-3e622f3a47bc7d31f22513a79892c3c52febd2d3.tar.gz
check_users: improve performance
This patch use the utxent function family to collect the user data. It improve the check speed. Need a system conforming to POSIX.1-2001.
Diffstat (limited to 'plugins/check_users.c')
-rw-r--r--plugins/check_users.c38
1 files changed, 8 insertions, 30 deletions
diff --git a/plugins/check_users.c b/plugins/check_users.c
index 8368612a..fb8bcca3 100644
--- a/plugins/check_users.c
+++ b/plugins/check_users.c
@@ -35,8 +35,8 @@ const char *copyright = "2000-2007";
const char *email = "nagiosplug-devel@lists.sourceforge.net";
#include "common.h"
-#include "popen.h"
#include "utils.h"
+#include <utmpx.h>
#define possibly_set(a,b) ((a) == 0 ? (b) : 0)
@@ -54,6 +54,7 @@ main (int argc, char **argv)
int result = STATE_UNKNOWN;
char input_buffer[MAX_INPUT_BUFFER];
char *perf;
+ struct utmpx *putmpx;
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
@@ -67,43 +68,20 @@ main (int argc, char **argv)
if (process_arguments (argc, argv) == ERROR)
usage4 (_("Could not parse arguments"));
- /* 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);
-
users = 0;
- while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
+ /* get currently logged users from utmpx */
+ setutxent();
- /* increment 'users' on all lines except total user count */
- if (input_buffer[0] != '#') {
+ while( (putmpx=getutxent()) ) {
+ if( (putmpx->ut_type==USER_PROCESS) ) {
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);
+ endutxent();
- /* else check the user count against warning and critical thresholds */
+ /* check the user count against warning and critical thresholds */
if (users > cusers)
result = STATE_CRITICAL;
else if (users > wusers)