mirror of
https://github.com/munin-monitoring/contrib.git
synced 2025-07-22 14:16:00 +00:00
Initial commit of pypmmn
A port of pmmn to python. * Currently only handles text from stdin (as with pmmn). * The location of the ``plugins_folder`` is currently hardcoded. * No command line arguments parsed yet.
This commit is contained in:
parent
aa2691a4c2
commit
b8ed5d1005
4 changed files with 182 additions and 0 deletions
33
tools/pypmmn/README.rst
Normal file
33
tools/pypmmn/README.rst
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
PyPMMN
|
||||||
|
======
|
||||||
|
|
||||||
|
PyPMMN is a pure python port of pmmn_.
|
||||||
|
|
||||||
|
Requirements
|
||||||
|
============
|
||||||
|
|
||||||
|
PyPMMN does not have any requirements other than the python standard library.
|
||||||
|
For compatibility, it's targeted for Python 2.4 and up.
|
||||||
|
|
||||||
|
Installation
|
||||||
|
============
|
||||||
|
|
||||||
|
The python way
|
||||||
|
--------------
|
||||||
|
|
||||||
|
Download the folder and run::
|
||||||
|
|
||||||
|
python setup.py install
|
||||||
|
|
||||||
|
This will install ``pypmmn.py`` into your system's ``bin`` folder. Commonly,
|
||||||
|
this is ``/usr/local/bin``.
|
||||||
|
|
||||||
|
Manually
|
||||||
|
--------
|
||||||
|
|
||||||
|
Download the folder and copy the file ``pypmmn/pypmmn.py`` to a location of
|
||||||
|
your choice and ensure it's executable.
|
||||||
|
|
||||||
|
|
||||||
|
.. _pmmn: http://blog.pwkf.org/post/2008/11/04/A-Poor-Man-s-Munin-Node-to-Monitor-Hostile-UNIX-Servers
|
||||||
|
|
0
tools/pypmmn/pypmmn/__init__.py
Normal file
0
tools/pypmmn/pypmmn/__init__.py
Normal file
126
tools/pypmmn/pypmmn/pypmmn.py
Normal file
126
tools/pypmmn/pypmmn/pypmmn.py
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
from os.path import join, isdir
|
||||||
|
from subprocess import call
|
||||||
|
from os import listdir, access, X_OK
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
__version__ = '1.0dev1'
|
||||||
|
|
||||||
|
|
||||||
|
spoolfetch_dir = ''
|
||||||
|
plugin_dir = 'plugins'
|
||||||
|
host = 'hostname'
|
||||||
|
|
||||||
|
|
||||||
|
class CmdHandler(object):
|
||||||
|
|
||||||
|
def __init__(self, out_stream):
|
||||||
|
self.out_stream = out_stream
|
||||||
|
|
||||||
|
def do_version(self, arg):
|
||||||
|
"""
|
||||||
|
Prints the version of this instance.
|
||||||
|
"""
|
||||||
|
self.out_stream.write('pypmmn on %s version: %s' % (host,
|
||||||
|
__version__))
|
||||||
|
|
||||||
|
def do_nodes(self, arg):
|
||||||
|
"""
|
||||||
|
Prints this hostname
|
||||||
|
"""
|
||||||
|
self.out_stream.write('%s\n' % host)
|
||||||
|
self.out_stream.write('.')
|
||||||
|
|
||||||
|
def do_quit(self, arg):
|
||||||
|
"""
|
||||||
|
Stops this process
|
||||||
|
"""
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
def do_list(self, arg):
|
||||||
|
"""
|
||||||
|
Print a list of plugins
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
for filename in listdir(plugin_dir):
|
||||||
|
if not access(join(plugin_dir, filename), X_OK):
|
||||||
|
continue
|
||||||
|
self.out_stream.write("%s " % filename)
|
||||||
|
except OSError, exc:
|
||||||
|
sys.stdout.writte("# ERROR: %s" % exc)
|
||||||
|
|
||||||
|
def _caf(self, arg, cmd):
|
||||||
|
"""
|
||||||
|
handler for ``config``, ``alert`` and ``fetch``
|
||||||
|
Calls the plugin with ``cmd`` as only argument.
|
||||||
|
"""
|
||||||
|
plugin_filename = join(plugin_dir, arg)
|
||||||
|
if isdir(plugin_filename) or not access(plugin_filename, X_OK):
|
||||||
|
self.out_stream.write("# Unknown plugin [%s] for %s" % (arg, cmd))
|
||||||
|
return
|
||||||
|
|
||||||
|
if cmd == 'fetch':
|
||||||
|
arg_plugin = ''
|
||||||
|
else:
|
||||||
|
arg_plugin = cmd
|
||||||
|
|
||||||
|
try:
|
||||||
|
call([plugin_filename, arg_plugin])
|
||||||
|
except OSError, exc:
|
||||||
|
self.out_stream.write("# ERROR: %s" % exc)
|
||||||
|
return
|
||||||
|
self.out_stream.write('.')
|
||||||
|
|
||||||
|
def do_alert(self, arg):
|
||||||
|
self._caf(arg, 'alert')
|
||||||
|
|
||||||
|
def do_fetch(self, arg):
|
||||||
|
self._caf(arg, 'fetch')
|
||||||
|
|
||||||
|
def do_config(self, arg):
|
||||||
|
self._caf(arg, 'config')
|
||||||
|
|
||||||
|
def do_cap(self, arg):
|
||||||
|
self.out_stream.write("cap ")
|
||||||
|
if spoolfetch_dir:
|
||||||
|
self.out_stream.write("spool ")
|
||||||
|
|
||||||
|
def do_spoolfetch(self, arg):
|
||||||
|
call(['%s/spoolfetch_%s' % (spoolfetch_dir, host), arg])
|
||||||
|
self.out_stream.write('.')
|
||||||
|
|
||||||
|
# aliases
|
||||||
|
do_exit = do_quit
|
||||||
|
|
||||||
|
|
||||||
|
def handle_input(in_stream, handler, out_stream):
|
||||||
|
for line in in_stream:
|
||||||
|
line = line.strip()
|
||||||
|
line = line.split(' ')
|
||||||
|
cmd = line[0]
|
||||||
|
if len(line) == 1:
|
||||||
|
arg = ''
|
||||||
|
elif len(line) == 2:
|
||||||
|
arg = line[1]
|
||||||
|
else:
|
||||||
|
raise ValueError('Invalid input: %s' % line)
|
||||||
|
|
||||||
|
if not cmd:
|
||||||
|
continue
|
||||||
|
|
||||||
|
func = getattr(handler, 'do_%s' % cmd, None)
|
||||||
|
if not func:
|
||||||
|
commands = [_[3:] for _ in dir(handler) if _.startswith('do_')]
|
||||||
|
print "# Unknown command. Supported commands: %s" % commands
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
func(arg)
|
||||||
|
out_stream.write('\n')
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
handle_input(sys.stdin, CmdHandler(sys.stdout), sys.stdout)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
23
tools/pypmmn/setup.py
Normal file
23
tools/pypmmn/setup.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
from distutils.core import setup
|
||||||
|
from pypmmn.pypmmn import __version__
|
||||||
|
|
||||||
|
PACKAGE = "pypmmn"
|
||||||
|
NAME = "pypmmn"
|
||||||
|
DESCRIPTION = "Python port of the 'Poor man's munin-node'"
|
||||||
|
AUTHOR = "Michel Albert"
|
||||||
|
AUTHOR_EMAIL = "michel@albert.lu"
|
||||||
|
|
||||||
|
setup(
|
||||||
|
name=NAME,
|
||||||
|
version=__version__,
|
||||||
|
description=DESCRIPTION,
|
||||||
|
long_description=open("README.rst").read(),
|
||||||
|
author=AUTHOR,
|
||||||
|
author_email=AUTHOR_EMAIL,
|
||||||
|
license="BSD",
|
||||||
|
include_package_data=True,
|
||||||
|
packages=['pypmmn'],
|
||||||
|
scripts=['pypmmn/pypmmn.py'],
|
||||||
|
zip_safe=False,
|
||||||
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue