void CheckWMI::check_wmi(const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response) { typedef wmi_filter::filter filter_type; modern_filter::data_container data; modern_filter::cli_helper<filter_type> filter_helper(request, response, data); std::string given_target; target_helper::target_info target_info; boost::optional<target_helper::target_info> t; std::string query, ns = "root\\cimv2"; filter_type filter; filter_helper.add_options("", "", "", filter.get_filter_syntax(), "ignored"); filter_helper.add_syntax("${list}", filter.get_format_syntax(), "%(line)", "", "", ""); filter_helper.get_desc().add_options() ("target", po::value<std::string>(&given_target), "The target to check (for checking remote machines).") ("user", po::value<std::string>(&target_info.username), "Remote username when checking remote machines.") ("password", po::value<std::string>(&target_info.password), "Remote password when checking remote machines.") ("namespace", po::value<std::string>(&ns)->default_value("root\\cimv2"), "The WMI root namespace to bind to.") ("query", po::value<std::string>(&query), "The WMI query to execute.") ; if (!filter_helper.parse_options()) return; if (query.empty()) return nscapi::protobuf::functions::set_response_bad(*response, "No query specified"); if (!given_target.empty()) { t = targets.find(given_target); if (t) target_info.update_from(*t); else target_info.hostname = given_target; } try { ns = build_namespace(ns, target_info.hostname); wmi_impl::query wmiQuery(query, ns, target_info.username, target_info.password); filter.context->registry_.add_string() ("line", boost::bind(&wmi_filter::filter_obj::get_row, _1), "Get a list of all columns"); BOOST_FOREACH(const std::string &col, wmiQuery.get_columns()) { filter.context->registry_.add_int() (col, boost::bind(&wmi_filter::filter_obj::get_int, _1, col), boost::bind(&wmi_filter::filter_obj::get_string, _1, col), "Column: " + col).add_perf("", col, ""); } if (!filter_helper.build_filter(filter)) return; wmi_impl::row_enumerator e = wmiQuery.execute(); while (e.has_next()) { boost::shared_ptr<wmi_filter::filter_obj> record(new wmi_filter::filter_obj(e.get_next())); filter.match(record); } } catch (const wmi_impl::wmi_exception &e) { return nscapi::protobuf::functions::set_response_bad(*response, "WMIQuery failed: " + e.reason()); } filter_helper.post_process(filter); }
NSCAPI::nagiosReturn CheckWMI::commandLineExec(const int target_mode, const std::string &command, const std::list<std::string> &arguments, std::string &result) { try { if (command == "wmi" || command == "help" || command.empty()) { namespace po = boost::program_options; std::string query, ns, user, password, list_cls, list_inst; std::string computer; bool simple; int limit = -1; po::options_description desc("Allowed options"); desc.add_options() ("help,h", "Show help screen") ("select,s", po::value<std::string>(&query), "Execute a query") ("simple", "Use simple format") ("list-classes", po::value<std::string>(&list_cls)->implicit_value(""), "list all classes of a given type") ("list-instances", po::value<std::string>(&list_inst), "list all instances of a given type") ("list-ns", "list all name spaces") ("list-all-ns", "list all name spaces recursively") ("limit,l", po::value<int>(&limit), "Limit number of rows") ("namespace,n", po::value<std::string>(&ns)->default_value("root\\cimv2"), "Namespace") ("computer,c", po::value<std::string>(&computer), "A remote computer to connect to ") ("user,u", po::value<std::string>(&user), "The user for the remote computer") ("password,p", po::value<std::string>(&password), "The password for the remote computer") ; boost::program_options::variables_map vm; if (command == "help") { std::stringstream ss; ss << "wmi Command line syntax:" << std::endl; ss << desc; result = ss.str(); return NSCAPI::exec_return_codes::returnOK; } std::vector<std::string> args(arguments.begin(), arguments.end()); po::parsed_options parsed = po::basic_command_line_parser<char>(args).options(desc).run(); po::store(parsed, vm); po::notify(vm); if (vm.count("help") || (vm.count("select") == 0 && vm.count("list-classes") == 0 && vm.count("list-instances") == 0 && vm.count("list-ns") == 0 && vm.count("list-all-ns") == 0)) { std::stringstream ss; ss << "CheckWMI Command line syntax:" << std::endl; ss << desc; result = ss.str(); return NSCAPI::exec_return_codes::returnOK; } simple = vm.count("simple") > 0; ns = build_namespace(ns, computer); if (vm.count("select")) { row_type headers; std::vector<std::size_t> widths; std::size_t count = 0; try { wmi_impl::query wmiQuery(query, ns, user, password); std::list<std::string> cols = wmiQuery.get_columns(); count = cols.size(); BOOST_FOREACH(const std::string &col, cols) { headers.push_back(col); widths.push_back(col.size()); } result = render(headers, widths, wmiQuery.execute()); return NSCAPI::exec_return_codes::returnOK; } catch (const wmi_impl::wmi_exception &e) { result += "ERROR: " + e.reason(); return NSCAPI::exec_return_codes::returnERROR; } } else if (vm.count("list-classes")) {