]> granicus.if.org Git - icinga2/commitdiff
Change how check_swap works and a debug line in check_disk
authorJean Flach <jean-marcel.flach@netways.de>
Mon, 2 Feb 2015 10:11:43 +0000 (11:11 +0100)
committerJean Flach <jean-marcel.flach@netways.de>
Mon, 2 Feb 2015 10:21:44 +0000 (11:21 +0100)
check_swap should now be faster and more accurate, it also checks against free space instead of taken swap.
The output and CMakeLists have been updated accordingly.

fixes #8287 refs #8060

plugins/CMakeLists.txt
plugins/check_disk.cpp
plugins/check_swap.cpp

index 0a665538c33968ed8358cda396446e7588a1d744..aa11f11a9f828f9a14b6de9fc8f7e3913f7d6172 100644 (file)
 # along with this program; if not, write to the Free Software Foundation
 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
-if (WIN32)
+if ( WIN32 )
 
-  add_definitions(-DUNICODE -D_UNICODE)
+  add_definitions( -DUNICODE -D_UNICODE )
   
-  add_library(thresholds thresholds)
+  add_library( thresholds thresholds )
     set_target_properties (
     thresholds PROPERTIES
     INSTALL_RPATH ${CMAKE_INSTALL_FULL_LIBDIR}/icinga2
     FOLDER Plugins
   )
   
-  list(APPEND check_SOURCES 
+  list( APPEND check_SOURCES 
     check_disk.cpp check_load.cpp check_network.cpp check_ping.cpp check_procs.cpp check_service.cpp 
-               check_swap.cpp check_update.cpp check_uptime.cpp check_users.cpp)
+               check_swap.cpp check_update.cpp check_uptime.cpp check_users.cpp )
   
-  foreach (source ${check_SOURCES}
-    string (REGEX REPLACE ".cpp\$" "" check_OUT "${source}")
+  foreach ( source ${check_SOURCES} 
+    string ( REGEX REPLACE ".cpp\$" "" check_OUT "${source}" )
                
-    add_executable (${check_OUT} ${source})
-    target_link_libraries(${check_OUT} thresholds Shlwapi.lib ${Boost_PROGRAM_OPTIONS_LIBRARY})
+    add_executable ( ${check_OUT} ${source} )
+    target_link_libraries( ${check_OUT} thresholds Shlwapi.lib ${Boost_PROGRAM_OPTIONS_LIBRARY} )
     
     set_target_properties (
       ${check_OUT} PROPERTIES
@@ -43,16 +43,15 @@ if (WIN32)
       FOLDER Plugins)
   endforeach(source)
 
-  target_link_libraries(check_load Pdh.lib)
-  target_link_libraries(check_network Pdh.lib)
-       target_link_libraries(check_ping Ntdll.lib iphlpapi.lib Ws2_32.lib )
-  target_link_libraries(check_procs Pdh.lib)
-  target_link_libraries(check_swap Pdh.lib)
-  target_link_libraries(check_uptime ${Boost_SYSTEM_LIBRARY})
-  target_link_libraries(check_users wtsapi32.lib)
+  target_link_libraries( check_load Pdh.lib )
+  target_link_libraries( check_network Pdh.lib )
+       target_link_libraries( check_ping Ntdll.lib iphlpapi.lib Ws2_32.lib )
+  target_link_libraries( check_procs Pdh.lib )
+  target_link_libraries( check_uptime ${Boost_SYSTEM_LIBRARY} )
+  target_link_libraries( check_users wtsapi32.lib )
 
   install (
     TARGETS check_disk check_load check_network check_procs check_ping check_service check_swap check_update check_uptime check_users
-    RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR})
+    RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR} )
     
 endif()
\ No newline at end of file
index 5b2e12a6486057e871da7ec24369410c80206968..549bc5ec58f1b578dbbe9831eb723de8ddad7420 100644 (file)
@@ -312,7 +312,7 @@ int check_drives(vector<drive>& vDrives)
                FindNextVolume(hVolume, szVolumeName, MAX_PATH);
        }
        if (debug)
-               wcout << L"Creating vector from found volumes, removing cd drives etc.:" << endl;
+               wcout << L"Creating vector from found volumes, ignoring cd drives etc.:" << endl;
        for (set<wstring>::iterator it = sDrives.begin(); it != sDrives.end(); ++it) {
                UINT type = GetDriveType(it->c_str());
                if (type == DRIVE_FIXED || type == DRIVE_REMOTE) {
index ab9519f692867817730835d742d8e5941b5c1041..d627e41f2df5ec143001c245a2e5a43678c48b90 100644 (file)
@@ -17,8 +17,8 @@
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
  ******************************************************************************/
 #include <Shlwapi.h>
-#include <Pdh.h>
 #include <iostream>
+#include <WinBase.h>
 
 #include "thresholds.h"
 
@@ -36,7 +36,7 @@ static BOOL debug = FALSE;
 struct printInfoStruct 
 {
        threshold warn, crit;
-       double swap;
+       DWORD tSwap, aSwap;
 };
 
 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
@@ -100,7 +100,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"\tSWAP WARNING 23.8304%%|swap=23.8304%%;19.5;30;0;100\n\n"
+                       L"\tSWAP WARNING - 20%% free | swap=2000B;3000;500;0;10000\n\n"
                        L"\"SWAP\" being the type of the check, \"WARNING\" the returned status\n"
                        L"and \"23.8304%%\" is the returned value.\n"
                        L"The performance data is found behind the \"|\", in order:\n"
@@ -166,25 +166,29 @@ int printOutput(printInfoStruct& printInfo)
                wcout << L"Constructing output string" << endl;
 
        state state = OK;
+       double fswap = (printInfo.aSwap / printInfo.tSwap) * 100.0;
 
-       if (printInfo.warn.rend(printInfo.swap))
+       if (!printInfo.warn.rend(printInfo.aSwap, printInfo.tSwap))
                state = WARNING;
 
-       if (printInfo.crit.rend(printInfo.swap))
+       if (!printInfo.crit.rend(printInfo.aSwap, printInfo.tSwap))
                state = CRITICAL;
 
        switch (state) {
        case OK:
-               wcout << L"SWAP OK " << printInfo.swap << L"% | swap=" << printInfo.swap << L"%;" 
-                       << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
+               wcout << L"SWAP OK - " << fswap << L"% free | swap=" << printInfo.aSwap << L"B;"
+                       << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap) 
+                       << L";0;" << printInfo.tSwap << endl;
                break;
        case WARNING:
-               wcout << L"SWAP WARNING " << printInfo.swap << L"% | swap=" << printInfo.swap << L"%;"
-                       << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
+               wcout << L"SWAP WARNING - " << fswap << L"% free | swap=" << printInfo.aSwap << L"B;"
+                       << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap) 
+                       << L";0;" << printInfo.tSwap << endl;
                break;
        case CRITICAL:
-               wcout << L"SWAP CRITICAL " << printInfo.swap << L"% | swap=" << printInfo.swap << L"%;"
-                       << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
+               wcout << L"SWAP CRITICAL - " << fswap << L"% free | swap=" << printInfo.aSwap << L"B;"
+                       << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap) 
+                       << L";0;" << printInfo.tSwap << endl;
                break;
        }
 
@@ -193,49 +197,14 @@ int printOutput(printInfoStruct& printInfo)
 
 int check_swap(printInfoStruct& printInfo) 
 {
-       PDH_HQUERY phQuery;
-       PDH_HCOUNTER phCounter;
-       DWORD dwBufferSize = 0;
-       DWORD CounterType;
-       PDH_FMT_COUNTERVALUE DisplayValue;
-       PDH_STATUS err;
+       _MEMORYSTATUS *pMemBuf = new _MEMORYSTATUS;
 
-       LPCWSTR path = L"\\Paging File(*)\\% Usage";
+       GlobalMemoryStatus(pMemBuf);
 
-       if (debug)
-               wcout << L"Opening querry handle" << endl;
-
-       err = PdhOpenQuery(NULL, NULL, &phQuery);
-       if (!SUCCEEDED(err))
-               goto die;
-
-       if (debug)
-               wcout << L"Adding counter" << endl;
-
-       err = PdhAddEnglishCounter(phQuery, path, NULL, &phCounter);
-       if (!SUCCEEDED(err))
-               goto die;
-
-       if (debug)
-               wcout << L"Collecting querry data" << endl;
-
-       err = PdhCollectQueryData(phQuery);
-       if (!SUCCEEDED(err))
-               goto die;
-
-       if (debug)
-               wcout << L"Formatting counter data" << endl;
+       printInfo.tSwap = pMemBuf->dwTotalPageFile;
+       printInfo.aSwap = pMemBuf->dwAvailPageFile;
 
-       err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
-       if (SUCCEEDED(err)) {
-               printInfo.swap = DisplayValue.doubleValue;
-               PdhCloseQuery(phQuery);
-               return -1;
-       }
+       delete pMemBuf;
 
-die:
-       if (phQuery)
-               PdhCloseQuery(phQuery);
-       die(err);
-       return 3;
+       return -1;
 }
\ No newline at end of file