Skip to content

Commit d75ca12

Browse files
committed
feat(check_container): possibility to filter by name and check status or health or uptime
Signed-off-by: Davide Madrisan <d.madrisan@proton.me>
1 parent 718daf4 commit d75ca12

4 files changed

Lines changed: 29 additions & 18 deletions

File tree

include/container.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ extern "C"
8181
struct docker_memory_desc *memdesc);
8282

8383
int docker_running_containers (char *socket, unsigned int *count,
84-
const char *image, char **perfdata,
85-
bool verbose);
84+
const char *image, const char *name,
85+
char **perfdata, bool verbose);
8686
int docker_running_containers_memory (char *socket,
8787
long long unsigned int *memory,
8888
bool verbose);

lib/container.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ docker_api_call (char *socket, chunk_t *chunk,
269269
/* Returns the number of running Docker/Podman containers grouped by images */
270270

271271
int
272-
docker_running_containers (char *socket, unsigned int *count,
273-
const char *image, char **perfdata, bool verbose)
272+
docker_running_containers (char *socket, unsigned int *count, const char *image,
273+
const char *name, char **perfdata, bool verbose)
274274
{
275275
chunk_t chunk;
276276
#ifndef NPL_TESTING

plugins/check_container.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: GPL-3.0-or-later
22
/*
33
* License: GPLv3+
4-
* Copyright (c) 2018,2024 Davide Madrisan <davide.madrisan@gmail.com>
4+
* Copyright (c) 2018,2025 Davide Madrisan <davide.madrisan@gmail.com>
55
*
66
* A Nagios plugin that returns some runtime metrics exposed by Docker.
77
*
@@ -40,11 +40,12 @@
4040
#include "xstrton.h"
4141

4242
static const char *program_copyright =
43-
"Copyright (C) 2018,2024 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
43+
"Copyright (C) 2018,2025 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
4444

4545
static struct option const longopts[] = {
4646
{(char *) "image", required_argument, NULL, 'i'},
4747
{(char *) "memory", no_argument, NULL, 'M'},
48+
{(char *) "name", required_argument, NULL, 'n'},
4849
{(char *) "socket", required_argument, NULL, 's'},
4950
{(char *) "critical", required_argument, NULL, 'c'},
5051
{(char *) "warning", required_argument, NULL, 'w'},
@@ -61,7 +62,8 @@ static _Noreturn void
6162
usage (FILE *out)
6263
{
6364
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 ("This plugin returns some runtime metrics exposed by docker/podman containers\n",
66+
out);
6567
fputs (program_copyright, out);
6668
fputs (USAGE_HEADER, out);
6769
fprintf (out,
@@ -75,6 +77,9 @@ usage (FILE *out)
7577
fputs
7678
(" -i, --image IMAGE limit the investigation only to the containers "
7779
"running IMAGE\n", out);
80+
fputs
81+
(" -n, --name CONTAINER_NAME limit the investigation to a container with a given "
82+
"name\n", out);
7883
fputs (" -M, --memory check memory utilisation for running containers\n",
7984
out);
8085
fputs (" -s, --socket SOCKET the path of the docker or podman socket, usually\n"
@@ -93,9 +98,9 @@ usage (FILE *out)
9398
" environment variable will be used\n");
9499
fputs (USAGE_EXAMPLES, out);
95100
fprintf (out, " export DOCKER_HOST=\"" DOCKER_SOCKET "\"\n");
101+
fprintf (out, " %s -w 100 -c 120\n", program_name);
96102
fprintf (out, " %s --socket /run/user/1000/podman/podman.sock\n",
97103
program_name);
98-
fprintf (out, " %s -w 100 -c 120\n", program_name);
99104
/* fprintf (out, " %s --socket " PODMAN_SOCKET " --image nginx -c 5:\n",
100105
program_name);
101106
fprintf (out,
@@ -123,6 +128,7 @@ main (int argc, char **argv)
123128
int shift = k_shift;
124129
bool check_memory = false, verbose = false;
125130
char *image = NULL;
131+
char *name = NULL;
126132
char *socket = NULL;
127133
char *critical = NULL, *warning = NULL;
128134
char *status_msg, *perfdata_msg;
@@ -134,7 +140,7 @@ main (int argc, char **argv)
134140
set_program_name (argv[0]);
135141

136142
while ((c = getopt_long (argc, argv,
137-
"i:s:Mkmgc:w:v" GETOPT_HELP_VERSION_STRING,
143+
"i:n:s:Mkmgc:w:v" GETOPT_HELP_VERSION_STRING,
138144
longopts, NULL)) != -1)
139145
{
140146
switch (c)
@@ -145,6 +151,9 @@ main (int argc, char **argv)
145151
case 'i':
146152
image = optarg;
147153
break;
154+
case 'n':
155+
name = optarg;
156+
break;
148157
case 's':
149158
socket = optarg;
150159
break;
@@ -190,7 +199,7 @@ main (int argc, char **argv)
190199
"too large delay value (greater than %d)", DELAY_MAX);
191200
}
192201

193-
if (check_memory && image)
202+
if (check_memory && (image || name))
194203
usage (stderr);
195204

196205
status = set_thresholds (&my_threshold, warning, critical);
@@ -219,8 +228,8 @@ main (int argc, char **argv)
219228
else
220229
{
221230
unsigned int containers;
222-
docker_running_containers (socket, &containers, image, &perfdata_msg,
223-
verbose);
231+
docker_running_containers (socket, &containers, image, name,
232+
&perfdata_msg, verbose);
224233
status = get_status (containers, my_threshold);
225234
status_msg = image ?
226235
xasprintf ("%s: %u running container(s) of type \"%s\"",

tests/tslibcontainer_count.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ docker_close (chunk_t *chunk)
6767
typedef struct test_data
6868
{
6969
char *image;
70+
char *name;
7071
char *perfdata;
7172
unsigned int expect_value;
7273
} test_data;
@@ -80,8 +81,8 @@ test_docker_running_containers (const void *tdata)
8081
unsigned int containers;
8182

8283
err =
83-
docker_running_containers (NULL, &containers, data->image, &perfdata,
84-
false);
84+
docker_running_containers (NULL, &containers, data->image, data->name,
85+
&perfdata, false);
8586
if (err != 0)
8687
{
8788
free (perfdata);
@@ -100,11 +101,12 @@ mymain (void)
100101
{
101102
int ret = 0;
102103

103-
#define DO_TEST(MSG, IMAGE, PERFDATA, EXPECT_VALUE) \
104+
#define DO_TEST(MSG, IMAGE, NAME, PERFDATA, EXPECT_VALUE) \
104105
do \
105106
{ \
106107
test_data data = { \
107108
.image = IMAGE, \
109+
.name = NAME, \
108110
.perfdata = PERFDATA, \
109111
.expect_value = EXPECT_VALUE \
110112
}; \
@@ -113,11 +115,11 @@ mymain (void)
113115
} \
114116
while (0)
115117

116-
DO_TEST ("check running containers with image set", "nginx",
118+
DO_TEST ("check running containers with image set", "nginx", NULL,
117119
"containers_nginx=3", 3);
118-
DO_TEST ("check running containers with image set", "redis",
120+
DO_TEST ("check running containers with image set", "redis", NULL,
119121
"containers_redis=1", 1);
120-
DO_TEST ("check running containers", NULL,
122+
DO_TEST ("check running containers", NULL, NULL,
121123
"containers_redis=1 containers_nginx=3 containers_total=4", 4);
122124

123125
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;

0 commit comments

Comments
 (0)