| 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 <settings/client/settings_client.hpp>
|
|---|
| 27 | namespace sh = nscapi::settings_helper;
|
|---|
| 28 |
|
|---|
| 29 | CheckNSCP gCheckNSCP;
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | bool CheckNSCP::loadModule() {
|
|---|
| 34 | return false;
|
|---|
| 35 | }
|
|---|
| 36 | bool CheckNSCP::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) {
|
|---|
| 37 | try {
|
|---|
| 38 |
|
|---|
| 39 | sh::settings_registry settings(get_settings_proxy());
|
|---|
| 40 | settings.set_alias(_T("crash"), alias);
|
|---|
| 41 |
|
|---|
| 42 | settings.alias().add_path_to_settings()
|
|---|
| 43 | (_T("CRASH SECTION"), _T("Configure crash handling properties."))
|
|---|
| 44 | ;
|
|---|
| 45 |
|
|---|
| 46 | settings.alias().add_key_to_settings()
|
|---|
| 47 | (_T("archive folder"), sh::wstring_key(&crashFolder, CRASH_ARCHIVE_FOLDER),
|
|---|
| 48 | CRASH_ARCHIVE_FOLDER_KEY, _T("The archive folder for crash dunpes."))
|
|---|
| 49 | ;
|
|---|
| 50 |
|
|---|
| 51 | settings.register_all();
|
|---|
| 52 | settings.notify();
|
|---|
| 53 |
|
|---|
| 54 | get_core()->registerCommand(_T("check_nscp"), _T("Check the internal healt of NSClient++."));
|
|---|
| 55 | } catch (nscapi::nscapi_exception &e) {
|
|---|
| 56 | NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_);
|
|---|
| 57 | } catch (...) {
|
|---|
| 58 | NSC_LOG_ERROR_STD(_T("Failed to register command."));
|
|---|
| 59 | }
|
|---|
| 60 | return true;
|
|---|
| 61 | }
|
|---|
| 62 | bool CheckNSCP::unloadModule() {
|
|---|
| 63 | return true;
|
|---|
| 64 | }
|
|---|
| 65 | bool CheckNSCP::hasCommandHandler() {
|
|---|
| 66 | return true;
|
|---|
| 67 | }
|
|---|
| 68 | bool CheckNSCP::hasMessageHandler() {
|
|---|
| 69 | return true;
|
|---|
| 70 | }
|
|---|
| 71 | std::string CheckNSCP::render(int msgType, const std::string file, int line, std::string message) {
|
|---|
| 72 | return message;
|
|---|
| 73 | }
|
|---|
| 74 | void CheckNSCP::handleMessage(int msgType, const std::string file, int line, std::string message) {
|
|---|
| 75 | if (msgType != NSCAPI::error||msgType != NSCAPI::critical)
|
|---|
| 76 | return;
|
|---|
| 77 | std::string err = render(msgType, file, line, message);
|
|---|
| 78 | {
|
|---|
| 79 | boost::unique_lock<boost::timed_mutex> lock(mutex_, boost::get_system_time() + boost::posix_time::seconds(5));
|
|---|
| 80 | if (!lock.owns_lock())
|
|---|
| 81 | return;
|
|---|
| 82 | errors_.push_back(err);
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | int CheckNSCP::get_crashes(std::wstring &last_crash) {
|
|---|
| 88 | #ifdef WIN32
|
|---|
| 89 | if (!file_helpers::checks::is_directory(crashFolder)) {
|
|---|
| 90 | return 0;
|
|---|
| 91 | }
|
|---|
| 92 | WIN32_FIND_DATA wfd;
|
|---|
| 93 | FILETIME previous;
|
|---|
| 94 | previous.dwHighDateTime = 0;
|
|---|
| 95 | previous.dwLowDateTime = 0;
|
|---|
| 96 | int count = 0;
|
|---|
| 97 | std::wstring find = crashFolder + _T("\\*.txt");
|
|---|
| 98 | HANDLE hFind = FindFirstFile(find.c_str(), &wfd);
|
|---|
| 99 | std::wstring last_file;
|
|---|
| 100 | if (hFind != INVALID_HANDLE_VALUE) {
|
|---|
| 101 | do {
|
|---|
| 102 | if (CompareFileTime(&wfd.ftCreationTime, &previous) == 1) {
|
|---|
| 103 | previous = wfd.ftCreationTime;
|
|---|
| 104 | last_file = wfd.cFileName;
|
|---|
| 105 | }
|
|---|
| 106 | count++;
|
|---|
| 107 | } while (FindNextFile(hFind, &wfd));
|
|---|
| 108 | FindClose(hFind);
|
|---|
| 109 | }
|
|---|
| 110 | if (count > 0)
|
|---|
| 111 | last_crash = last_file;
|
|---|
| 112 | return count;
|
|---|
| 113 | #else
|
|---|
| 114 | return 0;
|
|---|
| 115 | #endif
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | int CheckNSCP::get_errors(std::wstring &last_error) {
|
|---|
| 119 | boost::unique_lock<boost::timed_mutex> lock(mutex_, boost::get_system_time() + boost::posix_time::seconds(5));
|
|---|
| 120 | if (!lock.owns_lock())
|
|---|
| 121 | return 1;
|
|---|
| 122 | if (errors_.empty())
|
|---|
| 123 | return 0;
|
|---|
| 124 | last_error = utf8::cvt<std::wstring>(errors_.front());
|
|---|
| 125 | return errors_.size();
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | NSCAPI::nagiosReturn CheckNSCP::check_nscp( std::list<std::wstring> arguments, std::wstring & msg, std::wstring & perf )
|
|---|
| 129 | {
|
|---|
| 130 | std::wstring last_crash;
|
|---|
| 131 | int crash_count = get_crashes(last_crash);
|
|---|
| 132 | if (crash_count > 0){
|
|---|
| 133 | std::wstring tmp = strEx::itos(crash_count) + _T(" crash(es), last crash: ") + last_crash;
|
|---|
| 134 | strEx::append_list(msg, tmp, _T(", "));
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | std::wstring last_error;
|
|---|
| 138 | int err_count = get_errors(last_error);
|
|---|
| 139 | if (err_count > 0) {
|
|---|
| 140 | std::wstring tmp = strEx::itos(err_count) + _T(" error(s), last error: ") + last_error;
|
|---|
| 141 | strEx::append_list(msg, tmp, _T(", "));
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | if (msg.empty())
|
|---|
| 145 | msg = _T("OK: 0 crash(es), 0 error(s)");
|
|---|
| 146 | else
|
|---|
| 147 | msg = _T("ERROR: ") + msg;
|
|---|
| 148 |
|
|---|
| 149 | return (err_count > 0 || crash_count > 0) ? NSCAPI::returnCRIT:NSCAPI::returnOK;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | NSCAPI::nagiosReturn CheckNSCP::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) {
|
|---|
| 153 | if (command == _T("check_nscp")) {
|
|---|
| 154 | return check_nscp(arguments, message, perf);
|
|---|
| 155 | }
|
|---|
| 156 | return NSCAPI::returnIgnored;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | NSC_WRAP_DLL();
|
|---|
| 160 | NSC_WRAPPERS_MAIN_DEF(gCheckNSCP);
|
|---|
| 161 | NSC_WRAPPERS_HANDLE_MSG_DEF(gCheckNSCP);
|
|---|
| 162 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckNSCP);
|
|---|