Git Product home page Git Product logo

python-protobix's Introduction

python-protobix

  • dev Branch: Build Status
  • upstream Branch (default): Build Status

python-protobix is a very simple python module which implements Zabbix Sender protocol 2.0.
It allows to build a list of Zabbix items and send them as trappers.

Currently python-protobix supports "classics" items as well as Low Level Discovery ones.

Please note that python-protobix is developped and tested on Debian GNU/Linux only.
I can't enforce compatibility with other distributions, though it should work on any distribution providing Python 2.7 or Python 3.x.

Any feedback on this is, of course, welcomed.

Test

To install all required dependencies and launch test suite

python setup.py test

By default, all tests named like *need_backend* are disabled, since they need a working Zabbix Server.

If you want to run theses tests as well, you will need:

  • a working Zabbix Server 3.x configuration file like the one in tests/zabbix/zabbix_server.conf
  • SQL statements in tests/zabbix/zabbix_server_mysql.sql with all informations to create testing hosts & items

You can then start Zabbix Server with zabbix_server -c tests/zabbix/zabbix_server.conf -f and launch test suite with

py.test --cov protobix --cov-report term-missing

Using a docker container

You can also use docker to run test suite on any Linux distribution of your choice.
You can use provided script docker-tests.sh as entrypoint example:

docker run --volume=$(pwd):/home/python-protobix --entrypoint=/home/python-protobix/tests/docker-tests.sh -ti debian:jessie

Currently, entrypoint docker-tests.sh only supports Debian GNU/Linux.

Please note that this docker entrypoint does not provide a way to execute test that need a backend.

Installation

With pip (stable version):

pip install protobix

With pip (test version):

pip install -i https://testpypi.python.org/simple/ protobix

Python is available as Debian package for Debian GNU/Linux sid and testing.

Usage

Once module is installed, you can use it either extending protobix.SampleProbe or directly using protobix.Datacontainer.

Extend protobix.SampleProbe

python-protobix provides a convenient sample probe you can extend to fit your own needs.

Using protobix.SampleProbe allows you to concentrate on getting metrics or Low Level Discovery items without taking care of anything related to protobix itself.
This is the recommanded way of using python-protobix.

protobix.SampleProbe provides a run method which take care of everything related to protobix.

Some probes are available from my Github repository python-zabbix

#!/usr/bin/env python
# -*- coding: utf-8 -*-
''' Copyright (c) 2013 Jean Baptiste Favre.
    Sample Python class which extends protobix.SampleProbe
'''
import protobix
import argparse
import socket
import sys

class ExampleProbe(protobix.SampleProbe):

    __version__ = '1.0.2'
    # discovery_key is *not* the one declared in Zabbix Agent configuration
    # it's the one declared in Zabbix template's "Discovery rules"
    discovery_key = "example.probe.llddiscovery"

    def _parse_probe_args(self, parser):
        # Parse the script arguments
        # parser is an instance of argparse.parser created by SampleProbe._parse_args method
        # you *must* return parser to SampleProbe so that your own options are taken into account
        example_probe_options = parser.add_argument_group('ExampleProbe configuration')
        example_probe_options.add_argument(
            "-o", "--option", default="default_value",
            help="WTF do this option"
        )
        return parser

    def _init_probe(self):
        # Whatever you need to initiliaze your probe
        # Can be establishing a connection
        # Or reading a configuration file
        # If you have nothing special to do
        # Just do not override this method
        # Or use:
        pass

    def _get_discovery(self):
        # Whatever you need to do to discover LLD items
        # this method is mandatory
        # If not declared, calling the probe ith --discovery option will resut in a NotimplementedError
        # If you get discovery infos for only one node you should return data as follow
        return { self.hostname: data }
        # If you get discovery infos for many hosts, then you should build data dict by yourself
        # and return result as follow
        return data

    def _get_metrics(self):
        # Whatever you need to do to collect metrics
        # this method is mandatory
        # If not declared, calling the probe with --update-items option will resut in a NotimplementedError
        # If you get metrics for only one node you should return data as follow
        return { self.hostname: data }
        # If you get metrics for many hosts, then you should build data dict by your self
        # and return result as follow
        return data

if __name__ == '__main__':
    ret = RedisServer().run()
    print ret
    sys.exit(ret)

Declare your newly created probe as Zabbix Agent user parameters:

UserParameter=example.probe.check,/usr/local/bin/example_probe.py --update-items
UserParameter=example.probe.discovery,/usr/local/bin/example_probe.py --discovery

You're done.

The protobix.SampleProbe exit code will be sent to Zabbix.
You'll be able to setup triggers if needed.

Exit codes mapping:

  • 0: everything went well
  • 1: probe failed at step 1 (probe initialization)
  • 2: probe failed at step 2 (probe data collection)
  • 3: probe failed at step 3 (add data to DataContainer)
  • 4: probe failed at step 4 (send data to Zabbix)

Use protobix.Datacontainer

If you don't want or can't use protobix.SampleProbe, you can also directly use protobix.Datacontainer.

How to send items updates

#!/usr/bin/env python

''' import module '''
import protobix

DATA = {
    "protobix.host1": {
        "my.protobix.item.int": 0,
        "my.protobix.item.string": "item string"
    },
    "protobix.host2": {
        "my.protobix.item.int": 0,
        "my.protobix.item.string": "item string"
    }
}

zbx_datacontainer = protobix.DataContainer()
zbx_datacontainer.data_type = 'items'
zbx_datacontainer.add(DATA)
zbx_datacontainer.send()

How to send Low Level Discovery

#!/usr/bin/env python

''' import module '''
import protobix

DATA = {
    'protobix.host1': {
        'my.protobix.lld_item1': [
            { '{#PBX_LLD_KEY11}': 0,
              '{#PBX_LLD_KEY12}': 'lld string' },
            { '{#PBX_LLD_KEY11}': 1,
              '{#PBX_LLD_KEY12}': 'another lld string' }
        ],
        'my.protobix.lld_item2': [
            { '{#PBX_LLD_KEY21}': 10,
              '{#PBX_LLD_KEY21}': 'yet an lld string' },
            { '{#PBX_LLD_KEY21}': 2,
              '{#PBX_LLD_KEY21}': 'yet another lld string' }
        ]
    },
    'protobix.host2': {
        'my.protobix.lld_item1': [
            { '{#PBX_LLD_KEY11}': 0,
              '{#PBX_LLD_KEY12}': 'lld string' },
            { '{#PBX_LLD_KEY11}': 1,
              '{#PBX_LLD_KEY12}': 'another lld string' }
        ],
        'my.protobix.lld_item2': [
            { '{#PBX_LLD_KEY21}': 10,
              '{#PBX_LLD_KEY21}': 'yet an lld string' },
            { '{#PBX_LLD_KEY21}': 2,
              '{#PBX_LLD_KEY21}': 'yet another lld string' }
        ]
    }
}

zbx_datacontainer = protobix.DataContainer()
zbx_datacontainer.data_type = 'lld'
zbx_datacontainer.add(DATA)
zbx_datacontainer.send()

Advanced configuration

python-protobix behaviour can be altered in many ways using options.
All configuration options are stored in a protobix.ZabbixAgentConfig instance.

Protobix specific configuration options

Option name Default value ZabbixAgentConfig property Command-line option (SampleProbe)
data_type None data_type --update-items or --discovery
dryrun False dryrun -d or --dryrun

Zabbix Agent configuration options

Option name Default value ZabbixAgentConfig property Command-line option (SampleProbe)
ServerActive 127.0.0.1 server_active -z or --zabbix-server
ServerPort 10051 server_port -p or --port
LogType file log_type none
LogFile /tmp/zabbix_agentd.log log_file none
DebugLevel 3 debug_level -v (from none to -vvvvv)
Timeout 3 timeout none
Hostname socket.getfqdn() hostname none
TLSConnect unencrypted tls_connect --tls-connect
TLSCAFile None tls_ca_file --tls-ca-file
TLSCertFile None tls_cert_file --tls-cert-file
TLSCRLFile None tls_crl_file --tls-crl-file
TLSKeyFile None tls_key_file --tls-key-file
TLSServerCertIssuer None tls_server_cert_issuer --tls-server-cert-issuer
TLSServerCertSubject None tls_server_cert_subject --tls-server-cert-subject

How to contribute

You can contribute to protobix:

  • fork this repository
  • write tests and documentation (tests must pass for both Python 2.7 & 3.x)
  • implement the feature you need
  • open a pull request against upstream branch

python-protobix's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-protobix's Issues

Parsing failed with several errors.

Running protobix with it automatically reading the zabbix_agentd.conf results in traceback:

Traceback (most recent call last):
  File "./aamv2.py", line 168, in <module>
    ret = AAMv2().run()
  File "/usr/local/lib/python2.7/dist-packages/protobix/sampleprobe.py", line 281, in run
    self.zbx_config = self._init_config()
  File "/usr/local/lib/python2.7/dist-packages/protobix/sampleprobe.py", line 204, in _init_config
    zbx_config = ZabbixAgentConfig(self.options.config_file)
  File "/usr/local/lib/python2.7/dist-packages/protobix/zabbixagentconfig.py", line 56, in __init__
    tmp_config = configobj.ConfigObj(config_file, list_values=False)
  File "/usr/lib/python2.7/dist-packages/configobj.py", line 1229, in __init__
    self._load(infile, configspec)
  File "/usr/lib/python2.7/dist-packages/configobj.py", line 1318, in _load
    raise error
configobj.ConfigObjError: Parsing failed with several errors.
First error at line 202.

Line 202 of the /etc/zabbix/zabbix_agentd.conf is a userparameter:

UserParameter=updates.count.security,/usr/lib/update-notifier/apt-check 2>&1 | cut -d ';' -f 2

This is actually the second userparameter in the file. If I uncomment this particular line (the 2nd), the next one complain and so on. Parsing only completes whenever all UserParameters have been commented out.

ZBX_RESP_REGEX for Zabbix 2.0

Correct REGEXP for both 2.2 & 2.4 is:
ZBX_RESP_REGEX = r'processed: (\d+); failed: (\d+); total: (\d+); seconds spent: (\d\.\d+)'

Check wether it's the same with Zabbix 2.0 or not. Could be:
ZBX_RESP_REGEX = r'Processed (\d+) Failed (\d+) Total (\d+) Seconds spent (\d\.\d+)'

AttributeError: 'dict' object has no attribute 'data_type'

I'm struggling with getting this to work properly.

Traceback (most recent call last):
  File "/etc/salt/eventsd_workers/Zabbix_Return_Worker.py", line 170, in _store
    self.zbx_datacontainer.add(data)
  File "/usr/local/lib/python2.7/dist-packages/protobix/datacontainer.py", line 69, in add
    self.add_item(host, key, data[host][key])
  File "/usr/local/lib/python2.7/dist-packages/protobix/datacontainer.py", line 48, in add_item
    if self._config.data_type == "items":
AttributeError: 'dict' object has no attribute 'data_type'

The datacontainer is setup like this:

            config = {
                # Protobix specific options
                'data_type': None,
                'dryrun': False,

                # Zabbix Agent options
                'ServerActive': self.creds['server'],
                'ServerPort': 10051,
                'LogType': 'file',
                'LogFile': '/dev/null',
                'DebugLevel': 3,
                'Timeout': 3,
                'Hostname': getfqdn(),
                'TLSConnect': 'unencrypted',
                'TLSCAFile': None,
                'TLSCertFile': None,
                'TLSCRLFile': None,
                'TLSKeyFile': None,
                'TLSServerCertIssuer': None,
                'TLSServerCertSubject': None,
                'TLSPSKIdentity': None,
                'TLSPSKFile': None,
            }

        config['data_type'] = 'items'

        log.debug('[Zabbix] Creating data container with config {0}'.format(config))

        self.zbx_datacontainer = protobix.DataContainer(config=config)

Later on we try to send data:

            data = {
                minion: {
                    self.creds['failed_key']: failed_counter,
                    self.creds['changed_key']: changed_counter
                }
            }
            log.debug('[Zabbix] Dataset to send: {0}'.format(data))

            self.zbx_datacontainer.add(data)

            response = self.zbx_datacontainer.send()

Previously I was using add_item but this was affected by the same.

I also tried to set it like self.zbx_datacontainer.data_type = 'items' and self.zbx_datacontainer.data_type('items') but had no luck with those either.

Am I doing something wrong? This is (more or less) also in the tests so ISTM this should work?

Reading ServerActive from zabbix_agentd.conf is broken

It looks like reading ServerActive from zabbix_agentd.conf is broken in my environment.

On CentOS 6:

Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import protobix
>>> zbx_items = protobix.DataContainer(data_type = 'items')
>>> zbx_items._config
{'dryrun': False, 'data_type': 'items', 'server': '1', 'log_output': '/var/log/zabbix/zabbix_agentd.log', 'timeout': 3, 'log_level': 3, 'port': 10051}

On CentOS 7:

Python 2.7.5 (default, Nov 20 2015, 02:00:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import protobix
>>> zbx_items = protobix.DataContainer(data_type = 'items')
>>> zbx_items._config
{'dryrun': False, 'data_type': 'items', 'server': '1', 'log_output': '/var/log/zabbix/zabbix_agentd.log', 'timeout': 3, 'log_level': 3, 'port': 10051}

That's what my Zabbix Agent config looks like:

$ fgrep ServerActive /etc/zabbix/zabbix_agentd.conf
ServerActive=192.1.1.1

To me it looks like d88e2cd broke it, but my Python is not really good enough to judge.

Error installing protobix

Upon doing pip install protobix I get the following error:

(logster)$ pip install functools
Downloading/unpacking functools
  Downloading functools-0.5.tar.gz
  Running setup.py (path:/home/jeune/.virtualenvs/logster/build/functools/setup.py) egg_info for package functools
    Traceback (most recent call last):
      File "<string>", line 3, in <module>
      File "/home/jeune/.virtualenvs/logster/local/lib/python2.7/site-packages/setuptools/__init__.py", line 12, in <module>
        from setuptools.extension import Extension
      File "/home/jeune/.virtualenvs/logster/local/lib/python2.7/site-packages/setuptools/extension.py", line 3, in <module>
        import functools
      File "functools.py", line 72, in <module>
        globals()['c_%s' % x] = globals()[x] = getattr(_functools, x)
    AttributeError: 'module' object has no attribute 'compose'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 3, in <module>

  File "/home/jeune/.virtualenvs/logster/local/lib/python2.7/site-packages/setuptools/__init__.py", line 12, in <module>

    from setuptools.extension import Extension

  File "/home/jeune/.virtualenvs/logster/local/lib/python2.7/site-packages/setuptools/extension.py", line 3, in <module>

    import functools

  File "functools.py", line 72, in <module>

    globals()['c_%s' % x] = globals()[x] = getattr(_functools, x)

AttributeError: 'module' object has no attribute 'compose'

Downloading from github and doing python setup.py install works. Please update the documentation. Installing via pip seems to be broken.

Examples out of date

The examples as given don't seem to work - zbx_container.debug = True causes an error.
A guide on the module would be helpful to us beginners trying to use it.

The module does work well though, but required considerable trial and error and a good amount of perserverance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.