Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/plugins/check_container.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2018,2024 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin that returns some runtime metrics exposed by Docker.
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 */
22
23 #include <errno.h>
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <unistd.h>
29
30 #include "common.h"
31 #include "container.h"
32 #include "logging.h"
33 #include "messages.h"
34 #include "progname.h"
35 #include "progversion.h"
36 #include "thresholds.h"
37 #include "units.h"
38 #include "xalloc.h"
39 #include "xasprintf.h"
40 #include "xstrton.h"
41
42 static const char *program_copyright =
43 "Copyright (C) 2018,2024 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
44
45 static struct option const longopts[] = {
46 {(char *) "image", required_argument, NULL, 'i'},
47 {(char *) "memory", no_argument, NULL, 'M'},
48 {(char *) "socket", required_argument, NULL, 's'},
49 {(char *) "critical", required_argument, NULL, 'c'},
50 {(char *) "warning", required_argument, NULL, 'w'},
51 {(char *) "kilobyte", no_argument, NULL, 'k'},
52 {(char *) "megabyte", no_argument, NULL, 'm'},
53 {(char *) "gigabyte", no_argument, NULL, 'g'},
54 {(char *) "verbose", no_argument, NULL, 'v'},
55 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
56 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
57 {NULL, 0, NULL, 0}
58 };
59
60 static _Noreturn void
61 usage (FILE *out)
62 {
63 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
64 fputs ("This plugin returns some runtime metrics exposed by Docker\n", out);
65 fputs (program_copyright, out);
66 fputs (USAGE_HEADER, out);
67 fprintf (out,
68 " %s --socket SOCKET [--image IMAGE] [-w COUNTER] [-c COUNTER]\n",
69 program_name);
70 /*
71 fprintf (out,
72 " %s --socket SOCKET --memory [-k,-m,-g] [-w COUNTER] [-c COUNTER] [delay]\n",
73 program_name); */
74 fputs (USAGE_OPTIONS, out);
75 fputs
76 (" -i, --image IMAGE limit the investigation only to the containers "
77 "running IMAGE\n", out);
78 fputs (" -M, --memory check memory utilisation for running containers\n",
79 out);
80 fputs (" -s, --socket SOCKET the path of the docker or podman socket, usually\n"
81 " " DOCKER_SOCKET " and " PODMAN_SOCKET "\n", out);
82 fputs (" -k,-m,-g "
83 "show output in kB (the default), MB, or GB\n", out);
84 fputs (" -w, --warning COUNTER warning threshold\n", out);
85 fputs (" -c, --critical COUNTER critical threshold\n", out);
86 fputs (" -v, --verbose show details for command-line debugging "
87 "(Nagios may truncate output)\n", out);
88 fputs (USAGE_HELP, out);
89 fputs (USAGE_VERSION, out);
90 fprintf (out, " delay is the delay between updates in seconds "
91 "(default: %dsec)\n", DELAY_DEFAULT);
92 fprintf (out, " if no socket is specified, the value of the DOCKER_HOST"
93 " environment variable will be used\n");
94 fputs (USAGE_EXAMPLES, out);
95 fprintf (out, " export DOCKER_HOST=\"" DOCKER_SOCKET "\"\n");
96 fprintf (out, " %s --socket /run/user/1000/podman/podman.sock\n",
97 program_name);
98 fprintf (out, " %s -w 100 -c 120\n", program_name);
99 /* fprintf (out, " %s --socket " PODMAN_SOCKET " --image nginx -c 5:\n",
100 program_name);
101 fprintf (out,
102 " %s -s /run/user/1000/podman/podman.sock --memory -m -w 512 -c 640 5\n",
103 program_name);
104 */
105 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
106 }
107
108 static _Noreturn void
109 print_version (void)
110 {
111 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
112 fputs (program_copyright, stdout);
113 fputs (GPLv3_DISCLAIMER, stdout);
114
115 exit (STATE_OK);
116 }
117
118 #ifndef NPL_TESTING
119 int
120 main (int argc, char **argv)
121 {
122 int c;
123 int shift = k_shift;
124 bool check_memory = false, verbose = false;
125 char *image = NULL;
126 char *socket = NULL;
127 char *critical = NULL, *warning = NULL;
128 char *status_msg, *perfdata_msg;
129 char *units = NULL;
130 nagstatus status = STATE_OK;
131 thresholds *my_threshold = NULL;
132 unsigned long delay = DELAY_DEFAULT;
133
134 set_program_name (argv[0]);
135
136 while ((c = getopt_long (argc, argv,
137 "i:s:Mkmgc:w:v" GETOPT_HELP_VERSION_STRING,
138 longopts, NULL)) != -1)
139 {
140 switch (c)
141 {
142 default:
143 usage (stderr);
144 break;
145 case 'i':
146 image = optarg;
147 break;
148 case 's':
149 socket = optarg;
150 break;
151 case 'M':
152 check_memory = true;
153 break;
154 case 'k':
155 shift = k_shift;
156 units = xstrdup ("kB");
157 break;
158 case 'm':
159 shift = m_shift;
160 units = xstrdup ("MB");
161 break;
162 case 'g':
163 shift = g_shift;
164 units = xstrdup ("GB");
165 break;
166 case 'c':
167 critical = optarg;
168 break;
169 case 'w':
170 warning = optarg;
171 break;
172 case 'v':
173 verbose = true;
174 break;
175
176 case_GETOPT_HELP_CHAR
177 case_GETOPT_VERSION_CHAR
178
179 }
180 }
181
182 if (optind < argc)
183 {
184 delay = strtol_or_err (argv[optind++], "failed to parse argument");
185
186 if (delay < 1)
187 plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer");
188 else if (DELAY_MAX < delay)
189 plugin_error (STATE_UNKNOWN, 0,
190 "too large delay value (greater than %d)", DELAY_MAX);
191 }
192
193 if (check_memory && image)
194 usage (stderr);
195
196 status = set_thresholds (&my_threshold, warning, critical);
197 if (status == NP_RANGE_UNPARSEABLE)
198 usage (stderr);
199
200 if (check_memory)
201 {
202 long long unsigned int kb_memory_used_total;
203
204 /* output in kilobytes by default */
205 if (units == NULL)
206 units = xstrdup ("kB");
207
208 docker_running_containers_memory (socket, &kb_memory_used_total, verbose);
209 status = get_status (UNIT_CONVERT (kb_memory_used_total, shift),
210 my_threshold);
211
212 status_msg =
213 xasprintf ("%s: %llu %s memory used", state_text (status),
214 UNIT_STR (kb_memory_used_total));
215 perfdata_msg =
216 xasprintf ("used=%llu%s"
217 , UNIT_STR (kb_memory_used_total));
218 }
219 else
220 {
221 unsigned int containers;
222 docker_running_containers (socket, &containers, image, &perfdata_msg,
223 verbose);
224 status = get_status (containers, my_threshold);
225 status_msg = image ?
226 xasprintf ("%s: %u running container(s) of type \"%s\"",
227 state_text (status), containers, image) :
228 xasprintf ("%s: %u running container(s)", state_text (status),
229 containers);
230 }
231
232 printf ("%s%s %s | %s\n", program_name_short,
233 check_memory ? " memory" : " containers", status_msg, perfdata_msg);
234
235 free (my_threshold);
236 return status;
237 }
238 #endif /* NPL_TESTING */