This file is indexed.

/usr/lib/python2.7/dist-packages/pcp/pmi.py is in python-pcp 3.10.8build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# pylint: disable=C0103
"""Wrapper module for libpcp_import - Performace Co-Pilot Log Import API
#
# Copyright (C) 2012-2015 Red Hat.
#
# 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.
#

# Example use of this module for creating a PCP archive:

        import math
        import time
        import pmapi
        from pcp import pmi

        # Create a new archive
        log = pmi.pmiLogImport("loadtest")
        log.pmiSetHostname("www.abc.com")
        log.pmiSetTimezone("EST-10")

        # Add a metric with an instance domain
        domain = 60  # Linux kernel
        pmid = log.pmiID(domain, 2, 0)
        indom = log.pmiInDom(domain, 2)
        units = log.pmiUnits(0, 0, 0, 0, 0, 0)
        log.pmiAddMetric("kernel.all.load", pmid, pmapi.PM_TYPE_FLOAT,
                         indom, pmapi.PM_SEM_INSTANT, units)
        log.pmiAddInstance(indom, "1 minute", 1)
        log.pmiAddInstance(indom, "5 minute", 5)
        log.pmiAddInstance(indom, "15 minute", 15)

        # Create a record with a timestamp
        log.pmiPutValue("kernel.all.load", "1 minute", "%f" % 0.01)
        log.pmiPutValue("kernel.all.load", "5 minute", "%f" % 0.05)
        log.pmiPutValue("kernel.all.load", "15 minute", "%f" % 0.15)
        timetuple = math.modf(time.time())
        useconds = int(timetuple[0] * 1000000)
        seconds = int(timetuple[1])
        log.pmiWrite(seconds, useconds)
        del log
"""

from pcp.pmapi import pmID, pmInDom, pmUnits, pmResult
from cpmi import pmiErrSymDict, PMI_MAXERRMSGLEN

import ctypes
from ctypes import cast, c_int, c_char_p, POINTER

# Performance Co-Pilot PMI library (C)
LIBPCP_IMPORT = ctypes.CDLL(ctypes.util.find_library("pcp_import"))

##
# PMI Log Import Services

LIBPCP_IMPORT.pmiDump.restype = None
LIBPCP_IMPORT.pmiDump.argtypes = None

LIBPCP_IMPORT.pmiID.restype = pmID
LIBPCP_IMPORT.pmiID.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int]

LIBPCP_IMPORT.pmiInDom.restype = pmInDom
LIBPCP_IMPORT.pmiInDom.argtypes = [ctypes.c_int, ctypes.c_int]

LIBPCP_IMPORT.pmiUnits.restype = pmUnits
LIBPCP_IMPORT.pmiUnits.argtypes = [
        ctypes.c_int, ctypes.c_int, ctypes.c_int,
        ctypes.c_int, ctypes.c_int, ctypes.c_int]

LIBPCP_IMPORT.pmiErrStr_r.restype = c_char_p
LIBPCP_IMPORT.pmiErrStr_r.argtypes = [c_int, c_char_p, c_int]

LIBPCP_IMPORT.pmiStart.restype = c_int
LIBPCP_IMPORT.pmiStart.argtypes = [c_char_p, c_int]

LIBPCP_IMPORT.pmiUseContext.restype = c_int
LIBPCP_IMPORT.pmiUseContext.argtypes = [c_int]

LIBPCP_IMPORT.pmiEnd.restype = c_int
LIBPCP_IMPORT.pmiEnd.argtypes = None

LIBPCP_IMPORT.pmiSetHostname.restype = c_int
LIBPCP_IMPORT.pmiSetHostname.argtypes = [c_char_p]

LIBPCP_IMPORT.pmiSetTimezone.restype = c_int
LIBPCP_IMPORT.pmiSetTimezone.argtypes = [c_char_p]

LIBPCP_IMPORT.pmiAddMetric.restype = c_int
LIBPCP_IMPORT.pmiAddMetric.argtypes = [
        c_char_p, pmID, c_int, pmInDom, c_int, pmUnits]

LIBPCP_IMPORT.pmiAddInstance.restype = c_int
LIBPCP_IMPORT.pmiAddInstance.argtypes = [pmInDom, c_char_p, c_int]

LIBPCP_IMPORT.pmiPutValue.restype = c_int
LIBPCP_IMPORT.pmiPutValue.argtypes = [c_char_p, c_char_p, c_char_p]

LIBPCP_IMPORT.pmiGetHandle.restype = c_int
LIBPCP_IMPORT.pmiGetHandle.argtypes = [c_char_p, c_char_p]

LIBPCP_IMPORT.pmiPutValueHandle.restype = c_int
LIBPCP_IMPORT.pmiPutValueHandle.argtypes = [c_int, c_char_p]

LIBPCP_IMPORT.pmiWrite.restype = c_int
LIBPCP_IMPORT.pmiWrite.argtypes = [c_int, c_int]

LIBPCP_IMPORT.pmiPutResult.restype = c_int
LIBPCP_IMPORT.pmiPutResult.argtypes = [POINTER(pmResult)]

#
# definition of exception classes
#

class pmiErr(Exception):
    '''
    Encapsulation for PMI interface error code
    '''
    def __str__(self):
        error_code = self.args[0]
        try:
            error_symbol = pmiErrSymDict[error_code]
            error_string = ctypes.create_string_buffer(PMI_MAXERRMSGLEN)
            error_string = LIBPCP_IMPORT.pmiErrStr_r(error_code,
                                        error_string, PMI_MAXERRMSGLEN)
        except KeyError:
            error_symbol = error_string = ""
        return "%s %s" % (error_symbol, error_string)


#
# class LogImport
#
# This class wraps the PMI (Log Import) library functions
#

class pmiLogImport(object):
    """Defines a PCP Log Import archive context
       This is used to create a PCP archive from an external source
    """

    ##
    # property read methods

    def read_path(self):
        """ Property for archive path """
        return self._path
    def read_ctx(self):
        """ Property for log import context """
        return self._ctx

    ##
    # property definitions

    path = property(read_path, None, None, None)
    ctx = property(read_ctx, None, None, None)

    ##
    # overloads

    def __init__(self, path, inherit = 0):
        if type(path) != type(b''):
            path = path.encode('utf-8')
        self._path = path        # the archive path (file name)
        self._ctx = LIBPCP_IMPORT.pmiStart(c_char_p(path), inherit)
        if self._ctx < 0:
            raise pmiErr(self._ctx)

    def __del__(self):
        if LIBPCP_IMPORT:
            LIBPCP_IMPORT.pmiUseContext(self._ctx)
            LIBPCP_IMPORT.pmiEnd()
        self._ctx = -1

    ##
    # PMI Log Import Services

    def pmiSetHostname(self, hostname):
        """PMI - set the source host name for a Log Import archive """
        status = LIBPCP_IMPORT.pmiUseContext(self._ctx)
        if status < 0:
            raise pmiErr(status)
        if type(hostname) != type(b''):
            hostname = hostname.encode('utf-8')
        status = LIBPCP_IMPORT.pmiSetHostname(c_char_p(hostname))
        if status < 0:
            raise pmiErr(status)
        return status

    def pmiSetTimezone(self, timezone):
        """PMI - set the source timezone for a Log Import archive
        """
        status = LIBPCP_IMPORT.pmiUseContext(self._ctx)
        if status < 0:
            raise pmiErr(status)
        if type(timezone) != type(b''):
            timezone = timezone.encode('utf-8')
        status = LIBPCP_IMPORT.pmiSetTimezone(c_char_p(timezone))
        if status < 0:
            raise pmiErr(status)
        return status

    @staticmethod
    def pmiID(domain, cluster, item):
        """PMI - construct a pmID data structure (helper routine) """
        return LIBPCP_IMPORT.pmiID(domain, cluster, item)

    @staticmethod
    def pmiInDom(domain, serial):
        """PMI - construct a pmInDom data structure (helper routine) """
        return LIBPCP_IMPORT.pmiInDom(domain, serial)

    @staticmethod
    def pmiUnits(dim_space, dim_time, dim_count,
                        scale_space, scale_time, scale_count):
        # pylint: disable=R0913
        """PMI - construct a pmiUnits data structure (helper routine) """
        return LIBPCP_IMPORT.pmiUnits(dim_space, dim_time, dim_count,
                                       scale_space, scale_time, scale_count)

    def pmiAddMetric(self, name, pmid, typed, indom, sem, units):
        # pylint: disable=R0913
        """PMI - add a new metric definition to a Log Import context """
        status = LIBPCP_IMPORT.pmiUseContext(self._ctx)
        if status < 0:
            raise pmiErr(status)
        if type(name) != type(b''):
            name = name.encode('utf-8')
        status = LIBPCP_IMPORT.pmiAddMetric(c_char_p(name),
                                        pmid, typed, indom, sem, units)
        if status < 0:
            raise pmiErr(status)
        return status

    def pmiAddInstance(self, indom, instance, instid):
        """PMI - add element to an instance domain in a Log Import context """
        status = LIBPCP_IMPORT.pmiUseContext(self._ctx)
        if status < 0:
            raise pmiErr(status)
        if type(instance) != type(b''):
            instance = instance.encode('utf-8')
        status = LIBPCP_IMPORT.pmiAddInstance(indom, c_char_p(instance), instid)
        if status < 0:
            raise pmiErr(status)
        return status

    def pmiPutValue(self, name, inst, value):
        """PMI - add a value for a metric-instance pair """
        status = LIBPCP_IMPORT.pmiUseContext(self._ctx)
        if status < 0:
            raise pmiErr(status)
        if type(name) != type(b''):
            name = name.encode('utf-8')
        if type(inst) != type(b''):
            inst = inst.encode('utf-8')
        if type(value) != type(b''):
            value = value.encode('utf-8')
        status = LIBPCP_IMPORT.pmiPutValue(c_char_p(name),
                                        c_char_p(inst), c_char_p(value))
        if status < 0:
            raise pmiErr(status)
        return status

    def pmiGetHandle(self, name, inst):
        """PMI - define a handle for a metric-instance pair """
        status = LIBPCP_IMPORT.pmiUseContext(self._ctx)
        if status < 0:
            raise pmiErr(status)
        if type(name) != type(b''):
            name = name.encode('utf-8')
        if type(inst) != type(b''):
            inst = inst.encode('utf-8')
        status = LIBPCP_IMPORT.pmiGetHandle(c_char_p(name), c_char_p(inst))
        if status < 0:
            raise pmiErr(status)
        return status

    def pmiPutValueHandle(self, handle, value):
        """PMI - add a value for a metric-instance pair via a handle """
        status = LIBPCP_IMPORT.pmiUseContext(self._ctx)
        if status < 0:
            raise pmiErr(status)
        if type(value) != type(b''):
            value = value.encode('utf-8')
        status = LIBPCP_IMPORT.pmiPutValueHandle(handle, c_char_p(value))
        if status < 0:
            raise pmiErr(status)
        return status

    def pmiWrite(self, sec, usec):
        """PMI - flush data to a Log Import archive """
        status = LIBPCP_IMPORT.pmiUseContext(self._ctx)
        if status < 0:
            raise pmiErr(status)
        status = LIBPCP_IMPORT.pmiWrite(sec, usec)
        if status < 0:
            raise pmiErr(status)
        return status

    def put_result(self, result):
        """PMI - add a data record to a Log Import archive """
        status = LIBPCP_IMPORT.pmiUseContext(self._ctx)
        if status < 0:
            raise pmiErr(status)
        status = LIBPCP_IMPORT.pmiPutResult(cast(result, POINTER(pmResult)))
        if status < 0:
            raise pmiErr(status)
        return status

    @staticmethod
    def pmiDump():
        """PMI - dump the current Log Import contexts (diagnostic) """
        LIBPCP_IMPORT.pmiDump()

    def pmiEnd(self):
        """PMI - close current context and finish a Log Import archive """
        status = LIBPCP_IMPORT.pmiUseContext(self._ctx)
        if status < 0:
            raise pmiErr(status)
        status = LIBPCP_IMPORT.pmiEnd()
        self._ctx = -1
        if status < 0:
            raise pmiErr(status)
        return status