| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <getopt.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
|
| 26 |
#include "common.h" |
| 27 |
#include "cputopology.h" |
| 28 |
#include "messages.h" |
| 29 |
#include "progname.h" |
| 30 |
#include "progversion.h" |
| 31 |
#include "sysfsparser.h" |
| 32 |
#include "system.h" |
| 33 |
#include "xasprintf.h" |
| 34 |
|
| 35 |
static const char *program_copyright = |
| 36 |
"Copyright (C) 2014,2015,2017,2022 Davide Madrisan <" PACKAGE_BUGREPORT ">\n"; |
| 37 |
|
| 38 |
static struct option const longopts[] = { |
| 39 |
{(char *) "load1", required_argument, NULL, '1'}, |
| 40 |
{(char *) "load5", required_argument, NULL, '5'}, |
| 41 |
{(char *) "load15", required_argument, NULL, 'L'}, |
| 42 |
{(char *) "percpu", no_argument, NULL, 'r'}, |
| 43 |
{(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR}, |
| 44 |
{(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR}, |
| 45 |
{NULL, 0, NULL, 0} |
| 46 |
}; |
| 47 |
|
| 48 |
static _Noreturn void |
| 49 |
usage (FILE * out) |
| 50 |
{ |
| 51 |
fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version); |
| 52 |
fputs ("This plugin checks the current system load average.\n", out); |
| 53 |
fputs (program_copyright, out); |
| 54 |
fputs (USAGE_HEADER, out); |
| 55 |
fprintf (out, " %s [-r] [--load1=w,c] [--load5=w,c] [--load15=w,c]\n", |
| 56 |
program_name); |
| 57 |
fputs (USAGE_OPTIONS, out); |
| 58 |
fputs (" -r, --percpu divide the load averages by the number of CPUs\n", |
| 59 |
out); |
| 60 |
fputs (" -1, --load1=WLOAD1,CLOAD1" |
| 61 |
" warning and critical thresholds for load1\n", out); |
| 62 |
fputs (" -5, --load5=WLOAD5,CLOAD5" |
| 63 |
" warning and critical thresholds for load5\n", out); |
| 64 |
fputs (" -L, --load15=WLOAD15,CLOAD15" |
| 65 |
" warning and critical thresholds for load15\n", out); |
| 66 |
fputs (USAGE_HELP, out); |
| 67 |
fputs (USAGE_VERSION, out); |
| 68 |
fputs (USAGE_EXAMPLES, out); |
| 69 |
fprintf (out, " %s -r --load1=2,3 --load15=1.5,2.5\n", program_name); |
| 70 |
|
| 71 |
exit (out == stderr ? STATE_UNKNOWN : STATE_OK); |
| 72 |
} |
| 73 |
|
| 74 |
static _Noreturn void |
| 75 |
print_version (void) |
| 76 |
{ |
| 77 |
printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version); |
| 78 |
fputs (program_copyright, stdout); |
| 79 |
fputs (GPLv3_DISCLAIMER, stdout); |
| 80 |
|
| 81 |
exit (STATE_OK); |
| 82 |
} |
| 83 |
|
| 84 |
static void |
| 85 |
validate_input (int i, double w, double c) |
| 86 |
{ |
| 87 |
if (i != 2 || (w >= c)) |
| 88 |
plugin_error (STATE_UNKNOWN, 0, "command line error: bad thresholds"); |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
normalize_loadavg (double *loadavg, int numcpus) |
| 93 |
{ |
| 94 |
int i; |
| 95 |
|
| 96 |
if (!numcpus) |
| 97 |
numcpus = get_processor_number_online (); |
| 98 |
|
| 99 |
for (i = 0; i < 3; i++) |
| 100 |
{ |
| 101 |
if (numcpus > 1) |
| 102 |
loadavg[i] /= numcpus; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
static int |
| 107 |
loadavg_status (const double *loadavg, const double *wload, const double *cload, |
| 108 |
const bool *required) |
| 109 |
{ |
| 110 |
int i, status = STATE_OK; |
| 111 |
|
| 112 |
for (i = 0; i < 3; i++) |
| 113 |
{ |
| 114 |
if (required[i] == false) |
| 115 |
continue; |
| 116 |
if (loadavg[i] > cload[i]) |
| 117 |
{ |
| 118 |
status = STATE_CRITICAL; |
| 119 |
break; |
| 120 |
} |
| 121 |
else if (loadavg[i] > wload[i]) |
| 122 |
status = STATE_WARNING; |
| 123 |
} |
| 124 |
|
| 125 |
return status; |
| 126 |
} |
| 127 |
|
| 128 |
#ifndef NPL_TESTING |
| 129 |
int |
| 130 |
main (int argc, char **argv) |
| 131 |
{ |
| 132 |
int c, i, status = STATE_OK; |
| 133 |
const unsigned int lamin[3] = { 1, 5, 15 }; |
| 134 |
bool required[3] = { false, false, false }; |
| 135 |
bool normalize = false; |
| 136 |
double loadavg[3]; |
| 137 |
double wload[3] = { 0.0, 0.0, 0.0 }; |
| 138 |
double cload[3] = { 0.0, 0.0, 0.0 }; |
| 139 |
char *status_msg, *perfdata_msg; |
| 140 |
|
| 141 |
set_program_name (argv[0]); |
| 142 |
|
| 143 |
while ((c = getopt_long (argc, argv, "1:5:L:r" GETOPT_HELP_VERSION_STRING, |
| 144 |
longopts, NULL)) != -1) |
| 145 |
{ |
| 146 |
switch (c) |
| 147 |
{ |
| 148 |
default: |
| 149 |
usage (stderr); |
| 150 |
case '1': |
| 151 |
i = sscanf (optarg, "%lf,%lf", &wload[0], &cload[0]); |
| 152 |
validate_input (i, wload[0], cload[0]); |
| 153 |
required[0] = true; |
| 154 |
break; |
| 155 |
case '5': |
| 156 |
i = sscanf (optarg, "%lf,%lf", &wload[1], &cload[1]); |
| 157 |
validate_input (i, wload[1], cload[1]); |
| 158 |
required[1] = true; |
| 159 |
break; |
| 160 |
case 'L': |
| 161 |
i = sscanf (optarg, "%lf,%lf", &wload[2], &cload[2]); |
| 162 |
validate_input (i, wload[2], cload[2]); |
| 163 |
required[2] = true; |
| 164 |
break; |
| 165 |
case 'r': |
| 166 |
normalize = true; |
| 167 |
break; |
| 168 |
|
| 169 |
case_GETOPT_HELP_CHAR |
| 170 |
case_GETOPT_VERSION_CHAR |
| 171 |
|
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
sysfsparser_check_for_sysfs (); |
| 176 |
|
| 177 |
if (getloadavg (&loadavg[0], 3) != 3) |
| 178 |
plugin_error (STATE_UNKNOWN, 0, |
| 179 |
"the system load average was unobtainable"); |
| 180 |
if (normalize) |
| 181 |
normalize_loadavg (loadavg, 0); |
| 182 |
status = loadavg_status (loadavg, wload, cload, required); |
| 183 |
|
| 184 |
status_msg = |
| 185 |
xasprintf ("%s - average: %.2lf, %.2lf, %.2lf", |
| 186 |
state_text (status), loadavg[0], loadavg[1], loadavg[2]); |
| 187 |
|
| 188 |
|
| 189 |
perfdata_msg = |
| 190 |
xasprintf ("load%u=%.3lf;%.3lf;%.3lf;0 " |
| 191 |
"load%u=%.3lf;%.3lf;%.3lf;0 " |
| 192 |
"load%u=%.3lf;%.3lf;%.3lf;0" |
| 193 |
, lamin[0], loadavg[0], wload[0], cload[0] |
| 194 |
, lamin[1], loadavg[1], wload[1], cload[1] |
| 195 |
, lamin[2], loadavg[2], wload[2], cload[2]); |
| 196 |
|
| 197 |
printf ("%s %s | %s\n", program_name_short, status_msg, perfdata_msg); |
| 198 |
|
| 199 |
return status; |
| 200 |
} |
| 201 |
#endif |