]>
Commit | Line | Data |
---|---|---|
3adcb20f | 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 | ||
df5b7dcf MT |
22 | import argparse |
23 | import collecty | |
24 | import os | |
25 | import sys | |
73db5226 | 26 | |
df5b7dcf MT |
27 | from collecty.i18n import _ |
28 | ||
29 | class CLI(object): | |
30 | def parse_cli(self): | |
31 | parser = argparse.ArgumentParser( | |
32 | description=_("Collecty Client") | |
33 | ) | |
34 | subparsers = parser.add_subparsers(help="sub-command help") | |
35 | ||
36 | # generate-graph | |
37 | parser_generate_graph = subparsers.add_parser( | |
38 | "generate-graph", help=_("Generate a graph image"), | |
39 | ) | |
40 | parser_generate_graph.add_argument( | |
41 | "--filename", help=_("filename"), required=True, | |
42 | ) | |
43 | parser_generate_graph.add_argument( | |
44 | "--format", help=_("image format"), | |
45 | ) | |
46 | parser_generate_graph.add_argument( | |
47 | "--interval", help=_("interval"), | |
48 | ) | |
49 | parser_generate_graph.add_argument( | |
50 | "--object", help=_("Object identifier"), default="default", | |
51 | ) | |
52 | parser_generate_graph.add_argument( | |
53 | "--template", help=_("The graph template identifier"), required=True, | |
54 | ) | |
55 | parser_generate_graph.add_argument( | |
56 | "--timezone", help=_("Generate the graph with timestamps plotted for the given timezone"), | |
57 | default=os.environ.get("TZ", "UTC"), | |
58 | ) | |
59 | parser_generate_graph.add_argument( | |
60 | "--locale", help=_("Generate the graph with this locale"), | |
61 | default=os.environ.get("LANG", "en_GB.utf8"), | |
62 | ) | |
63 | # Dimensions | |
64 | parser_generate_graph.add_argument( | |
65 | "--height", type=int, default=0, help=_("Height of the generated image"), | |
66 | ) | |
67 | parser_generate_graph.add_argument( | |
68 | "--width", type=int, default=0, help=_("Width of the generated image"), | |
69 | ) | |
70 | parser_generate_graph.set_defaults(func=self._generate_graph) | |
71 | ||
72 | # last-update | |
73 | parser_last_update = subparsers.add_parser( | |
74 | "last-update", help=_("Fetch the last dataset in the database"), | |
75 | ) | |
76 | parser_last_update.add_argument( | |
77 | "--template", help=_("The graph template identifier"), required=True, | |
78 | ) | |
79 | parser_last_update.add_argument( | |
80 | "--object", help=_("Object identifier"), default="default", | |
81 | ) | |
82 | parser_last_update.set_defaults(func=self._last_update) | |
83 | ||
84 | # list-templates | |
85 | parser_list_templates = subparsers.add_parser( | |
86 | "list-templates", help=_("Lists all graph templates"), | |
87 | ) | |
88 | parser_list_templates.set_defaults(func=self._list_templates) | |
89 | ||
90 | # backup | |
91 | backup = subparsers.add_parser( | |
92 | "backup", help=_("Backup all RRD data"), | |
93 | ) | |
94 | backup.add_argument( | |
95 | "filename", nargs="?", help=_("Filename"), | |
96 | ) | |
97 | backup.set_defaults(func=self._backup) | |
98 | ||
99 | # version | |
100 | parser_version = subparsers.add_parser( | |
101 | "version", help=_("Show version"), | |
102 | ) | |
103 | parser_version.set_defaults(func=self._version) | |
104 | ||
105 | args = parser.parse_args() | |
106 | ||
107 | # Print usage if no action was given | |
108 | if not "func" in args: | |
109 | parser.print_usage() | |
110 | sys.exit(2) | |
111 | ||
112 | return args | |
113 | ||
114 | def run(self): | |
115 | # Parse command line arguments | |
116 | args = self.parse_cli() | |
117 | ||
118 | # Initialise client | |
119 | self.client = collecty.Collecty() | |
120 | ||
121 | # Call function | |
122 | ret = args.func(args) | |
123 | ||
124 | # Return with exit code | |
125 | if ret: | |
126 | sys.exit(ret) | |
127 | ||
128 | # Otherwise just exit | |
129 | sys.exit(0) | |
130 | ||
131 | def _backup(self, args): | |
132 | print(_("Backing up...")) | |
133 | ||
134 | self.client.backup(args.filename) | |
135 | ||
136 | def _generate_graph(self, args): | |
137 | kwargs = { | |
138 | "format" : args.format or collecty.util.guess_format(args.filename), | |
139 | "object_id" : args.object, | |
140 | "locale" : args.locale, | |
141 | "timezone" : args.timezone, | |
142 | } | |
143 | ||
144 | if args.height or args.width: | |
145 | kwargs.update({ | |
146 | "height" : args.height or 0, | |
147 | "width" : args.width or 0, | |
148 | }) | |
149 | ||
150 | if args.interval: | |
151 | kwargs["interval"] = args.interval | |
152 | ||
153 | # Generate the graph image | |
154 | graph = self.client.generate_graph(args.template, **kwargs) | |
155 | ||
156 | # Add some useful information | |
157 | info = self.client.graph_info(args.template, **kwargs) | |
158 | if info: | |
159 | graph.update(info) | |
160 | ||
161 | # Write file to disk | |
162 | with open(args.filename, "wb") as f: | |
163 | f.write(graph["image"]) | |
164 | ||
165 | print(_("Title : %(title)s (%(template)s - %(object_id)s)") % graph) | |
166 | print(_("Image size : %(image_width)sx%(image_height)spx") % graph) | |
167 | ||
168 | def _last_update(self, args): | |
169 | last_update = self.client.last_update(args.template, object_id=args.object) | |
170 | ||
171 | print(_("Last update: %s") % last_update.get("timestamp")) | |
172 | ||
173 | dataset = last_update.get("dataset") | |
174 | for k, v in dataset.items(): | |
175 | print("%16s = %s" % (k, v)) | |
176 | ||
177 | def _list_templates(self, args): | |
178 | templates = self.client.list_templates() | |
179 | ||
180 | for t in sorted(templates): | |
181 | print(t) | |
182 | ||
183 | def _version(self, args): | |
184 | version = self.client.version() | |
185 | ||
186 | print(version) | |
187 | ||
188 | ||
189 | def main(): | |
190 | # Run the command line interface | |
191 | c = CLI() | |
192 | c.run() | |
193 | ||
194 | main() |