7b3c3ec71e3fc0d76cd2197f1d1142177aed4e1c
[collecty.git] / src / collecty / client.py
1 #!/usr/bin/python3
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
22 import argparse
23 import dbus
24 import os
25 import platform
26 import sys
27
28 from .constants import *
29 from .i18n import _
30
31 import logging
32 log = logging.getLogger("collectly.client")
33
34 class CollectyClient(object):
35         def __init__(self):
36                 self.bus = dbus.SystemBus()
37
38                 self.proxy = self.bus.get_object(BUS_DOMAIN, "/GraphGenerator")
39
40         def list_templates(self):
41                 templates = self.proxy.ListTemplates()
42
43                 return ["%s" % t for t in templates]
44
45         def list_templates_cli(self, ns):
46                 templates = self.list_templates()
47
48                 for t in sorted(templates):
49                         print(t)
50
51         def generate_graph(self, template_name, **kwargs):
52                 byte_array = self.proxy.GenerateGraph(template_name, kwargs,
53                         signature="sa{sv}")
54
55                 # Convert the byte array into a byte string again
56                 if byte_array:
57                         return bytes(byte_array)
58
59         def generate_graph_cli(self, ns):
60                 kwargs = {
61                         "format"    : ns.format,
62                         "object_id" : ns.object,
63                 }
64
65                 if ns.height or ns.width:
66                         kwargs.update({
67                                 "height" : ns.height or 0,
68                                 "width"  : ns.width or 0,
69                         })
70
71                 if ns.interval:
72                         kwargs["interval"] = ns.interval
73
74                 kwargs.update({
75                         "locale"   : ns.locale,
76                         "timezone" : ns.timezone,
77                 })
78
79                 # Generate the graph image
80                 graph = self.generate_graph(ns.template, **kwargs)
81
82                 # Write file to disk
83                 with open(ns.filename, "wb") as f:
84                         f.write(graph)
85
86         def version_cli(self, args):
87                 daemon_version = self.proxy.Version()
88
89                 print(_("collecty %s running on Python %s") % \
90                         (COLLECTY_VERSION, platform.python_version()))
91
92                 if not COLLECTY_VERSION == daemon_version:
93                         print(_("daemon %s") % daemon_version)
94
95         def parse_cli(self, args):
96                 parser = argparse.ArgumentParser(prog="collecty-client")
97                 subparsers = parser.add_subparsers(help="sub-command help")
98
99                 # generate-graph
100                 parser_generate_graph = subparsers.add_parser("generate-graph",
101                         help=_("Generate a graph image"))
102                 parser_generate_graph.set_defaults(func=self.generate_graph_cli)
103                 parser_generate_graph.add_argument("--filename",
104                         help=_("filename"), required=True)
105                 parser_generate_graph.add_argument("--format",
106                         help=_("image format"), default=DEFAULT_IMAGE_FORMAT)
107                 parser_generate_graph.add_argument("--interval", help=_("interval"))
108                 parser_generate_graph.add_argument("--object",
109                         help=_("Object identifier"), default="default")
110                 parser_generate_graph.add_argument("--template",
111                         help=_("The graph template identifier"), required=True)
112                 parser_generate_graph.add_argument("--timezone", default=os.environ.get("TZ", "UTC"),
113                         help=_("Generate the graph with timestamps plotted for the given timezone"))
114                 parser_generate_graph.add_argument("--locale", default=os.environ.get("LANG", "en_GB.utf8"),
115                         help=_("Generate the graph with this locale"))
116
117                 # Dimensions
118                 parser_generate_graph.add_argument("--height", type=int, default=0,
119                         help=_("Height of the generated image"))
120                 parser_generate_graph.add_argument("--width", type=int, default=0,
121                         help=_("Width of the generated image"))
122
123                 # list-templates
124                 parser_list_templates = subparsers.add_parser("list-templates",
125                         help=_("Lists all graph templates"))
126                 parser_list_templates.set_defaults(func=self.list_templates_cli)
127
128                 # version
129                 parser_version = subparsers.add_parser("version", help=_("Show version"))
130                 parser_version.set_defaults(func=self.version_cli)
131
132                 return parser.parse_args(args)
133
134         def run_cli(self, args=None):
135                 args = self.parse_cli(args or sys.argv[1:])
136
137                 return args.func(args)