| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 90 |
|
| 91 |
|
| 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 |
} |