source: nscp/modules/CheckHelpers/CheckHelpers.cpp @ 79e734f

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

Re added some more stuff anf fixed *nix compatibility of sockets

  • Property mode set to 100644
File size: 7.0 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("CheckWARNING"), _T("Just return WARN (anything passed along will be used as a message)."));
50                NSCModuleHelper::registerCommand(_T("CheckCRITICAL"), _T("Just return CRIT (anything passed along will be used as a message)."));
51                NSCModuleHelper::registerCommand(_T("CheckVersion"), _T("Just return the nagios version (along with OK status)."));
52        } catch (NSCModuleHelper::NSCMHExcpetion &e) {
53                NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_);
54        } catch (...) {
55                NSC_LOG_ERROR_STD(_T("Failed to register command."));
56        }
57        return true;
58}
59bool CheckHelpers::unloadModule() {
60        return true;
61}
62
63bool CheckHelpers::hasCommandHandler() {
64        return true;
65}
66bool CheckHelpers::hasMessageHandler() {
67        return false;
68}
69NSCAPI::nagiosReturn CheckHelpers::checkSimpleStatus(NSCAPI::nagiosReturn status, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf)
70{
71        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
72        std::list<std::wstring> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
73        if (args.empty()) {
74                msg = NSCHelper::translateReturn(status) + _T(": Lets pretend everything is going to be ok.");
75                return status;
76        }
77        std::list<std::wstring>::const_iterator cit;
78        for (cit=args.begin();cit!=args.end();++cit)
79                msg += *cit;
80        return status;
81}
82
83NSCAPI::nagiosReturn CheckHelpers::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) {
84        if (command == _T("CheckAlwaysOK")) {
85                if (argLen < 2) {
86                        msg = _T("ERROR: Missing arguments.");
87                        return NSCAPI::returnUNKNOWN;
88                }
89                NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf);
90                return NSCAPI::returnOK;
91        } else if (command == _T("CheckVersion")) {
92                msg = NSCModuleHelper::getApplicationVersionString();
93                return NSCAPI::returnOK;
94        } else if (command == _T("CheckOK")) {
95                return checkSimpleStatus(NSCAPI::returnOK, argLen, char_args, msg, perf);
96        } else if (command == _T("CheckWARNING")) {
97                return checkSimpleStatus(NSCAPI::returnWARN, argLen, char_args, msg, perf);
98        } else if (command == _T("CheckCRITICAL")) {
99                return checkSimpleStatus(NSCAPI::returnCRIT, argLen, char_args, msg, perf);
100        } else if (command == _T("CheckAlwaysCRITICAL")) {
101                if (argLen < 2) {
102                        msg = _T("ERROR: Missing arguments.");
103                        return NSCAPI::returnUNKNOWN;
104                }
105                NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf);
106                return NSCAPI::returnCRIT;
107        } else if (command == _T("CheckAlwaysWARNING")) {
108                if (argLen < 2) {
109                        msg = _T("ERROR: Missing arguments.");
110                        return NSCAPI::returnUNKNOWN;
111                }
112                NSCModuleHelper::InjectCommand(char_args[0], argLen-1, &char_args[1], msg, perf);
113                return NSCAPI::returnWARN;
114        } else if (command == _T("CheckMultiple")) {
115                return checkMultiple(argLen, char_args, msg, perf);
116        }
117        return NSCAPI::returnIgnored;
118}
119NSCAPI::nagiosReturn CheckHelpers::checkMultiple(const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf)
120{
121        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
122        std::list<std::wstring> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
123        if (args.empty()) {
124                msg = _T("Missing argument(s).");
125                return NSCAPI::returnCRIT;
126        }
127        typedef std::pair<std::wstring, std::list<std::wstring> > sub_command;
128        std::list<sub_command> commands;
129        sub_command currentCommand;
130        std::list<std::wstring>::const_iterator cit;
131        for (cit=args.begin();cit!=args.end();++cit) {
132                std::wstring arg = *cit;
133                std::pair<std::wstring,std::wstring> p = strEx::split(arg,_T("="));
134                if (p.first == _T("command")) {
135                        if (!currentCommand.first.empty())
136                                commands.push_back(currentCommand);
137                        currentCommand.first = p.second;
138                        currentCommand.second.clear();
139                } else {
140                        currentCommand.second.push_back(*cit);
141                }
142        }
143        if (!currentCommand.first.empty())
144                commands.push_back(currentCommand);
145        std::list<sub_command>::iterator cit2;
146        for (cit2 = commands.begin(); cit2 != commands.end(); ++cit2) {
147                unsigned int length = 0;
148                TCHAR ** args = arrayBuffer::list2arrayBuffer((*cit2).second, length);
149                std::wstring tMsg, tPerf;
150                NSCAPI::nagiosReturn tRet = NSCModuleHelper::InjectCommand((*cit2).first.c_str(), length, args, tMsg, tPerf);
151                arrayBuffer::destroyArrayBuffer(args, length);
152                returnCode = NSCHelper::maxState(returnCode, tRet);
153                if (!msg.empty())
154                        msg += _T(", ");
155                msg += tMsg;
156                perf += tPerf;
157        }
158        return returnCode;
159}
160
161
162NSC_WRAPPERS_MAIN_DEF(gCheckHelpers);
163NSC_WRAPPERS_IGNORE_MSG_DEF();
164NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckHelpers);
Note: See TracBrowser for help on using the repository browser.