Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/plugins/check_users.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014,2015,2023 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin that ckecks for the number of logged on users.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include <getopt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #if !defined(WITH_SYSTEMD)
28 #include <utmpx.h>
29 #else
30 #include <systemd/sd-login.h>
31 #endif
32
33 #include "common.h"
34 #include "messages.h"
35 #include "progname.h"
36 #include "progversion.h"
37 #include "thresholds.h"
38
39 static const char *program_copyright =
40 "Copyright (C) 2014,2015,2023 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
41
42 static struct option const longopts[] = {
43 {(char *) "critical", required_argument, NULL, 'c'},
44 {(char *) "warning", required_argument, NULL, 'w'},
45 {(char *) "verbose", no_argument, NULL, 'v'},
46 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
47 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
48 {NULL, 0, NULL, 0}
49 };
50
51 static _Noreturn void
52 usage (FILE * out)
53 {
54 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
55 fputs ("This plugin displays the number of users "
56 "that are currently logged on.\n", out);
57 fputs (program_copyright, out);
58 fputs (USAGE_HEADER, out);
59 fprintf (out, " %s [-w COUNTER] [-c COUNTER]\n", program_name);
60 fputs (USAGE_OPTIONS, out);
61 fputs (" -w, --warning COUNTER warning threshold\n", out);
62 fputs (" -c, --critical COUNTER critical threshold\n", out);
63 fputs (" -v, --verbose show details for command-line debugging "
64 "(Nagios may truncate output)\n", out);
65 fputs (USAGE_HELP, out);
66 fputs (USAGE_VERSION, out);
67 fputs (USAGE_EXAMPLES, out);
68 fprintf (out, " %s -w 1\n", program_name);
69
70 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
71 }
72
73 static _Noreturn void
74 print_version (void)
75 {
76 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
77 fputs (program_copyright, stdout);
78 fputs (GPLv3_DISCLAIMER, stdout);
79
80 exit (STATE_OK);
81 }
82
83 static int
84 count_users (bool verbose)
85 {
86 int numuser = 0;
87
88 #if defined(WITH_SYSTEMD)
89 /* sd_get_sessions() will not return ENOENT as error, as this only means
90 * that there is no session, not that systemd is not used.
91 * Use the recommended sd_booted() function for this.
92 */
93 if (sd_booted() > 0)
94 return sd_get_sessions(NULL);
95 #endif
96
97 struct utmpx *ut;
98
99 if (verbose)
100 printf ("user PID line host date/time\n");
101 setutxent ();
102 while ((ut = getutxent ()))
103 {
104 if ((ut->ut_type == USER_PROCESS) && (ut->ut_user[0] != '\0'))
105 {
106 numuser++;
107 if (!verbose)
108 continue;
109 char buf[26];
110 time_t timetmp = ut->ut_tv.tv_sec;
111 printf ("%-8s %6ld %-6.6s %-9.9s %s", ut->ut_user,
112 (long) ut->ut_pid, ut->ut_line, ut->ut_host,
113 ctime_r (&timetmp, buf));
114 }
115 }
116 endutxent ();
117
118 return numuser;
119 }
120
121 int
122 main (int argc, char **argv)
123 {
124 int c, numuser;
125 bool verbose = false;
126 char *critical = NULL, *warning = NULL;
127 nagstatus status = STATE_OK;
128 thresholds *my_threshold = NULL;
129
130 set_program_name (argv[0]);
131
132 while ((c = getopt_long (argc, argv,
133 "c:w:v" GETOPT_HELP_VERSION_STRING,
134 longopts, NULL)) != -1)
135 {
136 switch (c)
137 {
138 default:
139 usage (stderr);
140 case 'c':
141 critical = optarg;
142 break;
143 case 'w':
144 warning = optarg;
145 break;
146 case 'v':
147 verbose = true;
148 break;
149
150 case_GETOPT_HELP_CHAR
151 case_GETOPT_VERSION_CHAR
152
153 }
154 }
155
156 status = set_thresholds (&my_threshold, warning, critical);
157 if (status == NP_RANGE_UNPARSEABLE)
158 usage (stderr);
159
160 numuser = count_users (verbose);
161 if (numuser < 0)
162 plugin_error (STATE_UNKNOWN, numuser,
163 "cannot determine all current login sessions");
164
165 status = get_status (numuser, my_threshold);
166 free (my_threshold);
167
168 printf ("%s %s - %d user%s logged on | logged_users=%d\n",
169 program_name_short, state_text (status), numuser,
170 (numuser == 1 ) ? "" : "s", numuser);
171
172 return status;
173 }