| 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 | NSC_WRAPPERS_MAIN(); |
|---|
| 22 | NSC_WRAPPERS_CLI(); |
|---|
| 23 | |
|---|
| 24 | #include <settings/macros.h> |
|---|
| 25 | #include <strEx.h> |
|---|
| 26 | #include <utils.h> |
|---|
| 27 | #include <checkHelpers.hpp> |
|---|
| 28 | |
|---|
| 29 | #include "eventlog_wrapper.hpp" |
|---|
| 30 | #include "eventlog_record.hpp" |
|---|
| 31 | |
|---|
| 32 | #include "filters.hpp" |
|---|
| 33 | |
|---|
| 34 | struct real_time_thread { |
|---|
| 35 | bool enabled_; |
|---|
| 36 | //std::wstring destination_; |
|---|
| 37 | unsigned long long start_age_; |
|---|
| 38 | unsigned long long max_age_; |
|---|
| 39 | //std::wstring syntax_; |
|---|
| 40 | //std::list<filter_container> filters_; |
|---|
| 41 | boost::shared_ptr<boost::thread> thread_; |
|---|
| 42 | HANDLE stop_event_; |
|---|
| 43 | std::list<std::wstring> lists_; |
|---|
| 44 | std::list<std::wstring> hit_cache_; |
|---|
| 45 | boost::timed_mutex cache_mutex_; |
|---|
| 46 | filters::filter_config_handler filters_; |
|---|
| 47 | |
|---|
| 48 | bool cache_; |
|---|
| 49 | bool debug_; |
|---|
| 50 | std::wstring filters_path_; |
|---|
| 51 | |
|---|
| 52 | real_time_thread() : enabled_(false), start_age_(0), max_age_(0), debug_(false), cache_(false) { |
|---|
| 53 | set_start_age(_T("30m")); |
|---|
| 54 | set_max_age(_T("5m")); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | void add_realtime_filter(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring key, std::wstring query); |
|---|
| 58 | void set_enabled(bool flag) { enabled_ = flag; } |
|---|
| 59 | void set_start_age(std::wstring age) { |
|---|
| 60 | start_age_ = strEx::stoi64_as_time(age); |
|---|
| 61 | } |
|---|
| 62 | void set_max_age(std::wstring age) { |
|---|
| 63 | if (age == _T("none") || age == _T("infinite") || age == _T("false")) |
|---|
| 64 | max_age_ = 0; |
|---|
| 65 | else |
|---|
| 66 | max_age_ = strEx::stoi64_as_time(age); |
|---|
| 67 | } |
|---|
| 68 | void set_eventlog(std::wstring log) { |
|---|
| 69 | lists_ = strEx::splitEx(log, _T(",")); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void set_language(std::string lang); |
|---|
| 73 | void set_filter(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring flt) { |
|---|
| 74 | if (!flt.empty()) |
|---|
| 75 | add_realtime_filter(proxy, _T("default"), flt); |
|---|
| 76 | } |
|---|
| 77 | bool has_filters() { |
|---|
| 78 | return !filters_.has_objects(); |
|---|
| 79 | } |
|---|
| 80 | bool start(); |
|---|
| 81 | bool stop(); |
|---|
| 82 | |
|---|
| 83 | bool check_cache(unsigned long &count, std::wstring &messages); |
|---|
| 84 | |
|---|
| 85 | void thread_proc(); |
|---|
| 86 | // void process_events(eventlog_filter::filter_engine engine, eventlog_wrapper &eventlog); |
|---|
| 87 | void process_no_events(const filters::filter_config_object &object); |
|---|
| 88 | void process_record(const filters::filter_config_object &object, const EventLogRecord &record); |
|---|
| 89 | void debug_miss(const EventLogRecord &record); |
|---|
| 90 | // void process_event(eventlog_filter::filter_engine engine, const EVENTLOGRECORD* record); |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | class CheckEventLog : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { |
|---|
| 94 | private: |
|---|
| 95 | bool debug_; |
|---|
| 96 | std::wstring syntax_; |
|---|
| 97 | int buffer_length_; |
|---|
| 98 | bool lookup_names_; |
|---|
| 99 | real_time_thread thread_; |
|---|
| 100 | |
|---|
| 101 | public: |
|---|
| 102 | CheckEventLog(); |
|---|
| 103 | virtual ~CheckEventLog(); |
|---|
| 104 | // Module calls |
|---|
| 105 | bool loadModule(); |
|---|
| 106 | bool loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode); |
|---|
| 107 | bool unloadModule(); |
|---|
| 108 | |
|---|
| 109 | static std::wstring getModuleName() { |
|---|
| 110 | return _T("Event log Checker."); |
|---|
| 111 | } |
|---|
| 112 | static nscapi::plugin_wrapper::module_version getModuleVersion() { |
|---|
| 113 | nscapi::plugin_wrapper::module_version version = {0, 0, 1 }; |
|---|
| 114 | return version; |
|---|
| 115 | } |
|---|
| 116 | static std::wstring getModuleDescription() { |
|---|
| 117 | return _T("Check for errors and warnings in the event log.\nThis is only supported through NRPE so if you plan to use only NSClient this wont help you at all."); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void parse(std::wstring expr); |
|---|
| 121 | |
|---|
| 122 | bool hasCommandHandler(); |
|---|
| 123 | bool hasMessageHandler(); |
|---|
| 124 | NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); |
|---|
| 125 | NSCAPI::nagiosReturn checkCache(std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); |
|---|
| 126 | NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); |
|---|
| 127 | NSCAPI::nagiosReturn insert_eventlog(std::vector<std::wstring> arguments, std::wstring &message); |
|---|
| 128 | |
|---|
| 129 | }; |
|---|