#!/usr/bin/env pmpython
#
# Copyright (c) 2012,2018 Red Hat.
# Copyright (c) 2008,2012 Aconex.  All Rights Reserved.
# Copyright (c) 2004 Silicon Graphics, Inc.  All Rights Reserved.
# 
# 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.
# 

import time
import cpmapi as c_api
from pcp.pmda import PMDA, pmdaMetric
from pcp.pmapi import pmUnits, pmContext as PCP

class TrivialPMDA(PMDA):
    ''' A trivial performance metrics domain agent '''

    def trivial_fetch_callback(self, cluster, item, inst):
        ''' Returns a list of value,status (single pair) for one metric '''

        if (cluster == 0 and item == 0):
            return [int(time.time()), 1]
        return [c_api.PM_ERR_PMID, 0]

    def __init__(self, name, domain):
        ''' Initialisation - register metrics, callbacks, drop privileges '''

        PMDA.__init__(self, name, domain)
        self.connect_pmcd()
        self.add_metric('trivial.time',         # metric name
                pmdaMetric(self.pmid(0, 0),     # ID, type, indom, semantics,
                c_api.PM_TYPE_U32, c_api.PM_INDOM_NULL, c_api.PM_SEM_COUNTER,
                pmUnits(0, 1, 0, 0, c_api.PM_TIME_SEC, 0)),     # and units.
                'time in seconds since 1 Jan 1970',     # short and long help
                'The time in seconds since the epoch (1st of January, 1970).')
        self.set_fetch_callback(self.trivial_fetch_callback)
        self.set_user(PCP.pmGetConfig('PCP_USER'))

if __name__ == '__main__':
    TrivialPMDA('trivial', 250).run()
