| 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 |
|
|---|
| 22 | #include "stdafx.h"
|
|---|
| 23 | #include "CheckTaskSched.h"
|
|---|
| 24 | #include <strEx.h>
|
|---|
| 25 | #include <time.h>
|
|---|
| 26 | #include <map>
|
|---|
| 27 | #include <vector>
|
|---|
| 28 |
|
|---|
| 29 | #include "filter.hpp"
|
|---|
| 30 |
|
|---|
| 31 | #include <settings/client/settings_client.hpp>
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | CheckTaskSched gCheckTaskSched;
|
|---|
| 35 |
|
|---|
| 36 | bool CheckTaskSched::loadModule() {
|
|---|
| 37 | return false;
|
|---|
| 38 | }
|
|---|
| 39 | bool CheckTaskSched::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) {
|
|---|
| 40 | try {
|
|---|
| 41 | get_core()->registerCommand(_T("CheckTaskSchedValue"), _T("Run a WMI query and check the resulting value (the values of each row determin the state)."));
|
|---|
| 42 | get_core()->registerCommand(_T("CheckTaskSched"), _T("Run a WMI query and check the resulting rows (the number of hits determine state)."));
|
|---|
| 43 |
|
|---|
| 44 | SETTINGS_REG_PATH(task_scheduler::SECTION);
|
|---|
| 45 | SETTINGS_REG_KEY_S(task_scheduler::SYNTAX);
|
|---|
| 46 | } catch (nscapi::nscapi_exception &e) {
|
|---|
| 47 | NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_);
|
|---|
| 48 | } catch (...) {
|
|---|
| 49 | NSC_LOG_ERROR_STD(_T("Failed to register command."));
|
|---|
| 50 | }
|
|---|
| 51 | syntax = SETTINGS_GET_STRING(task_scheduler::SYNTAX);
|
|---|
| 52 | return true;
|
|---|
| 53 | }
|
|---|
| 54 | bool CheckTaskSched::unloadModule() {
|
|---|
| 55 | return true;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | bool CheckTaskSched::hasCommandHandler() {
|
|---|
| 59 | return true;
|
|---|
| 60 | }
|
|---|
| 61 | bool CheckTaskSched::hasMessageHandler() {
|
|---|
| 62 | return false;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 | NSCAPI::nagiosReturn CheckTaskSched::TaskSchedule(std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf) {
|
|---|
| 69 | typedef checkHolders::CheckContainer<checkHolders::MaxMinBounds<checkHolders::NumericBounds<int, checkHolders::int_handler> > > WMIContainerQuery1;
|
|---|
| 70 | typedef checkHolders::CheckContainer<checkHolders::ExactBounds<checkHolders::NumericBounds<int, checkHolders::int_handler> > > WMIContainerQuery2;
|
|---|
| 71 |
|
|---|
| 72 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
|
|---|
| 73 | if (arguments.empty()) {
|
|---|
| 74 | message = _T("Missing argument(s).");
|
|---|
| 75 | return NSCAPI::returnCRIT;
|
|---|
| 76 | }
|
|---|
| 77 | unsigned int truncate = 0;
|
|---|
| 78 | std::wstring query, alias;
|
|---|
| 79 | bool bPerfData = true;
|
|---|
| 80 | std::wstring masterSyntax = _T("%list%");
|
|---|
| 81 | tasksched_filter::filter_argument args = tasksched_filter::factories::create_argument(_T("%task%"));
|
|---|
| 82 |
|
|---|
| 83 | WMIContainerQuery1 query1;
|
|---|
| 84 | WMIContainerQuery2 query2;
|
|---|
| 85 | try {
|
|---|
| 86 | MAP_OPTIONS_BEGIN(arguments)
|
|---|
| 87 | MAP_OPTIONS_STR2INT(_T("truncate"), truncate)
|
|---|
| 88 | MAP_OPTIONS_STR(_T("Alias"), alias)
|
|---|
| 89 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData)
|
|---|
| 90 | MAP_OPTIONS_BOOL_TRUE(_T("debug"), args->debug)
|
|---|
| 91 | MAP_OPTIONS_NUMERIC_ALL(query1, _T(""))
|
|---|
| 92 | MAP_OPTIONS_EXACT_NUMERIC_ALL(query2, _T(""))
|
|---|
| 93 | //MAP_OPTIONS_SHOWALL(result_query)
|
|---|
| 94 | MAP_OPTIONS_STR(_T("master-syntax"), masterSyntax)
|
|---|
| 95 | MAP_OPTIONS_STR(_T("filter"), args->filter)
|
|---|
| 96 | MAP_OPTIONS_STR(_T("syntax"), args->syntax)
|
|---|
| 97 | MAP_OPTIONS_MISSING(message,_T("Invalid argument: "))
|
|---|
| 98 | MAP_OPTIONS_END()
|
|---|
| 99 | } catch (filters::parse_exception e) {
|
|---|
| 100 | message = _T("TaskSched failed: ") + e.getMessage();
|
|---|
| 101 | return NSCAPI::returnCRIT;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | tasksched_filter::filter_engine impl = tasksched_filter::factories::create_engine(args);
|
|---|
| 105 | if (!impl) {
|
|---|
| 106 | message = _T("Failed to initialize filter subsystem.");
|
|---|
| 107 | return NSCAPI::returnUNKNOWN;
|
|---|
| 108 | }
|
|---|
| 109 | impl->boot();
|
|---|
| 110 | NSC_DEBUG_MSG_STD(_T("Using: ") + impl->get_name() + _T(" ") + impl->get_subject());
|
|---|
| 111 | if (!impl->validate(message)) {
|
|---|
| 112 | return NSCAPI::returnUNKNOWN;
|
|---|
| 113 | }
|
|---|
| 114 | tasksched_filter::filter_result result = tasksched_filter::factories::create_result(args);
|
|---|
| 115 |
|
|---|
| 116 | try {
|
|---|
| 117 | TaskSched query;
|
|---|
| 118 | query.findAll(result, args, impl);
|
|---|
| 119 | } catch (TaskSched::Exception e) {
|
|---|
| 120 | message = _T("WMIQuery failed: ") + e.getMessage();
|
|---|
| 121 | return NSCAPI::returnCRIT;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | int count = result->get_match_count();
|
|---|
| 125 | message = result->render(masterSyntax, returnCode);
|
|---|
| 126 | if (!bPerfData) {
|
|---|
| 127 | query1.perfData = false;
|
|---|
| 128 | query2.perfData = false;
|
|---|
| 129 | }
|
|---|
| 130 | if (query1.alias.empty())
|
|---|
| 131 | query1.alias = _T("eventlog");
|
|---|
| 132 | if (query2.alias.empty())
|
|---|
| 133 | query2.alias = _T("eventlog");
|
|---|
| 134 | if (query1.hasBounds())
|
|---|
| 135 | query1.runCheck(count, returnCode, message, perf);
|
|---|
| 136 | else if (query2.hasBounds())
|
|---|
| 137 | query2.runCheck(count, returnCode, message, perf);
|
|---|
| 138 | if ((truncate > 0) && (message.length() > (truncate-4)))
|
|---|
| 139 | message = message.substr(0, truncate-4) + _T("...");
|
|---|
| 140 | if (message.empty())
|
|---|
| 141 | message = _T("OK: All scheduled tasks are good.");
|
|---|
| 142 | return returnCode;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | NSCAPI::nagiosReturn CheckTaskSched::handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf) {
|
|---|
| 146 | if (command == _T("checktasksched"))
|
|---|
| 147 | return TaskSchedule(arguments, message, perf);
|
|---|
| 148 | return NSCAPI::returnIgnored;
|
|---|
| 149 | }
|
|---|
| 150 | int CheckTaskSched::commandLineExec(const TCHAR* command, const unsigned int argLen, TCHAR** char_args) {
|
|---|
| 151 | // std::wstring query = command;
|
|---|
| 152 | // query += _T(" ") + arrayBuffer::arrayBuffer2string(char_args, argLen, _T(" "));
|
|---|
| 153 | // TaskSched::result_type rows;
|
|---|
| 154 | // try {
|
|---|
| 155 | // task_sched::result::fetch_key key(true);
|
|---|
| 156 | // TaskSched wmiQuery;
|
|---|
| 157 | // rows = wmiQuery.findAll(key);
|
|---|
| 158 | // } catch (TaskSched::Exception e) {
|
|---|
| 159 | // NSC_LOG_ERROR_STD(_T("TaskSched failed: ") + e.getMessage());
|
|---|
| 160 | // return -1;
|
|---|
| 161 | // } catch (...) {
|
|---|
| 162 | // NSC_LOG_ERROR_STD(_T("TaskSched failed: UNKNOWN"));
|
|---|
| 163 | // return -1;
|
|---|
| 164 | // }
|
|---|
| 165 | // for (TaskSched::result_type::const_iterator cit = rows.begin(); cit != rows.end(); ++cit) {
|
|---|
| 166 | // std::wcout << (*cit).render(syntax) << std::endl;
|
|---|
| 167 | // }
|
|---|
| 168 | return 0;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 | NSC_WRAP_DLL();
|
|---|
| 173 | NSC_WRAPPERS_MAIN_DEF(gCheckTaskSched);
|
|---|
| 174 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 175 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckTaskSched);
|
|---|
| 176 | //NSC_WRAPPERS_CLI_DEF(gCheckTaskSched);
|
|---|