]>
Commit | Line | Data |
---|---|---|
f37913e8 | 1 | #!/usr/bin/python3 |
73db5226 MT |
2 | ############################################################################### |
3 | # # | |
4 | # collecty - A system statistics collection daemon for IPFire # | |
5 | # Copyright (C) 2012 IPFire development team # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
c968f6d9 | 22 | import argparse |
8ee5a71a | 23 | import datetime |
c968f6d9 | 24 | import dbus |
cb1ccb4f | 25 | import os |
73241420 | 26 | import platform |
c968f6d9 MT |
27 | import sys |
28 | ||
f37913e8 MT |
29 | from .constants import * |
30 | from .i18n import _ | |
73db5226 MT |
31 | |
32 | import logging | |
33 | log = logging.getLogger("collectly.client") | |
34 | ||
35 | class CollectyClient(object): | |
c968f6d9 MT |
36 | def __init__(self): |
37 | self.bus = dbus.SystemBus() | |
38 | ||
39 | self.proxy = self.bus.get_object(BUS_DOMAIN, "/GraphGenerator") | |
40 | ||
8ee5a71a MT |
41 | def last_update(self, template_name, **kwargs): |
42 | last_update = self.proxy.LastUpdate(template_name, kwargs) | |
43 | ||
44 | if last_update: | |
45 | last_update["timestamp"] = datetime.datetime.strptime(last_update["timestamp"], "%Y-%m-%dT%H:%M:%S") | |
46 | ||
47 | return last_update | |
48 | ||
49 | def last_update_cli(self, ns): | |
50 | last_update = self.last_update(ns.template, object_id=ns.object) | |
51 | ||
52 | print(_("Last update: %s") % last_update.get("timestamp")) | |
53 | ||
54 | dataset = last_update.get("dataset") | |
55 | for k, v in dataset.items(): | |
56 | print("%16s = %s" % (k, v)) | |
57 | ||
c968f6d9 MT |
58 | def list_templates(self): |
59 | templates = self.proxy.ListTemplates() | |
60 | ||
61 | return ["%s" % t for t in templates] | |
62 | ||
63 | def list_templates_cli(self, ns): | |
64 | templates = self.list_templates() | |
65 | ||
66 | for t in sorted(templates): | |
f37913e8 | 67 | print(t) |
c968f6d9 | 68 | |
a3864812 MT |
69 | def graph_info(self, template_name, **kwargs): |
70 | graph_info = self.proxy.GraphInfo(template_name, kwargs, | |
71 | signature="sa{sv}") | |
72 | ||
73 | return dict(graph_info) | |
74 | ||
c968f6d9 | 75 | def generate_graph(self, template_name, **kwargs): |
a3864812 | 76 | graph = self.proxy.GenerateGraph(template_name, kwargs, |
c968f6d9 MT |
77 | signature="sa{sv}") |
78 | ||
79 | # Convert the byte array into a byte string again | |
a3864812 MT |
80 | if graph: |
81 | graph["image"] = bytes(graph["image"]) | |
82 | ||
83 | return graph | |
c968f6d9 MT |
84 | |
85 | def generate_graph_cli(self, ns): | |
86 | kwargs = { | |
34318d08 | 87 | "format" : ns.format or self._guess_format(ns.filename), |
c968f6d9 MT |
88 | "object_id" : ns.object, |
89 | } | |
90 | ||
91 | if ns.height or ns.width: | |
92 | kwargs.update({ | |
93 | "height" : ns.height or 0, | |
94 | "width" : ns.width or 0, | |
95 | }) | |
96 | ||
97 | if ns.interval: | |
98 | kwargs["interval"] = ns.interval | |
99 | ||
cb1ccb4f MT |
100 | kwargs.update({ |
101 | "locale" : ns.locale, | |
102 | "timezone" : ns.timezone, | |
103 | }) | |
104 | ||
c968f6d9 MT |
105 | # Generate the graph image |
106 | graph = self.generate_graph(ns.template, **kwargs) | |
107 | ||
a3864812 MT |
108 | # Add some useful information |
109 | info = self.graph_info(ns.template, **kwargs) | |
110 | if info: | |
111 | graph.update(info) | |
112 | ||
c968f6d9 MT |
113 | # Write file to disk |
114 | with open(ns.filename, "wb") as f: | |
a3864812 MT |
115 | f.write(graph["image"]) |
116 | ||
117 | print(_("Title : %(title)s (%(template)s - %(object_id)s)") % graph) | |
118 | print(_("Image size : %(image_width)sx%(image_height)spx") % graph) | |
c968f6d9 | 119 | |
34318d08 MT |
120 | def _guess_format(self, filename): |
121 | parts = filename.split(".") | |
122 | ||
123 | if parts: | |
124 | # The extension is the last part | |
125 | extension = parts[-1] | |
126 | ||
127 | # Image formats are all uppercase | |
128 | extension = extension.upper() | |
129 | ||
130 | if extension in SUPPORTED_IMAGE_FORMATS: | |
131 | return extension | |
132 | ||
133 | # Otherwise fall back to the default format | |
134 | return DEFAULT_IMAGE_FORMAT | |
135 | ||
73241420 MT |
136 | def version_cli(self, args): |
137 | daemon_version = self.proxy.Version() | |
138 | ||
139 | print(_("collecty %s running on Python %s") % \ | |
140 | (COLLECTY_VERSION, platform.python_version())) | |
141 | ||
142 | if not COLLECTY_VERSION == daemon_version: | |
143 | print(_("daemon %s") % daemon_version) | |
144 | ||
c968f6d9 MT |
145 | def parse_cli(self, args): |
146 | parser = argparse.ArgumentParser(prog="collecty-client") | |
147 | subparsers = parser.add_subparsers(help="sub-command help") | |
148 | ||
149 | # generate-graph | |
150 | parser_generate_graph = subparsers.add_parser("generate-graph", | |
151 | help=_("Generate a graph image")) | |
152 | parser_generate_graph.set_defaults(func=self.generate_graph_cli) | |
153 | parser_generate_graph.add_argument("--filename", | |
154 | help=_("filename"), required=True) | |
34318d08 | 155 | parser_generate_graph.add_argument("--format", help=_("image format")) |
c968f6d9 MT |
156 | parser_generate_graph.add_argument("--interval", help=_("interval")) |
157 | parser_generate_graph.add_argument("--object", | |
158 | help=_("Object identifier"), default="default") | |
159 | parser_generate_graph.add_argument("--template", | |
160 | help=_("The graph template identifier"), required=True) | |
cb1ccb4f MT |
161 | parser_generate_graph.add_argument("--timezone", default=os.environ.get("TZ", "UTC"), |
162 | help=_("Generate the graph with timestamps plotted for the given timezone")) | |
163 | parser_generate_graph.add_argument("--locale", default=os.environ.get("LANG", "en_GB.utf8"), | |
164 | help=_("Generate the graph with this locale")) | |
73db5226 | 165 | |
c968f6d9 MT |
166 | # Dimensions |
167 | parser_generate_graph.add_argument("--height", type=int, default=0, | |
168 | help=_("Height of the generated image")) | |
169 | parser_generate_graph.add_argument("--width", type=int, default=0, | |
170 | help=_("Width of the generated image")) | |
73db5226 | 171 | |
8ee5a71a MT |
172 | # last-update |
173 | parser_last_update = subparsers.add_parser("last-update", | |
174 | help=_("Fetch the last dataset in the database")) | |
175 | parser_last_update.add_argument("--template", | |
176 | help=_("The graph template identifier"), required=True) | |
177 | parser_last_update.add_argument("--object", | |
178 | help=_("Object identifier"), default="default") | |
179 | parser_last_update.set_defaults(func=self.last_update_cli) | |
180 | ||
c968f6d9 MT |
181 | # list-templates |
182 | parser_list_templates = subparsers.add_parser("list-templates", | |
183 | help=_("Lists all graph templates")) | |
184 | parser_list_templates.set_defaults(func=self.list_templates_cli) | |
73db5226 | 185 | |
73241420 MT |
186 | # version |
187 | parser_version = subparsers.add_parser("version", help=_("Show version")) | |
188 | parser_version.set_defaults(func=self.version_cli) | |
189 | ||
c968f6d9 | 190 | return parser.parse_args(args) |
73db5226 | 191 | |
c968f6d9 MT |
192 | def run_cli(self, args=None): |
193 | args = self.parse_cli(args or sys.argv[1:]) | |
73db5226 | 194 | |
c968f6d9 | 195 | return args.func(args) |