- GRAYBYTE UNDETECTABLE CODES -

403Webshell
Server IP : 184.154.167.98  /  Your IP : 3.145.50.254
Web Server : Apache
System : Linux pink.dnsnetservice.com 4.18.0-553.22.1.lve.1.el8.x86_64 #1 SMP Tue Oct 8 15:52:54 UTC 2024 x86_64
User : puertode ( 1767)
PHP Version : 8.2.27
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/lib64/python3.6/site-packages/pcp/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /usr/lib64/python3.6/site-packages/pcp/__pycache__/pmapi.cpython-36.pyc
3

4>�fN��
@szdZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl	m
Z
mZmZm
Z
mZmZddl	mZmZmZmZmZmZddl	mZmZmZmZmZddl	mZmZmZmZmZddl m!Z!ee!d��Z"ej#d	kr�d
ndZ$ee!e$��Z%ej&dk�re'fZ(e'Z)e*Z+ne'e)fZ(e,Z+d
d�Z-Gdd�de.�Z/Gdd�de.�Z0ej1dk�rTeZ2nej1dk�rfeZ2ne/ej3d��ej4dk�r�eZ5nej4dk�r�eZ5ne/ej3d��Gdd�de�Z6Gdd�de�Z7Gdd�de�Z8Gdd�de�Z9Gdd �d e�Z:Gd!d"�d"e�Z;Gd#d$�d$e�Z<Gd%d&�d&e�Z=Gd'd(�d(e�Z>ee>�Z?e@d)d*�ddd�e?_Ae@d+d*�ddd�e?_Be@d,d*�ddd�e?_Ce@d-d*�ddd�e?_DGd.d/�d/e�ZEeZFeZGGd0d1�d1e�ZHeeH�ZIe@d2d*�ddd�eI_Je@d3d*�ddd�eI_Ke@d4d*�ddd�eI_Le@d5d*�ddd�eI_Md6d7�ZNGd8d9�d9e�ZOGd:d;�d;e�ZPGd<d=�d=e�ZQeeQ�ZRe@d>d*�ddd�eR_Se@d?d*�ddd�eR_Te@d@d*�ddd�eR_Ue@dAd*�ddd�eR_Ve@dBd*�ddd�eR_WGdCdD�dDe�ZXee"jY_Zeeee��ge"jY_[ee"j\_Zeeee��eee��ge"j\_[ee"j]_Zge"j]_[ee"j^_Zege"j^_[ee"j__ZeedEee�ge"j__[ee"j`_Zeeee��ge"j`_[ee"ja_Zeee�ge"ja_[ede�Zbee"jc_Zeebge"jc_[ee"jd_Zge"jd_[ee"je_Zeeee�ge"je_[ee"jf_Zege"jf_[ee"jg_ZeeeH�ge"jg_[ee"jh_Zeee�eeH�ge"jh_[ee"ji_Zeeee�ge"ji_[ee"jj_Zeeee�ge"jj_[ee"jk_Zeeee��eee��ge"jk_[ee"jl_Zeege"jl_[ee"jm_Zeeee�ge"jm_[ee"jn_Zeege"jn_[ee"jo_Zege"jo_[ee"jp_Zge"jp_[ee"jq_Zege"jq_[ee"jr_Zge"jr_[ee"js_Zeeee�ge"js_[ee"jt_Zeeee�ge"jt_[ee"ju_Zeee6�ege"ju_[ee"jv_Zege"jv_[ee"jw_Zeeege"jw_[ee"jx_Zge"jx_[ee"jy_Zege"jy_[ee"jz_Zege"jz_[ee"j{_Zee�ge"j{_[ee8�e"j|_Zee
�ee8�ge"j|_[ee"j}_Zee
�ege"j}_[ee"j~_Zeee�eeeE��ge"j~_[de"j_ZeeE�ge"j_[ee"j�_ZeeE�ge"j�_[ee"j�_ZeeP�ge"j�_[ee"j�_Zee6�ge"j�_[ee"j�_Zeeee��eee��ge"j�_[ee"j�_Zeege"jl_[eGege"j�_[ee"j�_ZeGege"j�_[ee"j�_ZeeeE��ge"j�_[ee"j�_Zege"j�_[ee"j�_Zeeege"j�_[ee"j�_Zeee=�eee9�ege"j�_[ee"j�_Zeee9�ee:�ee9�ee:�ge"j�_[ee"j�_Zee:�eege"j�_[ee"j�_Zeeege"j�_[ee"j�_Zeeege"j�_[ee"j�_Zege"j�_[ee"j�_Zege"j�_[ee"j�_Zege"j�_[ee"j�_Zeeege"j�_[ee"j�_Zeege"j�_[ee"j�_Zege"j�_[ee"j�_Zege"j�_[ee"j�_Zeeege"j�_[ee"j�_Zeeege"j�_[ee"j�_Zee9�eeege"j�_[ee"j�_Zeeege"j�_[de"j�_Zeeeee=�ege"j�_[ee"j�_Zeee6�ee�ge"j�_[ee"j�_ZeeeeeeO��ee�ge"j�_[ee"j�_Zge"j�_[ee"j�_Zege"j�_[de"j�_ZeeE�ge"j�_[ee"j�_ZeeeeX��ge"j�_[ee"j�_ZeeeeX��ge"j�_[ee"j�_ZeeeeX��ge"j�_[ee"j�_ZeeeeX��ge"j�_[ee"j�_ZeeeeX��ge"j�_[ee"j�_ZeeeeX��ge"j�_[ee"j�_ZeeeX��ge"j�_[ee"j�_Zee�eeege"j�_[eeeeQ�ee�Z�ee"j�_ZeeeX��eeee�ege"j�_[de"j�_ZeeX�ege"j�_[GdFdG�dGe��Z�GdHdI�dIe��Z�ee"j�_Zee�eege"j�_[ee"j�_Zege"j�_[ee"j�_Zege"j�_[ee"j�_Zege"j�_[ee"j�_Zeeeeee9�eee�ge"j�_[ee"j�_Zeeeee�ee�ee9�eee�eee�ee�ge"j�_[ee"j�_Zeeeeeee7�ee9�eee�eee�ee�ge"j�_[ee"j�_Zeee6�ge"j�_[ee"j�_Zege"j�_[GdJdK�dKe��Z�dS)La� Wrapper module for LIBPCP - the core Performace Co-Pilot API
#
# Copyright (C) 2012-2021 Red Hat
# Copyright (C) 2009-2012 Michael T. Werner
#
# This file is part of the "pcp" module, the python interfaces for the
# Performance Co-Pilot toolkit.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

# Additional Information:
#
# Performance Co-Pilot Web Site
# https://pcp.io
#
# Performance Co-Pilot Programmer's Guide
# cf. Chapter 3. PMAPI - The Performance Metrics API
#
# EXAMPLE

    from pcp import pmapi
    import cpmapi as c_api

    # Create a pcp class
    context = pmapi.pmContext(c_api.PM_CONTEXT_HOST, "local:")

    # Get ids for number cpus and load metrics
    metric_ids = context.pmLookupName(("hinv.ncpu","kernel.all.load"))
    # Get the description of the metrics
    descs = context.pmLookupDescs(metric_ids)
    # Fetch the current value for number cpus
    results = context.pmFetch(metric_ids)
    # Extract the value into a scalar value
    atom = context.pmExtractValue(results.contents.get_valfmt(0),
                                  results.contents.get_vlist(0, 0),
                                  descs[0].contents.type,
                                  c_api.PM_TYPE_U32)
    print "#cpus=", atom.ul

    # Get the instance ids for kernel.all.load
    inst1 = context.pmLookupInDom(descs[1], "1 minute")
    inst5 = context.pmLookupInDom(descs[1], "5 minute")

    # Loop through the metric ids
    for i in range(results.contents.numpmid):
        # Is this the kernel.all.load id?
        if (results.contents.get_pmid(i) != metric_ids[1]):
            continue
        # Extract the kernel.all.load instance
        for j in range(results.contents.get_numval(i) - 1):
            atom = context.pmExtractValue(results.contents.get_valfmt(i),
                                          results.contents.get_vlist(i, j),
                                          descs[i].contents.type,
                                          c_api.PM_TYPE_FLOAT)
            value = atom.f
            if results.contents.get_inst(i, j) == inst1:
                print "load average 1=",atom.f
            elif results.contents.get_inst(i, j) == inst5:
                print "load average 5=",atom.f


    # ... or, using the fetchgroup interface:

    from pcp import pmapi
    import cpmapi as c_api

    pmfg = pmapi.fetchgroup(c_api.PM_CONTEXT_HOST, "local:")
    v = pmfg.extend_item("hinv.ncpu")
    vv = pmfg.extend_indom("kernel.all.load", c_api.PM_TYPE_FLOAT)
    vvv = pmfg.extend_event("systemd.journal.records", field="systemd.journal.field.string")
    t = pmfg.extend_timestamp()

    pmfg.fetch()
    print("time: %s" % t())
    print("number of cpus: %d" % v())
    for icode, iname, value in vv():
        print("load average %s: %f" % (iname, value()))
    for ts, line in vvv():
        print("%s : %s" % (ts, line()))
�N)�c_char�c_int�c_uint�c_long�c_char_p�c_void_p)�c_float�c_double�c_int32�c_uint32�c_int64�c_uint64)�CDLL�POINTER�	CFUNCTYPE�	Structure�Union)�	addressof�pointer�sizeof�cast�byref)�find_libraryZpcpZwin32�cZmsvcrt�3cCsbtjdkr:ttjj_tjgtjj_t	j
tjj|�ddd�Sttjj_tjgtjj_tjj|�SdS)Nr�rF)�closefd)
�sys�versionr�ctypesZ	pythonapiZPyObject_AsFileDescriptor�restypeZ	py_object�argtypes�os�fdopenrZ
PyFile_AsFile)�fileObj�r%�/usr/lib64/python3.6/pmapi.py�
pyFileToCFile�s


r'cs<eZdZ�fdd�Zdd�Zdd�Zdd�Zd	d
�Z�ZS)�pmErrcsBtt|�j|�t|�|_|r8t|dt�r8|d|_nd|_dS)Nr)�superr(�__init__�list�args�
isinstance�int�code)�selfr,)�	__class__r%r&r*�s

zpmErr.__init__cCsJd}ytj|j}Wntk
r(YnX|dkr:|j�Sd||j�fS)Nz%s %s)�c_apiZpmErrSymDictr/�KeyError�message)r0ZerrSymr%r%r&�__str__�sz
pmErr.__str__cCs^tjtj�}tj|j|tj�}t|j��}x.t	dt
|j��D]}|dt|j|�7}q<W|S)N�� )r�create_string_bufferr2�PM_MAXERRMSGLEN�LIBPCP�
pmErrStr_rr/�str�decode�range�lenr,)r0ZerrStr�result�indexr%r%r&r4�sz
pmErr.messagecCsttjtd�jj��S)N�
pmProgname)r<r�in_dllr:�valuer=)r0r%r%r&�progname�szpmErr.prognamecCs|jS)N)r/)r0r%r%r&�errno�szpmErr.errno)	�__name__�
__module__�__qualname__r*r5r4rErF�
__classcell__r%r%)r1r&r(�s

r(cs$eZdZ�fdd�Zdd�Z�ZS)�
pmUsageErrcstt|�j|�t|�|_dS)N)r)rKr*r+r,)r0r,)r1r%r&r*�szpmUsageErr.__init__cCs<x2tdt|j��D]}tjt|j|�jd��qWtj�S)Nrzutf-8)	r>r?r,r:�pmprintfr<�encoder2ZpmUsageMessage)r0rAr%r%r&r4�szpmUsageErr.message)rGrHrIr*r4rJr%r%)r1r&rK�srK��zUnexpected suseconds_t sizezUnexpected time_t sizec@sjeZdZdefdefgZddd�Zedd��Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dd�Zdd�ZdS)�timeval�tv_sec�tv_usecrcCstj|�||_||_dS)N)rr*rQrR)r0�secZusecr%r%r&r*�s
ztimeval.__init__cCsL|�}t�}t|t�s |jd�}tj|t|�t|��}|dkrHt||��|S)z7 Construct timeval from a string using pmParseInterval zutf-8r)rr-�bytesrMr:�pmParseIntervalrr()�cls�interval�tvp�errmsg�statusr%r%r&�fromInterval�s


ztimeval.fromIntervalcCsdtj|j|j�S)Nz%.3f)r2�pmtimevalToRealrQrR)r0r%r%r&r5�sztimeval.__str__cCsttj|j|j��S)N)�floatr2r\rQrR)r0r%r%r&�	__float__�sztimeval.__float__cCsttj|j|j��S)N)�complexr2r\rQrR)r0r%r%r&�__complex__�sztimeval.__complex__cCs
t|j�S)N)r.rQ)r0r%r%r&�	__index__sztimeval.__index__cCs
t|j�S)N)�longrQ)r0r%r%r&�__long__sztimeval.__long__cCs
t|j�S)N)r.rQ)r0r%r%r&�__int__sztimeval.__int__cCstjt|��dS)z9 Delay for the amount of time specified by this timeval. N)�time�sleepr])r0r%r%r&rf	sz
timeval.sleepN)rr)rGrHrI�c_time_t�
c_suseconds_t�_fields_r*�classmethodr[r5r^r`rarcrdrfr%r%r%r&rP�s

rPc@s.eZdZdefdefgZd	dd�Zdd�ZdS)
�timespecrQ�tv_nsecrcCstj|�||_||_dS)N)rr*rQrl)r0rSZnsecr%r%r&r*s
ztimespec.__init__cCsdt|j�t|j�dS)Nz%.3fge��A)r]rQrl)r0r%r%r&r5sztimespec.__str__N)rr)rGrHrIrgrrir*r5r%r%r%r&rk
s

rkc@sbeZdZdefdefdefdefdefdefdefdefd	efd
efdefgZdd
�Zdd�ZdS)�tm�tm_sec�tm_min�tm_hour�tm_mday�tm_mon�tm_year�tm_wday�tm_yday�tm_isdst�	tm_gmtoff�tm_zonec	CsR|jd}|dkrd}|jd|jd|j|j|j|j||jd|jg	}t	j
|�S)Nr6r�il)rtrsrrrqrprornrurvre�struct_time)r0ZpywdayZstlistr%r%r&rz&s
ztm.struct_timec
Csttjd�}tj|j|j|j|j|j|j	|j
|j|jt
|j�t|j��}tt
|��}tjt|�|�t|jj��j�S)N� )rr8r2ZpmMktimernrorprqrrrsrtrurvrbrwr<rxrr:�pmCtimerrDr=�rstrip)r0r@�second�timetpr%r%r&r51s
z
tm.__str__N)	rGrHrIrrrrirzr5r%r%r%r&rms
rmc@s�eZdZdZdefdefdefdefdefde	fde
fd	efgZe
jd
d�e
jdd�e
jd
d�e
jdd�e
jdd�e
jdd�e
jdd�e
jdd�e
jdd�e
jdd�e
jdd�e
jdd�e
jdd�i
Zdd�ZdS)�pmAtomValuez�Union used for unpacking metric values according to type

    Constants for specifying metric types are defined in module pmapi
    �l�ul�ll�ull�f�d�cpZvpcCs|jS)N)r�)�xr%r%r&�<lambda>PszpmAtomValue.<lambda>cCs|jS)N)r�)r�r%r%r&r�QscCs|jS)N)r�)r�r%r%r&r�RscCs|jS)N)r�)r�r%r%r&r�SscCs|jS)N)r�)r�r%r%r&r�TscCs|jS)N)r�)r�r%r%r&r�UscCs|jS)N)r�)r�r%r%r&r�VscCsdS)Nr%)r�r%r%r&r�WscCsdS)Nr%)r�r%r%r&r�XscCsdS)Nr%)r�r%r%r&r�YscCsdS)Nr%)r�r%r%r&r�ZscCsdS)Nr%)r�r%r%r&r�[scCsdS)Nr%)r�r%r%r&r�\scCsL|j||�}|tjkrHyt|jd��}Wntk
rFt|�}YnX|S)Nzutf-8)�
_atomDrefDr2�PM_TYPE_STRINGr<r=�	Exception)r0�typedrDr%r%r&�dref_s
zpmAtomValue.drefN)rGrHrI�__doc__r
rrr
rr	rrrir2Z
PM_TYPE_32ZPM_TYPE_U32Z
PM_TYPE_64ZPM_TYPE_U64Z
PM_TYPE_FLOATZPM_TYPE_DOUBLEr�ZPM_TYPE_AGGREGATEZPM_TYPE_AGGREGATE_STATICZ
PM_TYPE_EVENTZPM_TYPE_HIGHRES_EVENTZPM_TYPE_NOSUPPORTZPM_TYPE_UNKNOWNr�r�r%r%r%r&r�Bs.












r�c	@s�eZdZdZejrPdedfdedfdedfdedfdedfdedfd	ed
fgZn<d	ed
fdedfdedfdedfdedfdedfdedfgZddd
�Zdd�Z	dd�Z
dS)�pmUnitsz�
    Compiler-specific bitfields specifying scale and dimension of metric values
    Constants for specifying metric units are defined in module pmapi
    IRIX => HAVE_BITFIELDS_LTOR, gcc => not so much
    �dimSpacerN�dimTime�dimCount�
scaleSpace�	scaleTime�
scaleCount�padrOrcCs8tj|�||_||_||_||_||_||_d|_dS)Nr)	rr*r�r�r�r�r�r�r�)r0ZdimSZdimTZdimCZscaleSZscaleTZscaleCr%r%r&r*s
zpmUnits.__init__cCs tj|j|j|j|j|j|j�S)N)r2ZpmUnits_intr�r�r�r�r�r�)r0r%r%r&rd�szpmUnits.__int__cCs$tjd�}tj||d�}t|j��S)N�@)rr8r:�pmUnitsStr_rr<r=)r0�unitstrr@r%r%r&r5�s
zpmUnits.__str__N)rrrrrr)rGrHrIr�r2�HAVE_BITFIELDS_LTORrrir*rdr5r%r%r%r&r�hs&

r�c@sTeZdZdZejr2dedfdedfdedfgZndedfdedfdedfgZdS)	�pmValueBlockz�Value block bitfields for different compilers
       A value block holds the value of an instance of a metric
       pointed to by the pmValue structure, when that value is
       too large (> 32 bits) to fit in the pmValue structure
    ZvtyperOZvlen�Zvbufr6N)	rGrHrIr�r2r�rrrir%r%r%r&r��sr�c@s,eZdZdZdee�fdefgZdd�ZdS)�	valueDrefz�Union in pmValue for dereferencing the value of an instance of a metric

    For small items, e.g. a 32-bit number, the union contains the actual value
    For large items, e.g. a text string, the union points to a pmValueBlock
    �pval�lvalcCs
d|jS)Nz
value=%#lx)r�)r0r%r%r&r5�szvalueDref.__str__N)	rGrHrIr�rr�rrir5r%r%r%r&r��s

r�c@s(eZdZdZdefdefgZdd�ZdS)�pmValuez1Structure holding the value of a metric instance �instrDcCs t|j�}dt|�|jf|S)NzpmValue@%#lx inst=%d )r<rDrr�)r0�vstrr%r%r&r5�s
zpmValue.__str__N)rGrHrIr�rr�rir5r%r%r%r&r��s
r�c@sNeZdZdZdefdefdefdedfgZdd�Zd	d
�Z	e
e	ddd�ZdS)�
pmValueSeta�Structure holding a metric's list of instance values

    A performance metric may contain one or more instance values, one for each
    item that the metric concerns. For example, a metric measuring filesystem
    free space would contain one instance value for each filesystem that exists
    on the target machine. Whereas, a metric measuring free memory would have
    only one instance value, representing the total amount of free memory on
    the target system.
    �pmid�numval�valfmt�vlistr6csT�jdkrLt�j�}t�fdd�|D��}t���j�j�jf}d||SdSdS)Nrcsg|]}dt�j|��qS)z %s)r<r�)�.0�i)r0r%r&�
<listcomp>�sz&pmValueSet.__str__.<locals>.<listcomp>z+pmValueSet@%#lx id=%#lx numval=%d valfmt=%d�)r�r>r�r<rr�)r0�valsr��vsetr%)r0r&r5�s

zpmValueSet.__str__cCst|jd�S)Nr)rZ_vlist)r0r%r%r&�
vlist_read�szpmValueSet.vlist_readN)rGrHrIr�rrr�rir5r��propertyr�r%r%r%r&r��s		r�cCs|jjS)N)�contentsr�)r�r%r%r&r��sr�cCs|jjS)N)r�r�)r�r%r%r&r��scCs|jjS)N)r�r�)r�r%r%r&r��scCs|jjS)N)r�r�)r�r%r%r&r��sc@sneZdZdZdefdefdee�dfgZdd�Z	dd	�Z
d
d�Zdd
�Zdd�Z
dd�Zdd�Zdd�ZdS)�pmResultz�Structure returned by pmFetch, with a value set for each metric queried

    The vset is defined with a "fake" array bounds of 1, which can give runtime
    array bounds complaints.  The getter methods are array bounds agnostic.
    Z	timestamp�numpmidr�r6cCstj|�d|_dS)Nr)rr*r�)r0r%r%r&r*�s
zpmResult.__init__cs6t�j�}t�fdd�|D��}dt���jf|S)Ncs g|]}dt�j|j��qS)z %s)r<r�r�)r�r�)r0r%r&r��sz$pmResult.__str__.<locals>.<listcomp>zpmResult@%#lx id#=%d )r>r�r<r)r0r�r�r%)r0r&r5�s
zpmResult.__str__cCst|jtt��}||jjS)z# Return the pmid of vset[vset_idx] )rr�r�
pmValueSetPtrr�r�)r0�vset_idx�vsetptrr%r%r&�get_pmid�szpmResult.get_pmidcCs t|jttt���}||jjS)z% Return the valfmt of vset[vset_idx] )rr�rr�r�r�)r0r�r�r%r%r&�
get_valfmt�szpmResult.get_valfmtcCs t|jttt���}||jjS)z% Return the numval of vset[vset_idx] )rr�rr�r�r�)r0r�r�r%r%r&�
get_numval�szpmResult.get_numvalcCst|jttt���}||S)z Return the vset[vset_idx] )rr�rr�)r0r�r�r%r%r&�get_vset�szpmResult.get_vsetcCs t|j|�jjtt��}||S)z/ Return the vlist[vlist_idx] of vset[vset_idx] )rr�r�r�rr�)r0r��	vlist_idxZlistptrr%r%r&�	get_vlistszpmResult.get_vlistcCs|j||�jS)z8 Return the inst for vlist[vlist_idx] of vset[vset_idx] )r�r�)r0r�r�r%r%r&�get_instszpmResult.get_instN)rGrHrIr�rPrrr�rir*r5r�r�r�r�r�r�r%r%r%r&r��sr�c@s:eZdZdZdefdefdefdefdefgZdd�Zd	S)
�pmDescz"Structure describing a metric
    r��type�indom�sem�unitscCst|�|j|jf}d|S)NzpmDesc@%#lx id=%#lx type=%d)rr�r�)r0�fieldsr%r%r&r5szpmDesc.__str__N)	rGrHrIr�rrr�rir5r%r%r%r&r�s
r�cCs|jjS)N)r�r�)r�r%r%r&r�scCs|jjS)N)r�r�)r�r%r%r&r�scCs|jjS)N)r�r�)r�r%r%r&r�scCs|jjS)N)r�r�)r�r%r%r&r�scCs<Gdd�dt�}t|tt��r&|jjS|�}||_|jSdS)z�Internal function to extract an indom from a pmdesc

       Allow functions requiring an indom to be passed a pmDesc* instead
    c@s eZdZdee�fdefgZdS)zget_indom.<locals>.Valuer�r�N)rGrHrIrr�rrir%r%r%r&�Value$s
r�N)rr-rr�r�r�r�r�)�pmdescr�rDr%r%r&�	get_indomsr�c@sLeZdZdZdefdefdefdefdee�fgZdd�Ze	ddd��Z
d
S)�pmMetricSpecz.Structure describing a metric's specification �isarch�source�metric�ninstr�cs8tt�fdd�t�j���}t���j�j|f}d|S)Ncst�j|�S)N)r<r�)r�)r0r%r&r�7sz&pmMetricSpec.__str__.<locals>.<lambda>z+pmMetricSpec@%#lx src=%s metric=%s insts=%s)r+�mapr>r�rr�r�)r0Zinstsr�r%)r0r&r56szpmMetricSpec.__str__rr�cCsht|��}t�}t|t�s$|jd�}t|t�s8|jd�}tj|||t|�t|��}|dkrdt||��|S)Nzutf-8r)	rrr-rTrMr:�pmParseMetricSpecrr()rV�stringr�r�r@rYrZr%r%r&�
fromString;s






zpmMetricSpec.fromStringN)rr�)rGrHrIr�rrrrir5rjr�r%r%r%r&r�/sr�c@sNeZdZdZdefdefdefdeejfdeej	fgZ
dd�Zd	d
�ZdS)�
pmLogLabelz,Label record at the start of every log file �magicZpid_t�start�hostname�tzcCst|jj��S)z6 Return the hostname from the structure as native str )r<r�r=)r0r%r%r&�get_hostnameQszpmLogLabel.get_hostnamecCst|jj��S)z6 Return the timezone from the structure as native str )r<r�r=)r0r%r%r&�get_timezoneUszpmLogLabel.get_timezoneN)
rGrHrIr�rrPrr2�PM_LOG_MAXHOSTLENZPM_TZ_MAXLENrir�r�r%r%r%r&r�Isr�c@sDeZdZdZdedfdedfdedfdedfdedfgZd	d
�ZdS)�pmLabelz*Structure describing label's specification�name��namelenrO�flagsrD�valuelencCs|jdd|jdS)N�:�")r�rD)r0r%r%r&r5`szpmLabel.__str__N)rGrHrIr�rrir5r%r%r%r&r�Ysr�cCs|jjS)N)r�r�)r�r%r%r&r�dscCs|jjS)N)r�r�)r�r%r%r&r�escCs|jjS)N)r�r�)r�r%r%r&r�fscCs|jjS)N)r�rD)r�r%r%r&r�gscCs|jjS)N)r�r�)r�r%r%r&r�hsc	@sVeZdZdZdefdefdefdedfdedfd	ed
fdee�fde	fgZ
d
d�ZdS)�
pmLabelSetz. Structure describing label set specificationsr�Znlabels�json�jsonlenr�Zpadding�Zcompoundr6�labels�hashcCs|jdkrdS|jj�S)Nz{})r�r=)r0r%r%r&r5ts
zpmLabelSet.__str__N)rGrHrIr�rrrrr�rrir5r%r%r%r&r�js

r�r6c@sheZdZdZdd�Zdd�Zdd�Zdd	�Zeed
d
d
�Z	eed
d
d
�Z
eeed
d
�Zd�dd
�Zdd�Z
edd��Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Z d4d5�Z!d6d7�Z"d8d9�Z#d:d;�Z$d<d=�Z%d>d?�Z&d@dA�Z'dBdC�Z(dDdE�Z)dFdG�Z*dHdI�Z+dJdK�Z,dLdM�Z-dNdO�Z.dPdQ�Z/dRdS�Z0dTdU�Z1dVdW�Z2dXdY�Z3dZd[�Z4d\d]�Z5d^d_�Z6d`da�Z7dbdc�Z8ddde�Z9dfdg�Z:dhdi�Z;djdk�Z<dldm�Z=dndo�Z>dpdq�Z?drds�Z@dtdu�ZAdvdw�ZBdxdy�ZCdzd{�ZDd|d}�ZEd~d�ZFd�d��ZGd�d��ZHd�d��ZId�d��ZJd�d��ZKd�d��ZLd�d��ZMd
S)��	pmOptionsz� Command line option parsing for short and long form arguments
        Passed into pmGetOptions, pmGetContextOptions, pmUsageMessage.
    cCs|jS)N)�_mode)r0r%r%r&�_R_mode�szpmOptions._R_modecCs|jS)N)�_delta)r0r%r%r&�_R_delta�szpmOptions._R_deltacCs|jS)N)�_need_reset)r0r%r%r&�
_R_need_reset�szpmOptions._R_need_resetcCs
||_dS)N)r�)r0rDr%r%r&�
_W_need_reset�szpmOptions._W_need_resetNrcCsdtj�|dk	rtj|�|dk	r,tj|�|dkr@tj|�ntjtj�d|_tj|_d|_	dS)Nrr6F)
r2�pmResetAllOptions�pmSetShortOptions�pmSetShortUsage�pmSetOptionFlagsZPM_OPTFLAG_BOUNDARIESr��PM_MODE_INTERPr�r�)r0�
short_options�short_usager�r%r%r&r*�s

zpmOptions.__init__cCstdk	r|jrtj�dS)N)r:r�r2r�)r0r%r%r&�__del__�szpmOptions.__del__cCstj�S)z> Cross-platform --daemonize (re-parent to init) option helper )r2Z
pmServerStartr%r%r%r&�	daemonize�szpmOptions.daemonizecCstj�S)N)r2�pmGetOptionFlags)r0r%r%r&r��szpmOptions.pmGetOptionFlagscCs
tj|�S)N)r2�pmSetOptionContext)r0�contextr%r%r&r��szpmOptions.pmSetOptionContextcCs
tj|�S)N)r2r�)r0r�r%r%r&r��szpmOptions.pmSetOptionFlagscCstj�S)N)r2�pmGetOptionErrors)r0r%r%r&r��szpmOptions.pmGetOptionErrorscCstj�d}tj|�S)Nr6)r2r��pmSetOptionErrors)r0�errorsr%r%r&r��szpmOptions.pmSetOptionErrorscCs
tj|�S)N)r2r�)r0r�r%r%r&r��szpmOptions.pmSetShortUsagecCs
tj|�S)N)r2r�)r0r�r%r%r&r��szpmOptions.pmSetShortOptionscCs
tj|�S)z/ Set sample count (converts string to integer) )r2�pmSetOptionSamples)r0�countr%r%r&r��szpmOptions.pmSetOptionSamplescCs
tj|�S)z0 Set sampling interval (pmParseInterval string) )r2�pmSetOptionInterval)r0rWr%r%r&r��szpmOptions.pmSetOptionIntervalcCstj�S)a! After a pmGetOptions(3) call has been made this method
            returns a list of any remaining parameters which were
            not parsed as command line options, aka "operands".
            http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_254
        )r2�
pmGetOperands)r0r%r%r&r�szpmOptions.pmGetOperandscCs
tj|�S)N)r2�pmGetNonOptionsFromList)r0�argvr%r%r&r�sz!pmOptions.pmGetNonOptionsFromListcCs
tj|�S)N)r2r)r0rr%r%r&�pmNonOptionsFromList�szpmOptions.pmNonOptionsFromListcCs
tj|�S)a� Handle individual command line options, outside of the PCP
            "standard" set (or overridden).

            For every non-standard or overridden option, this callback
            will be called with the short option character (as an int)
            or zero for long-option only, and the usual getopts global
            state (optind, opterr, optopt, optarg, and index - all int
            except optarg which is a str).
        )r2�pmSetOptionCallback)r0�funcr%r%r&r�s
zpmOptions.pmSetOptionCallbackcCs
tj|�S)az Allow a "standard" PCP option to be overridden.

            For every option parsed, this callback is called and it may
            return zero, meaning continue with processing the option in
            the standard way, or non-zero, meaning the caller wishes to
            override and interpret the option differently.
            Callback input: int, output: int
        )r2�pmSetOverrideCallback)r0rr%r%r&r	s	zpmOptions.pmSetOverrideCallbackcCs|dkrd}tj|||||�S)a& Add long option into the set of supported long options

            Pass in the option name (str), whether it takes an argument (int),
            its short option form (str), and two usage message hints (argname
            (str) and message (str) - see pmGetOptions(3) for details).
        Nr�)r2�pmSetLongOption)r0Zlong_optZhas_argZ	short_optZargnamer4r%r%r&rszpmOptions.pmSetLongOptioncCs
tj|�S)z> Add a new section heading into the long option usage message )r2�pmSetLongOptionHeader)r0Zheadingr%r%r&rszpmOptions.pmSetLongOptionHeadercCs
tj|�S)z> Add some descriptive text into the long option usage message )r2�pmSetLongOptionText)r0�textr%r%r&r	#szpmOptions.pmSetLongOptionTextcCstj�S)z4 Add support for -A/--align into PMAPI monitor tool )r2�pmSetLongOptionAlign)r0r%r%r&r'szpmOptions.pmSetLongOptionAligncCstj�S)z6 Add support for -a/--archive into PMAPI monitor tool )r2�pmSetLongOptionArchive)r0r%r%r&r+sz pmOptions.pmSetLongOptionArchivecCstj�S)z4 Add support for -D/--debug into PMAPI monitor tool )r2�pmSetLongOptionDebug)r0r%r%r&r
/szpmOptions.pmSetLongOptionDebugcCsdS)z Unimplemented Nr%)r0r%r%r&�pmSetLongOptionGuiMode3sz pmOptions.pmSetLongOptionGuiModecCstj�S)z3 Add support for -h/--host into PMAPI monitor tool )r2�pmSetLongOptionHost)r0r%r%r&r7szpmOptions.pmSetLongOptionHostcCstj�S)z8 Add support for -H/--hostsfile into PMAPI monitor tool )r2�pmSetLongOptionHostsFile)r0r%r%r&r;sz"pmOptions.pmSetLongOptionHostsFilecCstj�S)z9 Add support for -K/--spec-local into PMAPI monitor tool )r2�pmSetLongOptionSpecLocal)r0r%r%r&r?sz"pmOptions.pmSetLongOptionSpecLocalcCstj�S)z9 Add support for -L/--local-PMDA into PMAPI monitor tool )r2�pmSetLongOptionLocalPMDA)r0r%r%r&rCsz"pmOptions.pmSetLongOptionLocalPMDAcCstj�S)z5 Add support for -O/--origin into PMAPI monitor tool )r2�pmSetLongOptionOrigin)r0r%r%r&rGszpmOptions.pmSetLongOptionOrigincCsdS)z Unimplemented Nr%)r0r%r%r&�pmSetLongOptionGuiPortKsz pmOptions.pmSetLongOptionGuiPortcCstj�S)z4 Add support for -S/--start into PMAPI monitor tool )r2�pmSetLongOptionStart)r0r%r%r&rOszpmOptions.pmSetLongOptionStartcCstj�S)z6 Add support for -s/--samples into PMAPI monitor tool )r2�pmSetLongOptionSamples)r0r%r%r&rSsz pmOptions.pmSetLongOptionSamplescCstj�S)z5 Add support for -T/--finish into PMAPI monitor tool )r2�pmSetLongOptionFinish)r0r%r%r&rWszpmOptions.pmSetLongOptionFinishcCstj�S)z7 Add support for -t/--interval into PMAPI monitor tool )r2�pmSetLongOptionInterval)r0r%r%r&r[sz!pmOptions.pmSetLongOptionIntervalcCstj�S)z6 Add support for -V/--version into PMAPI monitor tool )r2�pmSetLongOptionVersion)r0r%r%r&r_sz pmOptions.pmSetLongOptionVersioncCstj�S)z7 Add support for -Z/--timezone into PMAPI monitor tool )r2�pmSetLongOptionTimeZone)r0r%r%r&rcsz!pmOptions.pmSetLongOptionTimeZonecCstj�S)z7 Add support for -z/--hostzone into PMAPI monitor tool )r2�pmSetLongOptionHostZone)r0r%r%r&rgsz!pmOptions.pmSetLongOptionHostZonecCstj�S)z3 Add support for -?/--help into PMAPI monitor tool )r2�pmSetLongOptionHelp)r0r%r%r&rkszpmOptions.pmSetLongOptionHelpcCstj�S)z8 Add support for --archive-list into PMAPI monitor tool )r2�pmSetLongOptionArchiveList)r0r%r%r&rosz$pmOptions.pmSetLongOptionArchiveListcCstj�S)z9 Add support for --archive-folio into PMAPI monitor tool )r2�pmSetLongOptionArchiveFolio)r0r%r%r&rssz%pmOptions.pmSetLongOptionArchiveFoliocCstj�S)z5 Add support for --container into PMAPI monitor tool )r2�pmSetLongOptionContainer)r0r%r%r&rwsz"pmOptions.pmSetLongOptionContainercCstj�S)z5 Add support for --host-list into PMAPI monitor tool )r2�pmSetLongOptionHostList)r0r%r%r&r {sz!pmOptions.pmSetLongOptionHostListcCstj�S)N)r2�pmGetOptionContext)r0r%r%r&r!szpmOptions.pmGetOptionContextcCstj�S)N)r2�pmGetOptionHosts)r0r%r%r&r"�szpmOptions.pmGetOptionHostscCstj�S)N)r2�pmGetOptionArchives)r0r%r%r&r#�szpmOptions.pmGetOptionArchivescCstj�}|dkrdStj|�S)N)r2�pmGetOptionAlign_optargrPr[)r0Z	alignmentr%r%r&�pmGetOptionAlignment�szpmOptions.pmGetOptionAlignmentcCs"tj�}|dkrdSt|tj��S)N)r2ZpmGetOptionStart_secrPZpmGetOptionStart_usec)r0rSr%r%r&�pmGetOptionStart�szpmOptions.pmGetOptionStartcCstj�S)N)r2r$)r0r%r%r&�pmGetOptionAlignOptarg�sz pmOptions.pmGetOptionAlignOptargcCstj�S)N)r2ZpmGetOptionFinish_optarg)r0r%r%r&�pmGetOptionFinishOptarg�sz!pmOptions.pmGetOptionFinishOptargcCs"tj�}|dkrdSt|tj��S)N)r2ZpmGetOptionFinish_secrPZpmGetOptionFinish_usec)r0rSr%r%r&�pmGetOptionFinish�szpmOptions.pmGetOptionFinishcCs"tj�}|dkrdSt|tj��S)N)r2ZpmGetOptionOrigin_secrPZpmGetOptionOrigin_usec)r0rSr%r%r&�pmGetOptionOrigin�szpmOptions.pmGetOptionOrigincCs"tj�}|dkrdSt|tj��S)N)r2ZpmGetOptionInterval_secrPZpmGetOptionInterval_usec)r0rSr%r%r&�pmGetOptionInterval�szpmOptions.pmGetOptionIntervalcCstj�S)N)r2�pmGetOptionSamples)r0r%r%r&r,�szpmOptions.pmGetOptionSamplescCstj�dkrdSdS)NrFT)r2�pmGetOptionHostZone)r0r%r%r&r-�szpmOptions.pmGetOptionHostZonecCstj�S)N)r2�pmGetOptionTimezone)r0r%r%r&r.�szpmOptions.pmGetOptionTimezonecCstj�S)N)r2�pmGetOptionContainer)r0r%r%r&r/�szpmOptions.pmGetOptionContainercCstj�dkrdSdS)NrFT)r2�pmGetOptionLocalPMDA)r0r%r%r&r0�szpmOptions.pmGetOptionLocalPMDAcCs
tj|�S)N)r2�pmSetOptionArchive)r0�archiver%r%r&r1�szpmOptions.pmSetOptionArchivecCs
tj|�S)N)r2�pmSetOptionArchiveList)r0�archivesr%r%r&r3�sz pmOptions.pmSetOptionArchiveListcCs
tj|�S)N)r2�pmSetOptionArchiveFolio)r0Zfolior%r%r&r5�sz!pmOptions.pmSetOptionArchiveFoliocCs
tj|�S)N)r2�pmSetOptionContainer)r0Z	containerr%r%r&r6�szpmOptions.pmSetOptionContainercCs
tj|�S)N)r2�pmSetOptionHost)r0�hostr%r%r&r7�szpmOptions.pmSetOptionHostcCs
tj|�S)N)r2�pmSetOptionHostList)r0�hostsr%r%r&r9�szpmOptions.pmSetOptionHostListcCs
tj|�S)N)r2�pmSetOptionSpecLocal)r0�specr%r%r&r;�szpmOptions.pmSetOptionSpecLocalcCstj�S)N)r2�pmSetOptionLocalPMDA)r0r%r%r&r=�szpmOptions.pmSetOptionLocalPMDA)NNr)NrGrHrIr�r�r�r�r�r��mode�delta�
need_resetr*r��staticmethodr�r�r�r�r�r�r�r�r�r�rrrrrrrr	rrr
rrrrrrrrrrrrrrrrrrr r!r"r#r%r&r'r(r)r*r+r,r-r.r/r0r1r3r5r6r7r9r;r=r%r%r%r&r��s�
	r�c@seZdZdZdd�Zdd�Zdd�Zeeddd�Zeeddd�Z	eeddd�Z
ejd	fd
d�Z
dd
�Zed�dd��Zedd��Zdd�Zdd�Zdd�Zdd�Zd�dd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zed)d*��Zd+d,�Zd-d.�Z ej!fd/d0�Z"ej!fd1d2�Z#d�d3d4�Z$d5d6�Z%d7d8�Z&d9d:�Z'd;d<�Z(d=d>�Z)d?d@�Z*dAdB�Z+edCdD��Z,dEdF�Z-dGdH�Z.dIdJ�Z/dKdL�Z0dMdN�Z1dOdP�Z2edQdR��Z3edSdT��Z4edUdV��Z5dWdX�Z6dYdZ�Z7d[d\�Z8ed]d^��Z9d_d`�Z:dadb�Z;dcdd�Z<dedf�Z=dgdh�Z>didj�Z?dkdl�Z@d�dndo�ZAdpdq�ZBdrds�ZCdtdu�ZDdvdw�ZEdxdy�ZFdzd{�ZGd|d}�ZHed~d��ZIed�d�d���ZJed�d�d���ZKed�d���ZLed�d���ZMed�d���ZNed�d���ZOed�d���ZPed�d���ZQed�d���ZRed�d���ZSed�d���ZTed�d���ZUed�d���ZVed�d���ZWed�d���ZXed�d���ZYed�d���ZZed�d���Z[ed�d���Z\ed�d���Z]ed�d���Z^ed�d���Z_ed�d���Z`ed�d���Zaed�d���Zbed�d�d���Zced�d���Zded�d���Zeed�d���Zfed�d���Zged�d�d���Zhed�d���Zied�dÄ�Zjeejkfd�dń�Zled�dDŽ�Zmd�dɄZndS)��	pmContexta�Defines a metrics source context (e.g. host, archive, etc) to operate on

    pmContext(c_api.PM_CONTEXT_HOST,"local:")
    pmContext(c_api.PM_CONTEXT_ARCHIVE,"FILENAME")

    This object defines a PMAPI context, and its methods wrap calls to PMAPI
    library functions. Detailled information about those C library functions
    can be found in the following document.

    SGI Document: 007-3434-005
    Performance Co-Pilot Programmer's Guide
    Section 3.7 - PMAPI Procedural Interface, pp. 67

    Detailed information about the underlying data structures can be found
    in the same document.

    Section 3.4 - Performance Metric Descriptions, pp. 59
    Section 3.5 - Performance Metric Values, pp. 62
    cCs|jS)N)�_type)r0r%r%r&�_R_type�szpmContext._R_typecCs|jS)N)�_target)r0r%r%r&�	_R_target�szpmContext._R_targetcCs|jS)N)�_ctx)r0r%r%r&�_R_ctx�szpmContext._R_ctxNzlocal:cCs^||_||_tj|_|r0t|t�r0|jd�}n|}tj	||�|_|jdkrZt
|j|g��dS)Nzutf-8r)rCrEr2�PM_ERR_NOCONTEXTrGr-rTrMr:�pmNewContextr()r0r��targetr�r%r%r&r*s
zpmContext.__init__cCs$tdk	r |jtjkr tj|j�dS)N)r:rGr2rI�pmDestroyContext)r0r%r%r&r�szpmContext.__del__rc	Cs�|dks|dkr,d|_tj|�r$t�|j�}|tjkrH|j�}||}nZ|tjkrd|j�}||}n>|tj	krtd}n.|j
�dk	r�|j�}||}tj}n
tj}d}|||�}tj|j|j
|j�r�t�|S)a� Helper interface, simple PCP monitor argument parsing.

            Take argv list, create a context using pmGetOptions(3)
            and standard options default values like local: etc
            based on the contents of the list.

            Caller should have already registered any options of
            interest using the option family of interfaces, i.e.
            pmSetShortOptions, pmSetLongOption*, pmSetOptionFlags,
            pmSetOptionCallback, and pmSetOptionOverrideCallback.

            When the MULTI/MIXED pmGetOptions flags are being used,
            the typed/index parameters can be used to setup several
            contexts based on the given command line parameters.
        NrTzlocal:)r@r2ZpmGetOptionsFromListrKr!�PM_CONTEXT_ARCHIVEr#�PM_CONTEXT_HOSTr"�PM_CONTEXT_LOCALr*ZpmSetContextOptions�ctxr>r?)	rV�optionsrr�rAr4r�r:r�r%r%r&�fromOptionss.






zpmContext.fromOptionscCs>d}tj�r,tj}|jtj�|j�d}nFtj�rTtj}|jtj�|j�d}ntj�rrtj}|jtj�d}|s�d|kr�tj}|j|�|jtj�nD|dkr�tj}|j	|�|jtj�ntj}|j
�|jtj�d}|tjko�|�rx$td|jd��D]}|j
|��qW|j�}|j|tjB�tj�||fS)zF Helper to set connection options and to get context/source for pmfg. Nr�/�@�;)r2r#rMr�r"rNr0rOr1r7r=�filter�splitr;r�r�ZPM_OPTFLAG_DONEZpmEndOptions)rQr�Z	speclocalr�r<r�r%r%r&�set_connect_optionsKsB

zpmContext.set_connect_optionscs�tt���tj|j�}|dkr&t|��t|t�s:|jd�}tj	|t
���}|dkrZt|��|dkr�tt�fdd�t
|���}tj��ndS|S)zsPMAPI - Return names of children of the given PMNS node NAME
        tuple names = pmGetChildren("kernel")
        rzutf-8cst�|j��S)N)r<r=)r�)�	offspringr%r&r��sz)pmContext.pmGetChildren.<locals>.<lambda>N)rrr:�pmUseContextrPr(r-rTrM�
pmGetChildrenrr+r�r>�LIBC�free)r0r�rZ�childLr%)rYr&r[zs


zpmContext.pmGetChildrencs�tt���tt���tj|j�}|dkr0t|��t|t�sD|j	d�}tj
|t��t���}|dkrjt|��|dkr�tt
�fdd�t|���}tt
�fdd�t|���}tj��tj��ndS||fS)z�PMAPI - Return names and status of children of the given metric NAME
        (tuple names,tuple status) = pmGetChildrenStatus("kernel")
        rzutf-8cst�|j��S)N)r<r=)r�)rYr%r&r��sz/pmContext.pmGetChildrenStatus.<locals>.<lambda>cst�|�S)N)r.)r�)�	childstatr%r&r��sN)NN)rrrr:rZrPr(r-rTrM�pmGetChildrenStatusrr+r�r>r\r])r0r�rZr^ZstatLr%)r_rYr&r`�s$




zpmContext.pmGetChildrenStatuscCs8tj|j�}|dkrt|��tj�}|dkr4t|��|S)zUPMAPI - Return the namespace location type
        loc = pmGetPMNSLocation()
        r)r:rZrPr(�pmGetPMNSLocation)r0rZr%r%r&ra�szpmContext.pmGetPMNSLocationcCsNtj|j�}|dkrt|��t|t�s0|jd�}tj|�}|dkrJt|��|S)zTPMAPI - Load a local namespace
        status = pmLoadNameSpace("filename")
        rzutf-8)r:rZrPr(r-rTrM�pmLoadNameSpace)r0�filenamerZr%r%r&rb�s


zpmContext.pmLoadNameSpacec
Cs4tj|j�}|dkrt|��t|ttf�r0d}nt|�}t|�}t|t�r`t|j	d��|d<n\t|t�rxt|�|d<nDxBt
|�D]6\}}t||t�r�t|�||<q�t|j	d��||<q�Wt|�}tt|t
t�gtj_tj|||�}|dkr�t||��|dk�r0||k�r0dd�t||�D�}	ttj|	��|S)z�PMAPI - Lookup pmIDs from a list of metric names nameA

        c_uint pmid [] = pmLookupName("MetricName")
        c_uint pmid [] = pmLookupName(("MetricName1", "MetricName2", ...))
        rr6zutf-8cSsg|]\}}|tjkr|�qSr%)r2Z
PM_ID_NULL)r�r�r�r%r%r&r��sz*pmContext.pmLookupName.<locals>.<listcomp>)r:rZrPr(r-r<rTr?rrM�	enumeraterrr�pmLookupNamer!�zipr2ZPM_ERR_NAME)
r0ZnameAZrelaxedrZ�n�namesr�r��pmidAZbadLr%r%r&re�s0




zpmContext.pmLookupNamecsntj|j�}|dkrt|��tt���tj|t���}|dkrFt|��tt	�fdd�t
|���}tj��|S)zvPMAPI - Return list of all metric names having this identical PMID
        tuple names = pmNameAll(metric_id)
        rcst�|j��S)N)r<r=)r�)�nameA_pr%r&r��sz%pmContext.pmNameAll.<locals>.<lambda>)
r:rZrPr(rr�	pmNameAllrr+r�r>r\r])r0r�rZ�nameLr%)rjr&rk�s

zpmContext.pmNameAllcCs^t�}tj|j�}|dkr"t|��tj|t|��}|dkrBt|��|j}tj	|�t
|j��S)zYPMAPI - Return a metric name from a PMID
        name = pmNameID(self.metric_id)
        r)rr:rZrPr(�pmNameIDrrDr\r]r<r=)r0r�r�rZr@r%r%r&rm�s
zpmContext.pmNameIDcCs<tj|j�}|dkrt|��tj||�}|dkr8t|��dS)z�PMAPI - Scan namespace, depth first, run CALLBACK at each node
        status = pmTraversePMNS("kernel", traverse_callback)
        rN)r:rZrPr(r2ZpmnsTraverse)r0r��callbackrZr%r%r&�pmTraversePMNSszpmContext.pmTraversePMNScCs8tj|j�}|dkrt|��tj�}|dkr4t|��dS)zmPMAPI - Unloads a local PMNS, if one was previously loaded
        pm.pmUnLoadNameSpace("NameSpace")
        rN)r:rZrPr(�pmUnloadNameSpace)r0rZr%r%r&�pmUnLoadNameSpaceszpmContext.pmUnLoadNameSpacecCs�t|t�s|jd�}t|t�s(|jd�}t�}tj||t|��}|dkrlt|jj	��}t
j|�tt
j|��tj|j�}|dkr�t|��dS)z�PMAPI - Register a derived metric name and definition
        pm.pmRegisterDerived("MetricName", "MetricName Expression")
        zutf-8rN)r-rTrMrr:�pmRegisterDerivedMetricrr<rDr=r\r]r(r2�PM_ERR_CONV�pmReconnectContextrP)r0r��exprrYr@r
rZr%r%r&�pmRegisterDeriveds




zpmContext.pmRegisterDerivedcCsNt|t�s|jd�}tj|�}|dkr.t|��tj|j�}|dkrJt|��dS)zuPMAPI - Register derived metric names and definitions from a file
        pm.pmLoadDerivedConfig("FileName")
        zutf-8rN)r-rTrMr:�pmLoadDerivedConfigr(rtrP)r0ZfnamerZr%r%r&rw)s


zpmContext.pmLoadDerivedConfigcCs tj�}|dk	rt|j��SdS)z�PMAPI - Return an error message if the pmRegisterDerived(3) metric
        definition cannot be parsed
        pm.pmRegisterDerived()
        N)r:�pmDerivedErrStrr<r=)r@r%r%r&rx8szpmContext.pmDerivedErrStrcCs`tj|j�}|dkrt|��tjtt��}t|t	t��}t
|�}tj||�}|dkr\t|��|S)zvPMAPI - Lookup a metric description structure from a pmID

        pmDesc* pmdesc = pmLookupDesc(c_uint pmid)
        r)r:rZrPr(rr8rr�rrr�pmLookupDesc)r0Zpmid_prZZdescbuf�descr�r%r%r&ryFszpmContext.pmLookupDescc	Cs�tj|j�}|dkrt|��t|t�r:td�}||d<n0tt|��}x t|�D]\}}t|�||<qRWt	j
t|�tt��}t
|tt��}tjt|�||�}|dkr�t|��tt�t|��}x$tt|��D]}t||�||<q�W|S)z�PMAPI - Lookup metric description structures from pmIDs

        (pmDesc* pmdesc)[] = pmLookupDescs(c_uint pmid[N])
        (pmDesc* pmdesc)[] = pmLookupDescs(c_uint pmid)
        rr6)r:rZrPr(r-�
integer_typesrr?rdrr8rr�rr�
pmLookupDescsr>r)	r0Zpmids_prZ�pmidsr�r�ZdescsbufZdesclist�descsr%r%r&r|Xs$


zpmContext.pmLookupDescscCsdt�}tj|j�}|dkr"t|��tjt|�|t|��}|dkrHt|��|j}t	j
|�t|j��S)z|PMAPI - Lookup the description of a metric's instance domain

        "instance" = pmLookupInDomText(pmDesc pmdesc)
        r)
rr:rZrPr(�pmLookupInDomTextr�rrDr\r]r<r=)r0r��kind�bufrZr@r%r%r&rws
zpmContext.pmLookupInDomTextcCsXt�}tj|j�}|dkr"t|��tj||t|��}|dkrDt|��|j}tj	|�|S)zePMAPI - Lookup the description of a metric from its pmID
        "desc" = pmLookupText(pmid)
        r)
rr:rZrPr(�pmLookupTextrrDr\r])r0r�r�r�rZr
r%r%r&r��s
zpmContext.pmLookupTextcs�|dkr|dkrttjd��tt���tt���tj|j�}|dkrLt|��|dkr\t	|�}tj
|t��t���}|dkr�t|��|dkr�tt
�fdd�t|���}tt
�fdd�t|���}tj��tj��nd}d}||fS)z�PMAPI - Lookup the list of instances from an instance domain PMDESCP or indom
        ([instance1, instance2...] [name1, name2...]) pmGetInDom(pmDesc pmdesc)
        Nzinvalid argumentsrcst�|jdd��S)N�ascii�ignore)r<r=)r�)rjr%r&r��sz&pmContext.pmGetInDom.<locals>.<lambda>cst�|�S)N)r.)r�)�instA_pr%r&r��s)r(r2ZPM_ERR_GENERICrrrr:rZrPr��
pmGetInDomrr+r�r>r\r])r0�pmdescpr�rZrl�instLr%)r�rjr&r��s(


zpmContext.pmGetInDomcCsxtj|j�}|dkrt|��i}|j|d�\}}|dks@|dkrD|St|�}x&t|�D]}|j||||i�qVW|S)z1 helper to return dict of inst:name for an indom r)r�N)r:rZrPr(r�r?r>�update)r0r�rZZretDr�rlrgr�r%r%r&�pmGetInDomDict�szpmContext.pmGetInDomDictcCsTtj|j�}|dkrt|��t|t�s0|jd�}tjt|�|�}|dkrPt|��|S)z�PMAPI - Lookup the instance id with the given NAME in the indom

        c_uint instid = pmLookupInDom(pmDesc pmdesc, "Instance")
        rzutf-8)	r:rZrPr(r-rTrM�
pmLookupInDomr�)r0r�r�rZr%r%r&r��s

zpmContext.pmLookupInDomcCsv|tjkrdSt�}tj|j�}|dkr0t|��tjt|�|t	|��}|dkrVt|��|j
}tj|�t
|jdd��S)z�PMAPI - Lookup the text name of an instance in an instance domain

        "string" = pmNameInDom(pmDesc pmdesc, c_uint instid)
        �
PM_IN_NULLrr�r�)r2r�rr:rZrPr(�pmNameInDomr�rrDr\r]r<r=)r0r�Zinstval�name_prZr@r%r%r&r��s

zpmContext.pmNameInDomcCsdS)z�PMAPI - NOOP - Establish a new PMAPI context (done in constructor)

        This is unimplemented. A new context is established when a pmContext
        object is created.
        Nr%)r0r�r�r%r%r&rJ�szpmContext.pmNewContextcCsdS)z�PMAPI - NOOP - Destroy a PMAPI context (done in destructor)

        This is unimplemented. The context is destroyed when the pmContext
        object is destroyed.
        Nr%)r0�handler%r%r&rL�szpmContext.pmDestroyContextcCs8tj|j�}|dkrt|��tj�}|dkr4t|��|S)zfPMAPI - Duplicate the current PMAPI Context

        This supports copying a pmContext object
        r)r:rZrPr(�pmDupContext)r0rZr%r%r&r��szpmContext.pmDupContextcCsdS)z�PMAPI - NOOP - Set the PMAPI context to that identified by handle

        This is unimplemented. Context changes are handled by the individual
        methods in a pmContext class instance.
        Nr%)r0r�r%r%r&rZszpmContext.pmUseContextcCstj�}|dkrt|��|S)zcPMAPI - Returns the handle of the current PMAPI context
        context = pmWhichContext()
        r)r:�pmWhichContextr()rZr%r%r&r�	szpmContext.pmWhichContextcCs�t|t�r"d}t|�}||d<nN|dks0|r@d}tt��}n0t|�}t|�}xt|�D]\}}|||<q\Wtj|j�}|dkr�t	|��tj
t|�||�}|dkr�t	|��|S)z�PMAPI - add instances to list that will be collected from indom

        status = pmAddProfile(pmDesc pmdesc, c_uint instid)
        r6rN)r-r.rrr?rdr:rZrPr(�pmAddProfiler�)r0r�r��numinst�instArArDrZr%r%r&r�s$



zpmContext.pmAddProfilecCs�|dks|rd}tt��}n0t|�}t|�}xt|�D]\}}|||<q:Wtj|j�}|dkrjt|��tjt	|�||�}|dkr�t|��|S)z�PMAPI - delete instances from list to be collected from indom

        status = pmDelProfile(pmDesc pmdesc, c_uint inst)
        status = pmDelProfile(pmDesc pmdesc, [c_uint inst])
        Nr)
rrr?rdr:rZrPr(�pmDelProfiler�)r0r�r�r�r�rArDrZr%r%r&r�,s
zpmContext.pmDelProfilecCsRtj|j�}|dkrt|��d}|dk	r0t|�}tj|||�}|dkrNt|��|S)z|PMAPI - set interpolation mode for reading archive files
        code = pmSetMode(c_api.PM_MODE_INTERP, timeval, 0)
        rN)r:rZrPr(r�	pmSetMode)r0r>ZtimeValr?rZZwhenr%r%r&r�BszpmContext.pmSetModecCs tj|j�}|dkrt|��|S)z�PMAPI - Reestablish the context connection

        Unlike the underlying PMAPI function, this method takes no parameter.
        This method simply attempts to reestablish the the context belonging
        to its pmContext instance object.
        r)r:rtrPr()r0rZr%r%r&rtQszpmContext.pmReconnectContextcCs,tj}tj|�}tj|j||�}t|j��S)z�PMAPI - Lookup the hostname for the given context

        This method simply returns the hostname for the context belonging to
        its pmContext instance object.

        "hostname" = pmGetContextHostName()
        )	r2r�rr8r:�pmGetContextHostName_rrPr<r=)r0Zbuflen�bufferr@r%r%r&�pmGetContextHostName]s
zpmContext.pmGetContextHostNamecCs8tj|j�}|dkrt|��tj�}|dkr4t|��|S)z5PMAPI - Query and set the current reporting timezone r)r:rZrPr(�pmNewContextZone)r0rZr%r%r&r�mszpmContext.pmNewContextZonecCs2t|t�s|jd�}tj|�}|dkr.t|��|S)z:PMAPI - Create new zone handle and set reporting timezone zutf-8r)r-rTrMr:�	pmNewZoner()r�rZr%r%r&r�ws


zpmContext.pmNewZonecCstj|�}|dkrt|��|S)z,PMAPI - Sets the current reporting timezone r)r:�	pmUseZoner()Z	tz_handlerZr%r%r&r��s
zpmContext.pmUseZonecCs6t�}tjt|��}|dkr$t|��|j}t|j��S)z-PMAPI - Query the current reporting timezone r)rr:�pmWhichZonerr(rDr<r=)Ztz_prZr�r%r%r&r��szpmContext.pmWhichZonecCsFtj|j�}|dkrt|��t�}tt|��}tjt|�t|��|S)z;PMAPI - convert the date and time for a reporting timezone r)	r:rZrPr(rmrrb�pmLocaltimer)r0�secondsrZr@rr%r%r&r��szpmContext.pmLocaltimecCsPtj|j�}|dkrt|��tjd�}tt|��}tjt	|�|�t
|jj��S)z:PMAPI - format the date and time for a reporting timezone rr{)
r:rZrPr(rr8rrbr|rr<rDr=)r0r�rZr@rr%r%r&r|�s
zpmContext.pmCtimecCsPtt��}tj|j�}|dkr&t|��tjt|�|t|��}|dkrLt|��|S)zkPMAPI - Fetch pmResult from the target source

        pmResult* pmresult = pmFetch(c_uint pmid[])
        r)	rr�r:rZrPr(�pmFetchr?r)r0ri�result_prZr%r%r&r��s
zpmContext.pmFetchcCstj|�dS)zgPMAPI - Free a result previously allocated by pmFetch
        pmFreeResult(pmResult* pmresult)
        N)r:�pmFreeResult)r�r%r%r&r��szpmContext.pmFreeResultcCsHt|�gtj_tj|j�}|dkr*t|��tj|�}|dkrDt|��|S)zoPMAPI - Set values on target source, inverse of pmFetch
        pmresult = pmStore(pmResult* pmresult)
        r)r�r:�pmStorer!rZrPr()r0r@rZr%r%r&r��s
zpmContext.pmStorecCsDt�}tj|j�}|dkr"t|��tjt|��}|dkr@t|��|S)z]PMAPI - Get the label record from the archive
        loglabel = pmGetArchiveLabel()
        r)r�r:rZrPr(�pmGetArchiveLabelr)r0ZloglabelrZr%r%r&r��szpmContext.pmGetArchiveLabelcCsDt�}tj|j�}|dkr"t|��tjt|��}|dkr@t|��|S)zAPMAPI - Get the last recorded timestamp from the archive
        r)rPr:rZrPr(�pmGetArchiveEndr)r0rXrZr%r%r&r��szpmContext.pmGetArchiveEndcs�tt���tt���tj|j�}|dkr0t|��t|�}tj|t	��t	���}|dkr^t|��|dkr�t
t�fdd�t|���}t
t�fdd�t|���}t
j��t
j��nd}d}||fS)z�PMAPI - Get the instance IDs and names for an instance domain

        ((instance1, instance2...) (name1, name2...)) pmGetInDom(pmDesc pmdesc)
        rcst�|jdd��S)Nr�r�)r<r=)r�)rjr%r&r��sz-pmContext.pmGetInDomArchive.<locals>.<lambda>cst�|�S)N)r.)r�)r�r%r&r��sN)rrrr:rZrPr(r��pmGetInDomArchiverr+r�r>r\r])r0r�rZr�rlr�r%)r�rjr&r��s"


zpmContext.pmGetInDomArchivecCsTtj|j�}|dkrt|��t|t�s0|jd�}tjt|�|�}|dkrPt|��|S)z�PMAPI - Lookup the instance id with the given name in the indom

        c_uint instid = pmLookupInDomArchive(pmDesc pmdesc, "Instance")
        rzutf-8)	r:rZrPr(r-rTrM�pmLookupInDomArchiver�)r0r�r�rZr%r%r&r�s

zpmContext.pmLookupInDomArchivecCslt�}tj|j�}|dkr"t|��t|�}tj||t|��}|dkrLt|��|j}t	j
|�t|jdd��S)z�PMAPI - Lookup the text name of an instance in an instance domain

        "string" = pmNameInDomArchive(pmDesc pmdesc, c_uint instid)
        rr�r�)
rr:rZrPr(r��pmNameInDomArchiverrDr\r]r<r=)r0r�r�r�rZr�r@r%r%r&r�s
zpmContext.pmNameInDomArchivecCsHtt��}tj|j�}|dkr&t|��tjt|��}|dkrDt|��|S)zbPMAPI - Fetch measurements from the target source

        pmResult* pmresult = pmFetch()
        r)rr�r:rZrPr(�pmFetchArchiver)r0r�rZr%r%r&r�!s
zpmContext.pmFetchArchive�cCs4i}|dk	r0|jdkr0|jdk	r0tj|jj��}|S)zk return a dict of a pmLabelSet, i.e. {name: value, ...}
            flags arg is currently ignored
        Nr)r�r��loadsr=)r0�lsetr��retr%r%r&�pmlabelset_to_dict/szpmContext.pmlabelset_to_dictcCs�i}tj|j�}|dkr t|��|j|�}|j�}|rF|jtj|i�|j	tj
|��}|rj|jtj|i�|jtj
kr�|j|j�}|r�|jtj|i�|j|�}|r�|jtj|i�|j|�}|r�|jtj|i�|S)z�PMAPI - Get all labels for a single metric, excluding instance
           level labels (use pmGetInstancesLabels for those).
           Return dict of {type: {name: value, ...}, ...}
        r)r:rZrPr(ry�pmGetContextLabelsr�r2ZPM_LABEL_CONTEXT�pmGetDomainLabels�pmID_domainZPM_LABEL_DOMAINr��
PM_INDOM_NULL�pmGetInDomLabelsZPM_LABEL_INDOM�pmGetClusterLabelsZPM_LABEL_CLUSTER�pmGetItemLabelsZ
PM_LABEL_ITEM)r0r�r�rZrzr�r%r%r&�pmLookupLabels:s,


zpmContext.pmLookupLabelscCs�i}tj|j�}|dkr t|��|tjkr.|Stt��}tj|t	|��}|dkrXt|��x>t
|�D]2}||}|jdk	rb|j|j
tj|jj��i�qbW|dkr�tj||�|S)z�PMAPI - Get instance level labels for all instances in indom
           return a dict {instid: {name: value, ...}, ...}
        rN)r:rZrPr(r2r�rr��pmGetInstancesLabelsrr>r�r�r�r�r=�pmFreeLabelSets)r0r�ZinstlabelsDrZr�r�r�r%r%r&r�\s"


 zpmContext.pmGetInstancesLabelscCsptt��}tj|j�}|dkr&t|��tj|t|��}|dkrFt|��|dkrRiS|j|d�}|j	|d�|S)z�PMAPI - Get labels of a given metric identifier
           On success, this returns a dict of the labels in a single pmLabelSet
        rr6)
rr�r:rZrPr(r�rr�r�)r0r�r�rZr�r%r%r&r�ss
zpmContext.pmGetItemLabelscCsptt��}tj|j�}|dkr&t|��tj|t|��}|dkrFt|��|dkrRiS|j|d�}|j	|d�|S)z�PMAPI - Get labels of a given metric cluster
           On success, this returns a dict of the labels in a single pmLabelSet
        rr6)
rr�r:rZrPr(r�rr�r�)r0r�r�rZr�r%r%r&r��s
zpmContext.pmGetClusterLabelscCs~i}tt��}tj|j�}|dkr*t|��|tjkr8|Stj|t	|��}|dkrXt|��|dkrz|j
|d�}|j|d�|S)z�PMAPI - Get labels of a given instance domain
           On success, this returns a dict of the labels in a single pmLabelSet
        rr6)rr�r:rZrPr(r2r�r�rr�r�)r0r�ZindomLabelsDr�rZr%r%r&r��s

zpmContext.pmGetInDomLabelscCsptt��}tj|j�}|dkr&t|��tj|t|��}|dkrFt|��|dkrRiS|j|d�}|j	|d�|S)z�PMAPI - Get labels of a given performance domain
           On success, this returns a dict of the labels in a single pmLabelSet
        rr6)
rr�r:rZrPr(r�rr�r�)r0Zdomainr�rZr�r%r%r&r��s
zpmContext.pmGetDomainLabelscCsntt��}tj|j�}|dkr&t|��tjt|��}|dkrDt|��|dkrPiS|j|d�}|j	|d�|S)z�PMAPI - Get labels of the current context
           On success, this returns a dict of the labels in a single pmLabelSet
        rr6)
rr�r:rZrPr(r�rr�r�)r0r�rZr�r%r%r&r��s
zpmContext.pmGetContextLabelscCs�t|t�s|g}n|}tjtj�}tt|��}x&t|�D]\}}t|j	d��||<q:Wt
j|t|�|t|��}|dkr�t|��|j
}t|jdd��S)z3PMAPI - Merges string labels into a string
        zutf-8rr�r�)r-r+rr8r2�PM_MAXLABELJSONLENrr?rdrMr:�
pmMergeLabelsr(rDr<r=)r�ZlabelsLr��arg_arrr�ZlabelrZr@r%r%r&r��s
zpmContext.pmMergeLabelscCstjtj�}|dkrdd�}t|�}|dkr4d|_n�t|t�sF|g}n:g}x4|D],}t|t�rj|j|�qP|dk	rP|j	|�qPWt
t�t|��}x*t
|�D]\}}	tt|	�t
t��||<q�Wtt|�t�}tj|t|�|t|�||�}
|
dkr�t|
��|j}t|jdd��S)z�PMAPI - Merges list of pmLabelSets based on labelSets hierarchy into
           a string. Each list element may also be either a list or None.
        NcSsdS)Nr6r%)r��y�zr%r%r&r��sz,pmContext.pmMergeLabelSets.<locals>.<lambda>s{}rr�r�)rr8r2r��mergeLabelSetsCB_typerDr-r+�extend�appendrr�r?rdrrrrr:�pmMergeLabelSetsr(r<r=)�	labelSetsrn�argr�Zcb_funcZ
labelSetsLr�r�r�ZlabelsetrZr@r%r%r&r��s0


zpmContext.pmMergeLabelSetsr6cCs<x6t|�D]*}tt||�tt��}|r
tj|d�q
WdS)z{PMAPI - Free the pmLabelSets memory. The labelsets argument is
           an array of nsets pmLabelSet structures.
        r6N)r>rrrr�r:r�)r�Znsetsr�r�r%r%r&r�	szpmContext.pmFreeLabelSetscCs6t|t�s|jd�}tj|�}|dkr*|St|j��S)z@PMAPI - Return single value from environment or pcp config file zutf-8N)r-rTrMr:�pmGetOptionalConfigr<r=)Zvariabler@r%r%r&�pmGetConfig	s


zpmContext.pmGetConfigcCs(tjtj�}tj||tj�}t|j��S)z4PMAPI - Convert an error code to a readable string  )rr8r2r9r:r;r<r=)r/Zerrstrr@r%r%r&�pmErrStr	szpmContext.pmErrStrcCs0t�}tj|||t|�|�}|dkr,t|��|S)aOPMAPI - Extract a value from a pmValue struct and convert its type

        pmAtomValue = pmExtractValue(results.contents.get_valfmt(i),
                                     results.contents.get_vlist(i, 0),
                                     descs[i].contents.type,
                                     c_api.PM_TYPE_FLOAT)
        r)r�r:�pmExtractValuerr()r�r�ZintypeZouttype�outAtomrZr%r%r&r�	s	
zpmContext.pmExtractValuecCsft|t�rt�}d|_||_n|}t�}tj|t|�t||j	j
�t|�t|��}|dkrbt|��|S)z�PMAPI - Convert a value to a different scale

        pmAtomValue = pmConvScale(c_api.PM_TYPE_FLOAT, pmAtomValue,
                                            pmDesc*, 3, c_api.PM_SPACE_MBYTE)
        r6r)r-r.r�r�r�r�r:�pmConvScalerr�r�r()ZinTypeZinAtomrzZ
metric_idxZoutUnitsZpmunitsr�rZr%r%r&r�/	s
zpmContext.pmConvScalecCs$tjd�}tj||d�}t|j��S)z2PMAPI - Convert units struct to a readable string r�)rr8r:r�r<r=)r�r�r@r%r%r&�
pmUnitsStrD	s
zpmContext.pmUnitsStrcCs$tjd�}tj||d�}t|j��S)z3PMAPI - Convert double value to fixed-width string rO)rr8r:�
pmNumberStr_rr<r=)rDZnumstrr@r%r%r&�pmNumberStrK	s
zpmContext.pmNumberStrcCstj|||�}|S)z/PMAPI - build a pmID from domain, cluster, item)r:�
pmID_build)r�rr�r@r%r%r&r�R	szpmContext.pmID_buildcCstj|�}|S)z&PMAPI - return domain number from pmID)r:r�)r�r@r%r%r&r�X	s
zpmContext.pmID_domaincCstj|�}|S)z'PMAPI - return cluster number from pmID)r:�pmID_cluster)r�r@r%r%r&r�^	s
zpmContext.pmID_clustercCstj|�}|S)z$PMAPI - return item number from pmID)r:�	pmID_item)r�r@r%r%r&r�d	s
zpmContext.pmID_itemcCs$tjd�}tj||d�}t|j��S)z,PMAPI - Convert a pmID to a readable string r{)rr8r:�	pmIDStr_rr<r=)r�Zpmidstrr@r%r%r&�pmIDStrj	s
zpmContext.pmIDStrcCstj||�}|S)z-PMAPI - build an indom from domain and serial)r:�
pmInDom_build)r��sr@r%r%r&r�q	szpmContext.pmInDom_buildcCstj|�}|S)z'PMAPI - return domain number from indom)r:�pmInDom_domain)r�r@r%r%r&r�w	s
zpmContext.pmInDom_domaincCstj|�}|S)z'PMAPI - return serial number from indom)r:�pmInDom_serial)r�r@r%r%r&r�}	s
zpmContext.pmInDom_serialcCs(tjd�}tjt|�|d�}t|j��S)zpPMAPI - Convert an instance domain ID  to a readable string
        "indom" = pmGetInDom(pmDesc pmdesc)
        r{)rr8r:�pmInDomStr_rr�r<r=)r�Zindomstrr@r%r%r&�
pmInDomStr�	s
zpmContext.pmInDomStrcCs$tjd�}tj||d�}t|j��S)zwPMAPI - Convert a performance metric type to a readable string
        "type" = pmTypeStr(c_api.PM_TYPE_FLOAT)
        r{)rr8r:�pmTypeStr_rr<r=)r�Ztypestrr@r%r%r&�	pmTypeStr�	s
zpmContext.pmTypeStrcCs*tjd�}tjt|�||d�}t|j��S)zoPMAPI - Convert a value atom to a readable string
        "value" = pmAtomStr(atom, c_api.PM_TYPE_U32)
        �`)rr8r:�pmAtomStr_rrr<r=)Zatomr�Zatomstrr@r%r%r&�	pmAtomStr�	s
zpmContext.pmAtomStrcCs$tjd�}tj||d�}t|j��S)z}PMAPI - Convert a performance metric semantic to a readable string
        "string" = pmSemStr(c_api.PM_SEM_COUNTER)
        r{)rr8r:�
pmSemStr_rr<r=)r�Zsemstrr@r%r%r&�pmSemStr�	s
zpmContext.pmSemStrcCsFtjt|�t|jj|jj�t|jj�t|jj|jj	|�|�dS)zzPMAPI - Print the value of a metric
        pmPrintValue(file, value, pmdesc, vset_index, vlist_index, min_width)
        N)
r:�pmPrintValuer'rr�r�r�r�rr�)r$r@Zptyper�r�Z	min_widthr%r%r&r��	s


zpmContext.pmPrintValuecCstj�}|dkrt|��|S)z7PMAPI - flush the internal buffer shared with pmprintf r)r:�pmflushr()rZr%r%r&r��	szpmContext.pmflushcGs$tj|f|��}|dkr t|��dS)z=PMAPI - append message to internal buffer for later printing rN)r:rLr()Zfmtr,rZr%r%r&rL�	szpmContext.pmprintfcCstj|�dS)z@PMAPI - sort all metric instances in result returned by pmFetch N)r:�pmSortInstances)r�r%r%r&r��	szpmContext.pmSortInstancescCstj|�dfS)z�PMAPI - parse a textual time interval into a timeval struct
        (timeval_ctype, '') = pmParseInterval("time string")
        r�)rPr[)rWr%r%r&rU�	szpmContext.pmParseIntervalr�cCstj|||�dfS)z�PMAPI - parse a textual metric specification into a struct
        (result, '') = pmParseMetricSpec("hinv.ncpu", 0, "localhost")
        r�)r�r�)r�r�r�r%r%r&r��	szpmContext.pmParseMetricSpeccCs�t|ttf�sttjt|���t|t�s2|jd�}t�}t	�}t
�}tj|t
|�t
|�t
|��}|dkr�t|jj��}tj|�t||��||jfS)Nzutf-8r)r-rT�	text_typer(r2rsr<rMr�rr	r:�pmParseUnitsStrrrDr=r\r])r�r@rYZ
multiplierrZr
r%r%r&r��	s



zpmContext.pmParseUnitsStrcCs|j�S)z� Delay for a specified amount of time (timeval).
            Useful for implementing tools that do metric sampling.
            Single arg is timeval in tuple returned from pmParseInterval().
        )rf)rXr%r%r&�pmtimevalSleep�	szpmContext.pmtimevalSleepcCsttjtd�jj��S)NrB)r<rrCr:rDr=r%r%r%r&rB�	szpmContext.pmPrognamecCstjtd�j|@rdSdS)N�pmDebugTF)rrCr:rD)r�r%r%r&r��	szpmContext.pmDebugcCs�|dkr|rdnd}n |r,tj|j��j}n
tj�j}|r@tjntj}tj|}|r�t|d�}t|dd�}|dkr�|d7}|t|�7}|r�|dt|�7}|S)z; Get current timezone offset string using POSIX convention rr6g �@i�<�+r�)	reZ	localtimer*rvZaltzone�timezoneZtznamer.r<)rQZset_dst�dst�offsetr�Z	offset_hrZ
offset_minr%r%r&�get_current_tz�	s 

zpmContext.get_current_tzcsd�sd�fdd�d	D�kr dSd�kr6�jd�dn�jd�d}d�krPdnd}d|t|�S)
zC Convert POSIX timezone offset string to human readable UTC offset Tcsg|]}|�k�qSr%r%)r�r)r�r%r&r�
sz4pmContext.posix_tz_to_utc_offset.<locals>.<listcomp>r��-zUTC+0r6ZUTC)r�r�)rWr<)r�r�Zsignr%)r�r&�posix_tz_to_utc_offset
s
$z pmContext.posix_tz_to_utc_offsetcCsx|j�r.|j�tjd<tj�tj|j��nF|j�rNtj�tjd<tj�n&tj	|�}|tjd<tj�tj|�dS)z  Set timezone for a Python tool ZTZN)
r.r"�environreZtzsetrBr�r-r�r�)rQr�r%r%r&�set_timezone
s


zpmContext.set_timezonecCs�|tjjd�}|tjkr>|j|j|jddd	d
}n�|tjkrl|j|j|jdddd}nd|tjkr�|j|j|jddd
d}n6|tj	kr�|j|j|jdddd}nt
d��|S)z6 Convert datetime value to seconds of given precision rg8@g �@g$@ry�g�?zUnsupported precision requestedg��.Ag��.Ag��.Ag@�@g��.Ag��.Ag@�@)�datetimeZ
fromtimestampr2�PM_TIME_SECZmicrosecondsr�Zdays�PM_TIME_MSECZPM_TIME_USECZPM_TIME_NSEC�
ValueError)rDZ	precisionZtdtZtstr%r%r&�datetime_to_secs'
s
$
$
$
$zpmContext.datetime_to_secscCsr|s
|rtj}d}nPtj}d}|j|krB|j}|tjtj�O}n$|jd|jd}|tjtj�O}|t|�fS)z! Get mode and step for pmSetMode ri�i�)	r2ZPM_MODE_FORWr�rQZ
PM_XTB_SETr�rRr�r.)r2�interpolrWr>�stepZsecs_in_24_daysr%r%r&�
get_mode_step7
s

zpmContext.get_mode_stepcCsZtj|j�}|dkrt|��|j|�|jtjkrVtj	|||�\}}|j
||j�|�dS)z Common execution preparation rN)r:rZrPr(r�r�r2rMrBr�r�r*)r0rQr2r�rWrZr>r�r%r%r&�prepare_executeH
s
zpmContext.prepare_execute)rr)r)NN)r�)NN)r6)rr����)Nr�)orGrHrIr�rDrFrHr�r�rKrPr2rNr*r�rjrRrArXr[r`rarbrerkrmrorqrvrwrxryr|ZPM_TEXT_ONELINErr�r�r�r�r�rJrLr�rZr�r�r�r�rtr�r�r�r�r�r�r|r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rLr�rUr�r�r�rBr�r�r�r�r�r�r�r�r%r%r%r&rB�s�//
#









"#	
						rBc@s�eZdZdZGdd�de�ZGdd�de�ZGdd�de�ZGdd	�d	e�ZGd
d�de	�Z
ejdfd
d�Z
dd�Zdd�Zd!dd�Zd"dd�Zdd�Zd#dd�Zdd�Zdd �ZdS)$�
fetchgroupa�Defines a PMAPI fetchgroup.

    This class wraps the pmFetchGroup set of C PMAPI functions (q.v.)
    in an object-oriented manner.

    Each instance of this class represents one fetchgroup, in which
    interest in several metrics (individual or indoms) is registered.
    Each registration results in an function-like object that may be
    called to decode that metric's value(s).  Errors are signalled
    with exceptions rather than result integers.  Strings are all
    UTF-8 encoded.
    c@s eZdZdZdd�Zdd�ZdS)zfetchgroup.fetchgroup_itemz�
        An internal class to receive value/status for a single item.
        It may be called as if it were a function object to decode
        the embedded pmAtomValue, which was set at the most recent
        .fetch() call.
        cCsttj�|_||_t�|_dS)z8Allocate a single instance to receive a fetchgroup item.N)rr2ZPM_ERR_VALUE�sts�pmtyper�rD)r0r�r%r%r&r*�
sz#fetchgroup.fetchgroup_item.__init__cCs&|jjdkrt|jj��|jj|j�S)z>Retrieve a converted value of a fetchgroup item, if available.r)r�rDr(r�r�)r0r%r%r&�__call__�
sz#fetchgroup.fetchgroup_item.__call__N)rGrHrIr�r*r�r%r%r%r&�fetchgroup_item�
sr�c@s eZdZdZdd�Zdd�ZdS)zfetchgroup.fetchgroup_timestampz�
        An internal class to receive value for a single timestamp.
        It may be called as if it were a function object to decode
        the timestamp, which was set at the most recent
        .fetch() call, into a datetime object.
        cCst�|_||_dS)z=Allocate a single instance to receive a fetchgroup timestamp.N)rPrDrP)r0rPr%r%r&r*�
sz(fetchgroup.fetchgroup_timestamp.__init__c	CsL|jj|jj�}t|jj�}tj|jd|jd|j	|j
|j|j|d�}|S)z�
            Retrieve a converted value of a timestamp, if available.  Use
            pmLocaltime() to convert to a datetime object.
            ilr6N)
rPr�rDrQr.rRr�rsrrrqrprorn)r0�ts�us�dtr%r%r&r��
s
z(fetchgroup.fetchgroup_timestamp.__call__N)rGrHrIr�r*r�r%r%r%r&�fetchgroup_timestamp�
src@s eZdZdZdd�Zdd�ZdS)zfetchgroup.fetchgroup_indomaq
        An internal class to receive value/status for an indom of
        items.  It may be called as if it were a function object to
        create an list of tuples containing instance-code/-name/value
        information.  Each value is a function object that decodes
        the embedded pmAtomValue, which was set at the most recent
        fetch() call.
        cCsZt|}t|}t|}t|}t�|_|�|_||_|�|_|�|_|�|_	t�|_
dS)z8Allocate a single instance to receive a fetchgroup item.N)rr�rrr��stssr��values�icodes�inames�num)r0r�r�stss_t�values_tZicodes_tZinames_tr%r%r&r*�
sz$fetchgroup.fetchgroup_indom.__init__cs|g}�jjdkrt�jj��xZt�jj�D]J}dd��|j�j|�j|r\�j|jd�nd��fdd�|�f�q*W|S)z>Retrieve a converted value of a fetchgroup item, if available.rcSs.|j|dkrt|j|��|j|j|j�S)Nr)rr(rr�r�)r0r�r%r%r&�
decode_one�
sz8fetchgroup.fetchgroup_indom.__call__.<locals>.decode_onezutf-8Ncs���fdd�S)Ncs
����S)Nr%r%)rr�r0r%r&r��
szHfetchgroup.fetchgroup_indom.__call__.<locals>.<lambda>.<locals>.<lambda>r%)r�)rr0)r�r&r��
sz6fetchgroup.fetchgroup_indom.__call__.<locals>.<lambda>)	r�rDr(r>rr�rrr=)r0�vvr�r%)rr0r&r��
sz$fetchgroup.fetchgroup_indom.__call__N)rGrHrIr�r*r�r%r%r%r&�fetchgroup_indom�
sr
c@s eZdZdZdd�Zdd�ZdS)zfetchgroup.fetchgroup_eventak
        An internal class to receive value/status for an
        event record field.  It may be called as if it were a function
        object to create an list of tuples containing timestamp/value
        information.  Each value is a function object that decodes
        the embedded pmAtomValue, which was set at the most recent
        fetch() call.
        cCsPt|}t|}t|}t�|_|�|_||_|�|_|�|_t�|_	||_
dS)z8Allocate a single instance to receive a fetchgroup item.N)rr�rkr�rr��timesrrrrP)r0r�rrPr	r
Z
timespec_tr%r%r&r*�
sz$fetchgroup.fetchgroup_event.__init__c
s�g}�jjdkrt�jj��x�t�jj�D]|}dd���jj�j|j�}t	�j|j
�d}tj|jd|j
d|j|j|j|j|d�}|j|��fdd	�|�f�q*W|S)
z>Retrieve a converted value of a fetchgroup item, if available.rcSs.|j|dkrt|j|��|j|j|j�S)Nr)rr(rr�r�)r0r�r%r%r&r�
sz8fetchgroup.fetchgroup_event.__call__.<locals>.decode_onei�ilr6Ncs���fdd�S)Ncs
����S)Nr%r%)rr�r0r%r&r�szHfetchgroup.fetchgroup_event.__call__.<locals>.<lambda>.<locals>.<lambda>r%)r�)rr0)r�r&r�sz6fetchgroup.fetchgroup_event.__call__.<locals>.<lambda>)r�rDr(r>rrPr�rrQr.rlr�rsrrrqrprornr�)r0rr�rrrr%)rr0r&r��
sz$fetchgroup.fetchgroup_event.__call__N)rGrHrIr�r*r�r%r%r%r&�fetchgroup_event�
s
rc@s eZdZdZdd�Zdd�ZdS)zfetchgroup.pmContext_borrowedz�
        An internal class for accessing the private PMAPI context
        belonging to a fetchgroup.  It works just like a pmContext,
        except it overrides the constructor/destructor to reflect
        the "borrowed" state of the context.
        cCs||_||_||_dS)z/Override pmContext ctor to eschew pmNewContext.N)rGrCrE)r0rPr�rKr%r%r&r*sz&fetchgroup.pmContext_borrowed.__init__cCs tdk	r|jtjkrtj|_dS)z3Override pmContext ctor to eschew pmDestroyContext.N)r2rGrI)r0r%r%r&r�sz%fetchgroup.pmContext_borrowed.__del__N)rGrHrIr�r*r�r%r%r%r&�pmContext_borrowedsrzlocal:cCsjt�|_g|_|tjkr$|dkr$d}tjt|j�||jd��}|dkrNt	|��t
jtj|j�||�|_
dS)z%Create a fetchgroup from a pmContext.Nr�zutf-8r)r�pmfg�itemsr2rOr:�pmCreateFetchGrouprrMr(r�r�pmGetFetchGroupContextrP)r0r�rKr�r%r%r&r* szfetchgroup.__init__cCs@tdk	r0|jjdk	r0tj|j�}|dkr0t|��|jdd�=dS)z?Destroy the fetchgroup.  Drop references to fetchgroup_* items.Nr)r:rrD�pmDestroyFetchGroupr(r)r0r�r%r%r&r�,s
zfetchgroup.__del__cCs|jS)z�
        Return the private pmContext used by the fetchgroup.
        WARNING: mutation of this context by other PMAPI functions
        may disrupt fetchgroup functionality.
        )rP)r0r%r%r&�get_context4szfetchgroup.get_contextNc		Cs�|dkrttj��|dkr>|jj|�}|jj|�}|dj}tj|�}t	j
|jt|r`|j
d�nd�t|rt|j
d�nd�t|r�|j
d�nd�t|j�t|�t|j��}|dkr�t|��|jj|�|S)z�Extend the fetchgroup with a single metric.  Infer type if
        necessary.  Convert scale/rate if appropriate/requested.
        Requires a specified instance if metric has an instance
        domain.
        Nrzutf-8)r(rF�EINVALrPrer|r�r�r�r:�pmExtendFetchGroup_itemrrrMrrDrr�rr�)	r0r��mtype�scale�instancer}r~�vr�r%r%r&�extend_item<s"

zfetchgroup.extend_item�dc	
Cs
|dks|dkrttj��|dkrF|jj|�}|jj|�}|dj}tj||�}t	j
|jt|rj|j
d�nd�t|r~|j
d�nd�tt|j�tt��tt|j�tt��tt|j�tt��t|�tt|j�tt��t|�t|j�t|j��}|dkr�t|��|jj|�|S)z�Extend the fetchgroup with up to @maxnum instances of a metric.
        (Metrics without instances are also accepted.)  Infer type if
        necessary.  Convert scale/rate if appropriate/requested.
        Nrzutf-8)r(rFrrPrer|r�r�r
r:�pmExtendFetchGroup_indomrrrMrrrrrrrr�rrrr�rr�)	r0r�rr�maxnumr}r~rr�r%r%r&�extend_indomWs(
zfetchgroup.extend_indomcCs@tj|j�}tj|jt|j��}|dkr0t|��|j	j
|�|S)z.Extend the fetchgroup with a timestamp query. r)r�rrPr:�pmExtendFetchGroup_timestamprrrDr(rr�)r0rr�r%r%r&�extend_timestampsszfetchgroup.extend_timestampcCs&|dks|dkrttj��|dkrF|jj|�}|jj|�}|dj}tj|||j�}	t	j
|jt|rn|j
d�nd�t|r�|j
d�nd�t|r�|j
d�nd�t|r�|j
d�nd�tt|	j�tt��tt|	j�tt��t|�tt|	j�tt��t|�t|	j�t|	j��}
|
dk�rt|
��|jj|	�|	S)z�Extend the fetchgroup with up to @maxnum instances of the given
        field of the given event metric's records.  Infer type if necessary.
        Convert scale if appropriate/requested.
        Nrzutf-8)r(rFrrPrer|r�r�rr:�pmExtendFetchGroup_eventrrrMrrrrrkrr�rrrrr�rr�)r0r�ZfieldZftyperrr r}r~rr�r%r%r&�extend_event}s*

zfetchgroup.extend_eventcCs tj|j�}|dkrt|��|S)z?Fetch all the metrics in this fetchgroup and update all values.r)r:�pmFetchGrouprr()r0r�r%r%r&�fetch�szfetchgroup.fetchcCs2tj|j�}|jdd�=|dkr(t|��g|_dS)z>Clear all the metrics in this fetchgroup ready to start again.Nr)r:�pmClearFetchGrouprrr()r0r�r%r%r&�clear�s
zfetchgroup.clear)NNNN)NNNr)NNNNNr)rGrHrIr��objectr�rr
rrBrr2rNr*r�rrr!r#r%r'r)r%r%r%r&r�z
s)-



r�)�r�r"rrerFr�r�Zcpmapir2rrrrrrrrr	r
rrr
rrrrrrrrrrZctypes.utilrr:�platformZ	libc_namer\rr.r{rbr<r�Zunicoder'r�r(rKZPM_SIZEOF_SUSECONDS_TrhrsZPM_SIZEOF_TIME_TrgrPrkrmr�r�r�r�r�r�r�r�r�r�r�r�r�ZpmIDZpmInDomr�Z	pmDescPtrr�r�r�r�r�r�r�r�Z
pmLabelPtrr�r�r�rDr�r�r[r r!r`rarbrerkrmZtraverseCB_typerorprrrwryr|rr�r�r�r�rJrLr�rZr�r�r�r�rtr�r�r�r�r�r�r|r�r�r�r�r�r�r�r�r�r�r;r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rUr�r�rLr�r�r�r�r�r�r�r�r�r�r�r�r*r�rBrrr(rrrr$r"r&r�r%r%r%r&�<module>Ys6  !+)&*0

















F






Youez - 2016 - github.com/yon3zu
LinuXploit