source: nscp/modules/RemoteConfiguration/RemoteConfiguration.cpp @ 99e4d8f

0.4.00.4.10.4.2stable
Last change on this file since 99e4d8f was 99e4d8f, checked in by Michael Medin <michael@…>, 6 years ago

2007-11-23 MickeM

  • Converted to unicode (damn sometimes I HATE C++) + Added support for escaping " on the /test syntax so now you can do: CheckWMI MaxCrit=3 "MinWarn=1" "Query:load=Select * from win32_Processor"

2007-11-22 MickeM

  • Fixed so the "default path" is correct even when running as a service (issue: #96)
  • Property mode set to 100644
File size: 7.5 KB
Line 
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 "RemoteConfiguration.h"
23#include <strEx.h>
24#include <time.h>
25
26
27RemoteConfiguration gRemoteConfiguration;
28
29BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
30{
31        NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call);
32        return TRUE;
33}
34
35RemoteConfiguration::RemoteConfiguration() {
36}
37RemoteConfiguration::~RemoteConfiguration() {
38}
39
40
41bool RemoteConfiguration::loadModule() {
42        return true;
43}
44bool RemoteConfiguration::unloadModule() {
45        return true;
46}
47
48bool RemoteConfiguration::hasCommandHandler() {
49        return true;
50}
51bool RemoteConfiguration::hasMessageHandler() {
52        return false;
53}
54
55// set writeConf type
56NSCAPI::nagiosReturn RemoteConfiguration::writeConf(const unsigned int argLen, TCHAR **char_args, std::wstring &message) {
57        std::list<std::wstring> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
58
59        if (args.size() > 0) {
60                if (args.front() == _T("reg")) {
61                        if (NSCModuleHelper::WriteSettings(NSCAPI::settings_registry) == NSCAPI::isSuccess) {
62                                message = _T("Settings written successfully.");
63                                return NSCAPI::returnOK;
64                        }
65                        message = _T("ERROR could not write settings.");
66                        return NSCAPI::returnCRIT;
67                }
68        }
69        if (NSCModuleHelper::WriteSettings(NSCAPI::settings_inifile) == NSCAPI::isSuccess) {
70                message = _T("Settings written successfully.");
71                return NSCAPI::returnOK;
72        }
73        message = _T("ERROR could not write settings.");
74        return NSCAPI::returnCRIT;
75}
76
77NSCAPI::nagiosReturn RemoteConfiguration::readConf(const unsigned int argLen, TCHAR **char_args, std::wstring &message) {
78        std::list<std::wstring> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
79
80        if (args.size() > 0) {
81                if (args.front() == _T("reg")) {
82                        if (NSCModuleHelper::ReadSettings(NSCAPI::settings_registry) == NSCAPI::isSuccess) {
83                                message = _T("Settings written successfully.");
84                                return NSCAPI::returnOK;
85                        }
86                        message = _T("ERROR could not write settings.");
87                        return NSCAPI::returnCRIT;
88                }
89        }
90        if (NSCModuleHelper::ReadSettings(NSCAPI::settings_inifile) == NSCAPI::isSuccess) {
91                message = _T("Settings written successfully.");
92                return NSCAPI::returnOK;
93        }
94        message = _T("ERROR could not write settings.");
95        return NSCAPI::returnCRIT;
96}
97// set setVariable int <section> <variable> <value>
98NSCAPI::nagiosReturn RemoteConfiguration::setVariable(const unsigned int argLen, TCHAR **char_args, std::wstring &message) {
99        std::list<std::wstring> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
100        if (args.size() < 3) {
101                message = _T("Invalid syntax.");
102                return NSCAPI::returnUNKNOWN;
103        }
104        std::wstring type = args.front(); args.pop_front();
105        std::wstring section = args.front(); args.pop_front();
106        std::wstring key = args.front(); args.pop_front();
107        std::wstring value;
108        if (args.size() >= 1) {
109                value = args.front();
110        }
111        if (type == _T("int")) {
112                NSCModuleHelper::SetSettingsInt(section, key, strEx::stoi(value));
113                message = _T("Settings ") + key + _T(" saved successfully.");
114                return NSCAPI::returnOK;
115        } else if (type == _T("string")) {
116                NSCModuleHelper::SetSettingsString(section, key, value);
117                message = _T("Settings ") + key + _T(" saved successfully.");
118                return NSCAPI::returnOK;
119        } else {
120                NSCModuleHelper::SetSettingsString(type, section, key);
121                message = _T("Settings ") + section + _T(" saved successfully.");
122                return NSCAPI::returnOK;
123        }
124}
125NSCAPI::nagiosReturn RemoteConfiguration::getVariable(const unsigned int argLen, TCHAR **char_args, std::wstring &message) {
126        std::list<std::wstring> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
127        if (args.size() < 2) {
128                message = _T("Invalid syntax.");
129                return NSCAPI::returnUNKNOWN;
130        }
131        std::wstring section = args.front(); args.pop_front();
132        std::wstring key = args.front(); args.pop_front();
133        std::wstring value;
134        value = NSCModuleHelper::getSettingsString(section, key, _T(""));
135        message = section+_T("/")+key+_T("=")+value;
136        return NSCAPI::returnOK;
137}
138int RemoteConfiguration::commandLineExec(const TCHAR* command,const unsigned int argLen,TCHAR** args) {
139        std::wstring str;
140        if (_wcsicmp(command, _T("setVariable")) == 0) {
141                setVariable(argLen, args, str);
142        } else if (_wcsicmp(command, _T("writeConf")) == 0) {
143                writeConf(argLen, args, str);
144        } else if (_wcsicmp(command, _T("getVariable")) == 0) {
145                setVariable(argLen, args, str);
146        } else if (_wcsicmp(command, _T("ini2reg")) == 0) {
147                std::cout << _T("Migrating to registry settings...")<< std::endl;
148                NSCModuleHelper::ReadSettings(NSCAPI::settings_inifile);
149                NSCModuleHelper::SetSettingsInt(MAIN_SECTION_TITLE, MAIN_USEFILE, 0);
150                NSCModuleHelper::WriteSettings(NSCAPI::settings_inifile);
151                NSCModuleHelper::SetSettingsInt(MAIN_SECTION_TITLE, MAIN_USEREG, 1);
152                NSCModuleHelper::WriteSettings(NSCAPI::settings_registry);
153        } else if (_wcsicmp(command, _T("reg2ini")) == 0) {
154                std::cout << _T("Migrating to INI file settings...")<< std::endl;
155                NSCModuleHelper::ReadSettings(NSCAPI::settings_registry);
156                NSCModuleHelper::SetSettingsInt(MAIN_SECTION_TITLE, MAIN_USEREG, 0);
157                NSCModuleHelper::WriteSettings(NSCAPI::settings_registry);
158                NSCModuleHelper::SetSettingsInt(MAIN_SECTION_TITLE, MAIN_USEFILE, 1);
159                NSCModuleHelper::WriteSettings(NSCAPI::settings_inifile);
160        }
161        return 0;
162}
163
164
165NSCAPI::nagiosReturn RemoteConfiguration::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) {
166        if (command == _T("setVariable")) {
167                setVariable(argLen, char_args, msg);
168                return NSCAPI::returnOK;
169        } else if (command == _T("getVariable")) {
170                getVariable(argLen, char_args, msg);
171                return NSCAPI::returnOK;
172        } else if (command == _T("readConf")) {
173                return readConf(argLen, char_args, msg);
174        } else if (command == _T("writeConf")) {
175                return writeConf(argLen, char_args, msg);
176        }       
177        return NSCAPI::returnIgnored;
178}
179
180
181NSC_WRAPPERS_MAIN_DEF(gRemoteConfiguration);
182NSC_WRAPPERS_IGNORE_MSG_DEF();
183NSC_WRAPPERS_HANDLE_CMD_DEF(gRemoteConfiguration);
184NSC_WRAPPERS_CLI_DEF(gRemoteConfiguration);
185
Note: See TracBrowser for help on using the repository browser.