[icinga-checkins] icinga.org: icinga2/feature/check_nscp-12874: Format changes to check_nscp

git at icinga.org git at icinga.org
Fri Oct 14 13:38:58 CEST 2016


Module: icinga2
Branch: feature/check_nscp-12874
Commit: da3fb2eb7caaabcaa8c75b9ec909555acfd6006c
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=da3fb2eb7caaabcaa8c75b9ec909555acfd6006c

Author: Jean Flach <jean-marcel.flach at netways.de>
Date:   Tue Oct 11 15:35:22 2016 +0200

Format changes to check_nscp

refs #12874

---

 plugins/check_nscp.cpp |   54 ++++++++++++++++++++++++++++++++++--------------
 plugins/check_nscp.h   |   39 ----------------------------------
 2 files changed, 38 insertions(+), 55 deletions(-)

diff --git a/plugins/check_nscp.cpp b/plugins/check_nscp.cpp
index 2535702..ac13e28 100644
--- a/plugins/check_nscp.cpp
+++ b/plugins/check_nscp.cpp
@@ -17,15 +17,23 @@
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ******************************************************************************/
 
-#include "check_nscp.h"
-
 #define VERSION 0.1
 
+#include "remote/httpclientconnection.hpp"
+#include "remote/httprequest.hpp"
+#include "remote/url-characters.hpp"
+#include "base/application.hpp"
+#include "base/json.hpp"
+#include "base/string.hpp"
+#include <boost/program_options.hpp>
+#include <boost/algorithm/string/split.hpp>
+
 using namespace icinga;
 namespace po = boost::program_options;
 
-void ResultHttpCompletionCallback(const HttpRequest& request, HttpResponse& response, bool& ready, 
-	    boost::condition_variable& cv, boost::mutex& mtx, Dictionary::Ptr& result) {
+static void ResultHttpCompletionCallback(const HttpRequest& request, HttpResponse& response, bool& ready,
+    boost::condition_variable& cv, boost::mutex& mtx, Dictionary::Ptr& result)
+{
 	String body;
 	char buffer[1024];
 	size_t count;
@@ -39,7 +47,8 @@ void ResultHttpCompletionCallback(const HttpRequest& request, HttpResponse& resp
 	cv.notify_all();
 }
 
-int MatchState(const String& state) {
+static int MatchState(const String& state)
+{
 	if (state.ToUpper() == "OK")
 		return 0;
 	if (state.ToUpper() == "WARNING")
@@ -50,8 +59,9 @@ int MatchState(const String& state) {
 	return 3;
 }
 
-Dictionary::Ptr QueryEndpoint(const String& host, const String& port, const String& password, const String& url) {
-
+static Dictionary::Ptr QueryEndpoint(const String& host, const String& port, const String& password,
+    const String& endpoint)
+{
 	HttpClientConnection::Ptr m_Connection = new HttpClientConnection(host, port, true);
 
 	try {
@@ -63,7 +73,7 @@ Dictionary::Ptr QueryEndpoint(const String& host, const String& port, const Stri
 		req->RequestMethod = "GET";
 
 		// Url() will call Utillity::UnescapeString() which will thrown an exception if it finds a lonely %
-		req->RequestUrl = new Url(Utility::EscapeString(url, "", true));
+		req->RequestUrl = new Url(endpoint);
 		req->AddHeader("password", password);
 		m_Connection->SubmitRequest(req, boost::bind(ResultHttpCompletionCallback, _1, _2,
 			boost::ref(ready), boost::ref(cv), boost::ref(mtx), boost::ref(result)));
@@ -72,15 +82,17 @@ Dictionary::Ptr QueryEndpoint(const String& host, const String& port, const Stri
 		while (!ready) {
 			cv.wait(lock);
 		}
+
 		return result;
 	}
 	catch (const std::exception& ex) {
-		std::cout << "Caught an exception: " << ex.what();
+		std::cout << "Caught an exception: " << ex.what() << '\n';
 		return Dictionary::Ptr();
 	}
 }
 
-int FormatOutput(Dictionary::Ptr result) {
+static int FormatOutput(const Dictionary::Ptr& result)
+{
 	if (!result)
 		return 3;
 
@@ -102,23 +114,33 @@ int FormatOutput(Dictionary::Ptr result) {
 		ObjectLock olock(perfs);
 
 		for (const Dictionary::Ptr& perf : perfs) {
-			std::cout << perf->Get("alias") << "=";
+			std::cout << "'" << perf->Get("alias") << "'=";
 			Dictionary::Ptr values = perf->Contains("int_value") ? perf->Get("int_value") : perf->Get("float_value");
-			std::cout << values->Get("value") << (values->Contains("unit") ? values->Get("unit") : "")
-				<< ';' << values->Get("warning") << ';' << values->Get("critical");
+			//TODO: Test ohne unit
+			std::cout << values->Get("value") << values->Get("unit") << ';' << values->Get("warning") << ';' << values->Get("critical");
 
-			if (values->Contains("minimum") && values->Contains("maximum"))
-				std::cout << ';' << values->Get("minimum") << ';' << values->Get("maximum");
+			if (values->Contains("minimum") || values->Contains("maximum")) {
+				std::cout << ';';
+
+				//TODO check standard
+				if (values->Contains("minimum"))
+					std::cout << values->Get("minimum");
+
+				if (values->Contains("maximum"))
+					std::cout << ';' << values->Get("maximum");
+			}
 
 			std::cout << ' ';
 		}
+
 		std::cout << '\n';
 	}
 
 	return MatchState(payload->Get("result"));
 }
 
-void main(int argc, char **argv) {
+void main(int argc, char **argv)
+{
 	po::variables_map vm;
 	po::options_description desc("Options");
 
diff --git a/plugins/check_nscp.h b/plugins/check_nscp.h
deleted file mode 100644
index eda10a3..0000000
--- a/plugins/check_nscp.h
+++ /dev/null
@@ -1,39 +0,0 @@
-/******************************************************************************
- * Icinga 2                                                                   *
- * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
- *                                                                            *
- * 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; either version 2             *
- * of the License, or (at your option) any later version.                     *
- *                                                                            *
- * 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.                               *
- *                                                                            *
- * 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.             *
- ******************************************************************************/
-
-#ifndef CHECK_NSCP_H
-#define CHECK_NSCP_H
-
-#include <WinSock2.h>
-#include <Windows.h>
-
-#include "base/application.hpp"
-#include "base/json.hpp"
-#include "base/string.hpp"
-#include "remote/httpclientconnection.hpp"
-#include "remote/httprequest.hpp"
-
-#include <boost/program_options.hpp>
-
-//int QueryEndpoint(const String&, const String&, const String&, const String&);
-//void ResultHttpCompletionCallback(const icinga::HttpRequest&, icinga::HttpResponse&, bool&, boost::condition_variable&, boost::mutex&, icinga::Dictionary::Ptr&);
-//int MatchState(const String&);
-//int FormatOutput(const icinga::Dictionary::Ptr&);
-
-#endif // !CHECK_NSCP_H



More information about the icinga-checkins mailing list