Module: check_mk
Branch: master
Commit: 35bf9c37a8ad6fb8001089b69d0fbf708c8d3f49
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=35bf9c37a8ad6f…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Thu Oct 29 13:38:29 2015 +0100
Embrace C++ strings in pnp4nagios. Scope helpers locally. Don't use "using"
in header.
---
livestatus/src/HostSpecialIntColumn.cc | 2 +-
livestatus/src/pnp4nagios.cc | 107 +++++++++++---------------------
livestatus/src/pnp4nagios.h | 12 ++--
3 files changed, 44 insertions(+), 77 deletions(-)
diff --git a/livestatus/src/HostSpecialIntColumn.cc
b/livestatus/src/HostSpecialIntColumn.cc
index 61b9da4..dad38ba 100644
--- a/livestatus/src/HostSpecialIntColumn.cc
+++ b/livestatus/src/HostSpecialIntColumn.cc
@@ -43,7 +43,7 @@ int32_t HostSpecialIntColumn::getValue(void *data, Query *)
return hst->last_hard_state;
case HSIC_PNP_GRAPH_PRESENT:
- return pnpgraph_present(hst->name, 0);
+ return pnpgraph_present(hst->name);
case HSIC_MK_INVENTORY_LAST:
return mk_inventory_last(hst->name);
diff --git a/livestatus/src/pnp4nagios.cc b/livestatus/src/pnp4nagios.cc
index fee674f..7bc0340 100644
--- a/livestatus/src/pnp4nagios.cc
+++ b/livestatus/src/pnp4nagios.cc
@@ -22,96 +22,59 @@
// to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA.
-#include <stdlib.h>
#include <string.h>
-#include <sys/stat.h>
#include <sys/types.h>
-#include <unistd.h>
+#include <sys/stat.h>
#include <unistd.h>
#include "pnp4nagios.h"
-
-extern char *g_pnp_path;
-
-char *cleanup_pnpname(char *name)
-{
- char *p = name;
- while (*p) {
- if (*p == ' ' || *p == '/' || *p == '\\' || *p ==
':')
- *p = '_';
- p++;
- }
- return name;
-}
+using namespace std;
-int pnpgraph_present(const char *host, const char *service)
-{
- if (!g_pnp_path[0])
- return -1;
+// Note: If the path is not empty, it always ends with '/', see
+// livestatus_parse_arguments.
+extern char* g_pnp_path;
- char path[4096];
- size_t needed_size = strlen(g_pnp_path) + strlen(host) + 16;
- if (service)
- needed_size += strlen(service);
- if (needed_size > sizeof(path))
- return -1;
+namespace {
- strcpy(path, g_pnp_path);
- char *end = path + strlen(path);
- strcpy(end, host);
- cleanup_pnpname(end);
- strcat(end, "/");
- end = end + strlen(end);
- if (service) {
- strcat(end, service);
- cleanup_pnpname(end);
- strcat(end, ".xml");
+string replace_all(const string& str, const string& chars, char replacement) {
+ string result(str);
+ size_t i = 0;
+ while ((i = result.find_first_of(chars, i)) != string::npos) {
+ result[i++] = replacement;
}
- else
- strcat(end, "_HOST_.xml");
-
- if (0 == access(path, R_OK))
- return 1;
- else
- return 0;
+ return result;
}
-// Determines if a RRD database is existent and returns its path name.
-// Returns an empty string otherwise.
-// This assumes paths created in the PNP4Nagios style with storage type MULTIPLE.
-string rrd_path(const char *host, const char *service, const char *varname)
-{
- if (!g_pnp_path[0])
- return "";
-
- string path = g_pnp_path;
- path += "/";
+string cleanup(const string& name) { return replace_all(name, " /\\:",
'_'); }
- char *cleaned_up;
- cleaned_up = cleanup_pnpname(strdup(host));
- path += cleaned_up;
- free(cleaned_up);
+} // namespace
- path += "/";
- cleaned_up = cleanup_pnpname(strdup(service));
- path += cleaned_up;
- free(cleaned_up);
-
- path += "_";
-
- cleaned_up = cleanup_pnpname(strdup(varname));
- path += cleaned_up;
- free(cleaned_up);
+int pnpgraph_present(const std::string& host, const std::string& service) {
+ string pnp_path(g_pnp_path);
+ if (pnp_path.empty()) return -1;
+ string path(pnp_path.append(cleanup(host))
+ .append("/")
+ .append(cleanup(service))
+ .append(".xml"));
+ return access(path.c_str(), R_OK) == 0 ? 1 : 0;
+}
- path += ".rrd";
+string rrd_path(const string& host, const string& service,
+ const string& varname) {
+ string pnp_path(g_pnp_path);
+ if (pnp_path.empty()) return "";
+ string path(pnp_path.append("/")
+ .append(cleanup(host))
+ .append("/")
+ .append(cleanup(service))
+ .append("_")
+ .append(cleanup(varname))
+ .append(".rrd"));
struct stat st;
- if (0 == stat(path.c_str(), &st))
- return path;
- else
- return "";
+ return stat(path.c_str(), &st) == 0 ? path : "";
}
diff --git a/livestatus/src/pnp4nagios.h b/livestatus/src/pnp4nagios.h
index 6434d73..7f2595e 100644
--- a/livestatus/src/pnp4nagios.h
+++ b/livestatus/src/pnp4nagios.h
@@ -26,10 +26,14 @@
#define pnp4nagios_h
#include <string>
-using namespace std;
-int pnpgraph_present(const char *host, const char *service);
-string rrd_path(const char *host, const char *service, const char *varname);
+int pnpgraph_present(const std::string& host,
+ const std::string& service = "_HOST_");
-#endif // pnp4nagios_h
+// Determines if a RRD database exists and returns its path name. Returns an
+// empty string otherwise. This assumes paths created in the PNP4Nagios style
+// with storage type MULTIPLE.
+std::string rrd_path(const std::string& host, const std::string& service,
+ const std::string& varname);
+#endif // pnp4nagios_h