source: nscp/trunk/modules/CheckSystem/CheckSystem.cpp @ e93e741

Last change on this file since e93e741 was e93e741, checked in by Michael Medin <michael@…>, 8 years ago
  • Fixed PROCSTATE and SERVICESTATE return state. + Added support for individual size in CheckDriveSize and CheckFileSize (size has to be specified before a drive/path)
  • Fixed performance data for drives (and possibly other places)
  • Property mode set to 100644
File size: 21.0 KB
Line 
1// NSClientCompat.cpp : Defines the entry point for the DLL application.
2//
3
4#include "stdafx.h"
5#include "CheckSystem.h"
6#include <utils.h>
7#include <tlhelp32.h>
8#include <EnumNtSrv.h>
9#include <EnumProcess.h>
10
11CheckSystem gNSClientCompat;
12
13/**
14 * DLL Entry point
15 * @param hModule
16 * @param ul_reason_for_call
17 * @param lpReserved
18 * @return
19 */
20BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
21{
22        NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call);
23        return TRUE;
24}
25
26/**
27 * Default c-tor
28 * @return
29 */
30CheckSystem::CheckSystem() : processMethod_(0) {}
31/**
32 * Default d-tor
33 * @return
34 */
35CheckSystem::~CheckSystem() {}
36/**
37 * Load (initiate) module.
38 * Start the background collector thread and let it run until unloadModule() is called.
39 * @return true
40 */
41bool CheckSystem::loadModule() {
42        pdhThread.createThread();
43
44        std::string wantedMethod = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_ENUMPROC_METHOD, C_SYSTEM_ENUMPROC_METHOD_DEFAULT);
45
46        CEnumProcess tmp;
47        int method = tmp.GetAvailableMethods();
48
49        if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_PSAPI) {
50                NSC_DEBUG_MSG_STD("Using PSAPI method.");
51                if (method == (method|ENUM_METHOD::PSAPI)) {
52                        processMethod_ = ENUM_METHOD::PSAPI;
53                } else {
54                        NSC_LOG_ERROR_STD("PSAPI method not available, check " C_SYSTEM_ENUMPROC_METHOD " option.");
55                }
56        } else {
57                NSC_DEBUG_MSG_STD("Using TOOLHELP method.");
58                if (method == (method|ENUM_METHOD::TOOLHELP)) {
59                        processMethod_ = ENUM_METHOD::TOOLHELP;
60                } else {
61                        NSC_LOG_ERROR_STD("TOOLHELP method not avalible, check " C_SYSTEM_ENUMPROC_METHOD " option.");
62                }
63        }
64        return true;
65}
66/**
67 * Unload (terminate) module.
68 * Attempt to stop the background processing thread.
69 * @return true if successfully, false if not (if not things might be bad)
70 */
71bool CheckSystem::unloadModule() {
72        if (!pdhThread.exitThread(20000)) {
73                std::cout << "MAJOR ERROR: Could not unload thread..." << std::endl;
74                NSC_LOG_ERROR("Could not exit the thread, memory leak and potential corruption may be the result...");
75        }
76        return true;
77}
78/**
79 * Return the module name.
80 * @return The module name
81 */
82std::string CheckSystem::getModuleName() {
83        return "System Checks Module.";
84}
85/**
86 * Module version
87 * @return module version
88 */
89NSCModuleWrapper::module_version CheckSystem::getModuleVersion() {
90        NSCModuleWrapper::module_version version = {0, 3, 0 };
91        return version;
92}
93/**
94 * Check if we have a command handler.
95 * @return true (as we have a command handler)
96 */
97bool CheckSystem::hasCommandHandler() {
98        return true;
99}
100/**
101 * Check if we have a message handler.
102 * @return false as we have no message handler
103 */
104bool CheckSystem::hasMessageHandler() {
105        return false;
106}
107/*
108*/
109/**
110 * Main command parser and delegator.
111 * This also handles a lot of the simpler responses (though some are deferred to other helper functions)
112 *
113 *
114 * @param command
115 * @param argLen
116 * @param **args
117 * @return
118 */
119NSCAPI::nagiosReturn CheckSystem::handleCommand(const strEx::blindstr command, const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) {
120        std::list<std::string> stl_args;
121        CheckSystem::returnBundle rb;
122        if (command == "checkCPU") {
123                return checkCPU(argLen, char_args, msg, perf);
124        } else if (command == "checkUpTime") {
125                return checkUpTime(argLen, char_args, msg, perf);
126        } else if (command == "checkServiceState") {
127                return checkServiceState(argLen, char_args, msg, perf);
128        } else if (command == "checkProcState") {
129                return checkProcState(argLen, char_args, msg, perf);
130        } else if (command == "checkMem") {
131                return checkMem(argLen, char_args, msg, perf);
132        } else if (command == "checkCounter") {
133                return checkCounter(argLen, char_args, msg, perf);
134        }
135        return NSCAPI::returnIgnored;
136}
137
138// checkCPU warn=80 crit=90 time=20m time=10s time=4
139// checkCPU warn=80 crit=90 time=20m time=10s time=4 showAll
140// checkCPU 20 10 4 nsclient
141NSCAPI::nagiosReturn CheckSystem::checkCPU(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf)
142{
143        std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
144        if (stl_args.empty()) {
145                msg = "ERROR: Missing argument exception.";
146                return NSCAPI::returnUNKNOWN;
147        }
148        int warn;
149        int crit;
150        std::list<std::string> times;
151        bool bNSCLientCompatible = false;
152        bool bShowAll = false;
153        NSCAPI::nagiosReturn ret = NSCAPI::returnOK;
154
155
156        for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) {
157                strEx::token t = strEx::getToken((*it), '=');
158                if (t.first == "crit")
159                        crit = strEx::stoi(t.second);
160                else if (t.first == "warn")
161                        warn = strEx::stoi(t.second);
162                else if (t.first == "time")
163                        times.push_back(t.second);
164                else if (t.first == NSCLIENT)
165                        bNSCLientCompatible = true;
166                else if (t.first == "showAll")
167                        bShowAll = true;
168                else
169                        times.push_back(t.first);
170        }
171
172        for (std::list<std::string>::iterator it = times.begin(); it != times.end(); ++it) {
173                PDHCollector *pObject = pdhThread.getThread();
174                if (!pObject) {
175                        msg = "ERROR: PDH Collection thread not running.";
176                        return NSCAPI::returnUNKNOWN;
177                }
178                if (bNSCLientCompatible) {
179                        int v = pObject->getCPUAvrage((*it) + "m");
180                        if (v == -1) {
181                                msg = "ERROR: We don't collect data this far back: " + (*it);
182                                return NSCAPI::returnUNKNOWN;
183                        }
184                        if (!msg.empty()) msg += "&";
185                        msg += strEx::itos(v);
186                } else {
187                        int v = pObject->getCPUAvrage((*it));
188                        if (v == -1) {
189                                msg = "ERROR: We don't collect data this far back: " + (*it);
190                                return NSCAPI::returnUNKNOWN;
191                        } else {
192                                if (v > warn) {
193                                        NSCHelper::escalteReturnCodeToWARN(ret);
194                                        msg += strEx::itos(v) + "% > " + strEx::itos(warn) + " " + (*it);
195                                } if (v > crit) {
196                                        NSCHelper::escalteReturnCodeToCRIT(ret);
197                                        msg += strEx::itos(v) + "% > " + strEx::itos(crit) + " " + (*it);
198                                } else if (bShowAll) {
199                                        msg += strEx::itos(v) + "% ";
200                                }
201                                perf += "'" + (*it) + " average'=" + strEx::itos(v) + "%;" + strEx::itos(warn) + ";" + strEx::itos(crit) + "; ";
202                        }
203                }
204        }
205        if (bNSCLientCompatible) {
206                // Don't prefix/postfix the output for NSClient
207        } else if (msg.empty()) {
208                msg = "CPU Load ok.";
209        } else {
210                msg = "CPU Load: " + msg;
211        }
212        return ret;
213}
214
215// checkUpTime crit=1d warn=6h
216// checkUpTime nsclient
217NSCAPI::nagiosReturn CheckSystem::checkUpTime(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf)
218{
219        std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
220        if (stl_args.empty()) {
221                msg = "ERROR: Missing argument exception.";
222                return NSCAPI::returnUNKNOWN;
223        }
224        unsigned long long warn;
225        unsigned long long crit;
226        bool bNSCLientCompatible = false;
227
228        for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) {
229                strEx::token t = strEx::getToken((*it), '=');
230                if (t.first == "crit")
231                        crit = strEx::stoi64_as_time(t.second);
232                else if (t.first == "warn")
233                        warn = strEx::stoi64_as_time(t.second);
234                else if (t.first == NSCLIENT)
235                        bNSCLientCompatible = true;
236                else {
237                        msg = "Invalid argument: " + t.first;
238                        return NSCAPI::returnUNKNOWN;
239                }
240        }
241        PDHCollector *pObject = pdhThread.getThread();
242        if (!pObject) {
243                msg = "ERROR: PDH Collection thread not running.";
244                return NSCAPI::returnUNKNOWN;
245        }
246        unsigned long long uptime = pObject->getUptime();
247        if (bNSCLientCompatible) {
248                msg = strEx::itos(uptime);
249                return NSCAPI::returnOK;
250        } else {
251                uptime *= 1000;
252                if (uptime < crit) {
253                        msg = "Client has uptime (" + strEx::itos_as_time(uptime) + ") < critical (" + strEx::itos_as_time(crit) + ")";
254                        return NSCAPI::returnCRIT;
255                }
256                if (uptime < warn) {
257                        msg = "Client has uptime (" + strEx::itos_as_time(uptime) + ") < warning (" + strEx::itos_as_time(warn) + ")";
258                        return NSCAPI::returnWARN;
259                }
260        }
261        return NSCAPI::returnOK;
262}
263
264
265
266
267/**
268 * Retrieve the service state of one or more services (by name).
269 * Parse a list with a service names and verify that all named services are running.
270 * <pre>
271 * Syntax:
272 * request: checkServiceState <option> [<option> [...]]
273 * Return: <return state>&<service1> : <state1> - <service2> : <state2> - ...
274 * Available options:
275 *              <name>=<state>  Check if a service has a specific state
276 *                      State can be wither started or stopped
277 *              ShowAll                 Show the state of all listed service. If not set only critical services are listed.
278 * Examples:
279 * checkServiceState showAll myService MyService
280 *</pre>
281 *
282 * @param command Command to execute
283 * @param argLen The length of the argument buffer
284 * @param **char_args The argument buffer
285 * @param &msg String to put message in
286 * @param &perf String to put performance data in
287 * @return The status of the command
288 */
289NSCAPI::nagiosReturn CheckSystem::checkServiceState(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf)
290{
291        std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
292        if (stl_args.empty()) {
293                msg = "ERROR: Missing argument exception.";
294                return NSCAPI::returnUNKNOWN;
295        }
296        std::list<std::pair<std::string,states> > services;
297        NSCAPI::nagiosReturn ret = NSCAPI::returnOK;
298        bool bShowAll = false;
299        bool bNSClient = false;
300
301        for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) {
302                strEx::token t = strEx::getToken((*it), '=');
303                if (t.first == SHOW_ALL)
304                        bShowAll = true;
305                else if (t.first == NSCLIENT)
306                        bNSClient = true;
307                else if (t.first == SHOW_FAIL)  {
308                        bShowAll = false;
309                } else {
310                        if (t.second.empty())
311                                services.push_back(std::pair<std::string,states>(t.first, started));
312                        else {
313                                if (t.second == "started")
314                                        services.push_back(std::pair<std::string,states>(t.first, started));
315                                else
316                                        services.push_back(std::pair<std::string,states>(t.first, stopped));
317                        }
318                }
319        }
320        for (std::list<std::pair<std::string,states> >::iterator it = services.begin(); it != services.end(); ++it) {
321                TNtServiceInfo info = TNtServiceInfo::GetService((*it).first.c_str());
322                std::string tmp;
323                if ( (info.m_dwCurrentState == SERVICE_RUNNING) && ((*it).second == started) ) {
324                        if (bShowAll)
325                                tmp = info.m_strServiceName + " : Started";
326                } else if ( (info.m_dwCurrentState == SERVICE_STOPPED) && ((*it).second == stopped) ) {
327                        if (bShowAll)
328                                tmp = info.m_strServiceName + " : Stopped";
329                } else if ((info.m_dwCurrentState == SERVICE_STOPPED) && ((*it).second == started) ) {
330                        NSCHelper::escalteReturnCodeToCRIT(ret);
331                        tmp = info.m_strServiceName + " : Stopped";
332                } else if ((info.m_dwCurrentState == SERVICE_RUNNING) && ((*it).second == stopped) ) {
333                        NSCHelper::escalteReturnCodeToCRIT(ret);
334                        tmp = info.m_strServiceName + " : Started";
335                } else {
336                        NSCHelper::escalteReturnCodeToWARN(ret);
337                        tmp = info.m_strServiceName + " : Unknown";
338                }
339                if (!msg.empty()&&!tmp.empty())
340                        msg += " - ";
341                msg += tmp;
342        }
343        if (msg.empty())
344                msg ="All services are running.";
345        else if (!bNSClient)
346                msg = NSCHelper::translateReturn(ret) + ": " + msg;
347        return ret;
348}
349
350
351/**
352 * Check available memory and return various check results
353 * Example: checkMem showAll maxWarn=50 maxCrit=75
354 *
355 * @param command Command to execute
356 * @param argLen The length of the argument buffer
357 * @param **char_args The argument buffer
358 * @param &msg String to put message in
359 * @param &perf String to put performance data in
360 * @return The status of the command
361 */
362NSCAPI::nagiosReturn CheckSystem::checkMem(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf)
363{
364        std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
365        if (stl_args.empty()) {
366                msg = "ERROR: Missing argument exception.";
367                return NSCAPI::returnUNKNOWN;
368        }
369        std::list<std::pair<std::string,states> > services;
370        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
371        bool bShowAll = false;
372        bool bNSCLientCompatible = false;
373
374        checkHolders::SizeMaxMinPercentage<> warn;
375        checkHolders::SizeMaxMinPercentage<> crit;
376
377        for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) {
378                strEx::token t = strEx::getToken((*it), '=');
379                if (t.first == SHOW_ALL)
380                        bShowAll = true;
381                else if (t.first == "MaxWarn") {
382                        warn.max.set(t.second);
383                } else if (t.first == "MinWarn") {
384                        warn.min.set(t.second);
385                } else if (t.first == "MaxCrit") {
386                        crit.max.set(t.second);
387                } else if (t.first == "MinCrit") {
388                        crit.min.set(t.second);
389                } else if (t.first == NSCLIENT)
390                        bNSCLientCompatible = true;
391                else {
392                        msg = "Invalid argument: " + t.first;
393                        return NSCAPI::returnUNKNOWN;
394                }
395        }
396
397        PDHCollector *pObject = pdhThread.getThread();
398        if (!pObject) {
399                msg = "ERROR: PDH Collection thread not running.";
400                return NSCAPI::returnUNKNOWN;
401        }
402        long long pageCommit = pObject->getMemCommit();
403        long long pageCommitLimit = pObject->getMemCommitLimit();
404        if (bNSCLientCompatible) {
405                msg = strEx::itos(pageCommitLimit) + "&" + strEx::itos(pageCommit);
406                return NSCAPI::returnOK;
407        } else {
408                std::string tStr;
409                if (crit.max.hasBounds() && crit.max.checkMAX(pageCommit, pageCommitLimit)) {
410                        tStr = crit.max.prettyPrint("page", pageCommit, pageCommitLimit) + " > critical";
411                        NSCHelper::escalteReturnCodeToCRIT(returnCode);
412                } else if (crit.min.hasBounds() && crit.min.checkMIN(pageCommit, pageCommitLimit)) {
413                        tStr = crit.min.prettyPrint("page", pageCommit, pageCommitLimit) + " < critical";
414                        NSCHelper::escalteReturnCodeToCRIT(returnCode);
415                } else if (warn.max.hasBounds() && warn.max.checkMAX(pageCommit, pageCommitLimit)) {
416                        tStr = warn.max.prettyPrint("page", pageCommit, pageCommitLimit) + " > warning";
417                        NSCHelper::escalteReturnCodeToWARN(returnCode);
418                } else if (warn.min.hasBounds() && warn.min.checkMIN(pageCommit, pageCommitLimit)) {
419                        tStr = warn.min.prettyPrint("page", pageCommit, pageCommitLimit) + " < warning";
420                        NSCHelper::escalteReturnCodeToWARN(returnCode);
421                } else if (bShowAll) {
422                        tStr = "page: " + strEx::itos_as_BKMG(pageCommit);
423                }
424                perf += checkHolders::SizeMaxMinPercentage<>::printPerf("page", pageCommit, pageCommitLimit, warn, crit);
425                msg += tStr;
426        }
427        if (msg.empty())
428                msg = "OK memory within bounds.";
429        else
430                msg = NSCHelper::translateReturn(returnCode) + ": " + msg;
431        return returnCode;
432}
433
434typedef std::hash_map<std::string,DWORD> NSPROCLST;
435/**
436* Get a hash_map with all running processes.
437* @return a hash_map with all running processes
438*/
439NSPROCLST GetProcessList(int processMethod)
440{
441        NSPROCLST ret;
442        if (processMethod == 0) {
443                NSC_LOG_ERROR_STD("ProcessMethod not defined or not available.");
444                return ret;
445        }
446        CEnumProcess enumeration;
447        enumeration.SetMethod(processMethod);
448        CEnumProcess::CProcessEntry entry;
449        for (BOOL OK = enumeration.GetProcessFirst(&entry); OK; OK = enumeration.GetProcessNext(&entry) ) {
450                ret[entry.lpFilename] = entry.dwPID;
451        }
452        return ret;
453}
454
455/**
456 * Check process state and return result
457 *
458 * @param command Command to execute
459 * @param argLen The length of the argument buffer
460 * @param **char_args The argument buffer
461 * @param &msg String to put message in
462 * @param &perf String to put performance data in
463 * @return The status of the command
464 */
465NSCAPI::nagiosReturn CheckSystem::checkProcState(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf)
466{
467        std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
468        if (stl_args.empty()) {
469                msg = "ERROR: Missing argument exception.";
470                return NSCAPI::returnUNKNOWN;
471        }
472        std::list<std::pair<std::string,states> > procs;
473        NSCAPI::nagiosReturn ret = NSCAPI::returnOK;
474        bool bShowAll = false;
475        bool bNSClient = false;
476
477        for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) {
478                strEx::token t = strEx::getToken((*it), '=');
479                if (t.first == SHOW_ALL)
480                        bShowAll = true;
481                else if (t.first == NSCLIENT)
482                        bNSClient = true;
483                else if (t.first == SHOW_FAIL)  {
484                        bShowAll = false;
485                } else {
486                        if (t.second.empty())
487                                procs.push_back(std::pair<std::string,states>(t.first, started));
488                        else {
489                                if (t.second == "started")
490                                        procs.push_back(std::pair<std::string,states>(t.first, started));
491                                else
492                                        procs.push_back(std::pair<std::string,states>(t.first, stopped));
493                        }
494                }
495        }
496        NSPROCLST runningProcs;
497        try {
498                runningProcs = GetProcessList(processMethod_);
499        } catch (char *c) {
500                NSC_LOG_ERROR_STD("ERROR: " + c);
501                msg = static_cast<std::string>("ERROR: ") + c;
502                return NSCAPI::returnCRIT;
503        }
504
505        for (std::list<std::pair<std::string,states> >::iterator it = procs.begin(); it != procs.end(); ++it) {
506                NSPROCLST::iterator proc = runningProcs.find((*it).first);
507                bool bFound = proc != runningProcs.end();
508                std::string tmp;
509                if ( (bFound) && ((*it).second == started) ) {
510                        if (bShowAll)
511                                tmp = (*it).first + " : Running";
512                } else if ( (!bFound) && ((*it).second == stopped) ) {
513                        if (bShowAll)
514                                tmp = (*it).first + " : Stopped";
515                } else if ( (!bFound) && ((*it).second == started) ) {
516                        NSCHelper::escalteReturnCodeToCRIT(ret);
517                        tmp = (*it).first + " : Stopped";
518                } else if ( (bFound) && ((*it).second == stopped) ) {
519                        NSCHelper::escalteReturnCodeToCRIT(ret);
520                        tmp = (*it).first + " : Running";
521                }
522                if (!msg.empty()&&!tmp.empty())
523                        msg += " - ";
524                msg += tmp;
525        }
526        if (msg.empty())
527                msg ="All processes ok.";
528        else if (!bNSClient)
529                msg = NSCHelper::translateReturn(ret) + ": " + msg;
530        return ret;
531}
532
533/**
534 * Check a counter and return the value
535 *
536 * @param command Command to execute
537 * @param argLen The length of the argument buffer
538 * @param **char_args The argument buffer
539 * @param &msg String to put message in
540 * @param &perf String to put performance data in
541 * @return The status of the command
542 *
543 * @todo add parsing support for NRPE
544 */
545NSCAPI::nagiosReturn CheckSystem::checkCounter(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf)
546{
547        std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
548        if (stl_args.empty()) {
549                msg = "ERROR: Missing argument exception.";
550                return NSCAPI::returnUNKNOWN;
551        }
552        std::list<std::pair<std::string,std::string> > counters;
553        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
554        bool bShowAll = false;
555        bool bNSCLientCompatible = false;
556
557        checkHolders::SizeMaxMin<__int64, checkHolders::int64_handler<> > warn;
558        checkHolders::SizeMaxMin<__int64, checkHolders::int64_handler<> > crit;
559
560        for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) {
561                strEx::token t = strEx::getToken((*it), '=');
562                if (t.first == SHOW_ALL)
563                        bShowAll = true;
564                else if (t.first == SHOW_FAIL)  {
565                        bShowAll = false;
566                } else if (t.first == "MaxWarn") {
567                        warn.max.set(t.second);
568                } else if (t.first == "MinWarn") {
569                        warn.min.set(t.second);
570                } else if (t.first == "MaxCrit") {
571                        crit.max.set(t.second);
572                } else if (t.first == "MinCrit") {
573                        crit.min.set(t.second);
574                } else if (t.first == NSCLIENT) {
575                        bNSCLientCompatible = true;
576                } else if (t.first == "Counter") {
577                        counters.push_back(std::pair<std::string,std::string>("",t.second));
578                } else if (t.first.find(":") != std::string::npos) {
579                        std::pair<std::string,std::string> t2 = strEx::split(t.first,":");
580                        if (t2.first == "Counter") {
581                                counters.push_back(std::pair<std::string,std::string>(t2.second,t.second));
582                        } else {
583                                msg = "Unknown command: " + t.first;
584                                return NSCAPI::returnUNKNOWN;
585                        }
586                } else {
587                        counters.push_back(std::pair<std::string,std::string>("",t.first));
588                }
589        }
590
591        for (std::list<std::pair<std::string,std::string> >::iterator it = counters.begin(); it != counters.end(); ++it) {
592                std::string name;
593                try {
594                        PDH::PDHQuery pdh;
595                        PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE> counter;
596                        std::string name = (*it).first;
597                        if (name.empty())
598                                name = (*it).second;
599                        pdh.addCounter((*it).second, &counter);
600                        pdh.open();
601                        pdh.collect();
602                        if (bNSCLientCompatible) {
603                                msg += strEx::itos(counter.getValue());
604                        } else {
605                                std::string tStr;
606                                if (crit.max.hasBounds() && crit.max.checkMAX(counter.getValue())) {
607                                        tStr = crit.max.prettyPrint(name, counter.getValue()) + " > critical";
608                                        NSCHelper::escalteReturnCodeToCRIT(returnCode);
609                                } else if (crit.min.hasBounds() && crit.min.checkMIN(counter.getValue())) {
610                                        tStr = crit.min.prettyPrint(name, counter.getValue()) + " < critical";
611                                        NSCHelper::escalteReturnCodeToCRIT(returnCode);
612                                } else if (warn.max.hasBounds() && warn.max.checkMAX(counter.getValue())) {
613                                        tStr = warn.max.prettyPrint(name, counter.getValue()) + " > warning";
614                                        NSCHelper::escalteReturnCodeToWARN(returnCode);
615                                } else if (warn.min.hasBounds() && warn.min.checkMIN(counter.getValue())) {
616                                        tStr = warn.min.prettyPrint(name, counter.getValue()) + " < warning";
617                                        NSCHelper::escalteReturnCodeToWARN(returnCode);
618                                } else if (bShowAll) {
619                                        tStr = name + ": " + strEx::itos(counter.getValue());
620                                }
621                                perf += checkHolders::SizeMaxMin<__int64, checkHolders::int64_handler<> >::printPerf(name, counter.getValue(), warn, crit);
622                                msg += tStr;
623                        }
624                        pdh.close();
625                } catch (const PDH::PDHException &e) {
626                        NSC_LOG_ERROR_STD("ERROR: " + e.str_ + " (" + name + ")");
627                        msg = static_cast<std::string>("ERROR: ") + e.str_;
628                        return 0;
629                }
630        }
631        if (msg.empty())
632                msg = "OK all counters within bounds.";
633        else if (!bNSCLientCompatible)
634                msg = NSCHelper::translateReturn(returnCode) + ": " + msg;
635        return returnCode;
636}
637NSC_WRAPPERS_MAIN_DEF(gNSClientCompat);
638NSC_WRAPPERS_IGNORE_MSG_DEF();
639NSC_WRAPPERS_HANDLE_CMD_DEF(gNSClientCompat);
Note: See TracBrowser for help on using the repository browser.