source: nscp/modules/CheckNSCP/CheckNSCP.cpp @ c74d7b6

0.4.00.4.10.4.2
Last change on this file since c74d7b6 was c74d7b6, checked in by Michael Medin <michael@…>, 15 months ago
  • Added new module CauseCrash? which has a single command CrashClient? to allow the service to be crashed remotly *WARNING* Dont use this command, it is for debugging and testing purpouses only
  • Fixed issue with BreakPad? which was disabled
  • Fixed some unix issues (sorry but hudson's stopped emailing me again:( )
  • Fixed service --install (now sets correct options)
  • Fixed service --install (now sets description correctly)
  • Fixed log level defined in protobuf (now same as rest of the system)
  • Fixed some issues with the check_nscp command
  • Property mode set to 100644
File size: 5.7 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 "CheckNSCP.h"
22
23#include <file_helpers.hpp>
24#include <unicode_char.hpp>
25
26#include <config.h>
27
28#include <settings/client/settings_client.hpp>
29namespace sh = nscapi::settings_helper;
30
31bool CheckNSCP::loadModule() {
32        return false;
33}
34bool CheckNSCP::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) {
35        try {
36
37                sh::settings_registry settings(get_settings_proxy());
38                settings.set_alias(_T("crash"), alias);
39
40                settings.alias().add_path_to_settings()
41                        (_T("CRASH SECTION"), _T("Configure crash handling properties."))
42                        ;
43
44                settings.alias().add_key_to_settings()
45                        (_T("archive folder"), sh::wpath_key(&crashFolder, CRASH_ARCHIVE_FOLDER),
46                        CRASH_ARCHIVE_FOLDER_KEY, _T("The archive folder for crash dunpes."))
47                        ;
48
49                settings.register_all();
50                settings.notify();
51
52                register_command(_T("check_nscp"), _T("Check the internal healt of NSClient++."));
53        } catch (nscapi::nscapi_exception &e) {
54                NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what()));
55                return false;
56        } catch (std::exception &e) {
57                NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what()));
58                return false;
59        } catch (...) {
60                NSC_LOG_ERROR_STD(_T("Failed to register command."));
61                return false;
62        }
63        return true;
64}
65bool CheckNSCP::unloadModule() {
66        return true;
67}
68bool CheckNSCP::hasCommandHandler() {
69        return true;
70}
71bool CheckNSCP::hasMessageHandler() {
72        return true;
73}
74std::string CheckNSCP::render(int msgType, const std::string file, int line, std::string message) {
75        return message;
76}
77void CheckNSCP::handleMessage(int msgType, const std::string file, int line, std::string message) {
78        if (msgType > NSCAPI::log_level::error)
79                return;
80        std::string err = render(msgType, file, line, message);
81        {
82                boost::unique_lock<boost::timed_mutex> lock(mutex_, boost::get_system_time() + boost::posix_time::seconds(5));
83                if (!lock.owns_lock())
84                        return;
85                errors_.push_back(err);
86        }
87}
88
89
90int CheckNSCP::get_crashes(std::wstring &last_crash) {
91#ifdef WIN32
92        NSC_DEBUG_MSG(_T("Crash folder is: ") + crashFolder);
93        if (!file_helpers::checks::is_directory(crashFolder)) {
94                return 0;
95        }
96        WIN32_FIND_DATA wfd;
97        FILETIME previous;
98        previous.dwHighDateTime = 0;
99        previous.dwLowDateTime = 0;
100        int count = 0;
101        std::wstring find = crashFolder + _T("\\*.txt");
102        HANDLE hFind = FindFirstFile(find.c_str(), &wfd);
103        std::wstring last_file;
104        if (hFind != INVALID_HANDLE_VALUE) {
105                do {
106                        if (CompareFileTime(&wfd.ftCreationTime, &previous) == 1) {
107                                previous = wfd.ftCreationTime;
108                                last_file = wfd.cFileName;
109                        }
110                        count++;
111                } while (FindNextFile(hFind, &wfd));
112                FindClose(hFind);
113        }
114        if (count > 0)
115                last_crash = last_file;
116        return count;
117#else
118        return 0;
119#endif
120}
121
122int CheckNSCP::get_errors(std::wstring &last_error) {
123        boost::unique_lock<boost::timed_mutex> lock(mutex_, boost::get_system_time() + boost::posix_time::seconds(5));
124        if (!lock.owns_lock())
125                return 1;
126        if (errors_.empty())
127                return 0;
128        last_error = utf8::cvt<std::wstring>(errors_.front());
129        return errors_.size();
130}
131
132NSCAPI::nagiosReturn CheckNSCP::check_nscp( std::list<std::wstring> arguments, std::wstring & msg, std::wstring & perf )
133{
134        std::wstring last_crash;
135        int crash_count = get_crashes(last_crash);
136        if (crash_count > 0){
137                std::wstring tmp = strEx::itos(crash_count) + _T(" crash(es), last crash: ") + last_crash;
138                strEx::append_list(msg, tmp, _T(", "));
139        }
140
141        std::wstring last_error;
142        int err_count = get_errors(last_error);
143        if (err_count > 0) {
144                std::wstring tmp = strEx::itos(err_count) + _T(" error(s), last error: ") + last_error;
145                strEx::append_list(msg, tmp, _T(", "));
146        }
147
148        if (msg.empty())
149                msg = _T("OK: 0 crash(es), 0 error(s)");
150        else
151                msg = _T("ERROR: ") + msg;
152
153        return (err_count > 0 || crash_count > 0) ? NSCAPI::returnCRIT:NSCAPI::returnOK;
154}
155
156NSCAPI::nagiosReturn CheckNSCP::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) {
157        if (command == _T("check_nscp")) {
158                return check_nscp(arguments, message, perf);
159        }
160        return NSCAPI::returnIgnored;
161}
162
163NSC_WRAP_DLL();
164NSC_WRAPPERS_MAIN_DEF(CheckNSCP);
165NSC_WRAPPERS_HANDLE_MSG_DEF();
166NSC_WRAPPERS_HANDLE_CMD_DEF();
Note: See TracBrowser for help on using the repository browser.