| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef _GNU_SOURCE |
| 22 |
# define _GNU_SOURCE |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <sys/stat.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <fcntl.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#include <unistd.h> |
| 33 |
|
| 34 |
#include "common.h" |
| 35 |
#include "cpustats.h" |
| 36 |
#include "getenv.h" |
| 37 |
#include "logging.h" |
| 38 |
#include "messages.h" |
| 39 |
#include "procparser.h" |
| 40 |
#include "system.h" |
| 41 |
#include "xalloc.h" |
| 42 |
#include "xasprintf.h" |
| 43 |
|
| 44 |
const char * |
| 45 |
get_path_proc_stat () |
| 46 |
{ |
| 47 |
const char *env_procstat = secure_getenv ("NPL_TEST_PATH_PROCSTAT"); |
| 48 |
if (env_procstat) |
| 49 |
return env_procstat; |
| 50 |
|
| 51 |
return "/proc/stat"; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
void |
| 58 |
cpu_stats_get_time (struct cpu_time * __restrict cputime, unsigned int lines) |
| 59 |
{ |
| 60 |
FILE *fp; |
| 61 |
size_t len = 0; |
| 62 |
ssize_t chread; |
| 63 |
char *line = NULL; |
| 64 |
bool found; |
| 65 |
const char *procpath = get_path_proc_stat (); |
| 66 |
|
| 67 |
if ((fp = fopen (procpath, "r")) == NULL) |
| 68 |
plugin_error (STATE_UNKNOWN, errno, "error opening %s", procpath); |
| 69 |
|
| 70 |
memset (cputime, '\0', lines * sizeof (struct cpu_time)); |
| 71 |
|
| 72 |
found = false; |
| 73 |
while ((chread = getline (&line, &len, fp)) != -1) |
| 74 |
{ |
| 75 |
if (!strncmp (line, "cpu ", 4)) |
| 76 |
{ |
| 77 |
cputime->cpuname = xstrdup ("cpu"); |
| 78 |
sscanf (line, |
| 79 |
"cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", |
| 80 |
&cputime->user, &cputime->nice, &cputime->system, |
| 81 |
&cputime->idle, &cputime->iowait, &cputime->irq, |
| 82 |
&cputime->softirq, &cputime->steal, &cputime->guest, |
| 83 |
&cputime->guestn); |
| 84 |
dbg ("line: %s", line); |
| 85 |
dbg (" \\ %s >> [1]user:%llu ... [5]iowait:%llu ...\n", |
| 86 |
cputime->cpuname, cputime->user, cputime->iowait); |
| 87 |
found = true; |
| 88 |
if (lines == 1) |
| 89 |
break; |
| 90 |
} |
| 91 |
else if (!strncmp (line, "cpu", 3)) |
| 92 |
{ |
| 93 |
char *endptr; |
| 94 |
unsigned int cpunum = strtol (line + 3, &endptr, 10); |
| 95 |
if (lines <= cpunum + 1) |
| 96 |
plugin_error (STATE_UNKNOWN, 0, |
| 97 |
"BUG: %s(): lines(%u) <= cpunum(%u) + 1", |
| 98 |
__FUNCTION__, lines, cpunum); |
| 99 |
|
| 100 |
unsigned int i = cpunum + 1; |
| 101 |
cputime[i].cpuname = xasprintf ("cpu%u", cpunum); |
| 102 |
sscanf (endptr, |
| 103 |
"%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", |
| 104 |
&cputime[i].user, &cputime[i].nice, |
| 105 |
&cputime[i].system, &cputime[i].idle, |
| 106 |
&cputime[i].iowait, &cputime[i].irq, |
| 107 |
&cputime[i].softirq, &cputime[i].steal, |
| 108 |
&cputime[i].guest, &cputime[i].guestn); |
| 109 |
dbg ("line: %s", line); |
| 110 |
dbg (" \\ %s >> [1]user:%llu ... [5]iowait:%llu ...\n", |
| 111 |
cputime[i].cpuname, cputime[i].user, cputime[i].iowait); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
free (line); |
| 116 |
fclose (fp); |
| 117 |
|
| 118 |
if (!found) |
| 119 |
plugin_error (STATE_UNKNOWN, 0, |
| 120 |
"%s: pattern not found: 'cpu '", procpath); |
| 121 |
} |
| 122 |
|
| 123 |
static unsigned long long |
| 124 |
cpu_stats_get_value_with_pattern (const char *pattern, bool mandatory) |
| 125 |
{ |
| 126 |
FILE *fp; |
| 127 |
size_t len = 0; |
| 128 |
ssize_t chread; |
| 129 |
char *line = NULL; |
| 130 |
bool found; |
| 131 |
unsigned long long value; |
| 132 |
const char *procpath = get_path_proc_stat (); |
| 133 |
|
| 134 |
if ((fp = fopen (procpath, "r")) == NULL) |
| 135 |
plugin_error (STATE_UNKNOWN, errno, "error opening %s", procpath); |
| 136 |
|
| 137 |
value = 0; |
| 138 |
found = false; |
| 139 |
|
| 140 |
while ((chread = getline (&line, &len, fp)) != -1) |
| 141 |
{ |
| 142 |
if (!strncmp (line, pattern, strlen (pattern))) |
| 143 |
{ |
| 144 |
sscanf (line + strlen (pattern), "%llu", &value); |
| 145 |
dbg ("line: %s \\ value for '%s': %llu\n", line, pattern, value); |
| 146 |
found = true; |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
free (line); |
| 152 |
fclose (fp); |
| 153 |
|
| 154 |
if (!found && mandatory) |
| 155 |
plugin_error (STATE_UNKNOWN, 0, |
| 156 |
"%s: pattern not found: '%s'", procpath, pattern); |
| 157 |
|
| 158 |
return value; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
unsigned long long |
| 164 |
cpu_stats_get_cswch () |
| 165 |
{ |
| 166 |
return cpu_stats_get_value_with_pattern ("ctxt ", true); |
| 167 |
} |
| 168 |
|
| 169 |
unsigned long long |
| 170 |
cpu_stats_get_intr () |
| 171 |
{ |
| 172 |
return cpu_stats_get_value_with_pattern ("intr ", true); |
| 173 |
} |
| 174 |
|
| 175 |
unsigned long long |
| 176 |
cpu_stats_get_softirq () |
| 177 |
{ |
| 178 |
|
| 179 |
return cpu_stats_get_value_with_pattern ("softirq ", false); |
| 180 |
} |