]> granicus.if.org Git - icinga2/blobdiff - plugins/check_nscp_api.cpp
Merge pull request #6882 from Icinga/bugfix/influxdb-gaps-6841
[icinga2] / plugins / check_nscp_api.cpp
index 828e391dfffcd34641003e9f75dad11e1e713765..f48c63ab88c7f856bf8799aadc2a5947d85d33b0 100644 (file)
@@ -1,23 +1,23 @@
 /******************************************************************************
-* Icinga 2                                                                   *
-* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
-*                                                                            *
-* 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.             *
-******************************************************************************/
-
-#define VERSION "1.0.0"
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
+ *                                                                            *
+ * 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.             *
+ ******************************************************************************/
+
+#define VERSION "1.0.1"
 
 #include "remote/httpclientconnection.hpp"
 #include "remote/httprequest.hpp"
@@ -25,6 +25,7 @@
 #include "base/application.hpp"
 #include "base/json.hpp"
 #include "base/string.hpp"
+#include "base/logger.hpp"
 #include "base/exception.hpp"
 #include <boost/program_options.hpp>
 #include <boost/algorithm/string/split.hpp>
@@ -33,7 +34,7 @@
 using namespace icinga;
 namespace po = boost::program_options;
 
-bool l_Debug = false;
+static bool l_Debug;
 
 /*
  * This function is called by an 'HttpRequest' once the server answers. After doing a short check on the 'response' it
@@ -88,9 +89,15 @@ static Dictionary::Ptr QueryEndpoint(const String& host, const String& port, con
 
                // Url() will call Utillity::UnescapeString() which will thrown an exception if it finds a lonely %
                req->RequestUrl = new Url(endpoint);
+
+               // NSClient++ uses `time=1m&time=5m` instead of `time[]=1m&time[]=5m`
+               req->RequestUrl->SetArrayFormatUseBrackets(false);
+
                req->AddHeader("password", password);
-               if (l_Debug)
-                       std::cout << "Sending request to 'https://" << host << ":" << port << req->RequestUrl->Format() << "'\n";
+               if (l_Debug) {
+                       std::cout << "Sending request to 'https://" << host << ":" << port << req->RequestUrl->Format(false, false) << "'\n"
+                               << "Headers: " << JsonEncode(req->Headers) << "\n";
+               }
 
                // Submits the request. The 'ResultHttpCompletionCallback' is called once the HttpRequest receives an answer,
                // which then sets 'ready' to true
@@ -219,7 +226,7 @@ static int FormatOutput(const Dictionary::Ptr& result)
                state == "UNKNOWN" ? 3 : 4;
 
        if (creturn == 4) {
-               std::cout << "check_nscp UNKNOWN Answer format error: 'result' was not a known state.\n";
+               std::cout << "check_nscp_api UNKNOWN Answer format error: 'result' was not a known state.\n";
                return 3;
        }
 
@@ -245,7 +252,7 @@ int main(int argc, char **argv)
                ("query,q", po::value<std::string>()->required(), "REQUIRED: NSCP API Query endpoint")
                ("arguments,a", po::value<std::vector<std::string>>()->multitoken(), "NSCP API Query arguments for the endpoint");
 
-       po::basic_command_line_parser<char> parser(argc, argv);
+       po::command_line_parser parser(argc, argv);
 
        try {
                po::store(
@@ -271,14 +278,18 @@ int main(int argc, char **argv)
                }
 
                vm.notify();
-       } catch (std::exception& e) {
+       } catch (const std::exception& e) {
                std::cout << e.what() << '\n' << desc << '\n';
                Application::Exit(3);
        }
 
-       if (vm.count("debug")) {
-               l_Debug = true;
-       }
+       l_Debug = vm.count("debug") > 0;
+
+       // Initialize logger
+       if (l_Debug)
+               Logger::SetConsoleLogSeverity(LogDebug);
+       else
+               Logger::SetConsoleLogSeverity(LogWarning);
 
        // Create the URL string and escape certain characters since Url() follows RFC 3986
        String endpoint = "/query/" + vm["query"].as<std::string>();