| 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_.start();
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | bool found = false;
|
|---|
| 58 | scheduler::target def = read_schedule(setting_keys::scheduler::DEFAULT_SCHEDULE_SECTION_PATH);
|
|---|
| 59 | std::list<std::wstring> items = NSCModuleHelper::getSettingsSection(setting_keys::scheduler::SCHEDULES_SECTION_PATH);
|
|---|
| 60 |
|
|---|
| 61 | for (std::list<std::wstring>::const_iterator cit = items.begin(); cit != items.end(); ++cit) {
|
|---|
| 62 | found = true;
|
|---|
| 63 | add_schedule(*cit, def);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | if (!found) {
|
|---|
| 67 | NSC_DEBUG_MSG_STD(_T("No scheduled commands found!"));
|
|---|
| 68 | SETTINGS_REG_KEY_S(scheduler::INTERVAL);
|
|---|
| 69 | SETTINGS_REG_KEY_S(scheduler::COMMAND);
|
|---|
| 70 | SETTINGS_REG_KEY_S(scheduler::CHANNEL);
|
|---|
| 71 | SETTINGS_REG_KEY_S(scheduler::REPORT_MODE);
|
|---|
| 72 |
|
|---|
| 73 | }
|
|---|
| 74 | /*
|
|---|
| 75 | add_schedule(_T("test: FIRST"));
|
|---|
| 76 | for (int i=0;i<1000;i++)
|
|---|
| 77 | add_schedule(_T("test: ") + to_wstring(i));
|
|---|
| 78 | add_schedule(_T("test: LAST"));
|
|---|
| 79 | */
|
|---|
| 80 | } catch (NSCModuleHelper::NSCMHExcpetion &e) {
|
|---|
| 81 | NSC_LOG_ERROR_STD(_T("Exception in module Scheduler: ") + e.msg_);
|
|---|
| 82 | return false;
|
|---|
| 83 | } catch (...) {
|
|---|
| 84 | NSC_LOG_ERROR_STD(_T("Unknown Exception in module Scheduler!"));
|
|---|
| 85 | return false;
|
|---|
| 86 | }
|
|---|
| 87 | return true;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | scheduler::target Scheduler::read_schedule(std::wstring path) {
|
|---|
| 91 | scheduler::target item;
|
|---|
| 92 | item.channel = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::CHANNEL, setting_keys::scheduler::CHANNEL_DEFAULT);
|
|---|
| 93 | item.command = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::COMMAND, setting_keys::scheduler::COMMAND_PATH);
|
|---|
| 94 | /*
|
|---|
| 95 | std::wstring report = SETTINGS_GET_STRING(scheduler::REPORT_MODE);
|
|---|
| 96 | item.report = parse_report_string(report);
|
|---|
| 97 | */
|
|---|
| 98 | std::wstring duration = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::INTERVAL, setting_keys::scheduler::INTERVAL_DEFAULT);
|
|---|
| 99 | item.duration = boost::posix_time::seconds(strEx::stoui_as_time(duration));
|
|---|
| 100 | return item;
|
|---|
| 101 | }
|
|---|
| 102 | scheduler::target Scheduler::read_schedule(std::wstring path, scheduler::target def) {
|
|---|
| 103 | scheduler::target item;
|
|---|
| 104 |
|
|---|
| 105 | item.channel = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::CHANNEL, def.channel);
|
|---|
| 106 | item.command = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::COMMAND, def.command);
|
|---|
| 107 | /*
|
|---|
| 108 | std::wstring report = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::REPORT_MODE, def.report);
|
|---|
| 109 | item.report = parse_report_string(report);
|
|---|
| 110 | */
|
|---|
| 111 | std::wstring duration = NSCModuleHelper::getSettingsString(path, setting_keys::scheduler::INTERVAL, to_wstring(def.duration.total_seconds()) + _T("s"));
|
|---|
| 112 | item.duration = boost::posix_time::seconds(strEx::stoui_as_time(duration));
|
|---|
| 113 | return item;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | void Scheduler::add_schedule(std::wstring command, scheduler::target def) {
|
|---|
| 117 | NSC_DEBUG_MSG_STD(_T("Adding scheduled command: ") + command);
|
|---|
| 118 | scheduler::target item = read_schedule(setting_keys::scheduler::SCHEDULES_SECTION_PATH + _T("/") + command, def);
|
|---|
| 119 |
|
|---|
| 120 | // std::wstring report = SETTINGS_GET_STRING(scheduler::REPORT_MODE);
|
|---|
| 121 | // report_ = parse_report_string(report);
|
|---|
| 122 |
|
|---|
| 123 | item.command = command;
|
|---|
| 124 | item.set_duration(boost::posix_time::seconds(5));
|
|---|
| 125 | scheduler_.add_task(item);
|
|---|
| 126 | /*
|
|---|
| 127 | std::wcout << _T("*** DURATION ") << item.duration << _T(" ***") << std::endl;
|
|---|
| 128 | */
|
|---|
| 129 | }
|
|---|
| 130 | bool Scheduler::unloadModule() {
|
|---|
| 131 | scheduler_.stop();
|
|---|
| 132 | return true;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | NSC_WRAP_DLL();
|
|---|
| 136 | NSC_WRAPPERS_MAIN_DEF(gInstance);
|
|---|
| 137 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 138 | NSC_WRAPPERS_IGNORE_CMD_DEF();
|
|---|