Пример #1
0
    def __init__(self, class_name, counter_name, log, instance_name = None, machine_name = None, precision=None):
        self._get_counter_dictionary()
        self._class_name = win32pdh.LookupPerfNameByIndex(None, int(WinPDHCounter.pdh_counter_dict[class_name]))
        self._counter_name = win32pdh.LookupPerfNameByIndex(None, int(WinPDHCounter.pdh_counter_dict[counter_name]))

        self._is_single_instance = False
        self.hq = win32pdh.OpenQuery()
        self.logger = log
        self.counterdict = {}
        if precision is None:
            self._precision = win32pdh.PDH_FMT_DOUBLE
        else:
            self._precision = precision
        counters, instances = win32pdh.EnumObjectItems(None, machine_name, self._class_name, win32pdh.PERF_DETAIL_WIZARD)
        if instance_name is None and len(instances) > 0:
            for inst in instances:
                path = win32pdh.MakeCounterPath((machine_name, self._class_name, inst, None, 0, self._counter_name))
                try:
                    self.counterdict[inst] = win32pdh.AddCounter(self.hq, path)
                except: # noqa: E722
                    self.logger.fatal("Failed to create counter.  No instances of %s\%s" % (
                        self._class_name, self._counter_name))
                try:
                    self.logger.debug("Path: %s\n" % unicode(path))
                except: # noqa: E722
                    # some unicode characters are not translatable here.  Don't fail just
                    # because we couldn't log
                    self.logger.debug("Failed to log path")
                    pass
        else:
            if instance_name is not None:
                # check to see that it's valid
                if len(instances) <= 0:
                    self.logger.error("%s doesn't seem to be a multi-instance counter, but asked for specific instance %s" % (
                        class_name, instance_name
                    ))
                    return
                if instance_name not in instances:
                    self.logger.error("%s is not a counter instance in %s" % (
                        instance_name, class_name
                    ))
                    return
            path = win32pdh.MakeCounterPath((machine_name, self._class_name, instance_name, None, 0, self._counter_name))
            try:
                self.logger.debug("Path: %s\n" % unicode(path))
            except: # noqa: E722
                # some unicode characters are not translatable here.  Don't fail just
                # because we couldn't log
                self.logger.debug("Failed to log path")
                pass
            try:
                self.counterdict[SINGLE_INSTANCE_KEY] = win32pdh.AddCounter(self.hq, path)
            except: # noqa: E722
                self.logger.fatal("Failed to create counter.  No instances of %s\%s" % (
                    self._class_name, self._counter_name))
                raise
            self._is_single_instance = True
Пример #2
0
    def __init__(self, class_name, instance_name, log):
        self._get_counter_dictionary()
        self._class_name = win32pdh.LookupPerfNameByIndex(
            None, int(WinPDHCounter.pdh_counter_dict[class_name]))
        self._instance_name = win32pdh.LookupPerfNameByIndex(
            None, int(WinPDHCounter.pdh_counter_dict[instance_name]))

        self._is_single_instance = False
        self.hq = win32pdh.OpenQuery()
        self.logger = log
        self.counterdict = {}
        counters, instances = win32pdh.EnumObjectItems(
            None, None, self._class_name, win32pdh.PERF_DETAIL_WIZARD)
        if len(instances) > 0:
            for inst in instances:
                path = win32pdh.MakeCounterPath((None, self._class_name, inst,
                                                 None, 0, self._instance_name))
                try:
                    self.counterdict[inst] = win32pdh.AddCounter(self.hq, path)
                except:
                    self.logger.fatal(
                        "Failed to create counter.  No instances of %s\%s" %
                        (self._class_name, self._instance_name))
                try:
                    self.logger.debug("Path: %s\n" % unicode(path))
                except:
                    # some unicode characters are not translatable here.  Don't fail just
                    # because we couldn't log
                    self.logger.debug("Failed to log path")
                    pass
        else:
            path = win32pdh.MakeCounterPath(
                (None, self._class_name, None, None, 0, self._instance_name))
            try:
                self.logger.debug("Path: %s\n" % unicode(path))
            except:
                # some unicode characters are not translatable here.  Don't fail just
                # because we couldn't log
                self.logger.debug("Failed to log path")
                pass
            try:
                self.counterdict[SINGLE_INSTANCE_KEY] = win32pdh.AddCounter(
                    self.hq, path)
            except:
                self.logger.fatal(
                    "Failed to create counter.  No instances of %s\%s" %
                    (self._class_name, self._instance_name))
                raise
            self._is_single_instance = True
Пример #3
0
def pdhTranslateEnglishCounter (counter):
	key = _winreg.OpenKey (_winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009")
	strings = _winreg.QueryValueEx (key, 'Counter')[0]
	for i in range(0,len(strings),2):
		if counter == strings[i+1]:
			return win32pdh.LookupPerfNameByIndex (None, int(strings[i]))
	return counter
Пример #4
0
    def __init__(self,
                 en_class_name,
                 en_counter_name,
                 log,
                 instance_name=None,
                 machine_name=None,
                 precision=None):
        self.counterdict = {}
        self.logger = log
        self._counter_name = en_counter_name
        self._en_class_name = en_class_name
        self._instance_name = instance_name
        self._machine_name = machine_name
        self._is_single_instance = False

        if precision is None:
            self._precision = win32pdh.PDH_FMT_DOUBLE
        else:
            self._precision = precision

        class_name_index_list = []
        try:
            self._get_counter_dictionary()
            class_name_index_list = WinPDHCounter.pdh_counter_dict[
                en_class_name]
        except WindowsError:
            WinPDHCounter._use_en_counter_names = True
            self.logger.warning(
                "Unable to get counter translations; attempting default English names"
            )
        except Exception as e:
            self.logger.error("Exception loading counter strings %s", str(e))
            raise

        if WinPDHCounter._use_en_counter_names:
            self._class_name = en_class_name
        else:
            if len(class_name_index_list) == 0:
                self.logger.warning(
                    "Class %s was not in counter name list, attempting english counter",
                    en_class_name)
                self._class_name = en_class_name
            else:
                if len(class_name_index_list) > 1:
                    self.logger.warning(
                        "Class %s had multiple (%d) indices, using first",
                        en_class_name, len(class_name_index_list))
                self._class_name = win32pdh.LookupPerfNameByIndex(
                    None, int(class_name_index_list[0]))

        self.hq = win32pdh.OpenQuery()
        self.collect_counters()

        if len(self.counterdict) == 0:
            raise AttributeError("No valid counters to report")
Пример #5
0
    def _make_counter_path(self, machine_name, counter_name, instance_name, counters):
        '''
        When handling non english versions, the counters don't work quite as documented.
        This is because strings like "Bytes Sent/sec" might appear multiple times in the
        english master, and might not have mappings for each index.

        Search each index, and make sure the requested counter name actually appears in
        the list of available counters; that's the counter we'll use.
        '''
        path = ""
        if WinPDHCounter._use_en_counter_names:
            '''
            In this case, we don't have any translations.  Just attempt to make the
            counter path
            '''
            try:
                path = win32pdh.MakeCounterPath((machine_name, self._class_name, instance_name, None, 0, counter_name))
                self.logger.debug("Successfully created English-only path")
            except Exception as e: # noqa: E722
                self.logger.warning("Unable to create English-only path %s" % str(e))
                raise
            return path

        counter_name_index_list = WinPDHCounter.pdh_counter_dict[counter_name]

        for index in counter_name_index_list:
            c = win32pdh.LookupPerfNameByIndex(None, int(index))
            if c is None or len(c) == 0:
                self.logger.debug("Index %s not found, skipping" % index)
                continue

            # check to see if this counter is in the list of counters for this class
            if c not in counters:
                try:
                    self.logger.debug("Index %s counter %s not in counter list" % (index, unicode(c)))
                except: # noqa: E722
                    # some unicode characters are not translatable here.  Don't fail just
                    # because we couldn't log
                    self.logger.debug("Index %s not in counter list" % index)
                    pass

                continue

            # see if we can create a counter
            try:
                path = win32pdh.MakeCounterPath((machine_name, self._class_name, instance_name, None, 0, c))
                self.logger.debug("Successfully created path %s" % index)
                break
            except: # noqa: E722
                try:
                    self.logger.info("Unable to make path with counter %s, trying next available" % unicode(c))
                except: # noqa: E722
                    self.logger.info("Unable to make path with counter index %s, trying next available" % index)
                    pass
        return path
Пример #6
0
def find_pdh_counter_localized_name(english_name, machine_name=None):
    if not counter_english_map:
        import win32api, win32con
        counter_reg_value = win32api.RegQueryValueEx(win32con.HKEY_PERFORMANCE_DATA,
                                                     "Counter 009")
        counter_list = counter_reg_value[0]
        for i in range(0, len(counter_list) - 1, 2):
            try:
                counter_id = int(counter_list[i])
            except ValueError:
                continue
            counter_english_map[counter_list[i + 1].lower()] = counter_id
    return win32pdh.LookupPerfNameByIndex(machine_name, counter_english_map[english_name.lower()])
Пример #7
0
    def __init__(self,
                 class_name,
                 counter_name,
                 log,
                 instance_name=None,
                 machine_name=None,
                 precision=None):
        self.logger = log

        try:
            self._get_counter_dictionary()
            class_name_index_list = WinPDHCounter.pdh_counter_dict[class_name]
        except WindowsError as e:
            WinPDHCounter._use_en_counter_names = True
            self.logger.warning(
                "Unable to get counter translations; attempting default English names"
            )
            pass
        except Exception as e:
            self.logger.error("Exception loading counter strings %s", str(e))
            raise

        if WinPDHCounter._use_en_counter_names:
            self._class_name = class_name
        else:
            if len(class_name_index_list) == 0:
                self.logger.warning(
                    "Class %s was not in counter name list, attempting english counter"
                    % class_name)
                self._class_name = class_name
            else:
                if len(class_name_index_list) > 1:
                    self.logger.warning(
                        "Class %s had multiple (%d) indices, using first" %
                        (class_name, len(class_name_index_list)))
                self._class_name = win32pdh.LookupPerfNameByIndex(
                    None, int(class_name_index_list[0]))

        self._is_single_instance = False
        self.hq = win32pdh.OpenQuery()

        self.counterdict = {}
        if precision is None:
            self._precision = win32pdh.PDH_FMT_DOUBLE
        else:
            self._precision = precision
        counters, instances = win32pdh.EnumObjectItems(
            None, machine_name, self._class_name, win32pdh.PERF_DETAIL_WIZARD)
        if instance_name is None and len(instances) > 0:
            for inst in instances:
                path = self._make_counter_path(machine_name, counter_name,
                                               inst, counters)
                if not path:
                    continue
                try:
                    self.counterdict[inst] = win32pdh.AddCounter(self.hq, path)
                except:  # noqa: E722
                    self.logger.fatal(
                        "Failed to create counter.  No instances of %s\%s" %
                        (self._class_name, self._counter_name))
                try:
                    self.logger.debug("Path: %s\n" % unicode(path))
                except:  # noqa: E722
                    # some unicode characters are not translatable here.  Don't fail just
                    # because we couldn't log
                    self.logger.debug("Failed to log path")
                    pass
        else:
            if instance_name is not None:
                # check to see that it's valid
                if len(instances) <= 0:
                    self.logger.error(
                        "%s doesn't seem to be a multi-instance counter, but asked for specific instance %s"
                        % (class_name, instance_name))
                    raise AttributeError("%s is not a multi-instance counter" %
                                         class_name)
                if instance_name not in instances:
                    self.logger.error("%s is not a counter instance in %s" %
                                      (instance_name, class_name))
                    raise AttributeError("%s is not an instance of %s" %
                                         (instance_name, class_name))
            path = self._make_counter_path(machine_name, counter_name,
                                           instance_name, counters)
            if not path:
                self.logger.warning("Empty path returned")
            else:
                try:
                    self.logger.debug("Path: %s\n" % unicode(path))
                except:  # noqa: E722
                    # some unicode characters are not translatable here.  Don't fail just
                    # because we couldn't log
                    self.logger.debug("Failed to log path")
                    pass
                try:
                    self.counterdict[
                        SINGLE_INSTANCE_KEY] = win32pdh.AddCounter(
                            self.hq, path)
                except:  # noqa: E722
                    self.logger.fatal(
                        "Failed to create counter.  No instances of %s\%s" %
                        (self._class_name, counter_name))
                    raise
                self._is_single_instance = True
        if len(self.counterdict) == 0:
            raise AttributeError("No valid counters to report")