X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fcollectd-nagios.c;h=12187f3070de63fb551929daa93a50a2985d1251;hb=afb38f31abd27d5b4ce08023f8bbbaeeb353c55f;hp=af8744de6da58353d0c47d69381fc6acec664b7b;hpb=6e0ffb3d9edba6a4b819ace076aea0e9b5fcea51;p=collectd.git diff --git a/src/collectd-nagios.c b/src/collectd-nagios.c index af8744de..12187f30 100644 --- a/src/collectd-nagios.c +++ b/src/collectd-nagios.c @@ -2,21 +2,26 @@ * collectd-nagios - src/collectd-nagios.c * Copyright (C) 2008-2010 Florian octo Forster * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; only version 2 of the License is applicable. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * Authors: - * Florian octo Forster + * Florian octo Forster **/ #if HAVE_CONFIG_H @@ -66,7 +71,7 @@ # endif #endif /* NAN_ZERO_ZERO */ -#include "libcollectdclient/client.h" +#include "libcollectdclient/collectd/client.h" #define RET_OKAY 0 #define RET_WARNING 1 @@ -99,7 +104,7 @@ static int consolitation_g = CON_NONE; static _Bool nan_is_error_g = 0; static char **match_ds_g = NULL; -static int match_ds_num_g = 0; +static size_t match_ds_num_g = 0; /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for * that, so here's an own implementation.. It's easy enough. The GCC attributes @@ -123,27 +128,25 @@ static int filter_ds (size_t *values_num, gauge_t *new_values; char **new_names; - size_t i; - if (match_ds_g == NULL) return (RET_OKAY); new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values)); if (new_values == NULL) { - fprintf (stderr, "malloc failed: %s\n", strerror (errno)); + fprintf (stderr, "calloc failed: %s\n", strerror (errno)); return (RET_UNKNOWN); } new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names)); if (new_names == NULL) { - fprintf (stderr, "malloc failed: %s\n", strerror (errno)); + fprintf (stderr, "calloc failed: %s\n", strerror (errno)); free (new_values); return (RET_UNKNOWN); } - for (i = 0; i < (size_t) match_ds_num_g; i++) + for (size_t i = 0; i < match_ds_num_g; i++) { size_t j; @@ -177,7 +180,7 @@ static int filter_ds (size_t *values_num, } free (*values); - for (i = 0; i < *values_num; i++) + for (size_t i = 0; i < *values_num; i++) free ((*values_names)[i]); free (*values_names); @@ -240,6 +243,7 @@ static int match_range (range_t *range, double value) return (((ret - range->invert) == 0) ? 0 : 1); } /* int match_range */ +__attribute__((noreturn)) static void usage (const char *name) { fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n" @@ -269,6 +273,67 @@ static void usage (const char *name) exit (1); } /* void usage */ +static int do_listval (lcc_connection_t *connection) +{ + lcc_identifier_t *ret_ident = NULL; + size_t ret_ident_num = 0; + + char *hostname = NULL; + + int status; + + status = lcc_listval (connection, &ret_ident, &ret_ident_num); + if (status != 0) { + printf ("UNKNOWN: %s\n", lcc_strerror (connection)); + if (ret_ident != NULL) + free (ret_ident); + return (RET_UNKNOWN); + } + + status = lcc_sort_identifiers (connection, ret_ident, ret_ident_num); + if (status != 0) { + printf ("UNKNOWN: %s\n", lcc_strerror (connection)); + if (ret_ident != NULL) + free (ret_ident); + return (RET_UNKNOWN); + } + + for (size_t i = 0; i < ret_ident_num; ++i) { + char id[1024]; + + if ((hostname_g != NULL) && (strcasecmp (hostname_g, ret_ident[i].host))) + continue; + + if ((hostname == NULL) || strcasecmp (hostname, ret_ident[i].host)) + { + free (hostname); + hostname = strdup (ret_ident[i].host); + printf ("Host: %s\n", hostname); + } + + /* empty hostname; not to be printed again */ + ret_ident[i].host[0] = '\0'; + + status = lcc_identifier_to_string (connection, + id, sizeof (id), ret_ident + i); + if (status != 0) { + printf ("ERROR: listval: Failed to convert returned " + "identifier to a string: %s\n", + lcc_strerror (connection)); + free (hostname); + hostname = NULL; + continue; + } + + /* skip over the (empty) hostname and following '/' */ + printf ("\t%s\n", id + 1); + } + + free (ret_ident); + free (hostname); + return (RET_OKAY); +} /* int do_listval */ + static int do_check_con_none (size_t values_num, double *values, char **values_names) { @@ -277,9 +342,8 @@ static int do_check_con_none (size_t values_num, int num_okay = 0; const char *status_str = "UNKNOWN"; int status_code = RET_UNKNOWN; - size_t i; - for (i = 0; i < values_num; i++) + for (size_t i = 0; i < values_num; i++) { if (isnan (values[i])) { @@ -322,7 +386,7 @@ static int do_check_con_none (size_t values_num, if (values_num > 0) { printf (" |"); - for (i = 0; i < values_num; i++) + for (size_t i = 0; i < values_num; i++) printf (" %s=%f;;;;", values_names[i], values[i]); } printf ("\n"); @@ -333,7 +397,6 @@ static int do_check_con_none (size_t values_num, static int do_check_con_average (size_t values_num, double *values, char **values_names) { - size_t i; double total; int total_num; double average; @@ -342,7 +405,7 @@ static int do_check_con_average (size_t values_num, total = 0.0; total_num = 0; - for (i = 0; i < values_num; i++) + for (size_t i = 0; i < values_num; i++) { if (isnan (values[i])) { @@ -383,7 +446,7 @@ static int do_check_con_average (size_t values_num, } printf ("%s: %g average |", status_str, average); - for (i = 0; i < values_num; i++) + for (size_t i = 0; i < values_num; i++) printf (" %s=%f;;;;", values_names[i], values[i]); printf ("\n"); @@ -393,7 +456,6 @@ static int do_check_con_average (size_t values_num, static int do_check_con_sum (size_t values_num, double *values, char **values_names) { - size_t i; double total; int total_num; const char *status_str = "UNKNOWN"; @@ -401,7 +463,7 @@ static int do_check_con_sum (size_t values_num, total = 0.0; total_num = 0; - for (i = 0; i < values_num; i++) + for (size_t i = 0; i < values_num; i++) { if (isnan (values[i])) { @@ -440,7 +502,7 @@ static int do_check_con_sum (size_t values_num, } printf ("%s: %g sum |", status_str, total); - for (i = 0; i < values_num; i++) + for (size_t i = 0; i < values_num; i++) printf (" %s=%f;;;;", values_names[i], values[i]); printf ("\n"); @@ -450,7 +512,6 @@ static int do_check_con_sum (size_t values_num, static int do_check_con_percentage (size_t values_num, double *values, char **values_names) { - size_t i; double sum = 0.0; double percentage; @@ -463,7 +524,7 @@ static int do_check_con_percentage (size_t values_num, return (RET_WARNING); } - for (i = 0; i < values_num; i++) + for (size_t i = 0; i < values_num; i++) { if (isnan (values[i])) { @@ -503,40 +564,24 @@ static int do_check_con_percentage (size_t values_num, } printf ("%s: %lf percent |", status_str, percentage); - for (i = 0; i < values_num; i++) + for (size_t i = 0; i < values_num; i++) printf (" %s=%lf;;;;", values_names[i], values[i]); return (status_code); } /* int do_check_con_percentage */ -static int do_check (void) +static int do_check (lcc_connection_t *connection) { - lcc_connection_t *connection; gauge_t *values; char **values_names; size_t values_num; - char address[1024]; char ident_str[1024]; lcc_identifier_t ident; - size_t i; int status; - snprintf (address, sizeof (address), "unix:%s", socket_file_g); - address[sizeof (address) - 1] = 0; - snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname_g, value_string_g); ident_str[sizeof (ident_str) - 1] = 0; - connection = NULL; - status = lcc_connect (address, &connection); - if (status != 0) - { - printf ("ERROR: Connecting to daemon at %s failed.\n", - socket_file_g); - return (RET_CRITICAL); - } - - memset (&ident, 0, sizeof (ident)); status = lcc_string_to_identifier (connection, &ident, ident_str); if (status != 0) { @@ -564,9 +609,9 @@ static int do_check (void) status = RET_UNKNOWN; if (consolitation_g == CON_NONE) - status = do_check_con_none (values_num, values, values_names); + status = do_check_con_none (values_num, values, values_names); else if (consolitation_g == CON_AVERAGE) - status = do_check_con_average (values_num, values, values_names); + status = do_check_con_average (values_num, values, values_names); else if (consolitation_g == CON_SUM) status = do_check_con_sum (values_num, values, values_names); else if (consolitation_g == CON_PERCENTAGE) @@ -574,7 +619,7 @@ static int do_check (void) free (values); if (values_names != NULL) - for (i = 0; i < values_num; i++) + for (size_t i = 0; i < values_num; i++) free (values_names[i]); free (values_names); @@ -583,6 +628,11 @@ static int do_check (void) int main (int argc, char **argv) { + char address[1024]; + lcc_connection_t *connection; + + int status; + range_critical_g.min = NAN; range_critical_g.max = NAN; range_critical_g.invert = 0; @@ -635,7 +685,7 @@ int main (int argc, char **argv) case 'd': { char **tmp; - tmp = (char **) realloc (match_ds_g, + tmp = realloc (match_ds_g, (match_ds_num_g + 1) * sizeof (char *)); if (tmp == NULL) @@ -664,11 +714,26 @@ int main (int argc, char **argv) } if ((socket_file_g == NULL) || (value_string_g == NULL) - || (hostname_g == NULL)) + || ((hostname_g == NULL) && (strcasecmp (value_string_g, "LIST")))) { fprintf (stderr, "Missing required arguments.\n"); usage (argv[0]); } - return (do_check ()); + snprintf (address, sizeof (address), "unix:%s", socket_file_g); + address[sizeof (address) - 1] = 0; + + connection = NULL; + status = lcc_connect (address, &connection); + if (status != 0) + { + printf ("ERROR: Connecting to daemon at %s failed.\n", + socket_file_g); + return (RET_CRITICAL); + } + + if (0 == strcasecmp (value_string_g, "LIST")) + return (do_listval (connection)); + + return (do_check (connection)); } /* int main */