| 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 "Scheduler.h"
|
|---|
| 23 | #include <strEx.h>
|
|---|
| 24 | #include <time.h>
|
|---|
| 25 | #include <utils.h>
|
|---|
| 26 | #include <settings/macros.h>
|
|---|
| 27 |
|
|---|
| 28 | Scheduler gInstance;
|
|---|
| 29 |
|
|---|
| 30 | bool Scheduler::loadModule(NSCAPI::moduleLoadMode mode) {
|
|---|
| 31 | try {
|
|---|
| 32 | SETTINGS_REG_PATH(scheduler::SECTION);
|
|---|
| 33 | SETTINGS_REG_PATH(scheduler::DEFAULT_SCHEDULE_SECTION);
|
|---|
| 34 | SETTINGS_REG_PATH(scheduler::SCHEDULES_SECTION);
|
|---|
| 35 |
|
|---|
| 36 | SETTINGS_REG_KEY_S(scheduler::INTERVAL_D);
|
|---|
| 37 | SETTINGS_REG_KEY_S(scheduler::COMMAND_D);
|
|---|
| 38 | SETTINGS_REG_KEY_S(scheduler::CHANNEL_D);
|
|---|
| 39 | SETTINGS_REG_KEY_S(scheduler::REPORT_MODE_D);
|
|---|
| 40 |
|
|---|
| 41 | SETTINGS_REG_KEY_I(scheduler::THREADS);
|
|---|
| 42 | } catch (NSCModuleHelper::NSCMHExcpetion &e) {
|
|---|
| 43 | NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_);
|
|---|
| 44 | } catch (...) {
|
|---|
| 45 | NSC_LOG_ERROR_STD(_T("Failed to register command."));
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | try {
|
|---|
| 50 |
|
|---|
| 51 | scheduler_.set_threads(SETTINGS_GET_INT(scheduler::THREADS));
|
|---|
| 52 |
|
|---|
| 53 | if (mode == NSCAPI::normalStart) {
|
|---|
| 54 | scheduler_.set_handler(this);
|
|---|
| 55 | scheduler_.start();
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | bool found = false;
|
|---|
| 59 | scheduler::target def = read_defaut_schedule(setting_keys::scheduler::DEFAULT_SCHEDULE_SECTION_PATH);
|
|---|
| 60 | std::list<std::wstring> items = NSCModuleHelper::getSettingsSection(setting_keys::scheduler::SCHEDULES_SECTION_PATH);
|
|---|
| 61 |
|
|---|
| 62 | for (std::list<std::wstring>::const_iterator cit = items.begin(); cit != items.end(); ++cit) {
|
|---|
| 63 | found = true;
|
|---|
| 64 | add_schedule(*cit, NSCModuleHelper::getSettingsString(setting_keys::scheduler::SCHEDULES_SECTION_PATH, *cit, _T("")), def);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | if (!found) {
|
|---|
| 68 | NSC_DEBUG_MSG_STD(_T("No scheduled commands found!"));
|
|---|
| 69 | SETTINGS_REG_KEY_S(scheduler::INTERVAL);
|
|---|
| 70 | SETTINGS_REG_KEY_S(scheduler::COMMAND);
|
|---|
| 71 | SETTINGS_REG_KEY_S(scheduler::CHANNEL);
|
|---|
| 72 | SETTINGS_REG_KEY_S(scheduler::REPORT_MODE);
|
|---|
| 73 |
|
|---|
| 74 | }
|
|---|
| 75 | /*
|
|---|
| 76 | add_schedule(_T("test: FIRST"));
|
|---|
| 77 | for (int i=0;i<1000;i++)
|
|---|
| 78 | add_schedule(_T("test: ") + to_wstring(i));
|
|---|
| 79 | add_schedule(_T("test: LAST"));
|
|---|
| 80 | */
|
|---|
| 81 | } catch (NSCModuleHelper::NSCMHExcpetion &e) {
|
|---|
| 82 | NSC_LOG_ERROR_STD(_T("Exception in module Scheduler: ") + e.msg_);
|
|---|
| 83 | return false;
|
|---|
| 84 | } catch (...) {
|
|---|
| 85 | NSC_LOG_ERROR_STD(_T("Unknown Exception in module Scheduler!"));
|
|---|
| 86 | return false;
|
|---|
| 87 | }
|
|---|
| 88 | return true;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | scheduler::target Scheduler::read_defaut_schedule(std::wstring path) {
|
|---|
| 92 | scheduler::target item;
|
|---|
| 93 | item.channel = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::CHANNEL, setting_keys::scheduler::CHANNEL_DEFAULT);
|
|---|
| 94 | item.command = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::COMMAND, setting_keys::scheduler::COMMAND_PATH);
|
|---|
| 95 | std::wstring report = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::REPORT_MODE, setting_keys::scheduler::REPORT_MODE_PATH);
|
|---|
| 96 | item.report = NSCHelper::report::parse(report);
|
|---|
| 97 | std::wstring duration = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::INTERVAL, setting_keys::scheduler::INTERVAL_DEFAULT);
|
|---|
| 98 | item.duration = boost::posix_time::seconds(strEx::stoui_as_time_sec(duration, 1));
|
|---|
| 99 | return item;
|
|---|
| 100 | }
|
|---|
| 101 | void Scheduler::add_schedule(std::wstring alias, std::wstring command, scheduler::target def) {
|
|---|
| 102 | scheduler::target item;
|
|---|
| 103 | std::wstring detail_path = setting_keys::scheduler::SCHEDULES_SECTION_PATH + _T("/") + alias;
|
|---|
| 104 | item.alias = alias;
|
|---|
| 105 | item.command = command;
|
|---|
| 106 | item.channel = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::CHANNEL, def.channel);
|
|---|
| 107 | item.command = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::COMMAND, item.command);
|
|---|
| 108 | std::wstring report = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::REPORT_MODE, NSCHelper::report::to_string(def.report));
|
|---|
| 109 | item.report = NSCHelper::report::parse(report);
|
|---|
| 110 | std::wstring duration = NSCModuleHelper::getSettingsString(detail_path, setting_keys::scheduler::INTERVAL, to_wstring(def.duration.total_seconds()) + _T("s"));
|
|---|
| 111 | item.duration = boost::posix_time::seconds(strEx::stoui_as_time_sec(duration, 1));
|
|---|
| 112 | //std::wcout << _T("Added: ") << item.to_string() << std::endl;
|
|---|
| 113 | scheduler_.add_task(item);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | bool Scheduler::unloadModule() {
|
|---|
| 117 | scheduler_.unset_handler();
|
|---|
| 118 | scheduler_.stop();
|
|---|
| 119 | return true;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | void Scheduler::handle_schedule(scheduler::target item) {
|
|---|
| 123 | try {
|
|---|
| 124 | std::wstring msg, perf;
|
|---|
| 125 | NSCAPI::nagiosReturn code = NSCModuleHelper::InjectCommand(item.command.c_str(), item.arguments, msg, perf);
|
|---|
| 126 | if (NSCHelper::report::matches(item.report, code)) {
|
|---|
| 127 | NSCModuleHelper::NotifyChannel(item.channel, item.alias, code, msg, perf);
|
|---|
| 128 | }
|
|---|
| 129 | } catch (NSCModuleHelper::NSCMHExcpetion &e) {
|
|---|
| 130 | NSC_LOG_ERROR_STD(_T("Exception handling: ") + item.alias + _T(": ") + e.msg_);
|
|---|
| 131 | scheduler_.remove_task(item.id);
|
|---|
| 132 | } catch (...) {
|
|---|
| 133 | NSC_LOG_ERROR_STD(_T("Unknown Exception handling: ") + item.alias);
|
|---|
| 134 | scheduler_.remove_task(item.id);
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 | NSC_WRAP_DLL();
|
|---|
| 142 | NSC_WRAPPERS_MAIN_DEF(gInstance);
|
|---|
| 143 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 144 | NSC_WRAPPERS_IGNORE_CMD_DEF();
|
|---|