Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/pressure.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2020,2022 Davide Madrisan <[email protected]>
5 *
6 * A library for checking the Linux Pressure Stall Information (PSI)
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 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "getenv.h"
27 #include "logging.h"
28 #include "messages.h"
29 #include "pressure.h"
30 #include "string-macros.h"
31 #include "xalloc.h"
32
33 #ifdef NPL_TESTING
34 static const char *
35 get_path_proc_pressure (enum linux_psi_id psi_id)
36 {
37 const char *env_procpressure = NULL;
38
39 switch (psi_id)
40 {
41 default:
42 break;
43 case LINUX_PSI_CPU:
44 env_procpressure = secure_getenv ("NPL_TEST_PATH_PROCPRESSURE_CPU");
45 break;
46 case LINUX_PSI_IO:
47 case LINUX_PSI_MEMORY:
48 env_procpressure = secure_getenv ("NPL_TEST_PATH_PROCPRESSURE_IO");
49 break;
50 }
51
52 return env_procpressure ? env_procpressure : NULL;
53 }
54 #endif
55
56 static int
57 proc_psi_parser (struct proc_psi_oneline *psi_stat,
58 const char *procpath, char *label)
59 {
60 FILE *fp;
61 int rc = 0;
62 size_t len = 0, label_len;
63 ssize_t chread;
64 char *line = NULL;
65
66 if ((fp = fopen (procpath, "r")) == NULL)
67 plugin_error (STATE_UNKNOWN, errno, "error opening %s", procpath);
68
69 dbg ("reading file %s\n", procpath);
70 while ((chread = getline (&line, &len, fp)) != -1)
71 {
72 dbg ("line: %s", line);
73 label_len = strlen (label);
74 if (STREQLEN (line, label, label_len))
75 {
76 dbg (" \\ matching label \"%s\"\n", label);
77 rc =
78 sscanf (line + label_len + 1,
79 "avg10=%32lf avg60=%32lf avg300=%32lf total=%llu",
80 &psi_stat->avg10,
81 &psi_stat->avg60,
82 &psi_stat->avg300,
83 &psi_stat->total);
84 dbg (" \\ avg10=%g avg60=%g avg300=%g total=%llu\n",
85 psi_stat->avg10, psi_stat->avg60, psi_stat->avg300,
86 psi_stat->total);
87 break;
88 }
89 }
90
91 free (line);
92 fclose (fp);
93
94 if (rc < 4)
95 plugin_error (STATE_UNKNOWN, errno, "error reading %s", procpath);
96
97 return 0;
98 }
99
100 int
101 proc_psi_read_cpu (struct proc_psi_oneline **psi_cpu,
102 unsigned long long *starvation, unsigned long delay)
103 {
104 #ifdef NPL_TESTING
105 const char *procpath = get_path_proc_pressure (LINUX_PSI_CPU);
106 #else
107 const char *procpath = PATH_PSI_PROC_CPU;
108 #endif
109 struct proc_psi_oneline psi, *stats = *psi_cpu;
110 unsigned long long total;
111
112 if (NULL == stats)
113 {
114 stats = xmalloc (sizeof (struct proc_psi_oneline));
115 *psi_cpu = stats;
116 }
117
118 proc_psi_parser (&psi, procpath, "some");
119 stats->avg10 = psi.avg10;
120 stats->avg60 = psi.avg60;
121 stats->avg300 = psi.avg300;
122 stats->total = total = psi.total;
123
124 /* calculate the starvation (in microseconds) per second */
125 sleep (delay);
126
127 proc_psi_parser (&psi, procpath, "some");
128 *starvation = (psi.total - total) / delay;
129 dbg ("delta (over %lusec): %llu ((%llu - %llu) / %lu)\n",
130 delay, *starvation, psi.total, total, delay);
131
132 return 0;
133 }
134
135 static int
136 proc_psi_read (struct proc_psi_twolines **psi_io,
137 unsigned long long *starvation, const char *procfile,
138 unsigned long delay)
139 {
140 struct proc_psi_oneline psi;
141 struct proc_psi_twolines *stats = *psi_io;
142 unsigned long long some_total;
143 unsigned long long full_total;
144
145 if (NULL == stats)
146 {
147 stats = xmalloc (sizeof (struct proc_psi_twolines));
148 *psi_io = stats;
149 }
150
151 proc_psi_parser (&psi, procfile, "some");
152 stats->some_avg10 = psi.avg10;
153 stats->some_avg60 = psi.avg60;
154 stats->some_avg300 = psi.avg300;
155 stats->some_total = some_total = psi.total;
156
157 proc_psi_parser (&psi, procfile, "full");
158 stats->full_avg10 = psi.avg10;
159 stats->full_avg60 = psi.avg60;
160 stats->full_avg300 = psi.avg300;
161 stats->full_total = full_total = psi.total;
162
163 sleep (delay);
164
165 proc_psi_parser (&psi, procfile, "some");
166 *starvation = (psi.total - some_total) / delay;
167 dbg ("delta (over %lusec) for some: %llu ((%llu - %llu) / %lu)\n",
168 delay, *starvation, psi.total, some_total, delay);
169
170 proc_psi_parser (&psi, procfile, "full");
171 *(starvation + 1) = (psi.total - full_total) / delay;
172 dbg ("delta (over %lusec) for full: %llu ((%llu - %llu) / %lu)\n",
173 delay, *(starvation + 1), psi.total, full_total, delay);
174
175 return 0;
176 }
177
178 int
179 proc_psi_read_io (struct proc_psi_twolines **psi_io,
180 unsigned long long *starvation, unsigned long delay)
181 {
182 #ifdef NPL_TESTING
183 const char *procpath = get_path_proc_pressure (LINUX_PSI_IO);
184 #else
185 const char *procpath = PATH_PSI_PROC_IO;
186 #endif
187 return proc_psi_read (psi_io, starvation, procpath, delay);
188 }
189
190 int
191 proc_psi_read_memory (struct proc_psi_twolines **psi_memory,
192 unsigned long long *starvation, unsigned long delay)
193 {
194 #ifdef NPL_TESTING
195 const char *procpath = get_path_proc_pressure (LINUX_PSI_MEMORY);
196 #else
197 const char *procpath = PATH_PSI_PROC_MEMORY;
198 #endif
199 return proc_psi_read (psi_memory, starvation, procpath, delay);
200 }