Exemplo n.º 1
0
def _make_agent_data():
    """Create generate data to post to API server"""
    # Initialize key variables
    config = Config()
    polling_interval = 60
    pattoo_agent_program = 1
    pattoo_agent_polled_target = 2
    pattoo_key = '3'
    pattoo_value = 4

    # We want to make sure we get a different AgentID each time
    filename = files.agent_id_file(pattoo_agent_program, config)
    if os.path.isfile(filename) is True:
        os.remove(filename)

    # Setup AgentPolledData
    apd = AgentPolledData(pattoo_agent_program, polling_interval)

    # Initialize TargetDataPoints
    ddv = TargetDataPoints(pattoo_agent_polled_target)

    # Setup DataPoint
    data_type = DATA_INT
    variable = DataPoint(pattoo_key, pattoo_value, data_type=data_type)

    # Add data to TargetDataPoints
    ddv.add(variable)

    # Create a result
    apd.add(ddv)

    # Return agent data
    return apd
Exemplo n.º 2
0
def poll():
    """Get Modbus agent data.

    Performance data from Modbus enabled targets.

    Args:
        None

    Returns:
        agentdata: AgentPolledData object for all data gathered by the agent

    """
    # Initialize key variables.
    config = Config()
    _pi = config.polling_interval()

    # Initialize AgentPolledData
    agent_program = PATTOO_AGENT_OPCUAD
    agentdata = AgentPolledData(agent_program, _pi)

    # Get registers to be polled
    tpp_list = config.target_polling_points()
    arguments = [(tpp, ) for tpp in tpp_list]

    # Poll registers for all targets and update the TargetDataPoints
    target_datapoints_list = _parallel_poller(arguments)
    agentdata.add(target_datapoints_list)

    # Return data
    return agentdata
Exemplo n.º 3
0
def poll(bacnet):
    """Get BACnetIP agent data.

    Performance data from BACnetIP enabled targets.

    Args:
        None

    Returns:
        agentdata: AgentPolledData object for all data gathered by the agent

    """
    # Initialize key variables.
    config = configuration.ConfigBACnetIP()
    _pi = config.polling_interval()

    # Initialize AgentPolledData
    agent_program = PATTOO_AGENT_BACNETIPD
    agentdata = AgentPolledData(agent_program, _pi)

    # Poll oids for all targets and update the TargetDataPoints
    poller = _PollBACnetIP(bacnet)
    ddv_list = poller.data()
    agentdata.add(ddv_list)

    # Return data
    return agentdata
Exemplo n.º 4
0
def poll():
    """Get Modbus agent data.

    Performance data from Modbus enabled targets.

    Args:
        None

    Returns:
        agentdata: AgentPolledData object for all data gathered by the agent

    """
    # Initialize key variables.
    config = Config()
    _pi = config.polling_interval()
    arguments = []

    # Initialize AgentPolledData
    agent_program = PATTOO_AGENT_MODBUSTCPD
    agentdata = AgentPolledData(agent_program, _pi)

    # Get registers to be polled
    drvs = config.registervariables()

    # Create a dict of register lists keyed by ip_target
    for drv in drvs:
        arguments.append((drv, ))

    # Poll registers for all targets and update the TargetDataPoints
    ddv_list = _parallel_poller(arguments)
    agentdata.add(ddv_list)

    # Return data
    return agentdata
Exemplo n.º 5
0
    def test_agentdata_to_post(self):
        """Testing method or function named agentdata_to_post."""
        # Setup AgentPolledData
        agent_program = 'panda_bear'
        polling_interval = 20
        apd = AgentPolledData(agent_program, polling_interval)

        # Initialize TargetDataPoints
        target = 'teddy_bear'
        ddv = TargetDataPoints(target)

        # Setup DataPoint
        value = 457
        key = 'gummy_bear'
        data_type = DATA_INT
        variable = DataPoint(key, value, data_type=data_type)

        # Add data to TargetDataPoints
        ddv.add(variable)

        # Test TargetGateway to AgentPolledData
        apd.add(ddv)
        result = converter.agentdata_to_post(apd)
        self.assertEqual(result.pattoo_agent_id, apd.agent_id)
        self.assertEqual(
            result.pattoo_agent_polling_interval, polling_interval * 1000)
        self.assertTrue(isinstance(result.pattoo_datapoints, dict))

        # Test the key value pairs
        item = result.pattoo_datapoints['key_value_pairs']
        self.assertTrue('datapoint_pairs' in result.pattoo_datapoints)
        self.assertTrue('key_value_pairs' in result.pattoo_datapoints)
        self.assertTrue(isinstance(item, dict))

        # Convert item to a list of tuples for ease of testing
        tester = [(k, v) for k, v in sorted(item.items())]
        self.assertEqual(
            tester[0],
            (0, ('pattoo_agent_polling_interval', '20000')))

        self.assertEqual(
            tester[3:8],
            [
                (3, ('pattoo_agent_polled_target', 'teddy_bear')),
                (4, ('pattoo_agent_program', 'panda_bear')),
                (5, ('pattoo_key', 'gummy_bear')),
                (6, ('pattoo_data_type', 99)),
                (7, ('pattoo_value', 457))]
        )

        # Test the pointers to the key value pairs
        item = result.pattoo_datapoints['datapoint_pairs']
        self.assertTrue(isinstance(item, list))
        self.assertEqual(len(item), 1)
        self.assertEqual(len(item[0]), 10)
Exemplo n.º 6
0
def poll():
    """Get PATOO_SNMP agent data.

    Performance data from SNMP enabled targets.

    Args:
        None

    Returns:
        agentdata: AgentPolledData object for all data gathered by the agent

    """
    # Initialize key variables.
    config = Config()
    ip_snmpvariables = {}
    ip_polltargets = {}
    _pi = config.polling_interval()

    # Initialize AgentPolledData
    agent_program = PATTOO_AGENT_SNMPD
    agentdata = AgentPolledData(agent_program, _pi)

    # Get SNMP OIDs to be polled (Along with authorizations and ip_targets)
    cfg_snmpvariables = config.snmpvariables()
    target_poll_targets = config.target_polling_points()

    # Create a dict of snmpvariables keyed by ip_target
    for snmpvariable in cfg_snmpvariables:
        ip_snmpvariables[snmpvariable.ip_target] = snmpvariable

    # Create a dict of oid lists keyed by ip_target
    for dpt in target_poll_targets:
        # Ignore invalid data
        if dpt.valid is False:
            continue

        # Process
        next_target = dpt.target
        if next_target in ip_polltargets:
            ip_polltargets[next_target].extend(dpt.data)
        else:
            ip_polltargets[next_target] = dpt.data

    # Poll oids for all targets and update the TargetDataPoints
    ddv_list = _snmpwalks(ip_snmpvariables, ip_polltargets)
    agentdata.add(ddv_list)

    # Return data
    return agentdata
Exemplo n.º 7
0
def test_agent():
    # Define the polling interval in seconds (integer).
    polling_interval = 300

    # Give the agent a name
    agent_name = 'sample_agent_script'

    # Let's assume the script has already received this data from SITE_A
    site_a_data = [['ABC', 123.456], ['DEF', 456.789]]

    # Let's assume the script has already received this data from WORK_1
    work_1_data = [['GHI', 654.321], ['JKL', 987.654]]

    # Setup the agent's AgentPolledData object.
    agent = AgentPolledData(agent_name, polling_interval)

    # Let's add some metadata that you don't want to affect charting in the
    # event of a change. Department names change all the time.
    metadata_static = DataPointMetadata('Department Name',
                                        'The Palisadoes Foundation',
                                        update_checksum=False)

    # Let's add some metadata that will change and trigger a new chart.
    metadata_dynamic = DataPointMetadata('Financial Year', '2020')

    # Create target objects for SITE_A
    target = TargetDataPoints('SITE_A')
    for quote in site_a_data:
        key, value = quote
        datapoint = DataPoint(key, value, data_type=DATA_FLOAT)
        datapoint.add(metadata_static)
        datapoint.add(metadata_dynamic)
        target.add(datapoint)
    agent.add(target)

    # Create target objects for WORK_1
    target = TargetDataPoints('WORK_1')
    for quote in work_1_data:
        key, value = quote
        datapoint = DataPoint(key, value, data_type=DATA_FLOAT)
        datapoint.add(metadata_static)
        datapoint.add(metadata_dynamic)
        target.add(datapoint)
    agent.add(target)

    # Return agent
    return agent
Exemplo n.º 8
0
def create_cache():
    """Testing method / function records."""
    # Initialize key variables
    config = ServerConfig()
    polling_interval = 20
    cache_directory = config.agent_cache_directory(PATTOO_API_AGENT_NAME)
    result = {
        'pattoo_agent_program': data.hashstring(str(random())),
        'pattoo_agent_polled_target': socket.getfqdn(),
        'pattoo_key': data.hashstring(str(random())),
        'pattoo_value': round(uniform(1, 100), 5),
        'pattoo_agent_hostname': socket.getfqdn()
    }

    # We want to make sure we get a different AgentID each time
    filename = files.agent_id_file(
        result['pattoo_agent_program'],
        config)
    if os.path.isfile(filename) is True:
        os.remove(filename)
    result['pattoo_agent_id'] = files.get_agent_id(
        result['pattoo_agent_program'],
        config)

    # Setup AgentPolledData
    apd = AgentPolledData(result['pattoo_agent_program'], polling_interval)

    # Initialize TargetDataPoints
    ddv = TargetDataPoints(result['pattoo_agent_hostname'])

    # Setup DataPoint
    data_type = DATA_INT
    variable = DataPoint(
        result['pattoo_key'], result['pattoo_value'], data_type=data_type)

    # Add data to TargetDataPoints
    ddv.add(variable)

    # Write data to cache
    apd.add(ddv)
    cache_dict = converter.posting_data_points(
        converter.agentdata_to_post(apd))
    cache_file = '{}{}cache_test.json'.format(cache_directory, os.sep)
    with open(cache_file, 'w') as _fp:
        json.dump(cache_dict, _fp)

    return result
Exemplo n.º 9
0
    def test_agentdata_to_datapoints(self):
        """Testing method or function named agentdata_to_datapoints."""
        # Setup AgentPolledData
        agent_program = 'panda_bear'
        polling_interval = 20
        apd = AgentPolledData(agent_program, polling_interval)

        # Initialize TargetDataPoints
        target = 'teddy_bear'
        ddv = TargetDataPoints(target)

        # Setup DataPoint
        value = 457
        key = 'gummy_bear'
        data_type = DATA_INT
        variable = DataPoint(key, value, data_type=data_type)

        # Add data to TargetDataPoints
        ddv.add(variable)

        # Test TargetGateway to AgentPolledData
        apd.add(ddv)

        # Test contents
        expected_metadata = {
            'pattoo_agent_id': apd.agent_id,
            'pattoo_agent_program': agent_program,
            'pattoo_agent_hostname': apd.agent_hostname,
            'pattoo_agent_polled_target': target,
            'pattoo_agent_polling_interval': apd.agent_polling_interval
        }
        result = converter.agentdata_to_datapoints(apd)

        self.assertEqual(len(result), 1)
        item = result[0]
        self.assertTrue(isinstance(item, DataPoint))
        self.assertEqual(item.value, value)
        self.assertEqual(item.data_type, DATA_INT)
        self.assertEqual(item.key, key)
        self.assertTrue(isinstance(item.metadata, dict))
        self.assertEqual(len(item.metadata), len(expected_metadata))
        for key, value in item.metadata.items():
            self.assertTrue(isinstance(value, str))
            self.assertTrue(isinstance(key, str))
            self.assertEqual(value, str(expected_metadata[key]))
Exemplo n.º 10
0
def poll(agent_program, polling_interval):
    """Get all agent data.

    Performance data on linux server on which this application is installed.

    Args:
        agentdata: AgentPolledData object for all data gathered by the agent
        polling_interval: Polling interval in seconds

    Returns:
        None

    """
    # Initialize AgentPolledData
    agent_hostname = socket.getfqdn()
    agentdata = AgentPolledData(agent_program, polling_interval)

    # Intialize data gathering
    ddv = TargetDataPoints(agent_hostname)

    #########################################################################
    # Get timeseries values
    #########################################################################

    performance = Performance()

    # Update agent with system data
    ddv.add(performance.stats_system())

    # Update agent with disk data
    ddv.add(performance.stats_disk_swap())
    ddv.add(performance.stats_disk_partitions())
    ddv.add(performance.stats_disk_io())

    # Update agent with network data
    ddv.add(performance.stats_network())

    # Add results to the AgentPolledData object for posting
    agentdata.add(ddv)
    return agentdata
Exemplo n.º 11
0
    def test_add(self):
        """Testing function append."""
        # Setup AgentPolledData
        agent_program = 'panda_bear'
        polling_interval = 20
        apd = AgentPolledData(agent_program, polling_interval)

        # Initialize TargetDataPoints
        target = 'teddy_bear'
        ddv = TargetDataPoints(target)
        self.assertEqual(ddv.target, target)
        self.assertFalse(ddv.valid)
        self.assertEqual(ddv.data, [])

        # Setup DataPoint
        value = 457
        _key_ = 'gummy_bear'
        data_type = DATA_INT
        variable = DataPoint(_key_, value, data_type=data_type)

        # Add data to TargetDataPoints
        self.assertFalse(ddv.valid)
        ddv.add(variable)
        self.assertTrue(ddv.valid)

        # Test add
        self.assertFalse(apd.valid)
        apd.add(None)
        self.assertFalse(apd.valid)
        apd.add(variable)
        self.assertFalse(apd.valid)
        apd.add(ddv)
        self.assertTrue(apd.valid)

        # Test contents
        data = apd.data
        self.assertTrue(isinstance(data, list))
        self.assertEqual(len(data), 1)

        _ddv = data[0]
        self.assertTrue(isinstance(_ddv, TargetDataPoints))
        self.assertEqual(_ddv.target, target)
        self.assertTrue(_ddv.valid)
        self.assertTrue(isinstance(_ddv.data, list))
        self.assertTrue(len(_ddv.data), 1)

        data = _ddv.data
        _variable = _ddv.data[0]
        self.assertEqual(_variable.data_type, data_type)
        self.assertEqual(_variable.value, value)
        self.assertEqual(_variable.key, _key_)
Exemplo n.º 12
0
def main():
    """Post data to pattoo server.

    Args:
        None

    Returns:
        None

    """
    '''
    NOTE:

    Scripts must be run at regular intervals and the polling_interval
    should be automatically provided to the main() function.

    Notes about CRON:
    When using cron, change this value to match the cron interval in seconds.
    It is not advised to use cron for polling unless you know the interval
    will not change. If using cron, remember to make the polling interval to
    match the cron interval in 'seconds'.

    Ideally your agents should run as daemons, not as cron jobs. See the daemon
    example script which explains how to do this.
    '''

    # Define the polling interval in seconds (integer).
    polling_interval = 300

    # Give the agent a name
    agent_name = 'sample_agent_script'

    # Let's assume the script has already received this data from SITE_A
    site_a_data = [['ABC', 123.456], ['DEF', 456.789]]

    # Let's assume the script has already received this data from WORK_1
    work_1_data = [['GHI', 654.321], ['JKL', 987.654]]
    '''
    NOTE:

    The AgentPolledData object contains unique identification information
    that the pattoo server will use to differentiate the information source.
    This includes: the hostname of the system on which the object was created,
    a unique hashed identifier stored in the cache directory of the agent
    configuration.

    You just need to write an agent to do one thing well, such data collection
    from a type of target source. For example, you don't need to give each
    agent that does the same thing a different "agent_name".  Just make sure
    that different types of agents have different "agent_name" values.

    The PattooShared library will take care of the rest.
    '''

    # Setup the agent's AgentPolledData object.
    agent = AgentPolledData(agent_name, polling_interval)
    '''
    NOTE:

    Metadata is normally expected to stay constant. If it changes then the
    datapoint ID changes and you'll start plotting a brand new chart on the
    pattoo server. The old chart will stop at the time the metadata changed.

    In some cases, you may not want changing metadata to cause a brand new
    plot. For example, your metadata for computer resource charting may include
    the operating system version. This is useful background information, but
    shouldn't impact charting if it changes. This type of metadata should be
    dynamic.

    '''
    # Let's add some metadata that you don't want to affect charting in the
    # event of a change. Department names change all the time.
    metadata_static = DataPointMetadata('Department Name',
                                        'The Palisadoes Foundation',
                                        update_checksum=False)

    # Let's add some metadata that will change and trigger a new chart.
    metadata_dynamic = DataPointMetadata('Financial Year', '2020')

    # Create target objects for SITE_A
    target = TargetDataPoints('SITE_A')
    for quote in site_a_data:
        key, value = quote
        '''
        NOTE:

        You don't have to specify the time when the data was collected.
        The DataPoint object captures that information automatically. You can
        also specify it using the timestamp= argument. See the class
        documentation for details.

        The default data_type is DATA_INT (integer). Read the documentation
        for the various other data which cover float and counter values.
        '''
        datapoint = DataPoint(key, value, data_type=DATA_FLOAT)
        datapoint.add(metadata_static)
        datapoint.add(metadata_dynamic)
        target.add(datapoint)
    agent.add(target)

    # Create target objects for WORK_1
    target = TargetDataPoints('WORK_1')
    for quote in work_1_data:
        key, value = quote
        datapoint = DataPoint(key, value, data_type=DATA_FLOAT)
        datapoint.add(metadata_static)
        datapoint.add(metadata_dynamic)
        target.add(datapoint)
    agent.add(target)

    # Post the data to pattoo
    post = PostAgent(agent)
    post.post()