Clean up check plugins more

This commit is contained in:
Jean Flach 2014-11-14 14:36:10 +01:00
parent 616716dc55
commit e1b5954e62
10 changed files with 82 additions and 113 deletions

View File

@ -55,7 +55,6 @@ static int printOutput(printInfoStruct&, vector<drive>&);
static int check_drives(vector<drive>&);
static int check_drives(vector<drive>&, printInfoStruct&);
static bool getFreeAndCap(drive&, const Bunit&);
static void die();
int wmain(int argc, wchar_t **argv)
{
@ -172,7 +171,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
if (vm.count("warning")) {
try {
printInfo.warn = parse(vm["warning"].as<wstring>());
printInfo.warn = threshold(vm["warning"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;
@ -180,7 +179,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
}
if (vm.count("critical")) {
try {
printInfo.crit = parse(vm["critical"].as<wstring>());
printInfo.crit = threshold(vm["critical"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;
@ -330,13 +329,4 @@ bool getFreeAndCap(drive& drive, const Bunit& unit)
drive.free = (tempFree.QuadPart / pow(1024.0, unit));
return TRUE;
}
void die()
{
DWORD 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;
}

View File

@ -41,7 +41,6 @@ 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)
{
@ -143,7 +142,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
if (vm.count("warning")) {
try {
printInfo.warn = parse(vm["warning"].as<wstring>());
printInfo.warn = threshold(vm["warning"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;
@ -151,7 +150,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
}
if (vm.count("critical")) {
try {
printInfo.crit = parse(vm["critical"].as<wstring>());
printInfo.crit = threshold(vm["critical"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;
@ -232,14 +231,4 @@ 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;
}

View File

@ -45,7 +45,6 @@ struct printInfoStruct
threshold warn, crit;
};
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>&);
@ -153,7 +152,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
if (vm.count("warning")) {
try {
printInfo.warn = parse(vm["warning"].as<wstring>());
printInfo.warn = threshold(vm["warning"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;
@ -161,7 +160,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
}
if (vm.count("critical")) {
try {
printInfo.crit = parse(vm["critical"].as<wstring>());
printInfo.crit = threshold(vm["critical"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;
@ -277,14 +276,4 @@ die:
if (pDisplayValuesOut)
delete pDisplayValuesOut;
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;
}

View File

@ -175,7 +175,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
if (vm.count("warning")) {
try {
printInfo.warn = parse(vm["warning"].as<wstring>());
printInfo.warn = threshold(vm["warning"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;
@ -183,7 +183,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
}
if (vm.count("critical")) {
try {
printInfo.crit = parse(vm["critical"].as<wstring>());
printInfo.crit = threshold(vm["critical"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;

View File

@ -40,7 +40,6 @@ 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)
{
@ -142,7 +141,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
if (vm.count("warning")) {
try {
printInfo.warn = parse(vm["warning"].as<wstring>());
printInfo.warn = threshold(vm["warning"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;
@ -150,7 +149,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
}
if (vm.count("critical")) {
try {
printInfo.crit = parse(vm["critical"].as<wstring>());
printInfo.crit = threshold(vm["critical"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;
@ -223,14 +222,4 @@ die:
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;
}

View File

@ -45,7 +45,6 @@ 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)
{
@ -242,18 +241,8 @@ int check_update(printInfoStruct& printInfo)
return 0;
die:
die(err);
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;
}

View File

@ -147,7 +147,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
if (vm.count("warning")) {
try {
printInfo.warn = parse(vm["warning"].as<wstring>());
printInfo.warn = threshold(vm["warning"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;
@ -155,7 +155,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
}
if (vm.count("critical")) {
try {
printInfo.crit = parse(vm["critical"].as<wstring>());
printInfo.crit = threshold(vm["critical"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;

View File

@ -142,7 +142,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
if (vm.count("warning")) {
try {
printInfo.warn = parse(vm["warning"].as<wstring>());
printInfo.warn = threshold(vm["warning"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;
@ -150,7 +150,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
}
if (vm.count("critical")) {
try {
printInfo.crit = parse(vm["critical"].as<wstring>());
printInfo.crit = threshold(vm["critical"].as<wstring>());
} catch (std::invalid_argument& e) {
cout << e.what() << endl;
return 3;

View File

@ -17,17 +17,59 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include <vector>
#include <iostream>
#include "thresholds.h"
#include "boost\algorithm\string.hpp"
#include "boost\lexical_cast.hpp"
#include "boost/algorithm/string.hpp"
#include "boost/lexical_cast.hpp"
using namespace boost::algorithm;
using std::wstring;
threshold parse(const wstring& stri)
threshold::threshold(bool l)
: set(false), legal(l)
{}
//return TRUE if the threshold is broken
bool threshold::rend(const double b)
{
if (!set)
return set;
if (lower == upper)
return b > upper == legal;
else
return (b < lower || upper < b) != legal;
}
//returns a printable string of the threshold
std::wstring threshold::pString()
{
if (!set)
return L"0";
std::wstring s;
if (!legal)
s.append(L"!");
if (lower != upper) {
if (perc)
s.append(L"[").append(std::to_wstring(lower)).append(L"%").append(L"-")
.append(std::to_wstring(upper)).append(L"%").append(L"]");
else
s.append(L"[").append(std::to_wstring(lower)).append(L"-")
.append(std::to_wstring(upper)).append(L"]");
} else {
if (perc)
s = std::to_wstring(lower).append(L"%");
else
s = std::to_wstring(lower);
}
return s;
}
threshold::threshold(const wstring& stri)
{
if (stri.empty())
throw std::invalid_argument("Threshold must not be empty");
@ -41,7 +83,7 @@ threshold parse(const wstring& stri)
if (low)
str = wstring(str.begin() + 1, str.end());
bool perc = false;
bool pc = false;
if (str.at(0) == L'[' && str.at(str.length() - 1) == L']') {//is range
str = wstring(str.begin() + 1, str.end() - 1);
@ -60,7 +102,7 @@ threshold parse(const wstring& stri)
try {
double d1 = boost::lexical_cast<double>(str1);
double d2 = boost::lexical_cast<double>(str2);
return threshold(d1, d2, !low, perc);
lower = d1; upper = d2; legal = !low; perc = pc; set = true;
} catch (const boost::bad_lexical_cast&) {
throw std::invalid_argument("Unknown Threshold type");
}
@ -71,8 +113,7 @@ threshold parse(const wstring& stri)
}
try {
double d = boost::lexical_cast<double>(str);
return threshold(d, d, !low, perc);
lower = d; upper = d; legal = !low; perc = pc; set = true;
} catch (const boost::bad_lexical_cast&) {
throw std::invalid_argument("Unknown Threshold type");
}
@ -142,4 +183,14 @@ wstring TunitStr(const Tunit& unit)
return L"h";
}
return NULL;
}
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);
std::wcout << mBuf << std::endl;
}

View File

@ -19,6 +19,7 @@
#ifndef THRESHOLDS_H
#define THRESHOLDS_H
#include <string>
#include <Windows.h>
enum Bunit { BunitB = 0, BunitkB = 1, BunitMB = 2, BunitGB = 3, BunitTB = 4 };
enum Tunit { TunitMS, TunitS, TunitM, TunitH };
@ -31,53 +32,24 @@ public:
//TRUE means everything BELOW upper/outside [lower-upper] is fine
bool legal, perc, set;
threshold(bool l = true)
: set(false), legal(l) {}
threshold(bool l = true);
threshold(const double v, const double c, bool l = true, bool p = false)
: lower(v), upper(c), legal(l), perc(p), set(true) {}
threshold(const double v, const double c, bool l = true, bool p = false);
threshold(const std::wstring&);
//return TRUE if the threshold is broken
bool rend(const double b)
{
if (!set)
return set;
if (lower == upper)
return b > upper == legal;
else
return (b < lower || upper < b) != legal;
}
bool rend(const double b);
//returns a printable string of the threshold
std::wstring pString()
{
if (!set)
return L"0";
std::wstring pString();
std::wstring s;
if (!legal)
s.append(L"!");
if (lower != upper) {
if (perc)
s.append(L"[").append(std::to_wstring(lower)).append(L"%").append(L"-")
.append(std::to_wstring(upper)).append(L"%").append(L"]");
else
s.append(L"[").append(std::to_wstring(lower)).append(L"-")
.append(std::to_wstring(upper)).append(L"]");
} else {
if (perc)
s = std::to_wstring(lower).append(L"%");
else
s = std::to_wstring(lower);
}
return s;
}
};
threshold parse(const std::wstring&);
Bunit parseBUnit(const std::wstring&);
std::wstring BunitStr(const Bunit&);
Tunit parseTUnit(const std::wstring&);
std::wstring TunitStr(const Tunit&);
void die(DWORD err = 0);
#endif