source: nscp/modules/CheckHelpers/CheckHelpers.cpp @ 50a5db2

0.4.00.4.10.4.2
Last change on this file since 50a5db2 was 50a5db2, checked in by Michael Medin <michael@…>, 3 years ago

Fixed some logica issues and changed so debug logging work on *nix (cout -> wcout)

  • Property mode set to 100644
File size: 7.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#include "stdafx.h"
22#include "CheckHelpers.h"
23#include <strEx.h>
24#include <time.h>
25#include <utils.h>
26
27CheckHelpers gCheckHelpers;
28#ifdef _WIN32
29BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
30{
31        NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call);
32        return TRUE;
33}
34#endif
35
36CheckHelpers::CheckHelpers() {
37}
38CheckHelpers::~CheckHelpers() {
39}
40
41
42bool CheckHelpers::loadModule(NSCAPI::moduleLoadMode mode) {
43        try {
44                NSCModuleHelper::registerCommand(_T("CheckAlwaysOK"), _T("Run another check and regardless of its return code return OK."));
45                NSCModuleHelper::registerCommand(_T("CheckAlwaysCRITICAL"), _T("Run another check and regardless of its return code return CRIT."));
46                NSCModuleHelper::registerCommand(_T("CheckAlwaysWARNING"), _T("Run another check and regardless of its return code return WARN."));
47                NSCModuleHelper::registerCommand(_T("CheckMultiple"), _T("Run more then one check and return the worst state."));
48                NSCModuleHelper::registerCommand(_T("CheckOK"), _T("Just return OK (anything passed along will be used as a message)."));
49                NSCModuleHelper::registerCommand(_T("check_ok"), _T("Just return OK (anything passed along will be used as a message)."));
50                NSCModuleHelper::registerCommand(_T("CheckWARNING"), _T("Just return WARN (anything passed along will be used as a message)."));
51                NSCModuleHelper::registerCommand(_T("CheckCRITICAL"), _T("Just return CRIT (anything passed along will be used as a message)."));
52                NSCModuleHelper::registerCommand(_T("CheckVersion"), _T("Just return the nagios version (along with OK status)."));
53        } catch (NSCModuleHelper::NSCMHExcpetion &e) {
54                NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_);
55        } catch (...) {
56                NSC_LOG_ERROR_STD(_T("Failed to register command."));
57        }
58        return true;
59}
60bool CheckHelpers::unloadModule() {
61        return true;
62}
63
64bool CheckHelpers::hasCommandHandler() {
65        return true;
66}
67bool CheckHelpers::hasMessageHandler() {
68        return false;
69}
70NSCAPI::nagiosReturn CheckHelpers::checkSimpleStatus(NSCAPI::nagiosReturn status, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf)
71{
72        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
73        std::list<std::wstring> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
74        if (args.empty()) {
75                msg = NSCHelper::translateReturn(status) + _T(": Lets pretend everything is going to be ok.");
76                return status;
77        }
78        std::list<std::wstring>::const_iterator cit;
79        for (cit=args.begin();cit!=args.end();++cit)
80                msg += *cit;
81        return status;
82}
83
84NSCAPI::nagiosReturn CheckHelpers::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) {
85        if (command == _T("CheckAlwaysOK")) {
86                if (argLen < 2) {
87                        msg = _T("ERROR: Missing arguments.");
88                        return NSCAPI::returnUNKNOWN;
89                }
90                NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf);
91                return NSCAPI::returnOK;
92        } else if (command == _T("CheckVersion")) {
93                msg = NSCModuleHelper::getApplicationVersionString();
94                return NSCAPI::returnOK;
95        } else if (command == _T("CheckOK")) {
96                return checkSimpleStatus(NSCAPI::returnOK, argLen, char_args, msg, perf);
97        } else if (command == _T("check_ok")) {
98                return checkSimpleStatus(NSCAPI::returnOK, argLen, char_args, msg, perf);
99        } else if (command == _T("CheckWARNING")) {
100                return checkSimpleStatus(NSCAPI::returnWARN, argLen, char_args, msg, perf);
101        } else if (command == _T("CheckCRITICAL")) {
102                return checkSimpleStatus(NSCAPI::returnCRIT, argLen, char_args, msg, perf);
103        } else if (command == _T("CheckAlwaysCRITICAL")) {
104                if (argLen < 2) {
105                        msg = _T("ERROR: Missing arguments.");
106                        return NSCAPI::returnUNKNOWN;
107                }
108                NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf);
109                return NSCAPI::returnCRIT;
110        } else if (command == _T("CheckAlwaysWARNING")) {
111                if (argLen < 2) {
112                        msg = _T("ERROR: Missing arguments.");
113                        return NSCAPI::returnUNKNOWN;
114                }
115                NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf);
116                return NSCAPI::returnWARN;
117        } else if (command == _T("CheckMultiple")) {
118                return checkMultiple(argLen, char_args, msg, perf);
119        }
120        return NSCAPI::returnIgnored;
121}
122NSCAPI::nagiosReturn CheckHelpers::checkMultiple(const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf)
123{
124        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
125        std::list<std::wstring> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
126        if (args.empty()) {
127                msg = _T("Missing argument(s).");
128                return NSCAPI::returnCRIT;
129        }
130        typedef std::pair<std::wstring, std::list<std::wstring> > sub_command;
131        std::list<sub_command> commands;
132        sub_command currentCommand;
133        std::list<std::wstring>::const_iterator cit;
134        for (cit=args.begin();cit!=args.end();++cit) {
135                std::wstring arg = *cit;
136                std::pair<std::wstring,std::wstring> p = strEx::split(arg,_T("="));
137                if (p.first == _T("command")) {
138                        if (!currentCommand.first.empty())
139                                commands.push_back(currentCommand);
140                        currentCommand.first = p.second;
141                        currentCommand.second.clear();
142                } else {
143                        currentCommand.second.push_back(*cit);
144                }
145        }
146        if (!currentCommand.first.empty())
147                commands.push_back(currentCommand);
148        std::list<sub_command>::iterator cit2;
149        for (cit2 = commands.begin(); cit2 != commands.end(); ++cit2) {
150                unsigned int length = 0;
151                TCHAR ** args = arrayBuffer::list2arrayBuffer((*cit2).second, length);
152                std::wstring tMsg, tPerf;
153                NSCAPI::nagiosReturn tRet = NSCModuleHelper::InjectCommand((*cit2).first.c_str(), length, args, tMsg, tPerf);
154                arrayBuffer::destroyArrayBuffer(args, length);
155                returnCode = NSCHelper::maxState(returnCode, tRet);
156                if (!msg.empty())
157                        msg += _T(", ");
158                msg += tMsg;
159                perf += tPerf;
160        }
161        return returnCode;
162}
163
164
165NSC_WRAPPERS_MAIN_DEF(gCheckHelpers);
166NSC_WRAPPERS_IGNORE_MSG_DEF();
167NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckHelpers);
Note: See TracBrowser for help on using the repository browser.