#include "thresholds.h"
-#include "boost\program_options.hpp"
+#include "boost/program_options.hpp"
#define VERSION 1.0
#include "thresholds.h"
-#include "boost\program_options.hpp"
+#include "boost/program_options.hpp"
#define VERSION 1.0
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)
{
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
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>&);
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++) {
return 3;
}
-void die(DWORD err = 0)
+void die(DWORD err)
{
if (!err)
err = GetLastError();
#include "thresholds.h"
-#include "boost\program_options.hpp"
+#include "boost/program_options.hpp"
#define VERSION 1.0
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)
{
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
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)
{
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
// 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);
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
#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
#include "thresholds.h"
-#include "boost\program_options.hpp"
+#include "boost/program_options.hpp"
#define VERSION 1.0
struct printInfoStruct
{
threshold warn, crit;
- int users;
+ double users;
};
static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
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;
}
int len;
if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, pSessionInfo[index].SessionId,
- WTSUserName, &name, &size))
+ WTSUserName, &name, &size))
continue;
len = lstrlenW(name);
WTSFreeMemory(pSessionInfo);
printInfo.users = users;
return -1;
-
}
\ No newline at end of file