]> granicus.if.org Git - icinga2/commitdiff
Clean up check plugins.
authorJean Flach <jean-marcel.flach@netways.de>
Fri, 14 Nov 2014 12:57:54 +0000 (13:57 +0100)
committerJean Flach <jean-marcel.flach@netways.de>
Fri, 14 Nov 2014 12:57:54 +0000 (13:57 +0100)
fixes #7670 #7669

plugins/check_disk.cpp
plugins/check_load.cpp
plugins/check_network.cpp
plugins/check_swap.cpp
plugins/check_update.cpp
plugins/check_uptime.cpp
plugins/check_users.cpp

index f423bc372d0d50bf81f98ebdca9cea7fea2c84cd..4c8d7a370c0a0f4c4f3a308af163a2c2fa72e3c4 100644 (file)
@@ -25,7 +25,7 @@
 
 #include "thresholds.h"
 
-#include "boost\program_options.hpp"
+#include "boost/program_options.hpp"
 
 #define VERSION 1.0
 
index 4a3c7b9d6ff8d6d4d5f4aa1e159137db331656bc..bcc3b18eb635ad1d34d67c5b060e8ba493972152 100644 (file)
@@ -23,7 +23,7 @@
 
 #include "thresholds.h"
 
-#include "boost\program_options.hpp"
+#include "boost/program_options.hpp"
 
 #define VERSION 1.0
 
@@ -41,6 +41,7 @@ struct printInfoStruct
 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
 static int printOutput(printInfoStruct&);
 static int check_load(printInfoStruct&);
+static void die(DWORD err = 0);
 
 int wmain(int argc, wchar_t **argv) 
 {
@@ -196,32 +197,49 @@ int check_load(printInfoStruct& printInfo)
        DWORD dwBufferSize = 0;
        DWORD CounterType;
        PDH_FMT_COUNTERVALUE DisplayValue;
+       PDH_STATUS err;
 
        LPCWSTR path = L"\\Processor(_Total)\\% Idle Time";
 
-       if (PdhOpenQuery(NULL, NULL, &phQuery) != ERROR_SUCCESS)
-               goto cleanup;
+       err = PdhOpenQuery(NULL, NULL, &phQuery);
+       if (!SUCCEEDED(err))
+               goto die;
 
-       if (PdhAddEnglishCounter(phQuery, path, NULL, &phCounter) != ERROR_SUCCESS)
-               goto cleanup;
+       err = PdhAddEnglishCounter(phQuery, path, NULL, &phCounter);
+       if (!SUCCEEDED(err))
+               goto die;
 
-       if (PdhCollectQueryData(phQuery) != ERROR_SUCCESS)
-               goto cleanup;
+       err = PdhCollectQueryData(phQuery);
+       if (!SUCCEEDED(err))
+               goto die;
 
        Sleep(1000);
 
-       if (PdhCollectQueryData(phQuery) != ERROR_SUCCESS)
-               goto cleanup;
+       err = PdhCollectQueryData(phQuery);
+       if (!SUCCEEDED(err))
+               goto die;
 
-       if (PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue) == ERROR_SUCCESS) {
+       err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
+       if (SUCCEEDED(err)) {
                if (DisplayValue.CStatus == PDH_CSTATUS_VALID_DATA)
                        printInfo.load = 100.0 - DisplayValue.doubleValue;
                PdhCloseQuery(phQuery);
                return -1;
        }
 
-cleanup:
+die:
+       die();
        if (phQuery)
                PdhCloseQuery(phQuery);
        return 3;
+}
+
+void die(DWORD err)
+{
+       if (!err)
+               err = GetLastError();
+       LPWSTR mBuf = NULL;
+       size_t mS = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+                                                         NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&mBuf, 0, NULL);
+       wcout << mBuf << endl;
 }
\ No newline at end of file
index b54dcb30f5992b09ece2a1a5beac95eff607a45e..9e21a107e22adfd7ee1be16bc757e5e864202829 100644 (file)
@@ -45,7 +45,7 @@ struct printInfoStruct
        threshold warn, crit;
 };
 
-static void die(const DWORD err);
+static void die(DWORD err = 0);
 static int parseArguments(int, TCHAR **, po::variables_map&, printInfoStruct&);
 static int printOutput(printInfoStruct&, const vector<nInterface>&);
 static int check_network(vector<nInterface>&);
@@ -217,45 +217,45 @@ int check_network(vector <nInterface>& vInterfaces)
        PDH_STATUS err;
 
        err = PdhOpenQuery(NULL, NULL, &phQuery);
-       if (err != ERROR_SUCCESS)
+       if (!SUCCEEDED(err))
                goto die;
 
        err = PdhAddEnglishCounter(phQuery, perfIn, NULL, &phCounterIn);
-       if (err != ERROR_SUCCESS
+       if (!SUCCEEDED(err)
                goto die;
        
        err = PdhAddEnglishCounter(phQuery, perfOut, NULL, &phCounterOut);
-       if (err != ERROR_SUCCESS
+       if (!SUCCEEDED(err)
                goto die;
        
        err = PdhCollectQueryData(phQuery);
-       if (err != ERROR_SUCCESS)
+       if (!SUCCEEDED(err))
                goto die;
        
        Sleep(1000);
 
        err = PdhCollectQueryData(phQuery);
-       if (err != ERROR_SUCCESS)
+       if (!SUCCEEDED(err))
                goto die;
 
        err = PdhGetFormattedCounterArray(phCounterIn, PDH_FMT_LONG, &dwBufferSizeIn, &dwItemCount, pDisplayValuesIn);
-       if (err == PDH_MORE_DATA || err == ERROR_SUCCESS)
+       if (err == PDH_MORE_DATA || SUCCEEDED(err))
                pDisplayValuesIn = new PDH_FMT_COUNTERVALUE_ITEM[dwItemCount*dwBufferSizeIn];
        else
                goto die;
        
        err = PdhGetFormattedCounterArray(phCounterOut, PDH_FMT_LONG, &dwBufferSizeOut, &dwItemCount, pDisplayValuesOut);
-       if (err == PDH_MORE_DATA || err == ERROR_SUCCESS)
+       if (err == PDH_MORE_DATA || SUCCEEDED(err))
                pDisplayValuesOut = new PDH_FMT_COUNTERVALUE_ITEM[dwItemCount*dwBufferSizeOut];
        else
                goto die;
 
        err = PdhGetFormattedCounterArray(phCounterIn, PDH_FMT_LONG, &dwBufferSizeIn, &dwItemCount, pDisplayValuesIn);
-       if (err != ERROR_SUCCESS)
+       if (!SUCCEEDED(err))
                goto die;
 
        err = PdhGetFormattedCounterArray(phCounterOut, PDH_FMT_LONG, &dwBufferSizeOut, &dwItemCount, pDisplayValuesOut);
-       if (err != ERROR_SUCCESS)
+       if (!SUCCEEDED(err))
                goto die;
 
        for (DWORD i = 0; i < dwItemCount; i++) {
@@ -279,7 +279,7 @@ die:
        return 3;
 }
 
-void die(DWORD err = 0
+void die(DWORD err) 
 {
        if (!err)
                err = GetLastError();
index 72595dea3394e3c74739899cd0c743cb6183e2be..5926d82844468822622eba01cc5899491a430a50 100644 (file)
@@ -22,7 +22,7 @@
 
 #include "thresholds.h"
 
-#include "boost\program_options.hpp"
+#include "boost/program_options.hpp"
 
 #define VERSION 1.0
 
@@ -40,6 +40,7 @@ struct printInfoStruct
 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
 static int printOutput(printInfoStruct&);
 static int check_swap(printInfoStruct&);
+static void die(DWORD err = 0);
 
 int wmain(int argc, wchar_t **argv) 
 {
@@ -194,26 +195,42 @@ int check_swap(printInfoStruct& printInfo)
        DWORD dwBufferSize = 0;
        DWORD CounterType;
        PDH_FMT_COUNTERVALUE DisplayValue;
+       PDH_STATUS err;
 
        LPCWSTR path = L"\\Paging File(*)\\% Usage";
 
-       if (PdhOpenQuery(NULL, NULL, &phQuery) != ERROR_SUCCESS)
-               goto cleanup;
+       err = PdhOpenQuery(NULL, NULL, &phQuery);
+       if (!SUCCEEDED(err))
+               goto die;
 
-       if (PdhAddEnglishCounter(phQuery, path, NULL, &phCounter) != ERROR_SUCCESS)
-               goto cleanup;
+       err = PdhAddEnglishCounter(phQuery, path, NULL, &phCounter);
+       if (!SUCCEEDED(err))
+               goto die;
 
-       if (PdhCollectQueryData(phQuery) != ERROR_SUCCESS)
-               goto cleanup;
+       err = PdhCollectQueryData(phQuery);
+       if (!SUCCEEDED(err))
+               goto die;
 
-       if (PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue) == ERROR_SUCCESS) {
+       err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
+       if (SUCCEEDED(err)) {
                printInfo.swap = DisplayValue.doubleValue;
                PdhCloseQuery(phQuery);
                return -1;
        }
 
-cleanup:
+die:
        if (phQuery)
                PdhCloseQuery(phQuery);
+       die(err);
        return 3;
+}
+
+void die(DWORD err)
+{
+       if (!err)
+               err = GetLastError();
+       LPWSTR mBuf = NULL;
+       size_t mS = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+                                                         NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&mBuf, 0, NULL);
+       wcout << mBuf << endl;
 }
\ No newline at end of file
index b646cf106075b0d996b6566af7c305ae3acbbf7f..346e8bae8e8cf32811ecbf691c7c55af7ef7913b 100644 (file)
@@ -45,6 +45,7 @@ struct printInfoStruct
 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
 static int printOutput(const printInfoStruct&);
 static int check_update(printInfoStruct&);
+static void die(DWORD err = 0);
 
 int main(int argc, wchar_t **argv) 
 {
@@ -186,10 +187,11 @@ int check_update(printInfoStruct& printInfo)
        IUpdateSession *pSession;
        IUpdateSearcher *pSearcher;
 
+       HRESULT err;
+
        CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSession, (LPVOID*)&pSession);
        pSession->CreateUpdateSearcher(&pSearcher);
 
-
        /*
         IsInstalled = 0: All updates, including languagepacks and features
         BrowseOnly = 0: No features or languagepacks, security and unnamed
@@ -201,7 +203,8 @@ int check_update(printInfoStruct& printInfo)
        // http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526%28v=vs.85%29.aspx
        // http://msdn.microsoft.com/en-us/library/ff357803%28v=vs.85%29.aspx
 
-       if (pSearcher->Search(criteria, &pResult) != S_OK)
+       err = pSearcher->Search(criteria, &pResult);
+       if (!SUCCEEDED(err))
                goto die;
        SysFreeString(criteria);
 
@@ -241,5 +244,16 @@ int check_update(printInfoStruct& printInfo)
 die:
        if (criteria)
                SysFreeString(criteria);
+       die(err);
        return 3;
+}
+
+void die(DWORD err)
+{
+       if (!err)
+               err = GetLastError();
+       LPWSTR mBuf = NULL;
+       size_t mS = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+                                                         NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&mBuf, 0, NULL);
+       wcout << mBuf << endl;
 }
\ No newline at end of file
index 9d23b77f5aae257f92c9e805fe554a04cebd6d61..ba0276dd5fc09309076529d0a0fd17d4af8f34fb 100644 (file)
@@ -22,8 +22,8 @@
 
 #include "thresholds.h"
 
-#include "boost\chrono.hpp"
-#include "boost\program_options.hpp"
+#include "boost/chrono.hpp"
+#include "boost/program_options.hpp"
 
 #define VERSION 1.0
 
index 65379bda7d953588a71bef4ad1e4229cbb35aff7..fb418de5ecbe32c00b7a7c63da9623895b7faa7a 100644 (file)
@@ -23,7 +23,7 @@
 
 #include "thresholds.h"
 
-#include "boost\program_options.hpp"
+#include "boost/program_options.hpp"
 
 #define VERSION 1.0
 
@@ -35,7 +35,7 @@ using std::cout; using std::wstring;
 struct printInfoStruct 
 {
        threshold warn, crit;
-       int users;
+       double users;
 };
 
 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
@@ -190,13 +190,15 @@ int printOutput(printInfoStruct& printInfo)
 
 int check_users(printInfoStruct& printInfo) 
 {
-       int users = 0;
-       WTS_SESSION_INFOW *pSessionInfo;
+       double users = 0;
+       WTS_SESSION_INFOW *pSessionInfo = NULL;
        DWORD count;
        DWORD index;
 
        if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionInfo, &count)) {
                wcout << L"Failed to enumerate terminal sessions" << endl;
+               if (pSessionInfo)
+                       WTSFreeMemory(pSessionInfo);
                return 3;
        }
 
@@ -206,7 +208,7 @@ int check_users(printInfoStruct& printInfo)
                int len;
 
                if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, pSessionInfo[index].SessionId,
-                       WTSUserName, &name, &size))
+                                                                               WTSUserName, &name, &size))
                        continue;
 
                len = lstrlenW(name);
@@ -223,5 +225,4 @@ int check_users(printInfoStruct& printInfo)
        WTSFreeMemory(pSessionInfo);
        printInfo.users = users;
        return -1;
-
 }
\ No newline at end of file