bool CheckExternalScripts::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode) { try { sh::settings_registry settings(get_settings_proxy()); settings.set_alias(alias, "external scripts"); aliases_.set_path(settings.alias().get_settings_path("alias")); std::string wrappings_path = settings.alias().get_settings_path("wrappings"); boost::filesystem::path scriptRoot; std::string scriptDirectory; std::map<std::string, std::string> wrappings; settings.alias().add_path_to_settings() ("wrappings", sh::string_map_path(&wrappings) , "Script wrappings", "A list of templates for defining script commands.\nEnter any command line here and they will be expanded by scripts placed under the wrapped scripts section. %SCRIPT% will be replaced by the actual script an %ARGS% will be replaced by any given arguments.", "WRAPPING", "An external script wrapping") ("alias", sh::fun_values_path(boost::bind(&CheckExternalScripts::add_alias, this, _1, _2)), "Command aliases", "A list of aliases for already defined commands (with arguments).\n" "An alias is an internal command that has been predefined to provide a single command without arguments. Be careful so you don't create loops (ie check_loop=check_a, check_a=check_loop)", "ALIAS", "Query alias") ; settings.register_all(); settings.notify(); settings.clear(); if (wrappings.find("ps1") == wrappings.end()) { wrappings["ps1"] = "cmd /c echo If (-Not (Test-Path \"scripts\\%SCRIPT%\") ) { Write-Host \"UNKNOWN: Script `\"%SCRIPT%`\" not found.\"; exit(3) }; scripts\\%SCRIPT% $ARGS$; exit($lastexitcode) | powershell.exe /noprofile -command -"; settings.register_key(wrappings_path, "ps1", NSCAPI::key_string, "POWERSHELL WRAPPING", "Command line used for executing wrapped ps1 (powershell) scripts", "cmd /c echo If (-Not (Test-Path \"scripts\\%SCRIPT%\") ) { Write-Host \"UNKNOWN: Script `\"%SCRIPT%`\" not found.\"; exit(3) }; scripts\\%SCRIPT% $ARGS$; exit($lastexitcode) | powershell.exe /noprofile -command -", false); settings.set_static_key(wrappings_path, "ps1", wrappings["ps1"]); } if (wrappings.find("vbs") == wrappings.end()) { wrappings["vbs"] = "cscript.exe //T:30 //NoLogo scripts\\\\lib\\\\wrapper.vbs %SCRIPT% %ARGS%"; settings.register_key(wrappings_path, "vbs", NSCAPI::key_string, "Visual basic script", "Command line used for wrapped vbs scripts", "cscript.exe //T:30 //NoLogo scripts\\\\lib\\\\wrapper.vbs %SCRIPT% %ARGS%", false); settings.set_static_key(wrappings_path, "vbs", wrappings["vbs"]); } if (wrappings.find("bat") == wrappings.end()) { wrappings["bat"] = "scripts\\\\%SCRIPT% %ARGS%"; settings.register_key(wrappings_path, "bat", NSCAPI::key_string, "Batch file", "Command used for executing wrapped batch files", "scripts\\\\%SCRIPT% %ARGS%", false); settings.set_static_key(wrappings_path, "bat", wrappings["bat"]); } if (aliases_.empty()) { NSC_DEBUG_MSG("No aliases found (adding default)"); add_alias("alias_cpu", "check_cpu"); add_alias("alias_cpu_ex", "check_cpu \"warn=load > $ARG1$\" \"crit=load > $ARG2$\" time=5m time=1m time=30s"); add_alias("alias_mem", "check_memory"); add_alias("alias_up", "check_uptime"); add_alias("alias_disk", "check_drivesize"); add_alias("alias_disk_loose", "check_drivesize"); add_alias("alias_volumes", "check_drivesize"); add_alias("alias_volumes_loose", "check_drivesize"); add_alias("alias_service", "check_service"); add_alias("alias_service_ex", "check_service \"exclude=Net Driver HPZ12\" \"exclude=Pml Driver HPZ12\" exclude=stisvc"); add_alias("alias_process", "check_process \"process=$ARG1$\" \"crit=state != 'started'\""); add_alias("alias_process_stopped", "check_process \"process=$ARG1$\" \"crit=state != 'stopped'\""); add_alias("alias_process_count", "check_process \"process=$ARG1$\" \"warn=count > $ARG2$\" \"crit=count > $ARG3$\""); add_alias("alias_process_hung", "check_process \"filter=is_hung\" \"crit=count>0\""); add_alias("alias_event_log", "check_eventlog"); add_alias("alias_file_size", "check_files \"path=$ARG1$\" \"crit=size > $ARG2$\" \"top-syntax=${list}\" \"detail-syntax=${filename] ${size}\" max-dir-depth=10"); add_alias("alias_file_age", "check_files \"path=$ARG1$\" \"crit=written > $ARG2$\" \"top-syntax=${list}\" \"detail-syntax=${filename] ${written}\" max-dir-depth=10"); add_alias("alias_sched_all", "check_tasksched show-all \"syntax=${title}: ${exit_code}\" \"crit=exit_code ne 0\""); add_alias("alias_sched_long", "check_tasksched \"filter=status = 'running'\" \"detail-syntax=${title} (${most_recent_run_time})\" \"crit=most_recent_run_time < -$ARG1$\""); add_alias("alias_sched_task", "check_tasksched show-all \"filter=title eq '$ARG1$'\" \"detail-syntax=${title} (${exit_code})\" \"crit=exit_code ne 0\""); // add_alias("alias_updates", "check_updates -warning 0 -critical 0"); } settings.alias().add_key_to_settings() ("timeout", sh::uint_key(&timeout, 60), "Command timeout", "The maximum time in seconds that a command can execute. (if more then this execution will be aborted). NOTICE this only affects external commands not internal ones.") ("allow arguments", sh::bool_key(&allowArgs_, false), "Allow arguments when executing external scripts", "This option determines whether or not the we will allow clients to specify arguments to commands that are executed.") ("allow nasty characters", sh::bool_key(&allowNasty_, false), "Allow certain potentially dangerous characters in arguments", "This option determines whether or not the we will allow clients to specify nasty (as in |`&><'\"\\[]{}) characters in arguments.") ("script path", sh::string_key(&scriptDirectory), "Load all scripts in a given folder", "Load all scripts in a given directory and use them as commands.") ("script root", sh::path_key(&scriptRoot, "${scripts}"), "Script root folder", "Root path where all scripts are contained (You can not upload/download scripts outside this folder).") ; settings.register_all(); settings.notify(); settings.clear(); provider_.reset(new script_provider(get_id(), get_core(), settings.alias().get_settings_path("scripts"), scriptRoot, wrappings)); settings.alias().add_path_to_settings() ("External script settings", "General settings for the external scripts module (CheckExternalScripts).") ("scripts", sh::fun_values_path(boost::bind(&CheckExternalScripts::add_command, this, _1, _2)), "External scripts", "A list of scripts available to run from the CheckExternalScripts module. Syntax is: `command=script arguments`", "SCRIPT", "For more configuration options add a dedicated section (if you add a new section you can customize the user and various other advanced features)") ("wrapped scripts", sh::fun_values_path(boost::bind(&CheckExternalScripts::add_wrapping, this, _1, _2)), "Wrapped scripts", "A list of wrapped scripts (ie. script using a template mechanism).\nThe template used will be defined by the extension of the script. Thus a foo.ps1 will use the ps1 wrapping from the wrappings section.", "WRAPPED SCRIPT", "A wrapped script definitions") ; settings.alias().add_templates() ("scripts", "plus", "Add a simple script", "Add binding for a simple script", "{" "\"fields\": [ " " { \"id\": \"alias\", \"title\" : \"Alias\", \"type\" : \"input\", \"desc\" : \"This will identify the command\"} , " " { \"id\": \"script\", \"title\" : \"Script\", \"type\" : \"data-choice\", \"desc\" : \"The name of the script\",\"exec\" : \"CheckExternalScripts list --json\" } , " " { \"id\": \"args\", \"title\" : \"Arguments\", \"type\" : \"input\", \"desc\" : \"Command line arguments for the script use $ARG1$ to specify arguments\" } , " " { \"id\": \"cmd\", \"key\" : \"command\", \"title\" : \"A\", \"type\" : \"hidden\", \"desc\" : \"A\" } " " ], " "\"events\": { " "\"onSave\": \"(function (node) { node.save_path = self.path; var f = node.get_field('cmd'); f.key = node.get_field('alias').value(); var val = node.get_field('script').value(); if (node.get_field('args').value()) { val += ' ' + node.get_field('args').value(); }; f.value(val)})\"" "}" "}") ("alias", "plus", "Add an alias", "Add binding for an alias", "{" "\"fields\": [ " " { \"id\": \"alias\", \"title\" : \"Alias\", \"type\" : \"input\", \"desc\" : \"This will identify the command\"} , " " { \"id\": \"command\", \"title\" : \"Command\", \"type\" : \"data-choice\", \"desc\" : \"The name of the command to execute\",\"exec\" : \"CheckExternalScripts list --json --query\" } , " " { \"id\": \"args\", \"title\" : \"Arguments\", \"type\" : \"input\", \"desc\" : \"Command line arguments for the command. use $ARG1$ to specify arguments\" } , " " { \"id\": \"cmd\", \"key\" : \"command\", \"title\" : \"A\", \"type\" : \"hidden\", \"desc\" : \"A\" } " " ], " "\"events\": { " "\"onSave\": \"(function (node) { node.save_path = self.path; \nvar f = node.get_field('cmd'); \nf.key = node.get_field('alias').value(); \nvar val = node.get_field('command').value(); \nif (node.get_field('args').value()) { \nval += ' ' + node.get_field('args').value(); }; \nf.value(val)})\"" "}" "}") ; settings.register_all(); settings.notify(); if (!scriptDirectory.empty()) { addAllScriptsFrom(scriptDirectory); } aliases_.add_samples(get_settings_proxy()); aliases_.add_missing(get_settings_proxy(), "default", ""); root_ = get_base_path(); nscapi::core_helper core(get_core(), get_id()); BOOST_FOREACH(const boost::shared_ptr<alias::command_object> &o, aliases_.get_object_list()) { core.register_alias(o->get_alias(), "Alias for: " + o->command); } } catch (...) { NSC_LOG_ERROR_EX("loading"); return false; } return true; }
bool CheckExternalScripts::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode) { try { sh::settings_registry settings(get_settings_proxy()); settings.set_alias(alias, "external scripts"); commands_path = settings.alias().get_settings_path("scripts"); aliases_path = settings.alias().get_settings_path("alias"); std::string wrappings_path = settings.alias().get_settings_path("wrappings"); settings.alias().add_path_to_settings() ("wrappings", sh::string_map_path(&wrappings_) , "EXTERNAL SCRIPT WRAPPINGS SECTION", "A list of templates for wrapped scripts.\n%SCRIPT% will be replaced by the actual script an %ARGS% will be replaced by any given arguments.", "WRAPPING", "An external script wrapping") ("alias", sh::fun_values_path(boost::bind(&CheckExternalScripts::add_alias, this, _1, _2)), "ALIAS SECTION", "A list of aliases available.\n" "An alias is an internal command that has been predefined to provide a single command without arguments. Be careful so you don't create loops (ie check_loop=check_a, check_a=check_loop)", "ALIAS", "Query alias") ; settings.register_all(); settings.notify(); settings.clear(); if (wrappings_.find("ps1") == wrappings_.end()) { wrappings_["ps1"] = "cmd /c echo scripts\\\\%SCRIPT% %ARGS%; exit($lastexitcode) | powershell.exe -command -"; settings.register_key(wrappings_path, "ps1", NSCAPI::key_string, "POWERSHELL WRAPPING", "", "", false); settings.set_static_key(wrappings_path, "ps1", wrappings_["ps1"]); } if (wrappings_.find("vbs") == wrappings_.end()) { wrappings_["vbs"] = "cscript.exe //T:30 //NoLogo scripts\\\\lib\\\\wrapper.vbs %SCRIPT% %ARGS%"; settings.register_key(wrappings_path, "vbs", NSCAPI::key_string, "VISUAL BASIC WRAPPING", "", "", false); settings.set_static_key(wrappings_path, "vbs", wrappings_["vbs"]); } if (wrappings_.find("bat") == wrappings_.end()) { wrappings_["bat"] = "scripts\\\\%SCRIPT% %ARGS%"; settings.register_key(wrappings_path, "bat", NSCAPI::key_string, "BATCH FILE WRAPPING", "", "", false); settings.set_static_key(wrappings_path, "bat", wrappings_["bat"]); } if (aliases_.empty()) { NSC_DEBUG_MSG("No aliases found (adding default)"); add_alias("alias_cpu", "check_cpu"); add_alias("alias_cpu_ex", "check_cpu \"warn=load > $ARG1$\" \"crit=load > $ARG2$\" time=5m time=1m time=30s"); add_alias("alias_mem", "check_memory"); add_alias("alias_up", "check_uptime"); add_alias("alias_disk", "check_drivesize"); add_alias("alias_disk_loose", "check_drivesize"); add_alias("alias_volumes", "check_drivesize"); add_alias("alias_volumes_loose", "check_drivesize"); add_alias("alias_service", "check_service"); add_alias("alias_service_ex", "check_service \"exclude=Net Driver HPZ12\" \"exclude=Pml Driver HPZ12\" exclude=stisvc"); add_alias("alias_process", "check_process \"process=$ARG1$\" \"crit=state != 'started'\""); add_alias("alias_process_stopped", "check_process \"process=$ARG1$\" \"crit=state != 'stopped'\""); add_alias("alias_process_count", "check_process \"process=$ARG1$\" \"warn=count > $ARG2$\" \"crit=count > $ARG3$\""); add_alias("alias_process_hung", "check_process \"filter=is_hung\" \"crit=count>0\""); add_alias("alias_event_log", "check_eventlog"); add_alias("alias_file_size", "check_files \"path=$ARG1$\" \"crit=size > $ARG2$\" \"top-syntax=${list}\" \"detail-syntax=${filename] ${size}\" max-dir-depth=10"); add_alias("alias_file_age", "check_files \"path=$ARG1$\" \"crit=written > $ARG2$\" \"top-syntax=${list}\" \"detail-syntax=${filename] ${written}\" max-dir-depth=10"); add_alias("alias_sched_all", "check_tasksched show-all \"syntax=${title}: ${exit_code}\" \"crit=exit_code ne 0\""); add_alias("alias_sched_long", "check_tasksched \"filter=status = 'running'\" \"detail-syntax=${title} (${most_recent_run_time})\" \"crit=most_recent_run_time < -$ARG1$\""); add_alias("alias_sched_task", "check_tasksched show-all \"filter=title eq '$ARG1$'\" \"detail-syntax=${title} (${exit_code})\" \"crit=exit_code ne 0\""); // add_alias("alias_updates", "check_updates -warning 0 -critical 0"); } settings.alias().add_path_to_settings() ("EXTERNAL SCRIPT SECTION", "Section for external scripts configuration options (CheckExternalScripts).") ("scripts", sh::fun_values_path(boost::bind(&CheckExternalScripts::add_command, this, _1, _2)), "SCRIPT SECTION", "A list of scripts available to run from the CheckExternalScripts module. Syntax is: <command>=<script> <arguments>", "SCRIPT", "For more configuration options add a dedicated section (if you add a new section you can customize the user and various other advanced features)") ("wrapped scripts", sh::fun_values_path(boost::bind(&CheckExternalScripts::add_wrapping, this, _1, _2)), "WRAPPED SCRIPTS SECTION", "A list of wrapped scripts (ie. scruts using a template mechanism). The template used will be defined by the extension of the script.", "WRAPPED SCRIPT", "A wrapped script defenitions") ; settings.alias().add_key_to_settings() ("timeout", sh::uint_key(&timeout, 60), "COMMAND TIMEOUT", "The maximum time in seconds that a command can execute. (if more then this execution will be aborted). NOTICE this only affects external commands not internal ones.") ("allow arguments", sh::bool_key(&allowArgs_, false), "COMMAND ARGUMENT PROCESSING", "This option determines whether or not the we will allow clients to specify arguments to commands that are executed.") ("allow nasty characters", sh::bool_key(&allowNasty_, false), "COMMAND ALLOW NASTY META CHARS", "This option determines whether or not the we will allow clients to specify nasty (as in |`&><'\"\\[]{}) characters in arguments.") ("script path", sh::string_key(&scriptDirectory_), "SCRIPT DIRECTORY", "Load all scripts in a directory and use them as commands. Probably dangerous but useful if you have loads of scripts :)") ; settings.register_all(); settings.notify(); if (!scriptDirectory_.empty()) { addAllScriptsFrom(scriptDirectory_); } root_ = get_base_path(); nscapi::core_helper core(get_core(), get_id()); BOOST_FOREACH(const commands::command_handler::object_list_type::value_type &o, commands_.object_list) { core.register_alias(o.second.tpl.alias, "External script: " + o.second.command); } BOOST_FOREACH(const alias::command_handler::object_list_type::value_type &o, aliases_.object_list) { core.register_alias(o.second.tpl.alias, "Alias for: " + o.second.command); } } catch (...) { NSC_LOG_ERROR_EX("loading"); return false; } return true; }