summaryrefslogtreecommitdiffstats
path: root/plugins/check_users.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-02-22 22:39:10 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-07 23:38:50 +0100
commitfb4f46f93da4ac50654fdcc2f26b2f37c73a9230 (patch)
tree2d09b35c6b033d85ff7117968ea6475ab8dec3dc /plugins/check_users.c
parent665e2f91306fa9b38044a382e4b7571a0c8c0c5f (diff)
downloadmonitoring-plugins-fb4f46f93da4ac50654fdcc2f26b2f37c73a9230.tar.gz
Migrate check_users to new ouput infrastructure
Diffstat (limited to 'plugins/check_users.c')
-rw-r--r--plugins/check_users.c249
1 files changed, 119 insertions, 130 deletions
diff --git a/plugins/check_users.c b/plugins/check_users.c
index f1e1c39d..6ec87d21 100644
--- a/plugins/check_users.c
+++ b/plugins/check_users.c
@@ -3,7 +3,7 @@
3 * Monitoring check_users plugin 3 * Monitoring check_users plugin
4 * 4 *
5 * License: GPL 5 * License: GPL
6 * Copyright (c) 2000-2024 Monitoring Plugins Development Team 6 * Copyright (c) 2000-2025 Monitoring Plugins Development Team
7 * 7 *
8 * Description: 8 * Description:
9 * 9 *
@@ -30,12 +30,19 @@
30 * 30 *
31 *****************************************************************************/ 31 *****************************************************************************/
32 32
33#include "check_users.d/config.h"
34#include "thresholds.h"
33const char *progname = "check_users"; 35const char *progname = "check_users";
34const char *copyright = "2000-2024"; 36const char *copyright = "2000-2025";
35const char *email = "devel@monitoring-plugins.org"; 37const char *email = "devel@monitoring-plugins.org";
36 38
37#include "common.h" 39#include "check_users.d/users.h"
38#include "utils.h" 40#include "output.h"
41#include "perfdata.h"
42#include "states.h"
43#include "utils_base.h"
44#include "./common.h"
45#include "./utils.h"
39 46
40#if HAVE_WTSAPI32_H 47#if HAVE_WTSAPI32_H
41# include <windows.h> 48# include <windows.h>
@@ -53,29 +60,16 @@ const char *email = "devel@monitoring-plugins.org";
53# include <systemd/sd-login.h> 60# include <systemd/sd-login.h>
54#endif 61#endif
55 62
56#define possibly_set(a, b) ((a) == 0 ? (b) : 0) 63typedef struct process_argument_wrapper {
64 int errorcode;
65 check_users_config config;
66} process_argument_wrapper;
57 67
58static int process_arguments(int, char **); 68process_argument_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
59static void print_help(void); 69void print_help(void);
60void print_usage(void); 70void print_usage(void);
61 71
62static char *warning_range = NULL;
63static char *critical_range = NULL;
64static thresholds *thlds = NULL;
65
66int main(int argc, char **argv) { 72int main(int argc, char **argv) {
67 int users = -1;
68 int result = STATE_UNKNOWN;
69#if HAVE_WTSAPI32_H
70 WTS_SESSION_INFO *wtsinfo;
71 DWORD wtscount;
72 DWORD index;
73#elif HAVE_UTMPX_H
74 struct utmpx *putmpx;
75#else
76 char input_buffer[MAX_INPUT_BUFFER];
77#endif
78
79 setlocale(LC_ALL, ""); 73 setlocale(LC_ALL, "");
80 bindtextdomain(PACKAGE, LOCALEDIR); 74 bindtextdomain(PACKAGE, LOCALEDIR);
81 textdomain(PACKAGE); 75 textdomain(PACKAGE);
@@ -83,121 +77,106 @@ int main(int argc, char **argv) {
83 /* Parse extra opts if any */ 77 /* Parse extra opts if any */
84 argv = np_extra_opts(&argc, argv, progname); 78 argv = np_extra_opts(&argc, argv, progname);
85 79
86 if (process_arguments(argc, argv) == ERROR) 80 process_argument_wrapper tmp = process_arguments(argc, argv);
87 usage4(_("Could not parse arguments"));
88
89 users = 0;
90
91#ifdef HAVE_LIBSYSTEMD
92 if (sd_booted() > 0)
93 users = sd_get_sessions(NULL);
94 else {
95#endif
96#if HAVE_WTSAPI32_H
97 if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &wtsinfo, &wtscount)) {
98 printf(_("Could not enumerate RD sessions: %d\n"), GetLastError());
99 return STATE_UNKNOWN;
100 }
101
102 for (index = 0; index < wtscount; index++) {
103 LPTSTR username;
104 DWORD size;
105 int len;
106
107 if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, wtsinfo[index].SessionId, WTSUserName, &username, &size))
108 continue;
109
110 len = lstrlen(username);
111
112 WTSFreeMemory(username);
113
114 if (len == 0)
115 continue;
116
117 if (wtsinfo[index].State == WTSActive || wtsinfo[index].State == WTSDisconnected)
118 users++;
119 }
120
121 WTSFreeMemory(wtsinfo);
122#elif HAVE_UTMPX_H
123 /* get currently logged users from utmpx */
124 setutxent();
125
126 while ((putmpx = getutxent()) != NULL)
127 if (putmpx->ut_type == USER_PROCESS)
128 users++;
129
130 endutxent();
131#else
132 /* run the command */
133 child_process = spopen(WHO_COMMAND);
134 if (child_process == NULL) {
135 printf(_("Could not open pipe: %s\n"), WHO_COMMAND);
136 return STATE_UNKNOWN;
137 }
138
139 child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r");
140 if (child_stderr == NULL)
141 printf(_("Could not open stderr for %s\n"), WHO_COMMAND);
142 81
143 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 82 if (tmp.errorcode == ERROR) {
144 /* increment 'users' on all lines except total user count */ 83 usage4(_("Could not parse arguments"));
145 if (input_buffer[0] != '#') {
146 users++;
147 continue;
148 }
149
150 /* get total logged in users */
151 if (sscanf(input_buffer, _("# users=%d"), &users) == 1)
152 break;
153 } 84 }
154 85
155 /* check STDERR */ 86 check_users_config config = tmp.config;
156 if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
157 result = possibly_set(result, STATE_UNKNOWN);
158 (void)fclose(child_stderr);
159 87
160 /* close the pipe */ 88#ifdef _WIN32
161 if (spclose(child_process)) 89# if HAVE_WTSAPI32_H
162 result = possibly_set(result, STATE_UNKNOWN); 90 get_num_of_users_wrapper user_wrapper = get_num_of_users_windows();
163#endif 91# else
164#ifdef HAVE_LIBSYSTEMD 92# error Did not find WTSAPI32
93# endif // HAVE_WTSAPI32_H
94#else
95# ifdef HAVE_LIBSYSTEMD
96 get_num_of_users_wrapper user_wrapper = get_num_of_users_systemd();
97# elif HAVE_UTMPX_H
98 get_num_of_users_wrapper user_wrapper = get_num_of_users_utmp();
99# else // !HAVE_LIBSYSTEMD && !HAVE_UTMPX_H
100 get_num_of_users_wrapper user_wrapper = get_num_of_users_who_command();
101# endif // HAVE_LIBSYSTEMD
102#endif // _WIN32
103
104 mp_check overall = mp_check_init();
105 mp_subcheck sc_users = mp_subcheck_init();
106
107 if (user_wrapper.errorcode != 0) {
108 sc_users = mp_set_subcheck_state(sc_users, STATE_UNKNOWN);
109 sc_users.output = "Failed to retrieve number of users";
110 mp_add_subcheck_to_check(&overall, sc_users);
111 mp_exit(overall);
165 } 112 }
166#endif
167
168 /* check the user count against warning and critical thresholds */ 113 /* check the user count against warning and critical thresholds */
169 result = get_status((double)users, thlds);
170 114
171 if (result == STATE_UNKNOWN) 115 mp_perfdata users_pd = {
172 printf("%s\n", _("Unable to read output")); 116 .label = "users",
173 else { 117 .value = mp_create_pd_value(user_wrapper.users),
174 printf(_("USERS %s - %d users currently logged in |%s\n"), state_text(result), users, 118 };
175 sperfdata_int("users", users, "", warning_range, critical_range, true, 0, false, 0)); 119
120 users_pd = mp_pd_set_thresholds(users_pd, config.thresholds);
121 mp_add_perfdata_to_subcheck(&sc_users, users_pd);
122
123 int tmp_status = mp_get_pd_status(users_pd);
124 sc_users = mp_set_subcheck_state(sc_users, tmp_status);
125
126 switch (tmp_status) {
127 case STATE_WARNING:
128 xasprintf(&sc_users.output, "%d users currently logged in. This violates the warning threshold", user_wrapper.users);
129 break;
130 case STATE_CRITICAL:
131 xasprintf(&sc_users.output, "%d users currently logged in. This violates the critical threshold", user_wrapper.users);
132 break;
133 default:
134 xasprintf(&sc_users.output, "%d users currently logged in", user_wrapper.users);
176 } 135 }
177 136
178 return result; 137 mp_add_subcheck_to_check(&overall, sc_users);
138 mp_exit(overall);
179} 139}
180 140
141#define output_format_index CHAR_MAX + 1
142
181/* process command-line arguments */ 143/* process command-line arguments */
182int process_arguments(int argc, char **argv) { 144process_argument_wrapper process_arguments(int argc, char **argv) {
183 static struct option longopts[] = {{"critical", required_argument, 0, 'c'}, 145 static struct option longopts[] = {{"critical", required_argument, 0, 'c'},
184 {"warning", required_argument, 0, 'w'}, 146 {"warning", required_argument, 0, 'w'},
185 {"version", no_argument, 0, 'V'}, 147 {"version", no_argument, 0, 'V'},
186 {"help", no_argument, 0, 'h'}, 148 {"help", no_argument, 0, 'h'},
149 {"output-format", required_argument, 0, output_format_index},
187 {0, 0, 0, 0}}; 150 {0, 0, 0, 0}};
188 151
189 if (argc < 2) 152 if (argc < 2) {
190 usage("\n"); 153 usage("\n");
154 }
155
156 char *warning_range = NULL;
157 char *critical_range = NULL;
158 check_users_config config = check_users_config_init();
191 159
192 int option_char;
193 while (true) { 160 while (true) {
194 int option = 0; 161 int counter = getopt_long(argc, argv, "+hVvc:w:", longopts, NULL);
195 option_char = getopt_long(argc, argv, "+hVvc:w:", longopts, &option);
196 162
197 if (option_char == -1 || option_char == EOF || option_char == 1) 163 if (counter == -1 || counter == EOF || counter == 1) {
198 break; 164 break;
165 }
199 166
200 switch (option_char) { 167 switch (counter) {
168 case output_format_index: {
169 parsed_output_format parser = mp_parse_output_format(optarg);
170 if (!parser.parsing_success) {
171 // TODO List all available formats here, maybe add anothoer usage function
172 printf("Invalid output format: %s\n", optarg);
173 exit(STATE_UNKNOWN);
174 }
175
176 config.output_format_is_set = true;
177 config.output_format = parser.output_format;
178 break;
179 }
201 case '?': /* print short usage statement if args not parsable */ 180 case '?': /* print short usage statement if args not parsable */
202 usage5(); 181 usage5();
203 case 'h': /* help */ 182 case 'h': /* help */
@@ -215,26 +194,35 @@ int process_arguments(int argc, char **argv) {
215 } 194 }
216 } 195 }
217 196
218 option_char = optind; 197 // TODO add proper verification for ranges here!
219 198 if (warning_range) {
220 if (warning_range == NULL && argc > option_char) 199 mp_range_parsed tmp = mp_parse_range_string(warning_range);
221 warning_range = argv[option_char++]; 200 if (tmp.error == MP_PARSING_SUCCES) {
222 201 config.thresholds.warning = tmp.range;
223 if (critical_range == NULL && argc > option_char) 202 config.thresholds.warning_is_set = true;
224 critical_range = argv[option_char++]; 203 } else {
225 204 printf("Failed to parse warning range: %s", warning_range);
226 /* this will abort in case of invalid ranges */ 205 exit(STATE_UNKNOWN);
227 set_thresholds(&thlds, warning_range, critical_range); 206 }
228
229 if (!thlds->warning) {
230 usage4(_("Warning threshold must be a valid range expression"));
231 } 207 }
232 208
233 if (!thlds->critical) { 209 if (critical_range) {
234 usage4(_("Critical threshold must be a valid range expression")); 210 mp_range_parsed tmp = mp_parse_range_string(critical_range);
211 if (tmp.error == MP_PARSING_SUCCES) {
212 config.thresholds.critical = tmp.range;
213 config.thresholds.critical_is_set = true;
214 } else {
215 printf("Failed to parse critical range: %s", critical_range);
216 exit(STATE_UNKNOWN);
217 }
235 } 218 }
236 219
237 return OK; 220 process_argument_wrapper result = {
221 .errorcode = OK,
222 .config = config,
223 };
224
225 return result;
238} 226}
239 227
240void print_help(void) { 228void print_help(void) {
@@ -257,6 +245,7 @@ void print_help(void) {
257 printf(" %s\n", _("Set WARNING status if number of logged in users violates RANGE_EXPRESSION")); 245 printf(" %s\n", _("Set WARNING status if number of logged in users violates RANGE_EXPRESSION"));
258 printf(" %s\n", "-c, --critical=RANGE_EXPRESSION"); 246 printf(" %s\n", "-c, --critical=RANGE_EXPRESSION");
259 printf(" %s\n", _("Set CRITICAL status if number of logged in users violates RANGE_EXPRESSION")); 247 printf(" %s\n", _("Set CRITICAL status if number of logged in users violates RANGE_EXPRESSION"));
248 printf(UT_OUTPUT_FORMAT);
260 249
261 printf(UT_SUPPORT); 250 printf(UT_SUPPORT);
262} 251}