]> granicus.if.org Git - icinga2/blobdiff - plugins/check_uptime.cpp
Make a few functions static
[icinga2] / plugins / check_uptime.cpp
index 3b3e91a79ec1284bfb991d9e497a43439a274d1c..568bb23eb444251c82acfd4c309d393346f0b5ff 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  * Icinga 2                                                                   *
- * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
+ * Copyright (C) 2012-2017 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                *
 #include <Shlwapi.h>
 #include <iostream>
 
-#include "thresholds.h"
+#include "check_uptime.h"
 
 #include "boost/chrono.hpp"
-#include "boost/program_options.hpp"
 
 #define VERSION 1.0
 
 namespace po = boost::program_options;
 
-using std::cout; using std::endl;
-using std::wcout; using std::wstring;
+static BOOL debug;
 
-struct printInfoStruct 
-{
-       threshold warn, crit;
-       long long time;
-       Tunit unit;
-};
-
-
-static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
-static int printOutput(printInfoStruct&);
-static void getUptime(printInfoStruct&);
-
-int main(int argc, wchar_t **argv)
+INT wmain(INT argc, WCHAR **argv)
 {
        po::variables_map vm;
        printInfoStruct printInfo = { };
-       int ret = parseArguments(argc, argv, vm, printInfo);
+       INT ret = parseArguments(argc, argv, vm, printInfo);
        
        if (ret != -1)
                return ret;
@@ -58,24 +44,24 @@ int main(int argc, wchar_t **argv)
        return printOutput(printInfo);
 }
 
-int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo) 
+INT parseArguments(INT ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo) 
 {
-       wchar_t namePath[MAX_PATH];
+       WCHAR namePath[MAX_PATH];
        GetModuleFileName(NULL, namePath, MAX_PATH);
-       wchar_t *progName = PathFindFileName(namePath);
+       WCHAR *progName = PathFindFileName(namePath);
 
        po::options_description desc;
        
        desc.add_options()
-               (",h", "print help message and exit")
-               ("help", "print verbose help and exit")
-               ("version,v", "print version and exit")
-               ("warning,w", po::wvalue<wstring>(), "warning threshold (Uses -unit)")
-               ("critical,c", po::wvalue<wstring>(), "critical threshold (Uses -unit)")
-               ("unit,u", po::wvalue<wstring>(), "desired unit of output\nh\t- hours\nm\t- minutes\ns\t- seconds (default)\nms\t- milliseconds")
+               ("help,h", "Print help message and exit")
+               ("version,V", "Print version and exit")
+               ("debug,d", "Verbose/Debug output")
+               ("warning,w", po::wvalue<std::wstring>(), "Warning threshold (Uses -unit)")
+               ("critical,c", po::wvalue<std::wstring>(), "Critical threshold (Uses -unit)")
+               ("unit,u", po::wvalue<std::wstring>(), "Unit to use:\nh\t- hours\nm\t- minutes\ns\t- seconds (default)\nms\t- milliseconds")
                ;
 
-       po::basic_command_line_parser<wchar_t> parser(ac, av);
+       po::basic_command_line_parser<WCHAR> parser(ac, av);
 
        try {
                po::store(
@@ -88,24 +74,19 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
                        vm);
                vm.notify();
        } catch (std::exception& e) {
-               cout << e.what() << endl << desc << endl;
+               std::cout << e.what() << '\n' << desc << '\n';
                return 3;
        }
 
-       if (vm.count("h")) {
-               cout << desc << endl;
-               return 0;
-       }
-
        if (vm.count("help")) {
-               wcout << progName << " Help\n\tVersion: " << VERSION << endl;
+               std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
                wprintf(
                        L"%s is a simple program to check a machines uptime.\n"
                        L"You can use the following options to define its behaviour:\n\n", progName);
-               cout << desc;
+               std::cout << desc;
                wprintf(
                        L"\nIt will then output a string looking something like this:\n\n"
-                       L"\tUPTIME WARNING 712h|uptime=712h;700;1800;0\n\n"
+                       L"\tUPTIME WARNING 712h | uptime=712h;700;1800;0\n\n"
                        L"\"UPTIME\" being the type of the check, \"WARNING\" the returned status\n"
                        L"and \"712h\" is the returned value.\n"
                        L"The performance data is found behind the \"|\", in order:\n"
@@ -136,46 +117,53 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
                        L"to end with a percentage sign.\n\n"
                        L"All of these options work with the critical threshold \"-c\" too.\n"
                        , progName);
-                       cout << endl;
+                       std::cout << '\n';
                return 0;
        }
 
        if (vm.count("version")) {
-               cout << VERSION << endl;
+               std::cout << VERSION << '\n';
                return 0;
        }
 
        if (vm.count("warning")) {
                try {
-                       printInfo.warn = threshold(vm["warning"].as<wstring>());
+                       printInfo.warn = threshold(vm["warning"].as<std::wstring>());
                } catch (std::invalid_argument& e) {
-                       cout << e.what() << endl;
+                       std::cout << e.what() << '\n';
                        return 3;
                }
        }
        if (vm.count("critical")) {
                try {
-                       printInfo.crit = threshold(vm["critical"].as<wstring>());
+                       printInfo.crit = threshold(vm["critical"].as<std::wstring>());
                } catch (std::invalid_argument& e) {
-                       cout << e.what() << endl;
+                       std::cout << e.what() << '\n';
                        return 3;
                }
        }
 
        if (vm.count("unit")) {
                try{
-                       printInfo.unit = parseTUnit(vm["unit"].as<wstring>());
+                       printInfo.unit = parseTUnit(vm["unit"].as<std::wstring>());
                } catch (std::invalid_argument) {
-
-               } wcout << L"Unknown unit type " << vm["unit"].as<wstring>() << endl;
+                       std::wcout << L"Unknown unit type " << vm["unit"].as<std::wstring>() << '\n';
+                       return 3;
+               } 
        } else
                printInfo.unit = TunitS;
-    
+
+       if (vm.count("debug"))
+               debug = TRUE;
+
        return -1;
 }
 
-static int printOutput(printInfoStruct& printInfo) 
+INT printOutput(printInfoStruct& printInfo) 
 {
+       if (debug)
+               std::wcout << L"Constructing output string" << '\n';
+
        state state = OK;
 
        if (printInfo.warn.rend(printInfo.time))
@@ -185,29 +173,35 @@ static int printOutput(printInfoStruct& printInfo)
 
        switch (state) {
        case OK:
-               wcout << L"UPTIME OK " << printInfo.time << TunitStr(printInfo.unit) << L"|uptime=" << printInfo.time 
-                       << TunitStr(printInfo.unit) << L";" << printInfo.warn.pString() << L";" 
-                       << printInfo.crit.pString() << L";0" << endl;
+               std::wcout << L"UPTIME OK " << printInfo.time << TunitStr(printInfo.unit) << L" | uptime=" << printInfo.time
+                       << TunitStr(printInfo.unit) << L";" << printInfo.warn.pString() << L";"
+                       << printInfo.crit.pString() << L";0;" << '\n';
                break;
        case WARNING:
-               wcout << L"UPTIME WARNING " << printInfo.time << TunitStr(printInfo.unit) << L"|uptime=" << printInfo.time
+               std::wcout << L"UPTIME WARNING " << printInfo.time << TunitStr(printInfo.unit) << L" | uptime=" << printInfo.time
                        << TunitStr(printInfo.unit) << L";" << printInfo.warn.pString() << L";"
-                       << printInfo.crit.pString() << L";0" << endl;
+                       << printInfo.crit.pString() << L";0;" << '\n';
                break;
        case CRITICAL:
-               wcout << L"UPTIME CRITICAL " << printInfo.time << TunitStr(printInfo.unit) << L"|uptime=" << printInfo.time
+               std::wcout << L"UPTIME CRITICAL " << printInfo.time << TunitStr(printInfo.unit) << L" | uptime=" << printInfo.time
                        << TunitStr(printInfo.unit) << L";" << printInfo.warn.pString() << L";"
-                       << printInfo.crit.pString() << L";0" << endl;
+                       << printInfo.crit.pString() << L";0;" << '\n';
                break;
        }
 
        return state;
 }
 
-void getUptime(printInfoStruct& printInfo) 
+VOID getUptime(printInfoStruct& printInfo) 
 {
+       if (debug)
+               std::wcout << L"Getting uptime in milliseconds" << '\n';
+
        boost::chrono::milliseconds uptime = boost::chrono::milliseconds(GetTickCount64());
        
+       if (debug)
+               std::wcout << L"Converting requested unit (default: seconds)" << '\n';
+
        switch (printInfo.unit) {
        case TunitH: 
                printInfo.time = boost::chrono::duration_cast<boost::chrono::hours>(uptime).count();
@@ -222,5 +216,4 @@ void getUptime(printInfoStruct& printInfo)
                printInfo.time = uptime.count();
                break;
        }
-       
-}
\ No newline at end of file
+}