示例#1
0
文件: pmda.py 项目: DundalkIT/pcp
 def cache_resize(self, maximum):
     if self.it_numinst <= 0:
         sts = LIBPCP_PMDA.pmdaCacheResize(self.it_indom, maximum)
         if sts < 0:
             raise pmErr(sts)
     else:
         raise pmErr(cpmapi.PM_ERR_NYI)
示例#2
0
文件: pmda.py 项目: DundalkIT/pcp
 def cache_load(self):
     if self.it_numinst <= 0:
         sts = LIBPCP_PMDA.pmdaCacheOp(self.it_indom, cpmda.PMDA_CACHE_LOAD)
         if sts < 0:
             raise pmErr(sts)
     else:
         raise pmErr(cpmapi.PM_ERR_NYI)
示例#3
0
 def cache_load(self):
     if self.it_numinst <= 0:
         sts = LIBPCP_PMDA.pmdaCacheOp(self.it_indom, cpmda.PMDA_CACHE_LOAD)
         if sts < 0:
             raise pmErr(sts)
     else:
         raise pmErr(cpmapi.PM_ERR_NYI)
示例#4
0
 def cache_resize(self, maximum):
     if self.it_numinst <= 0:
         sts = LIBPCP_PMDA.pmdaCacheResize(self.it_indom, maximum)
         if sts < 0:
             raise pmErr(sts)
     else:
         raise pmErr(cpmapi.PM_ERR_NYI)
示例#5
0
文件: pmgui.py 项目: Aconex/pcp
 def pmRecordAddHost(host, isdefault, config):
     """ GUI API - Adds host to an archive recording session
     (status, recordhost) = pmRecordAddHost("host", 1, "configuration")
     """
     rhp = POINTER(pmRecordHost)()
     status = LIBPCP_GUI.pmRecordAddHost(
                             c_char_p(host), isdefault, byref(rhp))
     if status < 0:
         raise pmErr(status)
     status = LIBC.fputs(c_char_p(config), c_long(rhp.contents.f_config))
     if (status < 0):
         LIBC.perror(c_char_p(""))
         raise pmErr(status)
     return status, rhp
示例#6
0
 def pmRecordAddHost(host, isdefault, config):
     """ GUI API - Adds host to an archive recording session
     (status, recordhost) = pmRecordAddHost("host", 1, "configuration")
     """
     rhp = POINTER(pmRecordHost)()
     status = LIBPCP_GUI.pmRecordAddHost(c_char_p(host), isdefault,
                                         byref(rhp))
     if status < 0:
         raise pmErr(status)
     status = LIBC.fputs(c_char_p(config), c_long(rhp.contents.f_config))
     if (status < 0):
         LIBC.perror(c_char_p(""))
         raise pmErr(status)
     return status, rhp
示例#7
0
    def execute(self):
        """ Using a PMAPI context (could be either host or archive),
            fetch and report a fixed set of values related to uptime.
        """
        metrics = ("kernel.all.uptime", "kernel.all.nusers", "kernel.all.load")
        pmids = self.context.pmLookupName(metrics)
        descs = self.context.pmLookupDescs(pmids)
        result = self.context.pmFetch(pmids)
        if result.contents.numpmid != len(metrics):
            raise pmapi.pmErr(PM_ERR_VALUE)

        uptime = ""

        sample_time = result.contents.timestamp.tv_sec
        time_struct = self.context.pmLocaltime(sample_time)
        uptime += print_timestamp(time_struct)

        atom = self.context.pmExtractValue(
            result.contents.get_valfmt(0), result.contents.get_vlist(0, 0), descs[0].contents.type, PM_TYPE_U32
        )
        uptime += print_uptime(atom.ul)

        atom = self.context.pmExtractValue(
            result.contents.get_valfmt(1), result.contents.get_vlist(1, 0), descs[1].contents.type, PM_TYPE_U32
        )
        uptime += print_users(atom.ul)

        averages = [1, 5, 15]
        for inst in range(3):
            averages[inst] = self.context.pmExtractValue(
                result.contents.get_valfmt(2), result.contents.get_vlist(2, inst), descs[2].contents.type, PM_TYPE_FLOAT
            )
        uptime += print_load(averages[0].f, averages[1].f, averages[2].f)
        print(uptime)
        self.context.pmFreeResult(result)
示例#8
0
 def check_metric(self, metric):
     """ Validate individual metric and get its details """
     try:
         pmid = self.util.context.pmLookupName(metric)[0]
         if pmid in self.pmids:
             # Always ignore duplicates
             return
         desc = self.util.context.pmLookupDescs(pmid)[0]
         if desc.contents.indom == pmapi.c_api.PM_IN_NULL:
             inst = ([pmapi.c_api.PM_IN_NULL], [None])  # mem.util.free
         else:
             if self.util.context.type == pmapi.c_api.PM_CONTEXT_ARCHIVE:
                 inst = self.util.context.pmGetInDomArchive(desc)
             else:
                 inst = self.util.context.pmGetInDom(desc)  # disk.dev.read
             if not inst[0]:
                 inst = ([pmapi.c_api.PM_IN_NULL], [None]
                         )  # pmcd.pmie.logfile
         # Reject unsupported types
         if not (desc.contents.type == pmapi.c_api.PM_TYPE_32
                 or desc.contents.type == pmapi.c_api.PM_TYPE_U32
                 or desc.contents.type == pmapi.c_api.PM_TYPE_64
                 or desc.contents.type == pmapi.c_api.PM_TYPE_U64
                 or desc.contents.type == pmapi.c_api.PM_TYPE_FLOAT
                 or desc.contents.type == pmapi.c_api.PM_TYPE_DOUBLE
                 or desc.contents.type == pmapi.c_api.PM_TYPE_STRING):
             raise pmapi.pmErr(pmapi.c_api.PM_ERR_TYPE)
         instances = self.util.instances if not self._tmp else self._tmp
         if hasattr(self.util,
                    'omit_flat') and self.util.omit_flat and not inst[1][0]:
             return
         if instances and inst[1][0] and not self.do_live_filtering():
             found = [[], []]
             for r in instances:
                 try:
                     cr = re.compile(r'\A' + r + r'\Z')
                     for i, s in enumerate(inst[1]):
                         if re.match(cr, s):
                             found[0].append(inst[0][i])
                             found[1].append(inst[1][i])
                     del cr
                 except Exception as error:
                     sys.stderr.write("Invalid regex '%s': %s.\n" %
                                      (r, error))
                     sys.exit(1)
             if not found[0]:
                 return
             inst = tuple(found)
         self.pmids.append(pmid)
         self.descs.append(desc)
         self.insts.append(inst)
     except pmapi.pmErr as error:
         if hasattr(self.util,
                    'ignore_incompat') and self.util.ignore_incompat:
             return
         sys.stderr.write("Invalid metric %s (%s).\n" %
                          (metric, str(error)))
         sys.exit(1)
示例#9
0
文件: pmgui.py 项目: yvk2493/pcp
 def pmRecordAddHost(host, isdefault, config):
     """ GUI API - Adds host to an archive recording session
     (status, recordhost) = pmRecordAddHost("host", 1, "configuration")
     """
     if not isinstance(host, bytes):
         host = host.encode('utf-8')
     rhp = POINTER(pmRecordHost)()
     status = LIBPCP_GUI.pmRecordAddHost(c_char_p(host), isdefault,
                                         byref(rhp))
     if status < 0:
         raise pmErr(status)
     if not isinstance(config, bytes):
         config = config.encode('utf-8')
     status = LIBC.fputs(c_char_p(config), c_long(rhp.contents.f_config))
     if status < 0:
         LIBC.perror(c_char_p(""))
         raise pmErr(status)
     return status, rhp
示例#10
0
文件: pmgui.py 项目: Aconex/pcp
 def pmRecordSetup(folio, creator, replay):
     """ GUI API - Setup an archive recording session
     File* file = pmRecordSetup("folio", "creator", 0)
     """
     file_result = LIBPCP_GUI.pmRecordSetup(
                             c_char_p(folio), c_char_p(creator), replay)
     if (file_result == 0):
         raise pmErr(file_result)
     return file_result
示例#11
0
 def pmRecordSetup(folio, creator, replay):
     """ GUI API - Setup an archive recording session
     File* file = pmRecordSetup("folio", "creator", 0)
     """
     file_result = LIBPCP_GUI.pmRecordSetup(c_char_p(folio),
                                            c_char_p(creator), replay)
     if (file_result == 0):
         raise pmErr(file_result)
     return file_result
示例#12
0
 def pmRecordControl(rhp, request, options):
     """PMAPI - Control an archive recording session
     status = pmRecordControl(0, cpmgui.PM_RCSETARG, "args")
     status = pmRecordControl(0, cpmgui.PM_REC_ON)
     status = pmRecordControl(0, cpmgui.PM_REC_OFF)
     """
     status = LIBPCP_GUI.pmRecordControl(cast(rhp, POINTER(pmRecordHost)),
                                         request, c_char_p(options))
     if status < 0 and status != PM_ERR_IPC:
         raise pmErr(status)
     return status
示例#13
0
文件: pmgui.py 项目: Aconex/pcp
 def pmRecordControl(rhp, request, options):
     """PMAPI - Control an archive recording session
     status = pmRecordControl(0, cpmgui.PM_RCSETARG, "args")
     status = pmRecordControl(0, cpmgui.PM_REC_ON)
     status = pmRecordControl(0, cpmgui.PM_REC_OFF)
     """
     status = LIBPCP_GUI.pmRecordControl(
                             cast(rhp, POINTER(pmRecordHost)),
                             request, c_char_p(options))
     if status < 0 and status != PM_ERR_IPC:
         raise pmErr(status)
     return status
示例#14
0
文件: pmgui.py 项目: jujis008/pcp
 def pmRecordSetup(folio, creator, replay):
     """ GUI API - Setup an archive recording session
     File* file = pmRecordSetup("folio", "creator", 0)
     """
     if type(folio) != type(b''):
         folio =  folio.encode('utf-8')
     if type(creator) != type(b''):
         creator = creator.encode('utf-8')
     file_result = LIBPCP_GUI.pmRecordSetup(
                             c_char_p(folio), c_char_p(creator), replay)
     if (file_result == 0):
         raise pmErr(file_result)
     return file_result
示例#15
0
文件: pmgui.py 项目: yvk2493/pcp
 def pmRecordSetup(folio, creator, replay):
     """ GUI API - Setup an archive recording session
     File* file = pmRecordSetup("folio", "creator", 0)
     """
     if not isinstance(folio, bytes):
         folio = folio.encode('utf-8')
     if not isinstance(creator, bytes):
         creator = creator.encode('utf-8')
     file_result = LIBPCP_GUI.pmRecordSetup(c_char_p(folio),
                                            c_char_p(creator), replay)
     if file_result == 0:
         raise pmErr(file_result)
     return file_result
示例#16
0
    def setup_metrics(self, pcp):
        # remove any unsupported metrics
        name_pattern = self.metrics[0].split(".")[0] + ".*"
        for j in range(len(self.metrics) - 1, -1, -1):
            try:
                self.metric_pmids = pcp.pmLookupName(self.metrics[j])
            except pmErr as e:
                self.metrics.remove(self.metrics[j])

        if len(self.metrics) == 0:
            raise pmErr(c_api.PM_ERR_NAME, "", name_pattern)
        self.metrics_dict = dict((i, self.metrics.index(i)) for i in self.metrics)
        self.metric_pmids = pcp.pmLookupName(self.metrics)
        self.metric_descs = pcp.pmLookupDescs(self.metric_pmids)
        self.metric_values = [0 for i in range(len(self.metrics))]
        self._last_values = [0 for i in range(len(self.metrics))]
示例#17
0
    def setup_metrics(self, pcp):
        # remove any unsupported metrics
        name_pattern = self.metrics[0].split(".")[0] + ".*"
        for j in range(len(self.metrics)-1, -1, -1):
            try:
                self.metric_pmids = pcp.pmLookupName(self.metrics[j])
            except pmapi.pmErr as e:
                self.metrics.remove(self.metrics[j])

        if (len(self.metrics) == 0):
            raise pmapi.pmErr(c_api.PM_ERR_NAME, "", name_pattern)
        self.metrics_dict = dict((i, self.metrics.index(i)) for i in self.metrics)
        self.metric_pmids = pcp.pmLookupName(self.metrics)
        self.metric_descs = pcp.pmLookupDescs(self.metric_pmids)
        self.metric_values = [0 for i in range(len(self.metrics))]
        self._last_values = [0 for i in range(len(self.metrics))]
 def execute(self):
     if self.context:
         metrics = ('disk.all.read',)
         pmids = self.context.pmLookupName(metrics)
         # print "PMID: ",pmids
         descs = self.context.pmLookupDescs(pmids)
         # print "Desc: ",descs
         result = self.context.pmFetch(pmids)
         if result.contents.numpmid != len(metrics):
             print "Got error here"
             raise pmapi.pmErr(PM_ERR_VALUE)
         atom = self.context.pmExtractValue(
                 result.contents.get_valfmt(0),
                 result.contents.get_vlist(0,0),
                 descs[0].contents.type,
                 PM_TYPE_U32)
         print "Total Disk Reads: ",atom.ul
         self.context.pmFreeResult(result)
示例#19
0
    def execute(self):
        if self.context:
            # Use a different metric to demonstrate as its easier to see whats going on
            metrics = ('proc.psinfo.pid',)
            pmids = self.context.pmLookupName(metrics)
            # print "PMID: ",pmids
            descs = self.context.pmLookupDescs(pmids)
            # print "Desc: ",descs
            result = self.context.pmFetch(pmids)
            if result.contents.numpmid != len(metrics):
                print "Got error here"
                raise pmapi.pmErr(PM_ERR_VALUE)
            num_inst = result.contents.get_numval(0)
            print "no of instances: ",num_inst
            # print "Inst domain: ",descs[0].contents.indom
            ''' Get external_names '''
            internal_instance_ids,external_names = self.context.pmGetInDom(descs[0])

            ''' Get values (pids) from the pmResult '''
            print "PID\t\t\tName"
            for i in range(num_inst):
                atom = self.context.pmExtractValue(
		                result.contents.get_valfmt(0),
		                result.contents.get_vlist(0,i),
		                descs[0].contents.type,
		                PM_TYPE_U32)

                #Get the name by either looking from the offset in the extername list
                # external_name_offset = internal_instance_ids.index(result.contents.get_inst(0,i))
                # external_name = external_names[external_name_offset]

                # Or lookup with pmNameInDom(pmdesc, internal_instance_id)
                external_name = self.context.pmNameInDom(descs[0], result.contents.get_inst(0,i))


                ''' Strip options from the external_name, a space followed by a - can be considered an option'''
                strip_options_index = external_name.find(" -")
                if strip_options_index > 0:
                    print atom.ul, "\t\t", external_name[:strip_options_index]
                else:
                    print atom.ul, "\t\t", external_name
            self.context.pmFreeResult(result)
示例#20
0
文件: pcpfast.py 项目: ubccr/supremm
def pcpfastExtractValues(result_p, vsetidx, vlistidx, dtype):
    """ quicker implementation of pmExtractValue than the default provided with the pcp python bindings
        this version saves converting the C indexes to python and back again
    """

    inst = c_int()
    outAtom = pmapi.pmAtomValue()
    status = LIBPCPFAST.pcpfastExtractValues(result_p, byref(inst), byref(outAtom), vsetidx, vlistidx, dtype)
    if status < 0:
        raise pmapi.pmErr(status)

    if dtype == c_api.PM_TYPE_STRING:
        # Get pointer to C string
        c_str = c_char_p()
        memmove(byref(c_str), addressof(outAtom) + pmapi.pmAtomValue.cp.offset, sizeof(c_char_p))
        # Convert to a python string and have result point to it
        outAtom.cp = outAtom.cp
        # Free the C string
        LIBC.free(c_str)

    return outAtom.dref(dtype), inst.value
示例#21
0
def pcpfastExtractValues(result_p, vsetidx, vlistidx, dtype):
    """ quicker implementation of pmExtractValue than the default provided with the pcp python bindings
        this version saves converting the C indexes to python and back again
    """

    inst = c_int()
    outAtom = pmapi.pmAtomValue()
    status = LIBPCPFAST.pcpfastExtractValues(result_p, byref(inst), byref(outAtom), vsetidx, vlistidx, dtype)
    if status < 0:
        raise pmapi.pmErr(status)

    if dtype == c_api.PM_TYPE_STRING:
        # Get pointer to C string
        c_str = c_char_p()
        memmove(byref(c_str), addressof(outAtom) + pmapi.pmAtomValue.cp.offset, sizeof(c_char_p))
        # Convert to a python string and have result point to it
        outAtom.cp = outAtom.cp
        # Free the C string
        LIBC.free(c_str)

    return outAtom.dref(dtype), inst.value
示例#22
0
文件: pcp-uptime.py 项目: wrabcak/pcp
    def execute(self):
        """ Using a PMAPI context (could be either host or archive),
            fetch and report a fixed set of values related to uptime.
        """
        metrics = ('kernel.all.uptime', 'kernel.all.nusers', 'kernel.all.load')
        pmids = self.context.pmLookupName(metrics)
        descs = self.context.pmLookupDescs(pmids)
        result = self.context.pmFetch(pmids)
        if result.contents.numpmid != len(metrics):
            raise pmapi.pmErr(PM_ERR_VALUE)

        uptime = ''

        sample_time = result.contents.timestamp.tv_sec
        time_struct = self.context.pmLocaltime(sample_time)
        uptime += print_timestamp(time_struct)

        atom = self.context.pmExtractValue(result.contents.get_valfmt(0),
                                           result.contents.get_vlist(0, 0),
                                           descs[0].contents.type, PM_TYPE_U32)
        uptime += print_uptime(atom.ul)

        atom = self.context.pmExtractValue(result.contents.get_valfmt(1),
                                           result.contents.get_vlist(1, 0),
                                           descs[1].contents.type, PM_TYPE_U32)
        uptime += print_users(atom.ul)

        averages = [None, None, None]
        for inst in range(3):
            averages[inst] = self.context.pmExtractValue(
                result.contents.get_valfmt(2),
                result.contents.get_vlist(2, inst), descs[2].contents.type,
                PM_TYPE_FLOAT)
        uptime += print_load(averages[0].f, averages[1].f, averages[2].f)
        print(uptime)
        self.context.pmFreeResult(result)
示例#23
0
    def test_errno_returns_the_error_number_that_caused_the_exception(self):
        error = pmapi.pmErr(cpmapi.PM_ERR_PMID)

        self.assertEqual(error.errno, cpmapi.PM_ERR_PMID)
示例#24
0
 def cache_mark_inactive(self):
     if self.it_numinst <= 0:
         LIBPCP_PMDA.pmdaCacheOp(self.it_indom, cpmda.PMDA_CACHE_INACTIVE)
     else:
         raise pmErr(cpmapi.PM_ERR_NYI)
示例#25
0
    def test_message_with_additional_arguments_appends_the_arguments_to_the_message(
            self):
        error = pmapi.pmErr(cpmapi.PM_ERR_PMID, "arg1", "arg2")

        self.assertEqual(error.message(),
                         "Unknown or illegal metric identifier arg1 arg2")
示例#26
0
文件: pmda.py 项目: DundalkIT/pcp
 def cache_mark_inactive(self):
     if self.it_numinst <= 0:
         LIBPCP_PMDA.pmdaCacheOp(self.it_indom, cpmda.PMDA_CACHE_INACTIVE)
     else:
         raise pmErr(cpmapi.PM_ERR_NYI)
示例#27
0
def pmdaAddLabelFlags(labels, flags):
    status = LIBPCP_PMDA.pmdaAddLabelFlags(labels, flags)
    if status < 0:
        raise pmErr(status)
    return status
示例#28
0
def pmdaAddLabels(label):
    result_p = POINTER(pmLabelSet)()
    status = LIBPCP_PMDA.pmdaAddLabels(byref(result_p), label)
    if status < 0:
        raise pmErr(status)
    return result_p
示例#29
0
    def test_message_returns_the_string_representation_of_the_error(self):
        error = pmapi.pmErr(cpmapi.PM_ERR_PMID)

        self.assertEqual(error.message(),
                         "Unknown or illegal metric identifier")
示例#30
0
def pmdaGetContext():
    status = LIBPCP_PMDA.pmdaGetContext()
    if status < 0:
        raise pmErr(status)
    return status