| 1 | // CheckEventLog.cpp : Defines the entry point for the DLL application.
|
|---|
| 2 | //
|
|---|
| 3 |
|
|---|
| 4 | #include "stdafx.h"
|
|---|
| 5 | #include "RemoteConfiguration.h"
|
|---|
| 6 | #include <strEx.h>
|
|---|
| 7 | #include <time.h>
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | RemoteConfiguration gRemoteConfiguration;
|
|---|
| 11 |
|
|---|
| 12 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
|---|
| 13 | {
|
|---|
| 14 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call);
|
|---|
| 15 | return TRUE;
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | RemoteConfiguration::RemoteConfiguration() {
|
|---|
| 19 | }
|
|---|
| 20 | RemoteConfiguration::~RemoteConfiguration() {
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | bool RemoteConfiguration::loadModule() {
|
|---|
| 25 | return true;
|
|---|
| 26 | }
|
|---|
| 27 | bool RemoteConfiguration::unloadModule() {
|
|---|
| 28 | return true;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | bool RemoteConfiguration::hasCommandHandler() {
|
|---|
| 32 | return true;
|
|---|
| 33 | }
|
|---|
| 34 | bool RemoteConfiguration::hasMessageHandler() {
|
|---|
| 35 | return false;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | // set writeConf type
|
|---|
| 39 | NSCAPI::nagiosReturn RemoteConfiguration::writeConf(const unsigned int argLen, char **char_args, std::string &message) {
|
|---|
| 40 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
|
|---|
| 41 |
|
|---|
| 42 | if (args.size() > 0) {
|
|---|
| 43 | if (args.front() == "reg") {
|
|---|
| 44 | if (NSCModuleHelper::WriteSettings(NSCAPI::settings_registry) == NSCAPI::isSuccess) {
|
|---|
| 45 | message = "Settings written successfully.";
|
|---|
| 46 | return NSCAPI::returnOK;
|
|---|
| 47 | }
|
|---|
| 48 | message = "ERROR could not write settings.";
|
|---|
| 49 | return NSCAPI::returnCRIT;
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 | if (NSCModuleHelper::WriteSettings(NSCAPI::settings_inifile) == NSCAPI::isSuccess) {
|
|---|
| 53 | message = "Settings written successfully.";
|
|---|
| 54 | return NSCAPI::returnOK;
|
|---|
| 55 | }
|
|---|
| 56 | message = "ERROR could not write settings.";
|
|---|
| 57 | return NSCAPI::returnCRIT;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | NSCAPI::nagiosReturn RemoteConfiguration::readConf(const unsigned int argLen, char **char_args, std::string &message) {
|
|---|
| 61 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
|
|---|
| 62 |
|
|---|
| 63 | if (args.size() > 0) {
|
|---|
| 64 | if (args.front() == "reg") {
|
|---|
| 65 | if (NSCModuleHelper::ReadSettings(NSCAPI::settings_registry) == NSCAPI::isSuccess) {
|
|---|
| 66 | message = "Settings written successfully.";
|
|---|
| 67 | return NSCAPI::returnOK;
|
|---|
| 68 | }
|
|---|
| 69 | message = "ERROR could not write settings.";
|
|---|
| 70 | return NSCAPI::returnCRIT;
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | if (NSCModuleHelper::ReadSettings(NSCAPI::settings_inifile) == NSCAPI::isSuccess) {
|
|---|
| 74 | message = "Settings written successfully.";
|
|---|
| 75 | return NSCAPI::returnOK;
|
|---|
| 76 | }
|
|---|
| 77 | message = "ERROR could not write settings.";
|
|---|
| 78 | return NSCAPI::returnCRIT;
|
|---|
| 79 | }
|
|---|
| 80 | // set setVariable int <section> <variable> <value>
|
|---|
| 81 | NSCAPI::nagiosReturn RemoteConfiguration::setVariable(const unsigned int argLen, char **char_args, std::string &message) {
|
|---|
| 82 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
|
|---|
| 83 | if (args.size() < 3) {
|
|---|
| 84 | message = "Invalid syntax.";
|
|---|
| 85 | return NSCAPI::returnUNKNOWN;
|
|---|
| 86 | }
|
|---|
| 87 | std::string type = args.front(); args.pop_front();
|
|---|
| 88 | std::string section = args.front(); args.pop_front();
|
|---|
| 89 | std::string key = args.front(); args.pop_front();
|
|---|
| 90 | std::string value;
|
|---|
| 91 | if (args.size() >= 1) {
|
|---|
| 92 | value = args.front();
|
|---|
| 93 | }
|
|---|
| 94 | if (type == "int") {
|
|---|
| 95 | NSCModuleHelper::SetSettingsInt(section, key, strEx::stoi(value));
|
|---|
| 96 | message = "Settings " + key + " saved successfully.";
|
|---|
| 97 | return NSCAPI::returnOK;
|
|---|
| 98 | } else if (type == "string") {
|
|---|
| 99 | NSCModuleHelper::SetSettingsString(section, key, value);
|
|---|
| 100 | message = "Settings " + key + " saved successfully.";
|
|---|
| 101 | return NSCAPI::returnOK;
|
|---|
| 102 | } else {
|
|---|
| 103 | NSCModuleHelper::SetSettingsString(type, section, key);
|
|---|
| 104 | message = "Settings " + section + " saved successfully.";
|
|---|
| 105 | return NSCAPI::returnOK;
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 | NSCAPI::nagiosReturn RemoteConfiguration::getVariable(const unsigned int argLen, char **char_args, std::string &message) {
|
|---|
| 109 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
|
|---|
| 110 | if (args.size() < 2) {
|
|---|
| 111 | message = "Invalid syntax.";
|
|---|
| 112 | return NSCAPI::returnUNKNOWN;
|
|---|
| 113 | }
|
|---|
| 114 | std::string section = args.front(); args.pop_front();
|
|---|
| 115 | std::string key = args.front(); args.pop_front();
|
|---|
| 116 | std::string value;
|
|---|
| 117 | value = NSCModuleHelper::getSettingsString(section, key, "");
|
|---|
| 118 | message = section+"/"+key+"="+value;
|
|---|
| 119 | return NSCAPI::returnOK;
|
|---|
| 120 | }
|
|---|
| 121 | int RemoteConfiguration::commandLineExec(const char* command,const unsigned int argLen,char** args) {
|
|---|
| 122 | std::string str;
|
|---|
| 123 | if (_stricmp(command, "setVariable") == 0) {
|
|---|
| 124 | setVariable(argLen, args, str);
|
|---|
| 125 | } else if (_stricmp(command, "writeConf") == 0) {
|
|---|
| 126 | writeConf(argLen, args, str);
|
|---|
| 127 | } else if (_stricmp(command, "getVariable") == 0) {
|
|---|
| 128 | setVariable(argLen, args, str);
|
|---|
| 129 | } else if (_stricmp(command, "ini2reg") == 0) {
|
|---|
| 130 | std::cout << "Migrating to registry settings..."<< std::endl;
|
|---|
| 131 | NSCModuleHelper::ReadSettings(NSCAPI::settings_inifile);
|
|---|
| 132 | NSCModuleHelper::SetSettingsInt(MAIN_SECTION_TITLE, MAIN_USEFILE, 0);
|
|---|
| 133 | NSCModuleHelper::WriteSettings(NSCAPI::settings_inifile);
|
|---|
| 134 | NSCModuleHelper::SetSettingsInt(MAIN_SECTION_TITLE, MAIN_USEREG, 1);
|
|---|
| 135 | NSCModuleHelper::WriteSettings(NSCAPI::settings_registry);
|
|---|
| 136 | } else if (_stricmp(command, "reg2ini") == 0) {
|
|---|
| 137 | std::cout << "Migrating to INI file settings..."<< std::endl;
|
|---|
| 138 | NSCModuleHelper::ReadSettings(NSCAPI::settings_registry);
|
|---|
| 139 | NSCModuleHelper::SetSettingsInt(MAIN_SECTION_TITLE, MAIN_USEREG, 0);
|
|---|
| 140 | NSCModuleHelper::WriteSettings(NSCAPI::settings_registry);
|
|---|
| 141 | NSCModuleHelper::SetSettingsInt(MAIN_SECTION_TITLE, MAIN_USEFILE, 1);
|
|---|
| 142 | NSCModuleHelper::WriteSettings(NSCAPI::settings_inifile);
|
|---|
| 143 | }
|
|---|
| 144 | return 0;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 | NSCAPI::nagiosReturn RemoteConfiguration::handleCommand(const strEx::blindstr command, const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) {
|
|---|
| 149 | if (command == "setVariable") {
|
|---|
| 150 | setVariable(argLen, char_args, msg);
|
|---|
| 151 | return NSCAPI::returnOK;
|
|---|
| 152 | } else if (command == "getVariable") {
|
|---|
| 153 | getVariable(argLen, char_args, msg);
|
|---|
| 154 | return NSCAPI::returnOK;
|
|---|
| 155 | } else if (command == "readConf") {
|
|---|
| 156 | return readConf(argLen, char_args, msg);
|
|---|
| 157 | } else if (command == "writeConf") {
|
|---|
| 158 | return writeConf(argLen, char_args, msg);
|
|---|
| 159 | }
|
|---|
| 160 | return NSCAPI::returnIgnored;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | NSC_WRAPPERS_MAIN_DEF(gRemoteConfiguration);
|
|---|
| 165 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 166 | NSC_WRAPPERS_HANDLE_CMD_DEF(gRemoteConfiguration);
|
|---|
| 167 | NSC_WRAPPERS_CLI_DEF(gRemoteConfiguration);
|
|---|
| 168 |
|
|---|