pcp
[Top] [All Lists]

Python/PMAPI timezone issues

To: pcp developers <pcp@xxxxxxxxxxx>
Subject: Python/PMAPI timezone issues
From: Marko Myllynen <myllynen@xxxxxxxxxx>
Date: Mon, 1 Feb 2016 08:55:26 +0200
Delivered-to: pcp@xxxxxxxxxxx
Organization: Red Hat
Reply-to: Marko Myllynen <myllynen@xxxxxxxxxx>
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0
Hi,

It seems that PCP Python/PMAPI timezone code provides its own set of
challenges in addition to the standard Python timezone quirks.

The below code works but if I uncomment any of the problematic
lines now commented out, things fall apart. I wonder what and where
should be fixed and also how the code should look like in ideal
conditions (i.e., so that -z/-Z are handled properly)?

#!/usr/bin/pcp python

import time
import sys
import os

import cpmapi as c_api
from pcp import pmapi

class Test(object):
    def __init__(self):
        self.context = None
        self.opts = self.options()
        self.interval = pmapi.timeval(1) # 1 sec
        self.opts.pmSetOptionInterval(str(1))
        self.metrics = []

    # RHBZ#1264147
    def pmids_to_ctypes(self, pmids):
        """ Convert a Python list of pmids (numbers) to
            a ctypes LP_c_uint (a C array of uints).
        """
        from ctypes import c_uint
        pmidA = (c_uint * len(pmids))()
        for i, p in enumerate(pmids):
            pmidA[i] = c_uint(p)
        return pmidA

    def options(self):
        opts = pmapi.pmOptions()
        opts.pmSetShortOptions("a:D:h:O:s:t:Z:z")
        opts.pmSetLongOptionArchive()      # -a/--archive
        opts.pmSetLongOptionDebug()        # -D/--debug
        opts.pmSetLongOptionHost()         # -h/--host
        opts.pmSetLongOptionFinish()       # -T/--finish
        opts.pmSetLongOptionOrigin()       # -O/--origin
        opts.pmSetLongOptionSamples()      # -s/--samples
        opts.pmSetLongOptionInterval()     # -t/--interval
        opts.pmSetLongOptionTimeZone()     # -Z/--timezone
        opts.pmSetLongOptionHostZone()     # -z/--hostzone
        return opts

    def connect(self):
        """ Establish a PMAPI context to archive, host or local, via args """
        self.context = pmapi.pmContext.fromOptions(self.opts, sys.argv)

    def execute(self):
        """ Execute a test """
        # Time
        #if not self.opts.pmGetOptionHostZone(): # permission error w/o -z/-Z
        if self.opts.pmGetOptionHostZone():
            tz = self.context.pmWhichZone()
            #self.context.pmWhichZone() # double free / corruption
            os.environ['TZ'] = tz
            time.tzset()
        else:
            os.environ['TZ'] = "EET-2"
            time.tzset()
            self.context.pmNewZone("EET-2")
            #print(self.context.pmWhichZone()) # error w/o the above lines
        if self.opts.pmGetOptionTimezone():
            os.environ['TZ'] = self.opts.pmGetOptionTimezone()
            time.tzset()
            self.context.pmNewZone(self.opts.pmGetOptionTimezone())
        print(os.environ['TZ'])

if __name__ == '__main__':
    try:
        T = Test()
        T.connect()
        T.execute()
    except pmapi.pmUsageErr as usage:
        usage.message()
    except Exception as error:
        sys.stderr.write(str(error) + "\n")

Thanks,

-- 
Marko Myllynen

<Prev in Thread] Current Thread [Next in Thread>
  • Python/PMAPI timezone issues, Marko Myllynen <=