]>
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 = { | |
5913a52c | 87 | "format" : ns.format, |
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 | |
73241420 MT |
120 | def version_cli(self, args): |
121 | daemon_version = self.proxy.Version() | |
122 | ||
123 | print(_("collecty %s running on Python %s") % \ | |
124 | (COLLECTY_VERSION, platform.python_version())) | |
125 | ||
126 | if not COLLECTY_VERSION == daemon_version: | |
127 | print(_("daemon %s") % daemon_version) | |
128 | ||
c968f6d9 MT |
129 | def parse_cli(self, args): |
130 | parser = argparse.ArgumentParser(prog="collecty-client") | |
131 | subparsers = parser.add_subparsers(help="sub-command help") | |
132 | ||
133 | # generate-graph | |
134 | parser_generate_graph = subparsers.add_parser("generate-graph", | |
135 | help=_("Generate a graph image")) | |
136 | parser_generate_graph.set_defaults(func=self.generate_graph_cli) | |
137 | parser_generate_graph.add_argument("--filename", | |
138 | help=_("filename"), required=True) | |
5913a52c MT |
139 | parser_generate_graph.add_argument("--format", |
140 | help=_("image format"), default=DEFAULT_IMAGE_FORMAT) | |
c968f6d9 MT |
141 | parser_generate_graph.add_argument("--interval", help=_("interval")) |
142 | parser_generate_graph.add_argument("--object", | |
143 | help=_("Object identifier"), default="default") | |
144 | parser_generate_graph.add_argument("--template", | |
145 | help=_("The graph template identifier"), required=True) | |
cb1ccb4f MT |
146 | parser_generate_graph.add_argument("--timezone", default=os.environ.get("TZ", "UTC"), |
147 | help=_("Generate the graph with timestamps plotted for the given timezone")) | |
148 | parser_generate_graph.add_argument("--locale", default=os.environ.get("LANG", "en_GB.utf8"), | |
149 | help=_("Generate the graph with this locale")) | |
73db5226 | 150 | |
c968f6d9 MT |
151 | # Dimensions |
152 | parser_generate_graph.add_argument("--height", type=int, default=0, | |
153 | help=_("Height of the generated image")) | |
154 | parser_generate_graph.add_argument("--width", type=int, default=0, | |
155 | help=_("Width of the generated image")) | |
73db5226 | 156 | |
8ee5a71a MT |
157 | # last-update |
158 | parser_last_update = subparsers.add_parser("last-update", | |
159 | help=_("Fetch the last dataset in the database")) | |
160 | parser_last_update.add_argument("--template", | |
161 | help=_("The graph template identifier"), required=True) | |
162 | parser_last_update.add_argument("--object", | |
163 | help=_("Object identifier"), default="default") | |
164 | parser_last_update.set_defaults(func=self.last_update_cli) | |
165 | ||
c968f6d9 MT |
166 | # list-templates |
167 | parser_list_templates = subparsers.add_parser("list-templates", | |
168 | help=_("Lists all graph templates")) | |
169 | parser_list_templates.set_defaults(func=self.list_templates_cli) | |
73db5226 | 170 | |
73241420 MT |
171 | # version |
172 | parser_version = subparsers.add_parser("version", help=_("Show version")) | |
173 | parser_version.set_defaults(func=self.version_cli) | |
174 | ||
c968f6d9 | 175 | return parser.parse_args(args) |
73db5226 | 176 | |
c968f6d9 MT |
177 | def run_cli(self, args=None): |
178 | args = self.parse_cli(args or sys.argv[1:]) | |
73db5226 | 179 | |
c968f6d9 | 180 | return args.func(args) |