| 1 | // CheckEventLog.cpp : Defines the entry point for the DLL application. |
|---|
| 2 | // |
|---|
| 3 | |
|---|
| 4 | #include "stdafx.h" |
|---|
| 5 | #include "CheckWMI.h" |
|---|
| 6 | #include <strEx.h> |
|---|
| 7 | #include <time.h> |
|---|
| 8 | #include <map> |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | CheckWMI gCheckWMI; |
|---|
| 12 | |
|---|
| 13 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 14 | { |
|---|
| 15 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 16 | return TRUE; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | CheckWMI::CheckWMI() { |
|---|
| 20 | } |
|---|
| 21 | CheckWMI::~CheckWMI() { |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | bool CheckWMI::loadModule() { |
|---|
| 26 | return wmiQuery.initialize(); |
|---|
| 27 | } |
|---|
| 28 | bool CheckWMI::unloadModule() { |
|---|
| 29 | wmiQuery.unInitialize(); |
|---|
| 30 | return true; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | bool CheckWMI::hasCommandHandler() { |
|---|
| 34 | return true; |
|---|
| 35 | } |
|---|
| 36 | bool CheckWMI::hasMessageHandler() { |
|---|
| 37 | return false; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | NSCAPI::nagiosReturn CheckWMI::CheckSimpleWMI(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { |
|---|
| 43 | typedef checkHolders::CheckConatiner<checkHolders::MaxMinBounds<checkHolders::NumericBounds<int, checkHolders::int_handler> > > WMIConatiner; |
|---|
| 44 | |
|---|
| 45 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 46 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 47 | if (args.empty()) { |
|---|
| 48 | message = "Missing argument(s)."; |
|---|
| 49 | return NSCAPI::returnCRIT; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | WMIConatiner tmpObject; |
|---|
| 53 | std::list<WMIConatiner> queries; |
|---|
| 54 | |
|---|
| 55 | MAP_OPTIONS_BEGIN(args) |
|---|
| 56 | MAP_OPTIONS_STR_AND("Query", tmpObject.data, queries.push_back(tmpObject)) |
|---|
| 57 | MAP_OPTIONS_NUMERIC_ALL(tmpObject, "") |
|---|
| 58 | MAP_OPTIONS_SHOWALL(tmpObject) |
|---|
| 59 | MAP_OPTIONS_SECONDARY_BEGIN(":", p2) |
|---|
| 60 | else if (p2.first == "Query") { |
|---|
| 61 | tmpObject.data = p__.second; |
|---|
| 62 | tmpObject.alias = p2.second; |
|---|
| 63 | queries.push_back(tmpObject); |
|---|
| 64 | } |
|---|
| 65 | MAP_OPTIONS_MISSING_EX(p2, message, "Unknown argument: ") |
|---|
| 66 | MAP_OPTIONS_SECONDARY_END() |
|---|
| 67 | MAP_OPTIONS_FALLBACK_AND(tmpObject.data, queries.push_back(tmpObject)) |
|---|
| 68 | MAP_OPTIONS_END() |
|---|
| 69 | |
|---|
| 70 | for (std::list<WMIConatiner>::const_iterator pit = queries.begin();pit!=queries.end();++pit) { |
|---|
| 71 | WMIConatiner query = (*pit); |
|---|
| 72 | std::map<std::string,int> vals; |
|---|
| 73 | try { |
|---|
| 74 | vals = wmiQuery.execute(query.data); |
|---|
| 75 | } catch (WMIException e) { |
|---|
| 76 | message = "WMIQuery failed..."; |
|---|
| 77 | return NSCAPI::returnCRIT; |
|---|
| 78 | } |
|---|
| 79 | int val = 0; //(*vals.begin()).second; |
|---|
| 80 | |
|---|
| 81 | for (std::map<std::string,int>::const_iterator it = vals.begin(); it != vals.end(); ++it) { |
|---|
| 82 | std::cout << "Values: " << (*it).first << " = " << (*it).second << std::endl; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | query.setDefault(tmpObject); |
|---|
| 86 | query.runCheck(val, returnCode, message, perf); |
|---|
| 87 | } |
|---|
| 88 | if (message.empty()) |
|---|
| 89 | message = "OK: Queries within bounds."; |
|---|
| 90 | return returnCode; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | NSCAPI::nagiosReturn CheckWMI::handleCommand(const strEx::blindstr command, const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) { |
|---|
| 96 | if (command == "CheckWMI") { |
|---|
| 97 | return CheckSimpleWMI(argLen, char_args, msg, perf); |
|---|
| 98 | } |
|---|
| 99 | return NSCAPI::returnIgnored; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | NSC_WRAPPERS_MAIN_DEF(gCheckWMI); |
|---|
| 104 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 105 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckWMI); |
|---|