| 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 |
|
| 27 |
#include "common.h" |
| 28 |
#include "messages.h" |
| 29 |
#include "pressure.h" |
| 30 |
#include "progname.h" |
| 31 |
#include "progversion.h" |
| 32 |
#include "system.h" |
| 33 |
#include "thresholds.h" |
| 34 |
#include "xasprintf.h" |
| 35 |
#include "xstrton.h" |
| 36 |
|
| 37 |
static const char *program_copyright = |
| 38 |
"Copyright (C) 2020 Davide Madrisan <" PACKAGE_BUGREPORT ">\n"; |
| 39 |
|
| 40 |
static struct option const longopts[] = { |
| 41 |
{(char *) "cpu", no_argument, NULL, 'C'}, |
| 42 |
{(char *) "full", no_argument, NULL, 'f'}, |
| 43 |
{(char *) "io", no_argument, NULL, 'i'}, |
| 44 |
{(char *) "memory", no_argument, NULL, 'm'}, |
| 45 |
{(char *) "critical", required_argument, NULL, 'c'}, |
| 46 |
{(char *) "warning", required_argument, NULL, 'w'}, |
| 47 |
{(char *) "verbose", no_argument, NULL, 'v'}, |
| 48 |
{(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR}, |
| 49 |
{(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR}, |
| 50 |
{NULL, 0, NULL, 0} |
| 51 |
}; |
| 52 |
|
| 53 |
static _Noreturn void |
| 54 |
usage (FILE * out) |
| 55 |
{ |
| 56 |
fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version); |
| 57 |
fputs ("This plugin checks Linux Pressure Stall Information (PSI) data.\n", out); |
| 58 |
fputs (program_copyright, out); |
| 59 |
fputs (USAGE_HEADER, out); |
| 60 |
fprintf (out, " %s --cpu [-w COUNTER] [-c COUNTER] [delay]\n", program_name); |
| 61 |
fprintf (out, " %s --io [--full] [-w COUNTER] [-c COUNTER] [delay]\n", |
| 62 |
program_name); |
| 63 |
fprintf (out, " %s --memory [--full] [-w COUNTER] [-c COUNTER] [delay]\n", |
| 64 |
program_name); |
| 65 |
fputs (USAGE_OPTIONS, out); |
| 66 |
fputs (" -C, --cpu return the cpu pressure metrics\n", out); |
| 67 |
fputs (" -i, --io return the io (block layer/filesystems) pressure " |
| 68 |
"metrics\n", out); |
| 69 |
fputs (" -m, --memory return the memory pressure metrics\n", out); |
| 70 |
fputs (" -f, --full select the data labeled \"full\" to calculate " |
| 71 |
"thresholds\n", out); |
| 72 |
fputs (" instead of the one with \"some\" " |
| 73 |
"(io and memory only)\n", out); |
| 74 |
fputs (" -w, --warning COUNTER warning threshold (in microseconds/s)\n", |
| 75 |
out); |
| 76 |
fputs (" -c, --critical COUNTER critical threshold (in microseconds/s)\n", |
| 77 |
out); |
| 78 |
fputs (USAGE_HELP, out); |
| 79 |
fputs (USAGE_VERSION, out); |
| 80 |
fprintf (out, " \"delay\" is the delay in seconds between two proc reads " |
| 81 |
"(default: %dsec)\n", DELAY_DEFAULT); |
| 82 |
fputs (USAGE_NOTE, out); |
| 83 |
fputs (" It requires at least a kernel 4.20, which provides this " |
| 84 |
"information in the\n" |
| 85 |
" /proc/pressure folder.\n", out); |
| 86 |
fputs (USAGE_EXAMPLES, out); |
| 87 |
fprintf (out, " %s --cpu\n", program_name); |
| 88 |
fprintf (out, " %s --io\n", program_name); |
| 89 |
fprintf (out, " %s --memory --full 100 2\n", program_name); |
| 90 |
exit (out == stderr ? STATE_UNKNOWN : STATE_OK); |
| 91 |
} |
| 92 |
|
| 93 |
static _Noreturn void |
| 94 |
print_version (void) |
| 95 |
{ |
| 96 |
printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version); |
| 97 |
fputs (program_copyright, stdout); |
| 98 |
fputs (GPLv3_DISCLAIMER, stdout); |
| 99 |
|
| 100 |
exit (STATE_OK); |
| 101 |
} |
| 102 |
|
| 103 |
int |
| 104 |
main (int argc, char **argv) |
| 105 |
{ |
| 106 |
bool threshold_full = false; |
| 107 |
int c; |
| 108 |
char *critical = NULL, *warning = NULL, |
| 109 |
*status_msg, *perfdata_mem_msg, *prefix = NULL; |
| 110 |
enum linux_psi_id pressure_mode = LINUX_PSI_NONE; |
| 111 |
unsigned long delay; |
| 112 |
unsigned long long starvation[2]; |
| 113 |
struct proc_psi_oneline *psi_cpu = NULL; |
| 114 |
struct proc_psi_twolines *psi = NULL; |
| 115 |
nagstatus status = STATE_OK; |
| 116 |
thresholds *my_threshold = NULL; |
| 117 |
int (*proc_psi_read) (struct proc_psi_twolines **, |
| 118 |
unsigned long long *, unsigned long) = NULL; |
| 119 |
|
| 120 |
set_program_name (argv[0]); |
| 121 |
|
| 122 |
while ((c = getopt_long (argc, argv, |
| 123 |
"Cimfc:w:v" GETOPT_HELP_VERSION_STRING, |
| 124 |
longopts, NULL)) != -1) |
| 125 |
{ |
| 126 |
switch (c) |
| 127 |
{ |
| 128 |
default: |
| 129 |
usage (stderr); |
| 130 |
case 'C': |
| 131 |
pressure_mode = LINUX_PSI_CPU; |
| 132 |
break; |
| 133 |
case 'c': |
| 134 |
critical = optarg; |
| 135 |
break; |
| 136 |
case 'f': |
| 137 |
threshold_full = true; |
| 138 |
break; |
| 139 |
case 'i': |
| 140 |
prefix = "io"; |
| 141 |
pressure_mode = LINUX_PSI_IO; |
| 142 |
proc_psi_read = &proc_psi_read_io; |
| 143 |
break; |
| 144 |
case 'm': |
| 145 |
prefix = "mem"; |
| 146 |
pressure_mode = LINUX_PSI_MEMORY; |
| 147 |
proc_psi_read = &proc_psi_read_memory; |
| 148 |
break; |
| 149 |
case 'w': |
| 150 |
warning = optarg; |
| 151 |
break; |
| 152 |
|
| 153 |
case_GETOPT_HELP_CHAR |
| 154 |
case_GETOPT_VERSION_CHAR |
| 155 |
|
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if ((LINUX_PSI_NONE == pressure_mode) || |
| 160 |
(LINUX_PSI_CPU == pressure_mode && threshold_full)) |
| 161 |
usage (stderr); |
| 162 |
|
| 163 |
status = set_thresholds (&my_threshold, warning, critical); |
| 164 |
if (status == NP_RANGE_UNPARSEABLE) |
| 165 |
usage (stderr); |
| 166 |
|
| 167 |
delay = DELAY_DEFAULT; |
| 168 |
if (optind < argc) |
| 169 |
{ |
| 170 |
delay = strtol_or_err (argv[optind++], "failed to parse argument"); |
| 171 |
|
| 172 |
if (delay < 1) |
| 173 |
plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer"); |
| 174 |
else if (DELAY_MAX < delay) |
| 175 |
plugin_error (STATE_UNKNOWN, 0, |
| 176 |
"too large delay value (greater than %d)", DELAY_MAX); |
| 177 |
} |
| 178 |
|
| 179 |
switch (pressure_mode) |
| 180 |
{ |
| 181 |
default: |
| 182 |
usage (stderr); |
| 183 |
case LINUX_PSI_CPU: |
| 184 |
proc_psi_read_cpu (&psi_cpu, &starvation[0], delay); |
| 185 |
|
| 186 |
status = get_status (starvation[0], my_threshold); |
| 187 |
|
| 188 |
status_msg = |
| 189 |
xasprintf ("%s (CPU starvation) %s: %llu microsecs/s", |
| 190 |
program_name_short, state_text (status), starvation[0]); |
| 191 |
perfdata_mem_msg = |
| 192 |
xasprintf ("cpu_avg10=%2.2lf%% cpu_avg60=%2.2lf%% cpu_avg300=%2.2lf%% " |
| 193 |
"cpu_starvation/s=%llu" |
| 194 |
, psi_cpu->avg10 |
| 195 |
, psi_cpu->avg60 |
| 196 |
, psi_cpu->avg300 |
| 197 |
, starvation[0]); |
| 198 |
break; |
| 199 |
case LINUX_PSI_IO: |
| 200 |
case LINUX_PSI_MEMORY: |
| 201 |
proc_psi_read (&psi, &starvation[0], delay); |
| 202 |
status = get_status (threshold_full ? starvation[1] : starvation[0], |
| 203 |
my_threshold); |
| 204 |
status_msg = |
| 205 |
xasprintf ("%s (%s starvation) %s: some:%llu full:%llu microsecs/s" |
| 206 |
, program_name_short |
| 207 |
, pressure_mode == LINUX_PSI_IO ? "IO" : "Memory" |
| 208 |
, state_text (status) |
| 209 |
, starvation[0], starvation[1]); |
| 210 |
perfdata_mem_msg = |
| 211 |
xasprintf ("%s_some_avg10=%2.2lf%% %s_some_avg60=%2.2lf%% " |
| 212 |
"%s_some_avg300=%2.2lf%% %s_some_starvation/s=%llu " |
| 213 |
"%s_full_avg10=%2.2lf%% %s_full_avg60=%2.2lf%% " |
| 214 |
"%s_full_avg300=%2.2lf%% %s_full_starvation/s=%llu " |
| 215 |
, prefix, psi->some_avg10 |
| 216 |
, prefix, psi->some_avg60 |
| 217 |
, prefix, psi->some_avg300 |
| 218 |
, prefix, starvation[0] |
| 219 |
, prefix, psi->full_avg10 |
| 220 |
, prefix, psi->full_avg60 |
| 221 |
, prefix, psi->full_avg300 |
| 222 |
, prefix, starvation[1]); |
| 223 |
break; |
| 224 |
} |
| 225 |
|
| 226 |
printf ("%s | %s\n", status_msg, perfdata_mem_msg); |
| 227 |
free (my_threshold); |
| 228 |
|
| 229 |
return status; |
| 230 |
} |