/usr/share/pyshared/mk_livestatus/livestatus.py is in python-mk-livestatus 0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #!/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()
|