|
@@ -0,0 +1,91 @@
|
|
1
|
+#!/usr/bin/python
|
|
2
|
+
|
|
3
|
+"""Generic mon echoer
|
|
4
|
+
|
|
5
|
+usage: gmon [--format=<format>] [-p|--plain] [-x|--xgp] <command> [<args>...]
|
|
6
|
+
|
|
7
|
+Options:
|
|
8
|
+ -c, --chars specify limit in characters
|
|
9
|
+ --format <format> specify format: plain is a plain text, xgp
|
|
10
|
+ is specific for xfce4-genmon-plugin
|
|
11
|
+ -p, --plain same as --format=plain
|
|
12
|
+ -x, --xgp same as --format=xgp
|
|
13
|
+ -h, --help show this screen
|
|
14
|
+ --version show version
|
|
15
|
+"""
|
|
16
|
+
|
|
17
|
+import os
|
|
18
|
+import sys
|
|
19
|
+import imp
|
|
20
|
+
|
|
21
|
+import docopt
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+class PluginData(object):
|
|
25
|
+
|
|
26
|
+ def __init__(self, cmdargs):
|
|
27
|
+ self.args = []
|
|
28
|
+ self.fmt = "plain"
|
|
29
|
+ self.maxlen = 40
|
|
30
|
+ self.name = ""
|
|
31
|
+ self._load(cmdargs)
|
|
32
|
+
|
|
33
|
+ def _load(self, cmdargs):
|
|
34
|
+ if cmdargs['--plain']:
|
|
35
|
+ self.fmt = "plain"
|
|
36
|
+ elif cmdargs['--xgp']:
|
|
37
|
+ self.fmt = "xgp"
|
|
38
|
+ self.name = cmdargs['<command>']
|
|
39
|
+ self.args = cmdargs['<args>']
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+class Subcommand(object):
|
|
43
|
+
|
|
44
|
+ def __init__(self, args, pluginpath):
|
|
45
|
+ self.plugindata = PluginData(args)
|
|
46
|
+ self.name = args['<command>']
|
|
47
|
+ self._load_plugin(pluginpath)
|
|
48
|
+
|
|
49
|
+ def _load_plugin(self, pluginpath):
|
|
50
|
+ modpath = "%s/%s.py" % (pluginpath, self.name)
|
|
51
|
+ with open(modpath) as fh:
|
|
52
|
+ module = imp.load_module(self.name, fh, modpath,
|
|
53
|
+ (".py", "r", imp.PY_SOURCE))
|
|
54
|
+ self.plugin = module.Plugin(self.plugindata)
|
|
55
|
+
|
|
56
|
+ def _format_output(self, raw):
|
|
57
|
+ suffix = "..."
|
|
58
|
+ cooked = raw
|
|
59
|
+ if self.plugindata.fmt == "plain":
|
|
60
|
+ if len(raw) > self.plugindata.maxlen:
|
|
61
|
+ newlen = self.plugindata.maxlen - len(suffix)
|
|
62
|
+ cooked = raw[:newlen] + suffix
|
|
63
|
+ return cooked
|
|
64
|
+
|
|
65
|
+ def get_output(self):
|
|
66
|
+ return self._format_output(self.plugin.render())
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+class App:
|
|
70
|
+
|
|
71
|
+ def __init__(self, args):
|
|
72
|
+ self._set_pluginpath()
|
|
73
|
+ self.subcommand = Subcommand(args, self.pluginpath)
|
|
74
|
+
|
|
75
|
+ def _set_pluginpath(self):
|
|
76
|
+ mypath = os.path.dirname(os.path.realpath(sys.argv[0]))
|
|
77
|
+ path = mypath + "/plugins"
|
|
78
|
+ path = os.environ.get("GMON_PLUGINS", path)
|
|
79
|
+ self.pluginpath = path
|
|
80
|
+
|
|
81
|
+ def run(self):
|
|
82
|
+ print self.subcommand.get_output()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+if __name__ == '__main__':
|
|
86
|
+
|
|
87
|
+ args = docopt.docopt(__doc__,
|
|
88
|
+ version="gmon 0.1",
|
|
89
|
+ options_first=True)
|
|
90
|
+ app = App(args)
|
|
91
|
+ app.run()
|