]> granicus.if.org Git - icinga2/commitdiff
Fix check_plugins dying with certain parameters
authorJean Flach <jean-marcel.flach@netways.de>
Mon, 10 Nov 2014 13:39:12 +0000 (14:39 +0100)
committerJean Flach <jean-marcel.flach@netways.de>
Mon, 10 Nov 2014 13:39:12 +0000 (14:39 +0100)
plugins/check_disk.cpp
plugins/check_load.cpp
plugins/check_network.cpp
plugins/check_procs.cpp
plugins/check_service.cpp
plugins/check_swap.cpp
plugins/check_update.cpp
plugins/check_uptime.cpp
plugins/check_users.cpp
plugins/thresholds.cpp

index a1daaecf475e8d8ff569b02edb66a76e307e7706..1eaaa5a0168b94dfff59771bd0e644c21496b3b3 100644 (file)
@@ -170,12 +170,22 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
        if (vm.count("version"))
                cout << "Version: " << VERSION << endl;
 
-       if (vm.count("warning")) 
-               printInfo.warn = parse(vm["warning"].as<wstring>());
-       
-
-       if (vm.count("critical")) 
-               printInfo.crit = parse(vm["critical"].as<wstring>());
+       if (vm.count("warning")) {
+               try {
+                       printInfo.warn = parse(vm["warning"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
+       if (vm.count("critical")) {
+               try {
+                       printInfo.crit = parse(vm["critical"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
        
        if (vm.count("drives")) 
                printInfo.drives = vm["drives"].as<vector<wstring>>();
@@ -205,11 +215,6 @@ int printOutput(printInfoStruct& printInfo, vector<drive>& vDrives)
                perf << L" drive=\"" << it->name << L"\";cap=" << it->cap << unit << L";free=" << it->free << unit;
        }
 
-       if (!printInfo.warn.set && !printInfo.crit.set) {
-               wcout << L"DISK OK " << tFree << unit << endl;
-        return 0;
-       }
-
        prePerf << L"|disk=" << tFree << unit << L";" << printInfo.warn.pString() << L";"
                << printInfo.crit.pString() << L";0;" << tCap;
 
index bbe74ff6c9699946b85a8011ee45fa51abd0cf75..4a3c7b9d6ff8d6d4d5f4aa1e159137db331656bc 100644 (file)
@@ -140,11 +140,22 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
        if (vm.count("version"))
                cout << "Version: " << VERSION << endl;
 
-       if (vm.count("warning")) 
-               printInfo.warn = parse(vm["warning"].as<wstring>());
-
-       if (vm.count("critical"))
-               printInfo.crit = parse(vm["critical"].as<wstring>());
+       if (vm.count("warning")) {
+               try {
+                       printInfo.warn = parse(vm["warning"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
+       if (vm.count("critical")) {
+               try {
+                       printInfo.crit = parse(vm["critical"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
 
        return -1;
 }
@@ -152,10 +163,6 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
 int printOutput(printInfoStruct& printInfo) 
 {
        state state = OK;
-       
-       if (!printInfo.warn.set && !printInfo.crit.set) {
-               wcout << L"LOAD OK " << printInfo.load << L"%" << endl;
-       }
 
        if (printInfo.warn.rend(printInfo.load))
                state = WARNING;
index 0c74c18f680f447c86e3891508746b7087fa30d4..8ae675269d8045fdd0f0892cbdfce0966741561c 100644 (file)
@@ -151,11 +151,22 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
        if (vm.count("version"))
                cout << "Version: " << VERSION << endl;
 
-       if (vm.count("warning"))
-               printInfo.warn = parse(vm["warning"].as<wstring>());
-
-       if (vm.count("critical")) 
-               printInfo.crit = parse(vm["critical"].as<wstring>());
+       if (vm.count("warning")) {
+               try {
+                       printInfo.warn = parse(vm["warning"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
+       if (vm.count("critical")) {
+               try {
+                       printInfo.crit = parse(vm["critical"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
        
        return -1;
 }
@@ -172,10 +183,6 @@ int printOutput(printInfoStruct& printInfo, const vector<nInterface>& vInterface
                tss << L"netI=\"" << it->name << L"\";in=" << it->BytesInSec << L"B/s;out=" << it->BytesOutSec << L"B/s ";
        }
 
-       if (!printInfo.warn.set && !printInfo.crit.set) {
-               wcout << L"NETWORK OK " << tIn+tOut << endl;
-       }
-
        if (printInfo.warn.rend(tIn + tOut))
                state = WARNING;
        if (printInfo.crit.rend(tIn + tOut))
index bbbcd5cd3edc6506e182200acc75fab4ab700059..87498b079a264134720e79e274d32cb475ecad58 100644 (file)
@@ -63,11 +63,6 @@ int printOutput(const int numProcs, printInfoStruct& printInfo)
 {
        state state = OK;
 
-       if (!printInfo.warn.set && !printInfo.crit.set) {
-               wcout << L"PROCS OK " << numProcs << endl;
-        return 0;
-       }
-
        if (printInfo.warn.rend(numProcs))
                state = WARNING;
 
@@ -178,11 +173,22 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
                return 0;
        }
 
-       if (vm.count("warning"))
-               printInfo.warn = parse(vm["warning"].as<wstring>());
-
-       if (vm.count("critical"))
-               printInfo.crit = parse(vm["critical"].as<wstring>());
+       if (vm.count("warning")) {
+               try {
+                       printInfo.warn = parse(vm["warning"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
+       if (vm.count("critical")) {
+               try {
+                       printInfo.crit = parse(vm["critical"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
 
        if (vm.count("user")) 
                printInfo.user = vm["user"].as<wstring>();
index b4f31b52a8ac0e57cb40857fa31e89b9e43670e2..4824cd2f682ee9bcc252b09f35d2afa259cd7753 100644 (file)
@@ -104,7 +104,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
                cout << desc;
                wprintf(
                        L"\nIt will then output a string looking something like this:\n\n"
-                       L"\tSERVICE CRITICAL NOT_RUNNING\n\n"
+                       L"\tSERVICE CRITICAL NOT_RUNNING|service=4;!4;!4;1;7\n\n"
                        L"\"SERVICE\" being the type of the check, \"CRITICAL\" the returned status\n"
                        L"and \"1\" is the returned value.\n"
                        L"A service is either running (Code 0x04) or not running (any other).\n"
@@ -150,13 +150,13 @@ int printOutput(const printInfoStruct& printInfo)
 
        switch (state) {
        case OK:
-               wcout << L"SERVICE OK RUNNING" << endl;
+               wcout << L"SERVICE OK RUNNING|service=4;!4;!4;1;7" << endl;
                break;
        case WARNING:
-               wcout << L"SERVICE WARNING NOT_RUNNING" << endl;
+               wcout << L"SERVICE WARNING NOT_RUNNING|service=" << printInfo.ServiceState << ";!4;!4;1;7" << endl;
                break;
        case CRITICAL:
-               wcout << L"SERVICE CRITICAL NOT_RUNNING" << endl;
+               wcout << L"SERVICE CRITICAL NOT_RUNNING|service=" << printInfo.ServiceState << ";!4;!4;1;7" << endl;
                break;
        }
     
index d53c06000cb01d4782adef30100da7fb47a776c6..72595dea3394e3c74739899cd0c743cb6183e2be 100644 (file)
@@ -139,11 +139,22 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
        if (vm.count("version"))
                wcout << L"Version: " << VERSION << endl;
 
-       if (vm.count("warning")) 
-               printInfo.warn = parse(vm["warning"].as<wstring>());
-
-       if (vm.count("critical")) 
-               printInfo.crit = parse(vm["critical"].as<wstring>());
+       if (vm.count("warning")) {
+               try {
+                       printInfo.warn = parse(vm["warning"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
+       if (vm.count("critical")) {
+               try {
+                       printInfo.crit = parse(vm["critical"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
 
        return -1;
 }
@@ -152,11 +163,6 @@ int printOutput(printInfoStruct& printInfo)
 {
        state state = OK;
 
-       if (!printInfo.warn.set && !printInfo.crit.set) {
-               wcout << L"SWAP OK " << printInfo.swap << L"%" << endl;
-        return 0;
-       }
-
        if (printInfo.warn.rend(printInfo.swap))
                state = WARNING;
 
index a3445b9d664bec45896d5114ad343e21077111fd..b646cf106075b0d996b6566af7c305ae3acbbf7f 100644 (file)
@@ -67,11 +67,6 @@ int printOutput(const printInfoStruct& printInfo)
        state state = OK;
        wstring output = L"UPDATE ";
 
-       if (!printInfo.warn && !printInfo.crit) {
-               wcout << L"UPDATE OK " << printInfo.numUpdates << endl;
-        return 0;
-       }
-
        if (printInfo.important)
                state = WARNING;
 
index ded0e48683b922f90e1f3ba852e6e8c047d3697e..bcd8fbf343ed6f410ed39c75cf29b7c1f0035751 100644 (file)
@@ -145,11 +145,22 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
                return 0;
        }
 
-       if (vm.count("warning"))
-               printInfo.warn = parse(vm["warning"].as<wstring>());
-
-       if (vm.count("critical"))
-               printInfo.crit = parse(vm["critical"].as<wstring>());
+       if (vm.count("warning")) {
+               try {
+                       printInfo.warn = parse(vm["warning"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
+       if (vm.count("critical")) {
+               try {
+                       printInfo.crit = parse(vm["critical"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
 
        if (vm.count("unit")) {
                try{
@@ -167,11 +178,6 @@ static int printOutput(printInfoStruct& printInfo)
 {
        state state = OK;
 
-       if (!printInfo.warn.set && !printInfo.crit.set) {
-               wcout << L"UPTIME OK " << printInfo.time << TunitStr(printInfo.unit) << endl;
-        return 0;
-       }
-
        if (printInfo.warn.rend(printInfo.time))
                state = WARNING;
        if (printInfo.crit.rend(printInfo.time))
index 311464acc4d2f19ebc987fb8ae755f1393b5722a..65379bda7d953588a71bef4ad1e4229cbb35aff7 100644 (file)
@@ -140,11 +140,22 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
        if (vm.count("version"))
                wcout << L"Version: " << VERSION << endl;
 
-       if (vm.count("warning")) 
-               printInfo.warn = parse(vm["warning"].as<wstring>());
-
-       if (vm.count("critical")) 
-               printInfo.crit = parse(vm["critical"].as<wstring>());
+       if (vm.count("warning")) {
+               try {
+                       printInfo.warn = parse(vm["warning"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
+       if (vm.count("critical")) {
+               try {
+                       printInfo.crit = parse(vm["critical"].as<wstring>());
+               } catch (std::invalid_argument& e) {
+                       cout << e.what() << endl;
+                       return 3;
+               }
+       }
 
        return -1;
 }
@@ -152,11 +163,6 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
 int printOutput(printInfoStruct& printInfo) 
 {
        state state = OK;
-       
-       if (!printInfo.warn.set && !printInfo.crit.set) {
-               wcout << L"USERS OK " << printInfo.users << endl;
-        return 0;
-       }
 
        if (printInfo.warn.rend(printInfo.users))
                state = WARNING;
@@ -166,15 +172,15 @@ int printOutput(printInfoStruct& printInfo)
 
        switch (state) {
        case OK:
-               wcout << L"USERS OK " << printInfo.users << L"|users=" << printInfo.users << L";" 
+               wcout << L"USERS OK " << printInfo.users << L"User|users=" << printInfo.users << L";" 
                        << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0" << endl;
                break;
        case WARNING:
-               wcout << L"USERS WARNING " << printInfo.users << L"|users=" << printInfo.users << L";"
+               wcout << L"USERS WARNING " << printInfo.users << L"User|users=" << printInfo.users << L";"
                        << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0" << endl;
                break;
        case CRITICAL:
-               wcout << L"USERS CRITICAL " << printInfo.users << L"|users=" << printInfo.users << L";"
+               wcout << L"USERS CRITICAL " << printInfo.users << L"User|users=" << printInfo.users << L";"
                        << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0" << endl;
                break;
        }
index 3a436e38e8833c30a071068c87b558ee11459da7..aa4c4d957a51d9809b990313c9dffa6b00e4d7ad 100644 (file)
@@ -28,7 +28,7 @@ using std::wstring;
 threshold parse(const wstring& stri)
 {
        if (stri.empty())
-               throw std::invalid_argument("thresholds must not be empty");
+               throw std::invalid_argument("Threshold must not be empty");
 
        wstring str = stri;
 
@@ -43,7 +43,7 @@ threshold parse(const wstring& stri)
                std::vector<wstring> svec;
                boost::split(svec, str, boost::is_any_of(L"-"));
                if (svec.size() != 2)
-                       throw std::invalid_argument("threshold range requires two arguments");
+                       throw std::invalid_argument("Threshold range requires two arguments");
                wstring str1 = svec.at(0), str2 = svec.at(1);
 
                if (str1.at(str1.length() - 1) == L'%' && str2.at(str2.length() - 1) == L'%') {
@@ -57,7 +57,7 @@ threshold parse(const wstring& stri)
                        double d2 = boost::lexical_cast<double>(str2);
                        return threshold(d1, d2, !low, perc);
                } catch (const boost::bad_lexical_cast&) {
-                       throw std::invalid_argument("threshold must be a number");
+                       throw std::invalid_argument("Unknown Threshold type");
                }
        } else { //not range
                if (str.at(str.length() - 1) == L'%') {
@@ -69,7 +69,7 @@ threshold parse(const wstring& stri)
                        return threshold(d, d, !low, perc);
 
                } catch (const boost::bad_lexical_cast&) {
-                       throw std::invalid_argument("threshold must be a number");
+                       throw std::invalid_argument("Unknown Threshold type");
                }
        }
 }