diff --git a/include/container.h b/include/container.h index 7519af8..c28c716 100644 --- a/include/container.h +++ b/include/container.h @@ -81,8 +81,8 @@ extern "C" struct docker_memory_desc *memdesc); int docker_running_containers (char *socket, unsigned int *count, - const char *image, char **perfdata, - bool verbose); + const char *image, const char *name, + char **perfdata, bool verbose); int docker_running_containers_memory (char *socket, long long unsigned int *memory, bool verbose); diff --git a/lib/container.c b/lib/container.c index 61b82aa..91ae120 100644 --- a/lib/container.c +++ b/lib/container.c @@ -269,8 +269,8 @@ docker_api_call (char *socket, chunk_t *chunk, /* Returns the number of running Docker/Podman containers grouped by images */ int -docker_running_containers (char *socket, unsigned int *count, - const char *image, char **perfdata, bool verbose) +docker_running_containers (char *socket, unsigned int *count, const char *image, + const char *name, char **perfdata, bool verbose) { chunk_t chunk; #ifndef NPL_TESTING diff --git a/plugins/check_container.c b/plugins/check_container.c index 7d01f70..617907f 100644 --- a/plugins/check_container.c +++ b/plugins/check_container.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-or-later /* * License: GPLv3+ - * Copyright (c) 2018,2024 Davide Madrisan + * Copyright (c) 2018,2025 Davide Madrisan * * A Nagios plugin that returns some runtime metrics exposed by Docker. * @@ -40,11 +40,12 @@ #include "xstrton.h" static const char *program_copyright = - "Copyright (C) 2018,2024 Davide Madrisan <" PACKAGE_BUGREPORT ">\n"; + "Copyright (C) 2018,2025 Davide Madrisan <" PACKAGE_BUGREPORT ">\n"; static struct option const longopts[] = { {(char *) "image", required_argument, NULL, 'i'}, {(char *) "memory", no_argument, NULL, 'M'}, + {(char *) "name", required_argument, NULL, 'n'}, {(char *) "socket", required_argument, NULL, 's'}, {(char *) "critical", required_argument, NULL, 'c'}, {(char *) "warning", required_argument, NULL, 'w'}, @@ -61,7 +62,8 @@ static _Noreturn void usage (FILE *out) { fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version); - fputs ("This plugin returns some runtime metrics exposed by Docker\n", out); + fputs ("This plugin returns some runtime metrics exposed by docker/podman containers\n", + out); fputs (program_copyright, out); fputs (USAGE_HEADER, out); fprintf (out, @@ -75,6 +77,9 @@ usage (FILE *out) fputs (" -i, --image IMAGE limit the investigation only to the containers " "running IMAGE\n", out); + fputs + (" -n, --name CONTAINER_NAME limit the investigation to a container with a given " + "name\n", out); fputs (" -M, --memory check memory utilisation for running containers\n", out); fputs (" -s, --socket SOCKET the path of the docker or podman socket, usually\n" @@ -93,9 +98,9 @@ usage (FILE *out) " environment variable will be used\n"); fputs (USAGE_EXAMPLES, out); fprintf (out, " export DOCKER_HOST=\"" DOCKER_SOCKET "\"\n"); + fprintf (out, " %s -w 100 -c 120\n", program_name); fprintf (out, " %s --socket /run/user/1000/podman/podman.sock\n", program_name); - fprintf (out, " %s -w 100 -c 120\n", program_name); /* fprintf (out, " %s --socket " PODMAN_SOCKET " --image nginx -c 5:\n", program_name); fprintf (out, @@ -123,6 +128,7 @@ main (int argc, char **argv) int shift = k_shift; bool check_memory = false, verbose = false; char *image = NULL; + char *name = NULL; char *socket = NULL; char *critical = NULL, *warning = NULL; char *status_msg, *perfdata_msg; @@ -134,7 +140,7 @@ main (int argc, char **argv) set_program_name (argv[0]); while ((c = getopt_long (argc, argv, - "i:s:Mkmgc:w:v" GETOPT_HELP_VERSION_STRING, + "i:n:s:Mkmgc:w:v" GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1) { switch (c) @@ -145,6 +151,9 @@ main (int argc, char **argv) case 'i': image = optarg; break; + case 'n': + name = optarg; + break; case 's': socket = optarg; break; @@ -190,7 +199,7 @@ main (int argc, char **argv) "too large delay value (greater than %d)", DELAY_MAX); } - if (check_memory && image) + if (check_memory && (image || name)) usage (stderr); status = set_thresholds (&my_threshold, warning, critical); @@ -219,8 +228,8 @@ main (int argc, char **argv) else { unsigned int containers; - docker_running_containers (socket, &containers, image, &perfdata_msg, - verbose); + docker_running_containers (socket, &containers, image, name, + &perfdata_msg, verbose); status = get_status (containers, my_threshold); status_msg = image ? xasprintf ("%s: %u running container(s) of type \"%s\"", diff --git a/tests/tslibcontainer_count.c b/tests/tslibcontainer_count.c index 36a8bf5..f4e497a 100644 --- a/tests/tslibcontainer_count.c +++ b/tests/tslibcontainer_count.c @@ -67,6 +67,7 @@ docker_close (chunk_t *chunk) typedef struct test_data { char *image; + char *name; char *perfdata; unsigned int expect_value; } test_data; @@ -80,8 +81,8 @@ test_docker_running_containers (const void *tdata) unsigned int containers; err = - docker_running_containers (NULL, &containers, data->image, &perfdata, - false); + docker_running_containers (NULL, &containers, data->image, data->name, + &perfdata, false); if (err != 0) { free (perfdata); @@ -100,11 +101,12 @@ mymain (void) { int ret = 0; -#define DO_TEST(MSG, IMAGE, PERFDATA, EXPECT_VALUE) \ +#define DO_TEST(MSG, IMAGE, NAME, PERFDATA, EXPECT_VALUE) \ do \ { \ test_data data = { \ .image = IMAGE, \ + .name = NAME, \ .perfdata = PERFDATA, \ .expect_value = EXPECT_VALUE \ }; \ @@ -113,11 +115,11 @@ mymain (void) } \ while (0) - DO_TEST ("check running containers with image set", "nginx", + DO_TEST ("check running containers with image set", "nginx", NULL, "containers_nginx=3", 3); - DO_TEST ("check running containers with image set", "redis", + DO_TEST ("check running containers with image set", "redis", NULL, "containers_redis=1", 1); - DO_TEST ("check running containers", NULL, + DO_TEST ("check running containers", NULL, NULL, "containers_redis=1 containers_nginx=3 containers_total=4", 4); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;