source: nscp/modules/CheckSystem/CheckSystem.cpp @ 34e7428

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

Added new option (namespace) to CheckWMI and CheckWMIValue use like so:

CheckWMI namespace=root
cimv2 MaxCrit=3 MinWarn=1 "Query:load=Select * from win32_Processor"

  • Property mode set to 100644
File size: 40.2 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
10*                                                                         *
11*   This program is distributed in the hope that it will be useful,       *
12*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14*   GNU General Public License for more details.                          *
15*                                                                         *
16*   You should have received a copy of the GNU General Public License     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21
22#include "stdafx.h"
23#include "CheckSystem.h"
24#include <utils.h>
25#include <tlhelp32.h>
26#include <EnumNtSrv.h>
27#include <EnumProcess.h>
28#include <checkHelpers.hpp>
29#include <map>
30#include <set>
31#include <sysinfo.h>
32#ifndef NO_BOOST_DEP
33#include <boost/regex.hpp>
34#endif
35
36CheckSystem gCheckSystem;
37
38/**
39 * DLL Entry point
40 * @param hModule
41 * @param ul_reason_for_call
42 * @param lpReserved
43 * @return
44 */
45BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
46{
47        NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call);
48        return TRUE;
49}
50
51/**
52 * Default c-tor
53 * @return
54 */
55CheckSystem::CheckSystem() : processMethod_(0), pdhThread(_T("pdhThread")) {}
56/**
57 * Default d-tor
58 * @return
59 */
60CheckSystem::~CheckSystem() {}
61/**
62 * Load (initiate) module.
63 * Start the background collector thread and let it run until unloadModule() is called.
64 * @return true
65 */
66bool CheckSystem::loadModule() {
67        pdhThread.createThread();
68        std::wstring wantedMethod = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_ENUMPROC_METHOD, C_SYSTEM_ENUMPROC_METHOD_DEFAULT);
69        CEnumProcess tmp;
70        int method = tmp.GetAvailableMethods();
71        if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_AUTO) {
72                OSVERSIONINFO osVer = systemInfo::getOSVersion();
73                /*
74                if (systemInfo::isBelowNT4(osVer)) {
75                        NSC_DEBUG_MSG_STD(_T("Autodetected NT4<, using PSAPI process enumeration."));
76                        if (method == (method|ENUM_METHOD::PSAPI)) {
77                                processMethod_ = ENUM_METHOD::PSAPI;
78                        } else {
79                                NSC_LOG_ERROR_STD(_T("PSAPI method not available, since you are on NT4 you need to install \"Platform SDK Redistributable: PSAPI for Windows NT\" from Microsoft."));
80                                NSC_LOG_ERROR_STD(_T("Try this URL: http://www.microsoft.com/downloads/details.aspx?FamilyID=3d1fbaed-d122-45cf-9d46-1cae384097ac"));
81                        }
82                } else if (systemInfo::isAboveW2K(osVer)) {
83                        NSC_DEBUG_MSG_STD(_T("Autodetected W2K>, using TOOLHELP process enumeration."));
84                        if (method == (method|ENUM_METHOD::TOOLHELP)) {
85                                processMethod_ = ENUM_METHOD::TOOLHELP;
86                        } else {
87                                NSC_LOG_ERROR_STD(_T("TOOLHELP was not available, since you are on > W2K you need top manually override the ") C_SYSTEM_ENUMPROC_METHOD _T("option in NSC:ini."));
88                        }
89                } else {
90                */
91                        //NSC_DEBUG_MSG_STD(_T("Autodetected failed, using PSAPI process enumeration."));
92                        processMethod_ = ENUM_METHOD::PSAPI;
93                        if (method == (method|ENUM_METHOD::PSAPI)) {
94                                processMethod_ = ENUM_METHOD::PSAPI;
95                        } else {
96                                NSC_LOG_ERROR_STD(_T("PSAPI method not availabletry installing \"Platform SDK Redistributable: PSAPI for Windows NT\" from Microsoft."));
97                                NSC_LOG_ERROR_STD(_T("Try this URL: http://www.microsoft.com/downloads/details.aspx?FamilyID=3d1fbaed-d122-45cf-9d46-1cae384097ac"));
98                        }
99                //}
100        } else if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_PSAPI) {
101                NSC_DEBUG_MSG_STD(_T("Using PSAPI method."));
102                if (method == (method|ENUM_METHOD::PSAPI)) {
103                        processMethod_ = ENUM_METHOD::PSAPI;
104                } else {
105                        NSC_LOG_ERROR_STD(_T("PSAPI method not available, check ") C_SYSTEM_ENUMPROC_METHOD _T(" option."));
106                }
107        } else {
108                NSC_LOG_ERROR_STD(_T("TOOLHELP method has been removed sine we dont really want to support w9x ") C_SYSTEM_ENUMPROC_METHOD _T("."));
109        }
110        try {
111                NSCModuleHelper::registerCommand(_T("checkCPU"), _T("Check the CPU load of the computer."));
112                NSCModuleHelper::registerCommand(_T("checkUpTime"), _T("Check the up-time of the computer."));
113                NSCModuleHelper::registerCommand(_T("checkServiceState"), _T("Check the state of one or more of the computer services."));
114                NSCModuleHelper::registerCommand(_T("checkProcState"), _T("Check the state of one or more of the processes running on the computer."));
115                NSCModuleHelper::registerCommand(_T("checkMem"), _T("Check free/used memory on the system."));
116                NSCModuleHelper::registerCommand(_T("checkCounter"), _T("Check a PDH counter."));
117        } catch (NSCModuleHelper::NSCMHExcpetion &e) {
118                NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_);
119        } catch (...) {
120                NSC_LOG_ERROR_STD(_T("Failed to register command."));
121        }
122        return true;
123}
124/**
125 * Unload (terminate) module.
126 * Attempt to stop the background processing thread.
127 * @return true if successfully, false if not (if not things might be bad)
128 */
129bool CheckSystem::unloadModule() {
130        if (!pdhThread.exitThread(20000)) {
131                std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl;
132                NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result..."));
133        }
134        return true;
135}
136/**
137 * Check if we have a command handler.
138 * @return true (as we have a command handler)
139 */
140bool CheckSystem::hasCommandHandler() {
141        return true;
142}
143/**
144 * Check if we have a message handler.
145 * @return false as we have no message handler
146 */
147bool CheckSystem::hasMessageHandler() {
148        return false;
149}
150
151int CheckSystem::commandLineExec(const TCHAR* command,const unsigned int argLen,TCHAR** args) {
152        if (_wcsicmp(command, _T("debugpdh")) == 0) {
153                PDH::Enumerations::Objects lst;
154                try {
155                        lst = PDH::Enumerations::EnumObjects();
156                } catch (const PDH::PDHException e) {
157                        std::wcout << _T("Service enumeration failed: ") << e.getError();
158                        return 0;
159                }
160                for (PDH::Enumerations::Objects::iterator it = lst.begin();it!=lst.end();++it) {
161                        if ((*it).instances.size() > 0) {
162                                for (PDH::Enumerations::Instances::const_iterator it2 = (*it).instances.begin();it2!=(*it).instances.end();++it2) {
163                                        for (PDH::Enumerations::Counters::const_iterator it3 = (*it).counters.begin();it3!=(*it).counters.end();++it3) {
164                                                std::wstring counter = _T("\\") + (*it).name + _T("(") + (*it2).name + _T(")\\") + (*it3).name;
165                                                std::wcout << _T("testing: ") << counter << _T(": ");
166                                                std::list<std::wstring> errors;
167                                                std::list<std::wstring> status;
168                                                std::wstring error;
169                                                bool bStatus = true;
170                                                if (PDH::Enumerations::validate(counter, error)) {
171                                                        status.push_back(_T("open"));
172                                                } else {
173                                                        errors.push_back(_T("NOT found: ") + error);
174                                                        bStatus = false;
175                                                }
176                                                if (bStatus) {
177                                                        PDH::PDHCounter *pCounter = NULL;
178                                                        PDH::PDHQuery pdh;
179                                                        try {
180                                                                PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE> cDouble;
181                                                                pCounter = pdh.addCounter(counter, &cDouble);
182                                                                pdh.open();
183
184                                                                if (pCounter != NULL) {
185                                                                        try {
186                                                                                PDH::PDHCounterInfo info = pCounter->getCounterInfo();
187                                                                                errors.push_back(_T("CounterName: ") + info.szCounterName);
188                                                                                errors.push_back(_T("ExplainText: ") + info.szExplainText);
189                                                                                errors.push_back(_T("FullPath: ") + info.szFullPath);
190                                                                                errors.push_back(_T("InstanceName: ") + info.szInstanceName);
191                                                                                errors.push_back(_T("MachineName: ") + info.szMachineName);
192                                                                                errors.push_back(_T("ObjectName: ") + info.szObjectName);
193                                                                                errors.push_back(_T("ParentInstance: ") + info.szParentInstance);
194                                                                                errors.push_back(_T("Type: ") + strEx::itos(info.dwType));
195                                                                                errors.push_back(_T("Scale: ") + strEx::itos(info.lScale));
196                                                                                errors.push_back(_T("Default Scale: ") + strEx::itos(info.lDefaultScale));
197                                                                                errors.push_back(_T("Status: ") + strEx::itos(info.CStatus));
198                                                                                status.push_back(_T("described"));
199                                                                        } catch (const PDH::PDHException e) {
200                                                                                errors.push_back(_T("Describe failed: ") + e.getError());
201                                                                                bStatus = false;
202                                                                        }
203                                                                }
204
205                                                                pdh.gatherData();
206                                                                pdh.close();
207                                                                status.push_back(_T("queried"));
208                                                        } catch (const PDH::PDHException e) {
209                                                                errors.push_back(_T("Query failed: ") + e.getError());
210                                                                bStatus = false;
211                                                                try {
212                                                                        pdh.gatherData();
213                                                                        pdh.close();
214                                                                        bStatus = true;
215                                                                } catch (const PDH::PDHException e) {
216                                                                        errors.push_back(_T("Query failed (again!): ") + e.getError());
217                                                                }
218                                                        }
219
220                                                }
221                                                if (!bStatus) {
222                                                        std::list<std::wstring>::const_iterator cit = status.begin();
223                                                        for (;cit != status.end(); ++cit) {
224                                                                std::wcout << *cit << _T(", ");
225                                                        }
226                                                        std::wcout << std::endl;
227                                                        std::wcout << _T("  | Log") << std::endl;
228                                                        std::wcout << _T("--+------  --    -") << std::endl;
229                                                        cit = errors.begin();
230                                                        for (;cit != errors.end(); ++cit) {
231                                                                std::wcout << _T("  | ") << *cit << std::endl;
232                                                        }
233                                                } else {
234                                                        std::list<std::wstring>::const_iterator cit = status.begin();
235                                                        for (;cit != status.end(); ++cit) {
236                                                                std::wcout << *cit << _T(", ");;
237                                                        }
238                                                        std::wcout << std::endl;
239                                                }
240                                        }
241                                }
242                        } else {
243                                for (PDH::Enumerations::Counters::const_iterator it2 = (*it).counters.begin();it2!=(*it).counters.end();++it2) {
244                                        std::wstring counter = _T("\\") + (*it).name + _T("\\") + (*it2).name;
245                                        std::wcout << _T("testing: ") << counter << _T(": ");
246                                        std::wstring error;
247                                        if (PDH::Enumerations::validate(counter, error)) {
248                                                std::wcout << _T(" found ");
249                                        } else {
250                                                std::wcout << _T(" *NOT* found (") << error << _T(") ") << std::endl;
251                                                break;
252                                        }
253                                        bool bOpend = false;
254                                        try {
255                                                PDH::PDHQuery pdh;
256                                                PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE> cDouble;
257                                                pdh.addCounter(counter, &cDouble);
258                                                pdh.open();
259                                                pdh.gatherData();
260                                                pdh.close();
261                                                bOpend = true;
262                                        } catch (const PDH::PDHException e) {
263                                                std::wcout << _T(" could *not* be open (") << e.getError() << _T(") ") << std::endl;
264                                                break;
265                                        }
266                                        std::wcout << _T(" open ");
267                                        std::wcout << std::endl;;
268                                }
269                        }
270                }
271        } else if (_wcsicmp(command, _T("listpdh")) == 0) {
272                PDH::Enumerations::Objects lst;
273                try {
274                        lst = PDH::Enumerations::EnumObjects();
275                } catch (const PDH::PDHException e) {
276                        std::wcout << _T("Service enumeration failed: ") << e.getError();
277                        return 0;
278                }
279                for (PDH::Enumerations::Objects::iterator it = lst.begin();it!=lst.end();++it) {
280                        if ((*it).instances.size() > 0) {
281                                for (PDH::Enumerations::Instances::const_iterator it2 = (*it).instances.begin();it2!=(*it).instances.end();++it2) {
282                                        for (PDH::Enumerations::Counters::const_iterator it3 = (*it).counters.begin();it3!=(*it).counters.end();++it3) {
283                                                std::wcout << _T("\\") << (*it).name << _T("(") << (*it2).name << _T(")\\") << (*it3).name << std::endl;;
284                                        }
285                                }
286                        } else {
287                                for (PDH::Enumerations::Counters::const_iterator it2 = (*it).counters.begin();it2!=(*it).counters.end();++it2) {
288                                        std::wcout << _T("\\") << (*it).name << _T("\\") << (*it2).name << std::endl;;
289                                }
290                        }
291                }
292        }
293        return 0;
294}
295
296
297/**
298 * Main command parser and delegator.
299 * This also handles a lot of the simpler responses (though some are deferred to other helper functions)
300 *
301 *
302 * @param command
303 * @param argLen
304 * @param **args
305 * @return
306 */
307NSCAPI::nagiosReturn CheckSystem::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) {
308        std::list<std::wstring> stl_args;
309        CheckSystem::returnBundle rb;
310        if (command == _T("checkCPU")) {
311                return checkCPU(argLen, char_args, msg, perf);
312        } else if (command == _T("checkUpTime")) {
313                return checkUpTime(argLen, char_args, msg, perf);
314        } else if (command == _T("checkServiceState")) {
315                return checkServiceState(argLen, char_args, msg, perf);
316        } else if (command == _T("checkProcState")) {
317                return checkProcState(argLen, char_args, msg, perf);
318        } else if (command == _T("checkMem")) {
319                return checkMem(argLen, char_args, msg, perf);
320        } else if (command == _T("checkCounter")) {
321                return checkCounter(argLen, char_args, msg, perf);
322        }
323        return NSCAPI::returnIgnored;
324}
325
326
327class cpuload_handler {
328public:
329        static int parse(std::wstring s) {
330                return strEx::stoi(s);
331        }
332        static int parse_percent(std::wstring s) {
333                return strEx::stoi(s);
334        }
335        static std::wstring print(int value) {
336                return strEx::itos(value) + _T("%");
337        }
338        static std::wstring print_unformated(int value) {
339                return strEx::itos(value);
340        }
341
342        static std::wstring get_perf_unit(__int64 value) {
343                return _T("");
344        }
345        static std::wstring print_perf(__int64 value, std::wstring unit) {
346                return strEx::itos(value);
347        }
348        static std::wstring print_percent(int value) {
349                return strEx::itos(value) + _T("%");
350        }
351        static std::wstring key_prefix() {
352                return _T("average load ");
353        }
354        static std::wstring key_postfix() {
355                return _T("");
356        }
357};
358NSCAPI::nagiosReturn CheckSystem::checkCPU(const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf)
359{
360        typedef checkHolders::CheckContainer<checkHolders::MaxMinBounds<checkHolders::NumericBounds<int, cpuload_handler> > > CPULoadContainer;
361
362        std::list<std::wstring> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
363        if (stl_args.empty()) {
364                msg = _T("ERROR: Missing argument exception.");
365                return NSCAPI::returnUNKNOWN;
366        }
367        std::list<CPULoadContainer> list;
368        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
369        bool bNSClient = false;
370        bool bPerfData = true;
371        CPULoadContainer tmpObject;
372
373        tmpObject.data = _T("cpuload");
374
375        MAP_OPTIONS_BEGIN(stl_args)
376                MAP_OPTIONS_NUMERIC_ALL(tmpObject, _T(""))
377                MAP_OPTIONS_STR(_T("warn"), tmpObject.warn.max)
378                MAP_OPTIONS_STR(_T("crit"), tmpObject.crit.max)
379                MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData)
380                MAP_OPTIONS_STR_AND(_T("time"), tmpObject.data, list.push_back(tmpObject))
381                MAP_OPTIONS_STR_AND(_T("Time"), tmpObject.data, list.push_back(tmpObject))
382                MAP_OPTIONS_SHOWALL(tmpObject)
383                MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient)
384                MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2)
385                        else if (p2.first == _T("Time")) {
386                                tmpObject.data = p__.second;
387                                tmpObject.alias = p2.second;
388                                list.push_back(tmpObject);
389                        }
390        MAP_OPTIONS_MISSING_EX(p2, msg, _T("Unknown argument: "))
391                MAP_OPTIONS_SECONDARY_END()
392                MAP_OPTIONS_FALLBACK_AND(tmpObject.data, list.push_back(tmpObject))
393        MAP_OPTIONS_END()
394
395        for (std::list<CPULoadContainer>::const_iterator it = list.begin(); it != list.end(); ++it) {
396                CPULoadContainer load = (*it);
397                PDHCollector *pObject = pdhThread.getThread();
398                if (!pObject) {
399                        msg = _T("ERROR: PDH Collection thread not running.");
400                        return NSCAPI::returnUNKNOWN;
401                }
402                int value = pObject->getCPUAvrage(load.data + _T("m"));
403                if (value == -1) {
404                        msg = _T("ERROR: Could not get data for ") + load.getAlias() + _T(" perhaps we don't collect data this far back?");
405                        return NSCAPI::returnUNKNOWN;
406                }
407                if (bNSClient) {
408                        if (!msg.empty()) msg += _T("&");
409                        msg += strEx::itos(value);
410                } else {
411                        load.setDefault(tmpObject);
412                        load.perfData = bPerfData;
413                        load.runCheck(value, returnCode, msg, perf);
414                }
415        }
416
417        if (msg.empty())
418                msg = _T("OK CPU Load ok.");
419        else if (!bNSClient)
420                msg = NSCHelper::translateReturn(returnCode) + _T(": ") + msg;
421        return returnCode;
422}
423
424NSCAPI::nagiosReturn CheckSystem::checkUpTime(const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf)
425{
426        typedef checkHolders::CheckContainer<checkHolders::MaxMinBoundsTime> UpTimeContainer;
427
428        std::list<std::wstring> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
429        if (stl_args.empty()) {
430                msg = _T("ERROR: Missing argument exception.");
431                return NSCAPI::returnUNKNOWN;
432        }
433        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
434        bool bNSClient = false;
435        bool bPerfData = true;
436        UpTimeContainer bounds;
437
438        bounds.data = _T("uptime");
439
440        MAP_OPTIONS_BEGIN(stl_args)
441                MAP_OPTIONS_NUMERIC_ALL(bounds, _T(""))
442                MAP_OPTIONS_STR(_T("warn"), bounds.warn.min)
443                MAP_OPTIONS_STR(_T("crit"), bounds.crit.min)
444                MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData)
445                MAP_OPTIONS_STR(_T("Alias"), bounds.data)
446                MAP_OPTIONS_SHOWALL(bounds)
447                MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient)
448                MAP_OPTIONS_MISSING(msg, _T("Unknown argument: "))
449                MAP_OPTIONS_END()
450
451
452        PDHCollector *pObject = pdhThread.getThread();
453        if (!pObject) {
454                msg = _T("ERROR: PDH Collection thread not running.");
455                return NSCAPI::returnUNKNOWN;
456        }
457        unsigned long long value = pObject->getUptime();
458        if (value == -1) {
459                msg = _T("ERROR: Could not get value");
460                return NSCAPI::returnUNKNOWN;
461        }
462        if (bNSClient) {
463                msg = strEx::itos(value);
464        } else {
465                value *= 1000;
466                bounds.perfData = bPerfData;
467                bounds.runCheck(value, returnCode, msg, perf);
468        }
469
470        if (msg.empty())
471                msg = _T("OK all counters within bounds.");
472        else if (!bNSClient)
473                msg = NSCHelper::translateReturn(returnCode) + _T(": ") + msg;
474        return returnCode;
475}
476
477// @todo state_handler
478
479
480inline int get_state(DWORD state) {
481        if (state == SERVICE_RUNNING)
482                return checkHolders::state_started;
483        else if (state == SERVICE_STOPPED)
484                return checkHolders::state_stopped;
485        return checkHolders::state_none;
486}
487
488/**
489 * Retrieve the service state of one or more services (by name).
490 * Parse a list with a service names and verify that all named services are running.
491 * <pre>
492 * Syntax:
493 * request: checkServiceState <option> [<option> [...]]
494 * Return: <return state>&<service1> : <state1> - <service2> : <state2> - ...
495 * Available options:
496 *              <name>=<state>  Check if a service has a specific state
497 *                      State can be wither started or stopped
498 *              ShowAll                 Show the state of all listed service. If not set only critical services are listed.
499 * Examples:
500 * checkServiceState showAll myService MyService
501 *</pre>
502 *
503 * @param command Command to execute
504 * @param argLen The length of the argument buffer
505 * @param **char_args The argument buffer
506 * @param &msg String to put message in
507 * @param &perf String to put performance data in
508 * @return The status of the command
509 */
510NSCAPI::nagiosReturn CheckSystem::checkServiceState(const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf)
511{
512        typedef checkHolders::CheckContainer<checkHolders::SimpleBoundsStateBoundsInteger> StateContainer;
513        std::list<std::wstring> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
514        if (stl_args.empty()) {
515                msg = _T("ERROR: Missing argument exception.");
516                return NSCAPI::returnUNKNOWN;
517        }
518        std::list<StateContainer> list;
519        std::set<std::wstring> excludeList;
520        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
521        bool bNSClient = false;
522        StateContainer tmpObject;
523        bool bPerfData = true;
524        bool bAutoStart = false;
525
526        tmpObject.data = _T("service");
527        tmpObject.crit.state = _T("started");
528        //{{
529        MAP_OPTIONS_BEGIN(stl_args)
530                MAP_OPTIONS_SHOWALL(tmpObject)
531                MAP_OPTIONS_STR(_T("Alias"), tmpObject.data)
532                MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData)
533                MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient)
534                MAP_OPTIONS_BOOL_TRUE(_T("CheckAll"), bAutoStart)
535                MAP_OPTIONS_INSERT(_T("exclude"), excludeList)
536                MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2)
537                MAP_OPTIONS_MISSING_EX(p2, msg, _T("Unknown argument: "))
538                MAP_OPTIONS_SECONDARY_END()
539                else {
540                        tmpObject.data = p__.first;
541                        if (p__.second.empty())
542                                tmpObject.crit.state = _T("started");
543                        else
544                                tmpObject.crit.state = p__.second;
545                        list.push_back(tmpObject);
546                }
547        MAP_OPTIONS_END()
548        //}}
549        if (bAutoStart) {
550                // get a list of all service with startup type Automatic
551                /*
552                ;check_all_services[SERVICE_BOOT_START]=ignored
553                ;check_all_services[SERVICE_SYSTEM_START]=ignored
554                ;check_all_services[SERVICE_AUTO_START]=started
555                ;check_all_services[SERVICE_DEMAND_START]=ignored
556                ;check_all_services[SERVICE_DISABLED]=stopped
557                std::wstring wantedMethod = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_ENUMPROC_METHOD, C_SYSTEM_ENUMPROC_METHOD_DEFAULT);
558                */
559                std::map<DWORD,std::wstring> lookups;
560                lookups[SERVICE_BOOT_START] = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_SVC_ALL_0, C_SYSTEM_SVC_ALL_0_DEFAULT);
561                lookups[SERVICE_SYSTEM_START] = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_SVC_ALL_1, C_SYSTEM_SVC_ALL_1_DEFAULT);
562                lookups[SERVICE_AUTO_START] = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_SVC_ALL_2, C_SYSTEM_SVC_ALL_2_DEFAULT);
563                lookups[SERVICE_DEMAND_START] = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_SVC_ALL_3, C_SYSTEM_SVC_ALL_3_DEFAULT);
564                lookups[SERVICE_DISABLED] = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_SVC_ALL_4, C_SYSTEM_SVC_ALL_4_DEFAULT);
565
566
567                std::list<TNtServiceInfo> service_list_automatic = TNtServiceInfo::EnumServices(SERVICE_WIN32,SERVICE_INACTIVE|SERVICE_ACTIVE);
568                for (std::list<TNtServiceInfo>::const_iterator service =service_list_automatic.begin();service!=service_list_automatic.end();++service) {
569                        if (excludeList.find((*service).m_strServiceName) == excludeList.end()) {
570                                tmpObject.data = (*service).m_strServiceName;
571                                tmpObject.crit.state = lookups[(*service).m_dwStartType];
572                                list.push_back(tmpObject);
573                        }
574                }
575        }
576        for (std::list<StateContainer>::iterator it = list.begin(); it != list.end(); ++it) {
577                TNtServiceInfo info;
578                if (bNSClient) {
579                        try {
580                                info = TNtServiceInfo::GetService((*it).data.c_str());
581                        } catch (NTServiceException e) {
582                                if (!msg.empty()) msg += _T(" - ");
583                                msg += (*it).data + _T(": Error");
584                                NSCHelper::escalteReturnCodeToWARN(returnCode);
585                                continue;
586                        }
587                        if ((*it).crit.state.hasBounds()) {
588                                bool ok = (*it).crit.state.check(get_state(info.m_dwCurrentState));
589                                if (!ok || (*it).showAll()) {
590                                        if (info.m_dwCurrentState == SERVICE_RUNNING) {
591                                                if (!msg.empty()) msg += _T(" - ");
592                                                msg += (*it).data + _T(": Started");
593                                        } else if (info.m_dwCurrentState == SERVICE_STOPPED) {
594                                                if (!msg.empty()) msg += _T(" - ");
595                                                msg += (*it).data + _T(": Stopped");
596                                        } else {
597                                                if (!msg.empty()) msg += _T(" - ");
598                                                msg += (*it).data + _T(": Unknown");
599                                        }
600                                        if (!ok)
601                                                NSCHelper::escalteReturnCodeToCRIT(returnCode);
602                                }
603                        }
604                } else {
605                        try {
606                                info = TNtServiceInfo::GetService((*it).data.c_str());
607                        } catch (NTServiceException e) {
608                                NSC_LOG_ERROR_STD(e.getError());
609                                msg = e.getError();
610                                return NSCAPI::returnUNKNOWN;
611                        }
612                        checkHolders::state_type value;
613                        if (info.m_dwCurrentState == SERVICE_RUNNING)
614                                value = checkHolders::state_started;
615                        else if (info.m_dwCurrentState == SERVICE_STOPPED)
616                                value = checkHolders::state_stopped;
617                        else
618                                value = checkHolders::state_none;
619                        (*it).perfData = bPerfData;
620                        (*it).runCheck(value, returnCode, msg, perf);
621                }
622
623        }
624        if (msg.empty())
625                msg = _T("OK: All services are running.");
626        else if (!bNSClient)
627                msg = NSCHelper::translateReturn(returnCode) + _T(": ") + msg;
628        return returnCode;
629}
630
631/**
632 * Check available memory and return various check results
633 * Example: checkMem showAll maxWarn=50 maxCrit=75
634 *
635 * @param command Command to execute
636 * @param argLen The length of the argument buffer
637 * @param **char_args The argument buffer
638 * @param &msg String to put message in
639 * @param &perf String to put performance data in
640 * @return The status of the command
641 */
642NSCAPI::nagiosReturn CheckSystem::checkMem(const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf)
643{
644        typedef checkHolders::CheckContainer<checkHolders::MaxMinBounds<checkHolders::NumericPercentageBounds<checkHolders::PercentageValueType<unsigned __int64, unsigned __int64>, checkHolders::disk_size_handler<unsigned __int64> > > > MemoryContainer;
645        std::list<std::wstring> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
646        if (stl_args.empty()) {
647                msg = _T("ERROR: Missing argument exception.");
648                return NSCAPI::returnUNKNOWN;
649        }
650        std::list<MemoryContainer> list;
651        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
652        bool bShowAll = false;
653        bool bPerfData = true;
654        bool bNSClient = false;
655        MemoryContainer tmpObject;
656
657        MAP_OPTIONS_BEGIN(stl_args)
658                MAP_OPTIONS_SHOWALL(tmpObject)
659                MAP_OPTIONS_STR_AND(_T("type"), tmpObject.data, list.push_back(tmpObject))
660                MAP_OPTIONS_STR_AND(_T("Type"), tmpObject.data, list.push_back(tmpObject))
661                MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2)
662                MAP_OPTIONS_SECONDARY_STR_AND(p2,_T("type"), tmpObject.data, tmpObject.alias, list.push_back(tmpObject))
663                        MAP_OPTIONS_MISSING_EX(p2, msg, _T("Unknown argument: "))
664                        MAP_OPTIONS_SECONDARY_END()
665                MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData)
666                MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient)
667                MAP_OPTIONS_DISK_ALL(tmpObject, _T(""), _T("Free"), _T("Used"))
668                MAP_OPTIONS_STR(_T("Alias"), tmpObject.data)
669                MAP_OPTIONS_SHOWALL(tmpObject)
670                MAP_OPTIONS_MISSING(msg, _T("Unknown argument: "))
671                MAP_OPTIONS_END()
672
673        if (bNSClient) {
674                tmpObject.data = _T("paged");
675                list.push_back(tmpObject);
676        }
677
678        checkHolders::PercentageValueType<unsigned long long, unsigned long long> dataPaged;
679        CheckMemory::memData data;
680        bool firstPaged = true;
681        bool firstMem = true;
682        for (std::list<MemoryContainer>::const_iterator pit = list.begin(); pit != list.end(); ++pit) {
683                MemoryContainer check = (*pit);
684                check.setDefault(tmpObject);
685                checkHolders::PercentageValueType<unsigned long long, unsigned long long> value;
686                if (firstPaged && (check.data == _T("paged"))) {
687                        firstPaged = false;
688                        PDHCollector *pObject = pdhThread.getThread();
689                        if (!pObject) {
690                                msg = _T("ERROR: PDH Collection thread not running.");
691                                return NSCAPI::returnUNKNOWN;
692                        }
693                        dataPaged.value = pObject->getMemCommit();
694                        if (dataPaged.value == -1) {
695                                msg = _T("ERROR: Failed to get PDH value.");
696                                return NSCAPI::returnUNKNOWN;
697                        }
698                        dataPaged.total = pObject->getMemCommitLimit();
699                        if (dataPaged.total == -1) {
700                                msg = _T("ERROR: Failed to get PDH value.");
701                                return NSCAPI::returnUNKNOWN;
702                        }
703                } else if (firstMem) {
704                        try {
705                                data = memoryChecker.getMemoryStatus();
706                        } catch (CheckMemoryException e) {
707                                msg = e.getError();
708                                return NSCAPI::returnCRIT;
709                        }
710                }
711
712                if (check.data == _T("page")) {
713                        value.value = data.pageFile.total-data.pageFile.avail; // mem.dwTotalPageFile-mem.dwAvailPageFile;
714                        value.total = data.pageFile.total; //mem.dwTotalPageFile;
715                        if (check.alias.empty())
716                                check.alias = _T("page file");
717                } else if (check.data == _T("physical")) {
718                        value.value = data.phys.total-data.phys.avail; //mem.dwTotalPhys-mem.dwAvailPhys;
719                        value.total = data.phys.total; //mem.dwTotalPhys;
720                        if (check.alias.empty())
721                                check.alias = _T("physical memory");
722                } else if (check.data == _T("virtual")) {
723                        value.value = data.virtualMem.total-data.virtualMem.avail;//mem.dwTotalVirtual-mem.dwAvailVirtual;
724                        value.total = data.virtualMem.total;//mem.dwTotalVirtual;
725                        if (check.alias.empty())
726                                check.alias = _T("virtual memory");
727                } else  if (check.data == _T("paged")) {
728                        value.value = dataPaged.value;
729                        value.total = dataPaged.total;
730                        if (check.alias.empty())
731                                check.alias = _T("paged bytes");
732                } else {
733                        msg = check.data + _T(" is not a known check...");
734                        return NSCAPI::returnCRIT;
735                }
736                if (bNSClient) {
737                        msg = strEx::itos(value.total) + _T("&") + strEx::itos(value.value);
738                        return NSCAPI::returnOK;
739                } else {
740                        check.perfData = bPerfData;
741                        check.runCheck(value, returnCode, msg, perf);
742                }
743        }
744
745        if (msg.empty())
746                msg = _T("OK memory within bounds.");
747        else
748                msg = NSCHelper::translateReturn(returnCode) + _T(": ") + msg;
749        return returnCode;
750}
751typedef struct NSPROCDATA__ {
752        unsigned int count;
753        CEnumProcess::CProcessEntry entry;
754        std::wstring key;
755
756        NSPROCDATA__() : count(0) {}
757        NSPROCDATA__(const NSPROCDATA__ &other) : count(other.count), entry(other.entry), key(other.key) {}
758} NSPROCDATA;
759typedef std::map<std::wstring,NSPROCDATA,strEx::case_blind_string_compare> NSPROCLST;
760/**
761* Get a hash_map with all running processes.
762* @return a hash_map with all running processes
763*/
764NSPROCLST GetProcessList(int processMethod, bool getCmdLines)
765{
766        NSPROCLST ret;
767        if (processMethod == 0) {
768                NSC_LOG_ERROR_STD(_T("ProcessMethod not defined or not available."));
769                return ret;
770        }
771        CEnumProcess enumeration;
772        if (enumeration.SetMethod(processMethod) != processMethod) {
773                NSC_LOG_ERROR_STD(_T("Failed to set process enumeration method"));
774                return ret;
775        }
776        int toFill = CEnumProcess::CProcessEntry::fill_filename;
777        if (getCmdLines)
778                toFill |= CEnumProcess::CProcessEntry::fill_command_line;
779        CEnumProcess::CProcessEntry entry(toFill);
780        for (BOOL OK = enumeration.GetProcessFirst(&entry); OK; OK = enumeration.GetProcessNext(&entry) ) {
781                std::wstring key;
782                if (getCmdLines)
783                        key = entry.command_line;
784                else
785                        key = entry.filename;
786                NSPROCLST::iterator it = ret.find(key);
787                if (it == ret.end()) {
788                        ret[key].entry = entry;
789                        ret[key].count = 1;
790                        ret[key].key = key;
791                } else
792                        (*it).second.count++;
793        }
794        return ret;
795}
796
797/**
798 * Check process state and return result
799 *
800 * @param command Command to execute
801 * @param argLen The length of the argument buffer
802 * @param **char_args The argument buffer
803 * @param &msg String to put message in
804 * @param &perf String to put performance data in
805 * @return The status of the command
806 */
807NSCAPI::nagiosReturn CheckSystem::checkProcState(const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf)
808{
809        typedef checkHolders::CheckContainer<checkHolders::MaxMinStateBoundsStateBoundsInteger> StateContainer;
810        std::list<std::wstring> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
811        if (stl_args.empty()) {
812                msg = _T("ERROR: Missing argument exception.");
813                return NSCAPI::returnUNKNOWN;
814        }
815        std::list<StateContainer> list;
816        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
817        bool bNSClient = false;
818        StateContainer tmpObject;
819        bool bPerfData = true;
820        bool useCmdLine = false;
821        typedef enum {
822                match_string, match_substring, match_regexp
823        } match_type;
824        match_type match = match_string;
825
826       
827
828        tmpObject.data = _T("uptime");
829        tmpObject.crit.state = _T("started");
830
831        MAP_OPTIONS_BEGIN(stl_args)
832                MAP_OPTIONS_NUMERIC_ALL(tmpObject, _T("Count"))
833                MAP_OPTIONS_STR(_T("Alias"), tmpObject.alias)
834                MAP_OPTIONS_SHOWALL(tmpObject)
835                MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData)
836                MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient)
837                MAP_OPTIONS_BOOL_TRUE(_T("cmdLine"), useCmdLine)
838                MAP_OPTIONS_MODE(_T("match"), _T("string"), match,  match_string)
839                MAP_OPTIONS_MODE(_T("match"), _T("regexp"), match,  match_regexp)
840                MAP_OPTIONS_MODE(_T("match"), _T("substr"), match,  match_substring)
841                MAP_OPTIONS_MODE(_T("match"), _T("substring"), match,  match_substring)
842                MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2)
843        else if (p2.first == _T("Proc")) {
844                        tmpObject.data = p__.second;
845                        tmpObject.alias = p2.second;
846                        list.push_back(tmpObject);
847                }
848        MAP_OPTIONS_MISSING_EX(p2, msg, _T("Unknown argument: "))
849                MAP_OPTIONS_SECONDARY_END()
850                else {
851                        tmpObject.data = p__.first;
852                        if (p__.second.empty())
853                                tmpObject.crit.state = _T("started");
854                        else
855                                tmpObject.crit.state = p__.second;
856                        list.push_back(tmpObject);
857                }
858        MAP_OPTIONS_END()
859
860        NSPROCLST runningProcs;
861        try {
862                runningProcs = GetProcessList(processMethod_, useCmdLine);
863        } catch (TCHAR *c) {
864                NSC_LOG_ERROR_STD(_T("ERROR: ") + c);
865                msg = static_cast<std::wstring>(_T("ERROR: ")) + c;
866                return NSCAPI::returnUNKNOWN;
867        }
868
869        for (std::list<StateContainer>::iterator it = list.begin(); it != list.end(); ++it) {
870                NSPROCLST::iterator proc;
871                if (match == match_string) {
872                        proc = runningProcs.find((*it).data);
873                } else if (match == match_substring) {
874                        for (proc=runningProcs.begin();proc!=runningProcs.end();++proc) {
875                                if ((*proc).first.find((*it).data) != std::wstring::npos)
876                                        break;
877                        }
878#ifndef NO_BOOST_DEP
879                } else if (match == match_regexp) {
880                        try {
881                                boost::wregex filter((*it).data,boost::regex::icase);
882                                for (proc=runningProcs.begin();proc!=runningProcs.end();++proc) {
883                                        std::wstring value = (*proc).first;
884                                        if (boost::regex_match(value, filter))
885                                                break;
886                                }
887                        } catch (const boost::bad_expression e) {
888                                NSC_LOG_ERROR_STD(_T("Failed to compile regular expression: ") + (*proc).first);
889                                msg = _T("Failed to compile regular expression: ") + (*proc).first;
890                                return NSCAPI::returnUNKNOWN;
891                        } catch (...) {
892                                NSC_LOG_ERROR_STD(_T("Failed to compile regular expression: ") + (*proc).first);
893                                msg = _T("Failed to compile regular expression: ") + (*proc).first;
894                                return NSCAPI::returnUNKNOWN;
895                        }
896#endif
897                } else {
898                        NSC_LOG_ERROR_STD(_T("Unsupported mode for: ") + (*proc).first);
899                        msg = _T("Unsupported mode for: ") + (*proc).first;
900                        return NSCAPI::returnUNKNOWN;
901                }
902                bool bFound = (proc != runningProcs.end());
903                if (bNSClient) {
904                        if (bFound && (*it).showAll()) {
905                                if (!msg.empty()) msg += _T(" - ");
906                                msg += (*proc).first + _T(": Running");
907                        } else if (bFound) {
908                        } else {
909                                if (!msg.empty()) msg += _T(" - ");
910                                msg += (*it).data + _T(": not running");
911                                NSCHelper::escalteReturnCodeToCRIT(returnCode);
912                        }
913                } else {
914                        checkHolders::MaxMinStateValueType<int, checkHolders::state_type> value;
915                        if (bFound) {
916                                value.count = (*proc).second.count;
917                                value.state = checkHolders::state_started;
918                        } else {
919                                value.count = 0;
920                                value.state = checkHolders::state_stopped;
921                        }
922                        if (bFound && (*it).alias.empty()) {
923                                (*it).alias = (*proc).first;
924                        }
925                        (*it).perfData = bPerfData;
926                        (*it).runCheck(value, returnCode, msg, perf);
927                }
928
929        }
930        if (msg.empty())
931                msg = _T("OK: All processes are running.");
932        else if (!bNSClient)
933                msg = NSCHelper::translateReturn(returnCode) + _T(": ") + msg;
934        return returnCode;
935}
936
937
938/**
939 * Check a counter and return the value
940 *
941 * @param command Command to execute
942 * @param argLen The length of the argument buffer
943 * @param **char_args The argument buffer
944 * @param &msg String to put message in
945 * @param &perf String to put performance data in
946 * @return The status of the command
947 *
948 * @todo add parsing support for NRPE
949 */
950NSCAPI::nagiosReturn CheckSystem::checkCounter(const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf)
951{
952        typedef checkHolders::CheckContainer<checkHolders::MaxMinBoundsDouble> CounterContainer;
953
954        std::list<std::wstring> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args);
955        if (stl_args.empty()) {
956                msg = _T("ERROR: Missing argument exception.");
957                return NSCAPI::returnUNKNOWN;
958        }
959        std::list<CounterContainer> counters;
960        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
961        bool bNSClient = false;
962        bool bPerfData = true;
963        /* average maax */
964        bool bCheckAverages = true;
965        std::wstring invalidStatus = _T("UNKNOWN");
966        unsigned int averageDelay = 1000;
967        CounterContainer tmpObject;
968
969        MAP_OPTIONS_BEGIN(stl_args)
970                MAP_OPTIONS_STR(_T("InvalidStatus"), invalidStatus)
971                MAP_OPTIONS_STR_AND(_T("Counter"), tmpObject.data, counters.push_back(tmpObject))
972                MAP_OPTIONS_STR(_T("MaxWarn"), tmpObject.warn.max)
973                MAP_OPTIONS_STR(_T("MinWarn"), tmpObject.warn.min)
974                MAP_OPTIONS_STR(_T("MaxCrit"), tmpObject.crit.max)
975                MAP_OPTIONS_STR(_T("MinCrit"), tmpObject.crit.min)
976                MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData)
977                MAP_OPTIONS_STR(_T("Alias"), tmpObject.data)
978                MAP_OPTIONS_SHOWALL(tmpObject)
979                MAP_OPTIONS_BOOL_EX(_T("Averages"), bCheckAverages, _T("true"), _T("false"))
980                MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient)
981                MAP_OPTIONS_FIRST_CHAR('\\', tmpObject.data, counters.push_back(tmpObject))
982                MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2)
983        else if (p2.first == _T("Counter")) {
984                tmpObject.data = p__.second;
985                                tmpObject.alias = p2.second;
986                                counters.push_back(tmpObject);
987                        }
988        MAP_OPTIONS_MISSING_EX(p2, msg, _T("Unknown argument: "))
989                MAP_OPTIONS_SECONDARY_END()
990                MAP_OPTIONS_FALLBACK_AND(tmpObject.data, counters.push_back(tmpObject))
991        MAP_OPTIONS_END()
992        for (std::list<CounterContainer>::const_iterator cit = counters.begin(); cit != counters.end(); ++cit) {
993                CounterContainer counter = (*cit);
994                try {
995                        std::wstring tstr;
996                        if (!PDH::Enumerations::validate(counter.data, tstr)) {
997                                msg = tstr;
998                                msg += _T(" (") + counter.getAlias() + _T("|") + counter.data + _T(")");
999                                return NSCHelper::translateReturn(invalidStatus);
1000                        }
1001                        PDH::PDHQuery pdh;
1002                        PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE> cDouble;
1003                        pdh.addCounter(counter.data, &cDouble);
1004                        pdh.open();
1005                        if (bCheckAverages) {
1006                                pdh.collect();
1007                                Sleep(1000);
1008                        }
1009                        pdh.gatherData();
1010                        pdh.close();
1011                        double value = cDouble.getValue();
1012                        if (bNSClient) {
1013                                msg += strEx::itos(static_cast<float>(value));
1014                        } else {
1015                                counter.perfData = bPerfData;
1016                                counter.setDefault(tmpObject);
1017                                counter.runCheck(value, returnCode, msg, perf);
1018                        }
1019                } catch (const PDH::PDHException e) {
1020                        NSC_LOG_ERROR_STD(_T("ERROR: ") + e.getError() + _T(" (") + counter.getAlias() + _T("|") + counter.data + _T(")"));
1021                        msg = static_cast<std::wstring>(_T("ERROR: ")) + e.getError()+ _T(" (") + counter.getAlias() + _T("|") + counter.data + _T(")");
1022                        return NSCAPI::returnUNKNOWN;
1023                }
1024        }
1025
1026        if (msg.empty())
1027                msg = _T("OK all counters within bounds.");
1028        else if (!bNSClient)
1029                msg = NSCHelper::translateReturn(returnCode) + _T(": ") + msg;
1030        return returnCode;
1031}
1032NSC_WRAPPERS_MAIN_DEF(gCheckSystem);
1033NSC_WRAPPERS_IGNORE_MSG_DEF();
1034NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckSystem);
1035NSC_WRAPPERS_HANDLE_CONFIGURATION(gCheckSystem);
1036NSC_WRAPPERS_CLI_DEF(gCheckSystem);
1037
1038
1039
1040MODULE_SETTINGS_START(CheckSystem, _T("System check module"), _T("..."))
1041
1042PAGE(_T("Check options"))
1043
1044ITEM_EDIT_TEXT(_T("Check resolution"), _T("This is how often the PDH data is polled and stored in the CPU buffer. (this is enterd in 1/th: of a second)"))
1045OPTION(_T("unit"), _T("1/10:th of a second"))
1046ITEM_MAP_TO(_T("basic_ini_text_mapper"))
1047OPTION(_T("section"), _T("Check System"))
1048OPTION(_T("key"), _T("CheckResolution"))
1049OPTION(_T("default"), _T("10"))
1050ITEM_END()
1051
1052ITEM_EDIT_TEXT(_T("CPU buffer size"), _T("This is the size of the buffer that stores CPU history."))
1053ITEM_MAP_TO(_T("basic_ini_text_mapper"))
1054OPTION(_T("section"), _T("Check System"))
1055OPTION(_T("key"), _T("CPUBufferSize"))
1056OPTION(_T("default"), _T("1h"))
1057ITEM_END()
1058
1059PAGE_END()
1060ADVANCED_PAGE(_T("Compatiblity settings"))
1061
1062ITEM_EDIT_TEXT(_T("MemoryCommitByte"), _T("The memory commited bytes used to calculate the avalible memory."))
1063OPTION(_T("disableCaption"), _T("Attempt to autodetect this."))
1064OPTION(_T("disabled"), _T("auto"))
1065ITEM_MAP_TO(_T("basic_ini_text_mapper"))
1066OPTION(_T("section"), _T("Check System"))
1067OPTION(_T("key"), _T("MemoryCommitByte"))
1068OPTION(_T("default"), _T("auto"))
1069ITEM_END()
1070
1071ITEM_EDIT_TEXT(_T("MemoryCommitLimit"), _T("The memory commit limit used to calculate the avalible memory."))
1072OPTION(_T("disableCaption"), _T("Attempt to autodetect this."))
1073OPTION(_T("disabled"), _T("auto"))
1074ITEM_MAP_TO(_T("basic_ini_text_mapper"))
1075OPTION(_T("section"), _T("Check System"))
1076OPTION(_T("key"), _T("MemoryCommitLimit"))
1077OPTION(_T("default"), _T("auto"))
1078ITEM_END()
1079
1080ITEM_EDIT_TEXT(_T("SystemSystemUpTime"), _T("The PDH counter for the System uptime."))
1081OPTION(_T("disableCaption"), _T("Attempt to autodetect this."))
1082OPTION(_T("disabled"), _T("auto"))
1083ITEM_MAP_TO(_T("basic_ini_text_mapper"))
1084OPTION(_T("section"), _T("Check System"))
1085OPTION(_T("key"), _T("SystemSystemUpTime"))
1086OPTION(_T("default"), _T("auto"))
1087ITEM_END()
1088
1089ITEM_EDIT_TEXT(_T("SystemTotalProcessorTime"), _T("The PDH conter usaed to measure CPU load."))
1090OPTION(_T("disableCaption"), _T("Attempt to autodetect this."))
1091OPTION(_T("disabled"), _T("auto"))
1092ITEM_MAP_TO(_T("basic_ini_text_mapper"))
1093OPTION(_T("section"), _T("Check System"))
1094OPTION(_T("key"), _T("SystemTotalProcessorTime"))
1095OPTION(_T("default"), _T("auto"))
1096ITEM_END()
1097
1098ITEM_EDIT_TEXT(_T("ProcessEnumerationMethod"), _T("The method to use when enumerating processes"))
1099OPTION(_T("count"), _T("3"))
1100OPTION(_T("caption_1"), _T("Autodetect (TOOLHELP for NT/4 and PSAPI for W2k)"))
1101OPTION(_T("value_1"), _T("auto"))
1102OPTION(_T("caption_2"), _T("TOOLHELP use this for NT/4 systems"))
1103OPTION(_T("value_2"), _T("TOOLHELP"))
1104OPTION(_T("caption_3"), _T("PSAPI use this for W2k (and abowe) systems"))
1105OPTION(_T("value_3"), _T("PSAPI"))
1106ITEM_MAP_TO(_T("basic_ini_text_mapper"))
1107OPTION(_T("section"), _T("Check System"))
1108OPTION(_T("key"), _T("ProcessEnumerationMethod"))
1109OPTION(_T("default"), _T("auto"))
1110ITEM_END()
1111
1112PAGE_END()
1113MODULE_SETTINGS_END()
Note: See TracBrowser for help on using the repository browser.