diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-12 21:34:58 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-12 21:34:58 +0100 |
commit | 39b9b62adda3887e7706754d4e34a7eff2f793b5 (patch) | |
tree | 750eb6aa290ccae34f0db478bf052674cfd44005 | |
parent | 84b823ae4ffeebc34aebf485774552b92244b62a (diff) | |
download | monitoring-plugins-39b9b62a.tar.gz |
check_users: clang-format
-rw-r--r-- | plugins/check_users.c | 50 |
1 files changed, 32 insertions, 18 deletions
diff --git a/plugins/check_users.c b/plugins/check_users.c index f1e1c39d..e91ed4a0 100644 --- a/plugins/check_users.c +++ b/plugins/check_users.c | |||
@@ -83,15 +83,16 @@ int main(int argc, char **argv) { | |||
83 | /* Parse extra opts if any */ | 83 | /* Parse extra opts if any */ |
84 | argv = np_extra_opts(&argc, argv, progname); | 84 | argv = np_extra_opts(&argc, argv, progname); |
85 | 85 | ||
86 | if (process_arguments(argc, argv) == ERROR) | 86 | if (process_arguments(argc, argv) == ERROR) { |
87 | usage4(_("Could not parse arguments")); | 87 | usage4(_("Could not parse arguments")); |
88 | } | ||
88 | 89 | ||
89 | users = 0; | 90 | users = 0; |
90 | 91 | ||
91 | #ifdef HAVE_LIBSYSTEMD | 92 | #ifdef HAVE_LIBSYSTEMD |
92 | if (sd_booted() > 0) | 93 | if (sd_booted() > 0) { |
93 | users = sd_get_sessions(NULL); | 94 | users = sd_get_sessions(NULL); |
94 | else { | 95 | } else { |
95 | #endif | 96 | #endif |
96 | #if HAVE_WTSAPI32_H | 97 | #if HAVE_WTSAPI32_H |
97 | if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &wtsinfo, &wtscount)) { | 98 | if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &wtsinfo, &wtscount)) { |
@@ -104,18 +105,21 @@ int main(int argc, char **argv) { | |||
104 | DWORD size; | 105 | DWORD size; |
105 | int len; | 106 | int len; |
106 | 107 | ||
107 | if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, wtsinfo[index].SessionId, WTSUserName, &username, &size)) | 108 | if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, wtsinfo[index].SessionId, WTSUserName, &username, &size)) { |
108 | continue; | 109 | continue; |
110 | } | ||
109 | 111 | ||
110 | len = lstrlen(username); | 112 | len = lstrlen(username); |
111 | 113 | ||
112 | WTSFreeMemory(username); | 114 | WTSFreeMemory(username); |
113 | 115 | ||
114 | if (len == 0) | 116 | if (len == 0) { |
115 | continue; | 117 | continue; |
118 | } | ||
116 | 119 | ||
117 | if (wtsinfo[index].State == WTSActive || wtsinfo[index].State == WTSDisconnected) | 120 | if (wtsinfo[index].State == WTSActive || wtsinfo[index].State == WTSDisconnected) { |
118 | users++; | 121 | users++; |
122 | } | ||
119 | } | 123 | } |
120 | 124 | ||
121 | WTSFreeMemory(wtsinfo); | 125 | WTSFreeMemory(wtsinfo); |
@@ -123,9 +127,11 @@ int main(int argc, char **argv) { | |||
123 | /* get currently logged users from utmpx */ | 127 | /* get currently logged users from utmpx */ |
124 | setutxent(); | 128 | setutxent(); |
125 | 129 | ||
126 | while ((putmpx = getutxent()) != NULL) | 130 | while ((putmpx = getutxent()) != NULL) { |
127 | if (putmpx->ut_type == USER_PROCESS) | 131 | if (putmpx->ut_type == USER_PROCESS) { |
128 | users++; | 132 | users++; |
133 | } | ||
134 | } | ||
129 | 135 | ||
130 | endutxent(); | 136 | endutxent(); |
131 | #else | 137 | #else |
@@ -137,8 +143,9 @@ int main(int argc, char **argv) { | |||
137 | } | 143 | } |
138 | 144 | ||
139 | child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); | 145 | child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); |
140 | if (child_stderr == NULL) | 146 | if (child_stderr == NULL) { |
141 | printf(_("Could not open stderr for %s\n"), WHO_COMMAND); | 147 | printf(_("Could not open stderr for %s\n"), WHO_COMMAND); |
148 | } | ||
142 | 149 | ||
143 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | 150 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { |
144 | /* increment 'users' on all lines except total user count */ | 151 | /* increment 'users' on all lines except total user count */ |
@@ -148,18 +155,21 @@ int main(int argc, char **argv) { | |||
148 | } | 155 | } |
149 | 156 | ||
150 | /* get total logged in users */ | 157 | /* get total logged in users */ |
151 | if (sscanf(input_buffer, _("# users=%d"), &users) == 1) | 158 | if (sscanf(input_buffer, _("# users=%d"), &users) == 1) { |
152 | break; | 159 | break; |
160 | } | ||
153 | } | 161 | } |
154 | 162 | ||
155 | /* check STDERR */ | 163 | /* check STDERR */ |
156 | if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | 164 | if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { |
157 | result = possibly_set(result, STATE_UNKNOWN); | 165 | result = possibly_set(result, STATE_UNKNOWN); |
166 | } | ||
158 | (void)fclose(child_stderr); | 167 | (void)fclose(child_stderr); |
159 | 168 | ||
160 | /* close the pipe */ | 169 | /* close the pipe */ |
161 | if (spclose(child_process)) | 170 | if (spclose(child_process)) { |
162 | result = possibly_set(result, STATE_UNKNOWN); | 171 | result = possibly_set(result, STATE_UNKNOWN); |
172 | } | ||
163 | #endif | 173 | #endif |
164 | #ifdef HAVE_LIBSYSTEMD | 174 | #ifdef HAVE_LIBSYSTEMD |
165 | } | 175 | } |
@@ -168,9 +178,9 @@ int main(int argc, char **argv) { | |||
168 | /* check the user count against warning and critical thresholds */ | 178 | /* check the user count against warning and critical thresholds */ |
169 | result = get_status((double)users, thlds); | 179 | result = get_status((double)users, thlds); |
170 | 180 | ||
171 | if (result == STATE_UNKNOWN) | 181 | if (result == STATE_UNKNOWN) { |
172 | printf("%s\n", _("Unable to read output")); | 182 | printf("%s\n", _("Unable to read output")); |
173 | else { | 183 | } else { |
174 | printf(_("USERS %s - %d users currently logged in |%s\n"), state_text(result), users, | 184 | printf(_("USERS %s - %d users currently logged in |%s\n"), state_text(result), users, |
175 | sperfdata_int("users", users, "", warning_range, critical_range, true, 0, false, 0)); | 185 | sperfdata_int("users", users, "", warning_range, critical_range, true, 0, false, 0)); |
176 | } | 186 | } |
@@ -186,16 +196,18 @@ int process_arguments(int argc, char **argv) { | |||
186 | {"help", no_argument, 0, 'h'}, | 196 | {"help", no_argument, 0, 'h'}, |
187 | {0, 0, 0, 0}}; | 197 | {0, 0, 0, 0}}; |
188 | 198 | ||
189 | if (argc < 2) | 199 | if (argc < 2) { |
190 | usage("\n"); | 200 | usage("\n"); |
201 | } | ||
191 | 202 | ||
192 | int option_char; | 203 | int option_char; |
193 | while (true) { | 204 | while (true) { |
194 | int option = 0; | 205 | int option = 0; |
195 | option_char = getopt_long(argc, argv, "+hVvc:w:", longopts, &option); | 206 | option_char = getopt_long(argc, argv, "+hVvc:w:", longopts, &option); |
196 | 207 | ||
197 | if (option_char == -1 || option_char == EOF || option_char == 1) | 208 | if (option_char == -1 || option_char == EOF || option_char == 1) { |
198 | break; | 209 | break; |
210 | } | ||
199 | 211 | ||
200 | switch (option_char) { | 212 | switch (option_char) { |
201 | case '?': /* print short usage statement if args not parsable */ | 213 | case '?': /* print short usage statement if args not parsable */ |
@@ -217,11 +229,13 @@ int process_arguments(int argc, char **argv) { | |||
217 | 229 | ||
218 | option_char = optind; | 230 | option_char = optind; |
219 | 231 | ||
220 | if (warning_range == NULL && argc > option_char) | 232 | if (warning_range == NULL && argc > option_char) { |
221 | warning_range = argv[option_char++]; | 233 | warning_range = argv[option_char++]; |
234 | } | ||
222 | 235 | ||
223 | if (critical_range == NULL && argc > option_char) | 236 | if (critical_range == NULL && argc > option_char) { |
224 | critical_range = argv[option_char++]; | 237 | critical_range = argv[option_char++]; |
238 | } | ||
225 | 239 | ||
226 | /* this will abort in case of invalid ranges */ | 240 | /* this will abort in case of invalid ranges */ |
227 | set_thresholds(&thlds, warning_range, critical_range); | 241 | set_thresholds(&thlds, warning_range, critical_range); |