python-mk-livestatus-0.2/0000755000000000000000000000000012230157254014201 5ustar rootrootpython-mk-livestatus-0.2/.gitignore0000644000000000000000000000046312230157254016174 0ustar rootroot# Template taken from: # https://github.com/github/gitignore/blob/master/Python.gitignore *.py[co] # Packages *.egg *.egg-info dist build eggs parts bin var sdist develop-eggs .installed.cfg # Installer logs pip-log.txt # Unit test / coverage reports .coverage .tox #Translations *.mo MANIFEST *.swp python-mk-livestatus-0.2/setup.py0000644000000000000000000000210612230157254015712 0ustar rootroot#!/usr/bin/env python import os from distutils.core import setup import mk_livestatus def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() setup(name='python-mk-livestatus', version=mk_livestatus.__version__, description='Helps to query MK livestatus and return results as dictionaries', long_description=read('README.rst'), author='Michael Fladischer', author_email='michael@fladi.at', url='https://github.com/arthru/python-mk-livestatus', download_url='http://pypi.python.org/pypi/python-mk-livestatus', packages=['mk_livestatus'], license='BSD', classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'Intended Audience :: System Administrators', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Python', 'Topic :: Software Development :: Libraries :: Python Modules', 'Topic :: System :: Monitoring', ], ) python-mk-livestatus-0.2/README.rst0000644000000000000000000000315712230157254015676 0ustar rootrootPython MK Livestatus parser =========================== :Author: Michael Fladischer :Version: 0.2 .. contents:: Access the data returned from MK Livestatus queries as Python lists or dictionaries. It does this by sending queries to the MK Livestatus UNIX socket and parses the returned rows. Read/write permission to the UNIX socket are required. Usage ----- Here a simple example to fetch the name and hostgroups for all servers in the UP (0) state: >>> from mk_livestatus import Socket >>> s = Socket("/var/lib/icinga/rw/live") >>> q = s.hosts.columns('name', 'groups').filter('state = 0') >>> print q GET hosts Columns: name groups Filter: state = 0 >>> q.call() [{'name': 'example.com', 'groups': ['ssh', 'snmp', 'smtp-server', 'ping-server', 'http-server', 'debian-server', 'apache2']}] ``s.hosts`` returns a Query to the ``hosts`` resources on Nagios. The ``columns`` and ``filter`` methods modify our query and return it, so we can chain the calls. The call to `call` method returns the rows as a list of dictionaries. If you use xinetd to bind the Unix socket to a TCP socket (like explained `here `_), you can create the socket like : >>> s = Socket(('192.168.1.1', 6557)) For more information please visit the `python-mk-livestatus website`_. Information about MK Livestatus and it's query syntax is available at the `mk-livestatus website`_. .. _python-mk-livestatus website: https://github.com/arthru/python-mk-livestatus .. _mk-livestatus website: http://mathias-kettner.de/checkmk_livestatus.html python-mk-livestatus-0.2/LICENSE0000644000000000000000000000256612230157254015217 0ustar rootrootCopyright (c) 2011, Michael Fladischer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. python-mk-livestatus-0.2/mk_livestatus/0000755000000000000000000000000012230157254017073 5ustar rootrootpython-mk-livestatus-0.2/mk_livestatus/livestatus.py0000644000000000000000000000305412230157254021652 0ustar rootroot#!/usr/bin/env python # -*- coding: utf-8 -*- import csv import socket __all__ = ['Query', 'Socket'] class Query(object): def __init__(self, conn, resource): self._conn = conn self._resource = resource self._columns = [] self._filters = [] def call(self): if self._columns: return self._conn.call(str(self), self._columns) return self._conn.call(str(self)) def __str__(self): request = 'GET %s' % (self._resource) if self._columns: request += '\nColumns: %s' % (' '.join(self._columns)) if self._filters: for filter_line in self._filters: request += '\nFilter: %s' % (filter_line) return request + '\n\n' def columns(self, *args): self._columns = args return self def filter(self, filter_str): self._filters.append(filter_str) return self class Socket(object): def __init__(self, peer): self.peer = peer def __getattr__(self, name): return Query(self, name) def call(self, request, columns=None): try: if len(self.peer) == 2: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(self.peer) s.send(request) s.shutdown(socket.SHUT_WR) csv_lines = csv.DictReader(s.makefile(), columns, delimiter=';') return list(csv_lines) finally: s.close() python-mk-livestatus-0.2/mk_livestatus/__init__.py0000644000000000000000000000015312230157254021203 0ustar rootroot#!/usr/bin/env python # -*- coding: utf-8 -*- __version__ = "0.2" from livestatus import Query, Socket python-mk-livestatus-0.2/MANIFEST.in0000644000000000000000000000006712230157254015742 0ustar rootrootinclude README.rst include LICENSE include MANIFEST.in