コード例 #1
0
ファイル: CheckWMI.cpp プロジェクト: 0000-bigtree/nscp
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);
}
コード例 #2
0
ファイル: XMLNode.cpp プロジェクト: bincker/Webkey
void printXMLBlock(ResXMLTree* block)
{
    block->restart();

    Vector<namespace_entry> namespaces;
    
    ResXMLTree::event_code_t code;
    int depth = 0;
    while ((code=block->next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
        String8 prefix = make_prefix(depth);
        int i;
        if (code == ResXMLTree::START_TAG) {
            size_t len;
            const uint16_t* ns16 = block->getElementNamespace(&len);
            String8 elemNs = build_namespace(namespaces, ns16);
            const uint16_t* com16 = block->getComment(&len);
            if (com16) {
                printf("%s <!-- %s -->\n", prefix.string(), String8(com16).string());
            }
            printf("%sE: %s%s (line=%d)\n", prefix.string(), elemNs.string(),
                   String8(block->getElementName(&len)).string(),
                   block->getLineNumber());
            int N = block->getAttributeCount();
            depth++;
            prefix = make_prefix(depth);
            for (i=0; i<N; i++) {
                uint32_t res = block->getAttributeNameResID(i);
                ns16 = block->getAttributeNamespace(i, &len);
                String8 ns = build_namespace(namespaces, ns16);
                String8 name(block->getAttributeName(i, &len));
                printf("%sA: ", prefix.string());
                if (res) {
                    printf("%s%s(0x%08x)", ns.string(), name.string(), res);
                } else {
                    printf("%s%s", ns.string(), name.string());
                }
                Res_value value;
                block->getAttributeValue(i, &value);
                if (value.dataType == Res_value::TYPE_NULL) {
                    printf("=(null)");
                } else if (value.dataType == Res_value::TYPE_REFERENCE) {
                    printf("=@0x%x", (int)value.data);
                } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) {
                    printf("=?0x%x", (int)value.data);
                } else if (value.dataType == Res_value::TYPE_STRING) {
                    printf("=\"%s\"",
                           String8(block->getAttributeStringValue(i, &len)).string());
                } else {
                    printf("=(type 0x%x)0x%x", (int)value.dataType, (int)value.data);
                }
                const char16_t* val = block->getAttributeStringValue(i, &len);
                if (val != NULL) {
                    printf(" (Raw: \"%s\")", String8(val).string());
                }
                printf("\n");
            }
        } else if (code == ResXMLTree::END_TAG) {
            depth--;
        } else if (code == ResXMLTree::START_NAMESPACE) {
            namespace_entry ns;
            size_t len;
            const uint16_t* prefix16 = block->getNamespacePrefix(&len);
            if (prefix16) {
                ns.prefix = String8(prefix16);
            } else {
                ns.prefix = "<DEF>";
            }
            ns.uri = String8(block->getNamespaceUri(&len));
            namespaces.push(ns);
            printf("%sN: %s=%s\n", prefix.string(), ns.prefix.string(),
                    ns.uri.string());
            depth++;
        } else if (code == ResXMLTree::END_NAMESPACE) {
            depth--;
            const namespace_entry& ns = namespaces.top();
            size_t len;
            const uint16_t* prefix16 = block->getNamespacePrefix(&len);
            String8 pr;
            if (prefix16) {
                pr = String8(prefix16);
            } else {
                pr = "<DEF>";
            }
            if (ns.prefix != pr) {
                prefix = make_prefix(depth);
                printf("%s*** BAD END NS PREFIX: found=%s, expected=%s\n",
                        prefix.string(), pr.string(), ns.prefix.string());
            }
            String8 uri = String8(block->getNamespaceUri(&len));
            if (ns.uri != uri) {
                prefix = make_prefix(depth);
                printf("%s *** BAD END NS URI: found=%s, expected=%s\n",
                        prefix.string(), uri.string(), ns.uri.string());
            }
            namespaces.pop();
        } else if (code == ResXMLTree::TEXT) {
            size_t len;
            printf("%sC: \"%s\"\n", prefix.string(), String8(block->getText(&len)).string());
        }
    }

    block->restart();
}
コード例 #3
0
ファイル: CheckWMI.cpp プロジェクト: mjesse88/nscp
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")) {