]>
git.ipfire.org Git - collecty.git/blob - src/collecty/client.py
2 ###############################################################################
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2012 IPFire development team #
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. #
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. #
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/>. #
20 ###############################################################################
29 from .constants
import *
33 log
= logging
.getLogger("collectly.client")
35 class CollectyClient(object):
37 self
.bus
= dbus
.SystemBus()
39 self
.proxy
= self
.bus
.get_object(BUS_DOMAIN
, "/GraphGenerator")
41 def last_update(self
, template_name
, **kwargs
):
42 last_update
= self
.proxy
.LastUpdate(template_name
, kwargs
)
45 last_update
["timestamp"] = datetime
.datetime
.strptime(last_update
["timestamp"], "%Y-%m-%dT%H:%M:%S")
49 def last_update_cli(self
, ns
):
50 last_update
= self
.last_update(ns
.template
, object_id
=ns
.object)
52 print(_("Last update: %s") % last_update
.get("timestamp"))
54 dataset
= last_update
.get("dataset")
55 for k
, v
in dataset
.items():
56 print("%16s = %s" % (k
, v
))
58 def list_templates(self
):
59 templates
= self
.proxy
.ListTemplates()
61 return ["%s" % t
for t
in templates
]
63 def list_templates_cli(self
, ns
):
64 templates
= self
.list_templates()
66 for t
in sorted(templates
):
69 def graph_info(self
, template_name
, **kwargs
):
70 graph_info
= self
.proxy
.GraphInfo(template_name
, kwargs
,
73 return dict(graph_info
)
75 def generate_graph(self
, template_name
, **kwargs
):
76 graph
= self
.proxy
.GenerateGraph(template_name
, kwargs
,
79 # Convert the byte array into a byte string again
81 graph
["image"] = bytes(graph
["image"])
85 def generate_graph_cli(self
, ns
):
88 "object_id" : ns
.object,
91 if ns
.height
or ns
.width
:
93 "height" : ns
.height
or 0,
94 "width" : ns
.width
or 0,
98 kwargs
["interval"] = ns
.interval
101 "locale" : ns
.locale
,
102 "timezone" : ns
.timezone
,
105 # Generate the graph image
106 graph
= self
.generate_graph(ns
.template
, **kwargs
)
108 # Add some useful information
109 info
= self
.graph_info(ns
.template
, **kwargs
)
114 with
open(ns
.filename
, "wb") as f
:
115 f
.write(graph
["image"])
117 print(_("Title : %(title)s (%(template)s - %(object_id)s)") % graph
)
118 print(_("Image size : %(image_width)sx%(image_height)spx") % graph
)
120 def version_cli(self
, args
):
121 daemon_version
= self
.proxy
.Version()
123 print(_("collecty %s running on Python %s") % \
124 (COLLECTY_VERSION
, platform
.python_version()))
126 if not COLLECTY_VERSION
== daemon_version
:
127 print(_("daemon %s") % daemon_version
)
129 def parse_cli(self
, args
):
130 parser
= argparse
.ArgumentParser(prog
="collecty-client")
131 subparsers
= parser
.add_subparsers(help="sub-command help")
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)
139 parser_generate_graph
.add_argument("--format",
140 help=_("image format"), default
=DEFAULT_IMAGE_FORMAT
)
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)
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"))
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"))
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
)
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
)
172 parser_version
= subparsers
.add_parser("version", help=_("Show version"))
173 parser_version
.set_defaults(func
=self
.version_cli
)
175 return parser
.parse_args(args
)
177 def run_cli(self
, args
=None):
178 args
= self
.parse_cli(args
or sys
.argv
[1:])
180 return args
.func(args
)