source: nscp/modules/CheckWMI/CheckWMI.cpp @ f42280d

0.4.00.4.10.4.2stable
Last change on this file since f42280d was f42280d, checked in by Michael Medin <michael@…>, 7 years ago

* empty log message *

  • Property mode set to 100644
File size: 2.8 KB
Line 
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
11CheckWMI gCheckWMI;
12
13BOOL 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
19CheckWMI::CheckWMI() {
20}
21CheckWMI::~CheckWMI() {
22}
23
24
25bool CheckWMI::loadModule() {
26        return wmiQuery.initialize();
27}
28bool CheckWMI::unloadModule() {
29        wmiQuery.unInitialize();
30        return true;
31}
32
33bool CheckWMI::hasCommandHandler() {
34        return true;
35}
36bool CheckWMI::hasMessageHandler() {
37        return false;
38}
39
40
41
42NSCAPI::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
95NSCAPI::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
103NSC_WRAPPERS_MAIN_DEF(gCheckWMI);
104NSC_WRAPPERS_IGNORE_MSG_DEF();
105NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckWMI);
Note: See TracBrowser for help on using the repository browser.