summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorM. Remy <mremy@gmx.ch>2012-04-17 22:15:15 +0200
committerHolger Weiss <holger@zedat.fu-berlin.de>2012-06-07 10:14:16 +0200
commit3e622f3a47bc7d31f22513a79892c3c52febd2d3 (patch)
tree75df841b5d21b36cd869a574e7857ec6a49fb6ae
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.
-rw-r--r--plugins/Makefile.am2
-rw-r--r--plugins/check_users.c38
2 files changed, 9 insertions, 31 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 36a28b0b..3a2afc15 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -101,7 +101,7 @@ check_tcp_LDADD = $(SSLOBJS) $(NETLIBS) $(SSLLIBS)
101check_time_LDADD = $(NETLIBS) 101check_time_LDADD = $(NETLIBS)
102check_ntp_time_LDADD = $(NETLIBS) $(MATHLIBS) 102check_ntp_time_LDADD = $(NETLIBS) $(MATHLIBS)
103check_ups_LDADD = $(NETLIBS) 103check_ups_LDADD = $(NETLIBS)
104check_users_LDADD = $(BASEOBJS) popen.o 104check_users_LDADD = $(BASEOBJS)
105check_by_ssh_LDADD = $(NETLIBS) 105check_by_ssh_LDADD = $(NETLIBS)
106check_ide_smart_LDADD = $(BASEOBJS) 106check_ide_smart_LDADD = $(BASEOBJS)
107negate_LDADD = $(BASEOBJS) 107negate_LDADD = $(BASEOBJS)
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";
35const char *email = "nagiosplug-devel@lists.sourceforge.net"; 35const char *email = "nagiosplug-devel@lists.sourceforge.net";
36 36
37#include "common.h" 37#include "common.h"
38#include "popen.h"
39#include "utils.h" 38#include "utils.h"
39#include <utmpx.h>
40 40
41#define possibly_set(a,b) ((a) == 0 ? (b) : 0) 41#define possibly_set(a,b) ((a) == 0 ? (b) : 0)
42 42
@@ -54,6 +54,7 @@ main (int argc, char **argv)
54 int result = STATE_UNKNOWN; 54 int result = STATE_UNKNOWN;
55 char input_buffer[MAX_INPUT_BUFFER]; 55 char input_buffer[MAX_INPUT_BUFFER];
56 char *perf; 56 char *perf;
57 struct utmpx *putmpx;
57 58
58 setlocale (LC_ALL, ""); 59 setlocale (LC_ALL, "");
59 bindtextdomain (PACKAGE, LOCALEDIR); 60 bindtextdomain (PACKAGE, LOCALEDIR);
@@ -67,43 +68,20 @@ main (int argc, char **argv)
67 if (process_arguments (argc, argv) == ERROR) 68 if (process_arguments (argc, argv) == ERROR)
68 usage4 (_("Could not parse arguments")); 69 usage4 (_("Could not parse arguments"));
69 70
70 /* run the command */
71 child_process = spopen (WHO_COMMAND);
72 if (child_process == NULL) {
73 printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
74 return STATE_UNKNOWN;
75 }
76
77 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
78 if (child_stderr == NULL)
79 printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
80
81 users = 0; 71 users = 0;
82 72
83 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 73 /* get currently logged users from utmpx */
74 setutxent();
84 75
85 /* increment 'users' on all lines except total user count */ 76 while( (putmpx=getutxent()) ) {
86 if (input_buffer[0] != '#') { 77 if( (putmpx->ut_type==USER_PROCESS) ) {
87 users++; 78 users++;
88 continue;
89 } 79 }
90
91 /* get total logged in users */
92 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
93 break;
94
95 } 80 }
96 81
97 /* check STDERR */ 82 endutxent();
98 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
99 result = possibly_set (result, STATE_UNKNOWN);
100 (void) fclose (child_stderr);
101
102 /* close the pipe */
103 if (spclose (child_process))
104 result = possibly_set (result, STATE_UNKNOWN);
105 83
106 /* else check the user count against warning and critical thresholds */ 84 /* check the user count against warning and critical thresholds */
107 if (users > cusers) 85 if (users > cusers)
108 result = STATE_CRITICAL; 86 result = STATE_CRITICAL;
109 else if (users > wusers) 87 else if (users > wusers)