Add graph info functionality
[collecty.git] / src / collecty / client.py
CommitLineData
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
MT
22import argparse
23import dbus
cb1ccb4f 24import os
73241420 25import platform
c968f6d9
MT
26import sys
27
f37913e8
MT
28from .constants import *
29from .i18n import _
73db5226
MT
30
31import logging
32log = logging.getLogger("collectly.client")
33
34class CollectyClient(object):
c968f6d9
MT
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):
f37913e8 49 print(t)
c968f6d9 50
a3864812
MT
51 def graph_info(self, template_name, **kwargs):
52 graph_info = self.proxy.GraphInfo(template_name, kwargs,
53 signature="sa{sv}")
54
55 return dict(graph_info)
56
c968f6d9 57 def generate_graph(self, template_name, **kwargs):
a3864812 58 graph = self.proxy.GenerateGraph(template_name, kwargs,
c968f6d9
MT
59 signature="sa{sv}")
60
61 # Convert the byte array into a byte string again
a3864812
MT
62 if graph:
63 graph["image"] = bytes(graph["image"])
64
65 return graph
c968f6d9
MT
66
67 def generate_graph_cli(self, ns):
68 kwargs = {
5913a52c 69 "format" : ns.format,
c968f6d9
MT
70 "object_id" : ns.object,
71 }
72
73 if ns.height or ns.width:
74 kwargs.update({
75 "height" : ns.height or 0,
76 "width" : ns.width or 0,
77 })
78
79 if ns.interval:
80 kwargs["interval"] = ns.interval
81
cb1ccb4f
MT
82 kwargs.update({
83 "locale" : ns.locale,
84 "timezone" : ns.timezone,
85 })
86
c968f6d9
MT
87 # Generate the graph image
88 graph = self.generate_graph(ns.template, **kwargs)
89
a3864812
MT
90 # Add some useful information
91 info = self.graph_info(ns.template, **kwargs)
92 if info:
93 graph.update(info)
94
c968f6d9
MT
95 # Write file to disk
96 with open(ns.filename, "wb") as f:
a3864812
MT
97 f.write(graph["image"])
98
99 print(_("Title : %(title)s (%(template)s - %(object_id)s)") % graph)
100 print(_("Image size : %(image_width)sx%(image_height)spx") % graph)
c968f6d9 101
73241420
MT
102 def version_cli(self, args):
103 daemon_version = self.proxy.Version()
104
105 print(_("collecty %s running on Python %s") % \
106 (COLLECTY_VERSION, platform.python_version()))
107
108 if not COLLECTY_VERSION == daemon_version:
109 print(_("daemon %s") % daemon_version)
110
c968f6d9
MT
111 def parse_cli(self, args):
112 parser = argparse.ArgumentParser(prog="collecty-client")
113 subparsers = parser.add_subparsers(help="sub-command help")
114
115 # generate-graph
116 parser_generate_graph = subparsers.add_parser("generate-graph",
117 help=_("Generate a graph image"))
118 parser_generate_graph.set_defaults(func=self.generate_graph_cli)
119 parser_generate_graph.add_argument("--filename",
120 help=_("filename"), required=True)
5913a52c
MT
121 parser_generate_graph.add_argument("--format",
122 help=_("image format"), default=DEFAULT_IMAGE_FORMAT)
c968f6d9
MT
123 parser_generate_graph.add_argument("--interval", help=_("interval"))
124 parser_generate_graph.add_argument("--object",
125 help=_("Object identifier"), default="default")
126 parser_generate_graph.add_argument("--template",
127 help=_("The graph template identifier"), required=True)
cb1ccb4f
MT
128 parser_generate_graph.add_argument("--timezone", default=os.environ.get("TZ", "UTC"),
129 help=_("Generate the graph with timestamps plotted for the given timezone"))
130 parser_generate_graph.add_argument("--locale", default=os.environ.get("LANG", "en_GB.utf8"),
131 help=_("Generate the graph with this locale"))
73db5226 132
c968f6d9
MT
133 # Dimensions
134 parser_generate_graph.add_argument("--height", type=int, default=0,
135 help=_("Height of the generated image"))
136 parser_generate_graph.add_argument("--width", type=int, default=0,
137 help=_("Width of the generated image"))
73db5226 138
c968f6d9
MT
139 # list-templates
140 parser_list_templates = subparsers.add_parser("list-templates",
141 help=_("Lists all graph templates"))
142 parser_list_templates.set_defaults(func=self.list_templates_cli)
73db5226 143
73241420
MT
144 # version
145 parser_version = subparsers.add_parser("version", help=_("Show version"))
146 parser_version.set_defaults(func=self.version_cli)
147
c968f6d9 148 return parser.parse_args(args)
73db5226 149
c968f6d9
MT
150 def run_cli(self, args=None):
151 args = self.parse_cli(args or sys.argv[1:])
73db5226 152
c968f6d9 153 return args.func(args)