Skip to content

feat(check_container): possibility to filter by name and check status… #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/container.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions plugins/check_container.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* License: GPLv3+
* Copyright (c) 2018,2024 Davide Madrisan <[email protected]>
* Copyright (c) 2018,2025 Davide Madrisan <[email protected]>
*
* A Nagios plugin that returns some runtime metrics exposed by Docker.
*
Expand Down Expand Up @@ -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'},
Expand All @@ -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,
Expand All @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -145,6 +151,9 @@ main (int argc, char **argv)
case 'i':
image = optarg;
break;
case 'n':
name = optarg;
break;
case 's':
socket = optarg;
break;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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\"",
Expand Down
14 changes: 8 additions & 6 deletions tests/tslibcontainer_count.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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 \
}; \
Expand All @@ -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;
Expand Down
Loading