root/lib/services/services_nagios.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. nagios_metadata_name
  2. services__list_nagios_agents
  3. services__nagios_agent_exists
  4. services__get_nagios_metadata

   1 /*
   2  * Copyright 2010-2019 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #ifndef _GNU_SOURCE
  13 #  define _GNU_SOURCE
  14 #endif
  15 
  16 #include <sys/types.h>
  17 #include <sys/stat.h>
  18 #include <sys/wait.h>
  19 #include <errno.h>
  20 #include <unistd.h>
  21 #include <dirent.h>
  22 #include <grp.h>
  23 #include <string.h>
  24 #include <sys/time.h>
  25 #include <sys/resource.h>
  26 
  27 #include "crm/crm.h"
  28 #include "crm/common/mainloop.h"
  29 #include "crm/services.h"
  30 
  31 #include "services_private.h"
  32 #include "services_nagios.h"
  33 
  34 static inline char *
  35 nagios_metadata_name(const char *plugin)
     /* [previous][next][first][last][top][bottom][index][help] */
  36 {
  37     return crm_strdup_printf(NAGIOS_METADATA_DIR "/%s.xml", plugin);
  38 }
  39 
  40 GList *
  41 services__list_nagios_agents(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  42 {
  43     GList *plugin_list = NULL;
  44     GList *result = NULL;
  45 
  46     plugin_list = services_os_get_directory_list(NAGIOS_PLUGIN_DIR, TRUE, TRUE);
  47 
  48     // Return only the plugins that have metadata
  49     for (GList *gIter = plugin_list; gIter != NULL; gIter = gIter->next) {
  50         struct stat st;
  51         const char *plugin = gIter->data;
  52         char *metadata = nagios_metadata_name(plugin);
  53 
  54         if (stat(metadata, &st) == 0) {
  55             result = g_list_append(result, strdup(plugin));
  56         }
  57         free(metadata);
  58     }
  59     g_list_free_full(plugin_list, free);
  60     return result;
  61 }
  62 
  63 gboolean
  64 services__nagios_agent_exists(const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
  65 {
  66     char *buf = NULL;
  67     gboolean rc = FALSE;
  68     struct stat st;
  69 
  70     if (name == NULL) {
  71         return rc;
  72     }
  73 
  74     buf = crm_strdup_printf(NAGIOS_PLUGIN_DIR "/%s", name);
  75     if (stat(buf, &st) == 0) {
  76         rc = TRUE;
  77     }
  78 
  79     free(buf);
  80     return rc;
  81 }
  82 
  83 int
  84 services__get_nagios_metadata(const char *type, char **output)
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86     int rc = pcmk_ok;
  87     FILE *file_strm = NULL;
  88     int start = 0, length = 0, read_len = 0;
  89     char *metadata_file = nagios_metadata_name(type);
  90 
  91     file_strm = fopen(metadata_file, "r");
  92     if (file_strm == NULL) {
  93         crm_err("Metadata file %s does not exist", metadata_file);
  94         free(metadata_file);
  95         return -EIO;
  96     }
  97 
  98     /* see how big the file is */
  99     start = ftell(file_strm);
 100     fseek(file_strm, 0L, SEEK_END);
 101     length = ftell(file_strm);
 102     fseek(file_strm, 0L, start);
 103 
 104     CRM_ASSERT(length >= 0);
 105     CRM_ASSERT(start == ftell(file_strm));
 106 
 107     if (length <= 0) {
 108         crm_info("%s was not valid", metadata_file);
 109         free(*output);
 110         *output = NULL;
 111         rc = -EIO;
 112 
 113     } else {
 114         crm_trace("Reading %d bytes from file", length);
 115         *output = calloc(1, (length + 1));
 116         read_len = fread(*output, 1, length, file_strm);
 117         if (read_len != length) {
 118             crm_err("Calculated and read bytes differ: %d vs. %d",
 119                     length, read_len);
 120             free(*output);
 121             *output = NULL;
 122             rc = -EIO;
 123         }
 124     }
 125 
 126     fclose(file_strm);
 127     free(metadata_file);
 128     return rc;
 129 }

/* [previous][next][first][last][top][bottom][index][help] */