/******************************************************************************
* Icinga 2 *
- * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
+ * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* 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"
-
-#include "boost/program_options.hpp"
+#include "check_swap.h"
#define VERSION 1.0
namespace po = boost::program_options;
-using std::endl; using std::wcout; using std::wstring;
-using std::cout;
+static BOOL debug = FALSE;
-struct printInfoStruct
-{
- threshold warn, crit;
- double swap;
-};
-
-static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
-static int printOutput(printInfoStruct&);
-static int check_swap(printInfoStruct&);
-
-int wmain(int argc, wchar_t **argv)
+INT wmain(INT argc, WCHAR **argv)
{
printInfoStruct printInfo = { };
po::variables_map vm;
- int ret = parseArguments(argc, argv, vm, printInfo);
+ INT ret = parseArguments(argc, argv, vm, printInfo);
if (ret != -1)
return ret;
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()
- ("help,h", "print help message and exit")
- ("version,V", "print version and exit")
- ("warning,w", po::wvalue<wstring>(), "warning threshold")
- ("critical,c", po::wvalue<wstring>(), "critical threshold")
+ ("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")
+ ("critical,c", po::wvalue<std::wstring>(), "Critical threshold")
+ ("unit,u", po::wvalue<std::wstring>(), "The unit to use for display (default MB)")
;
- po::basic_command_line_parser<wchar_t> parser(ac, av);
+ po::basic_command_line_parser<WCHAR> parser(ac, av);
try {
po::store(
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("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 swap in percent.\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"\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"and \"20%%\" is the returned value.\n"
L"The performance data is found behind the \"|\", in order:\n"
L"returned value, warning threshold, critical threshold, minimal value and,\n"
L"if applicable, the maximal value. Performance data will only be displayed when\n"
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"))
- wcout << L"Version: " << VERSION << endl;
+ std::wcout << L"Version: " << VERSION << '\n';
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;
}
+ printInfo.warn.legal = !printInfo.warn.legal;
}
+
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) {
+ std::cout << e.what() << '\n';
+ return 3;
+ }
+ printInfo.crit.legal = !printInfo.crit.legal;
+ }
+
+ if (vm.count("debug"))
+ debug = TRUE;
+
+ if (vm.count("unit")) {
+ try {
+ printInfo.unit = parseBUnit(vm["unit"].as<std::wstring>());
} catch (std::invalid_argument& e) {
- cout << e.what() << endl;
+ std::cout << e.what() << '\n';
return 3;
}
}
return -1;
}
-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.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;
+ std::wcout << L"SWAP OK - " << printInfo.percentFree << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
+ << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap)
+ << L";0;" << printInfo.tSwap << '\n';
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;
+ std::wcout << L"SWAP WARNING - " << printInfo.percentFree << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
+ << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap)
+ << L";0;" << printInfo.tSwap << '\n';
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;
+ std::wcout << L"SWAP CRITICAL - " << printInfo.percentFree << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
+ << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap)
+ << L";0;" << printInfo.tSwap << '\n';
break;
}
return state;
}
-int check_swap(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;
-
- LPCWSTR path = L"\\Paging File(*)\\% Usage";
-
- err = PdhOpenQuery(NULL, NULL, &phQuery);
- if (!SUCCEEDED(err))
- goto die;
-
- err = PdhAddEnglishCounter(phQuery, path, NULL, &phCounter);
- if (!SUCCEEDED(err))
- goto die;
-
- err = PdhCollectQueryData(phQuery);
- if (!SUCCEEDED(err))
- goto die;
-
- err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
- if (SUCCEEDED(err)) {
- printInfo.swap = DisplayValue.doubleValue;
- PdhCloseQuery(phQuery);
- return -1;
+ MEMORYSTATUSEX MemBuf;
+ MemBuf.dwLength = sizeof(MemBuf);
+
+ if (!GlobalMemoryStatusEx(&MemBuf)) {
+ die();
+ return 3;
}
-die:
- if (phQuery)
- PdhCloseQuery(phQuery);
- die(err);
- return 3;
-}
\ No newline at end of file
+ printInfo.tSwap = round(MemBuf.ullTotalPageFile / pow(1024.0, printInfo.unit));
+ printInfo.aSwap = round(MemBuf.ullAvailPageFile / pow(1024.0, printInfo.unit));
+ printInfo.percentFree = 100.0 * MemBuf.ullAvailPageFile / MemBuf.ullTotalPageFile;
+
+ return -1;
+}