summaryrefslogtreecommitdiffstats
path: root/web/attachments/418908-0001-check_users-use-utxent-functions-to-get-data.patch
blob: 84043bdcf04b9efb523d41ecf019d0f4911b4468 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
From 10d9e2aadb3f7db0ab784a1843abcea570adf9ec Mon Sep 17 00:00:00 2001
From: Marc Remy <mremy@gmx.ch>
Date: Wed, 20 Jul 2011 21:18:42 +0200
Subject: [PATCH] check_users: use utxent functions to get data

---
 plugins/Makefile.am   |    2 +-
 plugins/check_users.c |   38 ++++++++------------------------------
 2 files changed, 9 insertions(+), 31 deletions(-)

diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 36a28b0..3a2afc1 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -101,7 +101,7 @@ check_tcp_LDADD = $(SSLOBJS) $(NETLIBS) $(SSLLIBS)
 check_time_LDADD = $(NETLIBS)
 check_ntp_time_LDADD = $(NETLIBS) $(MATHLIBS)
 check_ups_LDADD = $(NETLIBS)
-check_users_LDADD = $(BASEOBJS) popen.o
+check_users_LDADD = $(BASEOBJS)
 check_by_ssh_LDADD = $(NETLIBS)
 check_ide_smart_LDADD = $(BASEOBJS)
 negate_LDADD = $(BASEOBJS)
diff --git a/plugins/check_users.c b/plugins/check_users.c
index 8368612..fb8bcca 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)
-- 
1.7.2.5