1 | # -*- coding: utf-8 -*- |
||
2 | # |
||
3 | # This file is part of Glances. |
||
4 | # |
||
5 | # Copyright (C) 2017 Nicolargo <[email protected]> |
||
6 | # |
||
7 | # Glances is free software; you can redistribute it and/or modify |
||
8 | # it under the terms of the GNU Lesser 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 | # Glances 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 Lesser General Public License for more details. |
||
16 | # |
||
17 | # You should have received a copy of the GNU Lesser General Public License |
||
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
19 | |||
20 | """Restful interface class.""" |
||
21 | |||
22 | import sys |
||
23 | |||
24 | from glances.compat import listkeys |
||
25 | from glances.logger import logger |
||
26 | from glances.exports.glances_export import GlancesExport |
||
27 | |||
28 | from requests import post |
||
29 | |||
30 | |||
31 | class Export(GlancesExport): |
||
32 | |||
33 | """This class manages the Restful export module. |
||
34 | Be aware that stats will be exported in one big POST request""" |
||
35 | |||
36 | View Code Duplication | def __init__(self, config=None, args=None): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
37 | """Init the Restful export IF.""" |
||
38 | super(Export, self).__init__(config=config, args=args) |
||
39 | |||
40 | # Mandatories configuration keys (additional to host and port) |
||
41 | self.protocol = None |
||
42 | self.path = None |
||
43 | |||
44 | # Load the Restful section in the configuration file |
||
45 | self.export_enable = self.load_conf('restful', |
||
46 | mandatories=['host', 'port', 'protocol', 'path']) |
||
47 | if not self.export_enable: |
||
48 | sys.exit(2) |
||
49 | |||
50 | # Init the stats buffer |
||
51 | # It's a dict of stats |
||
52 | self.buffer = {} |
||
53 | |||
54 | # Init the Statsd client |
||
55 | self.client = self.init() |
||
56 | |||
57 | def init(self): |
||
58 | """Init the connection to the restful server.""" |
||
59 | if not self.export_enable: |
||
60 | return None |
||
61 | # Build the Restful URL where the stats will be posted |
||
62 | url = '{}://{}:{}{}'.format(self.protocol, |
||
63 | self.host, |
||
64 | self.port, |
||
65 | self.path) |
||
66 | logger.info( |
||
67 | "Stats will be exported to the restful endpoint {}".format(url)) |
||
68 | return url |
||
69 | |||
70 | def export(self, name, columns, points): |
||
71 | """Export the stats to the Statsd server.""" |
||
72 | if name == self.plugins_to_export()[0] and self.buffer != {}: |
||
73 | # One complete loop have been done |
||
74 | logger.debug("Export stats ({}) to Restful endpoint ({})".format(listkeys(self.buffer), |
||
75 | self.client)) |
||
76 | # Export stats |
||
77 | post(self.client, json=self.buffer, allow_redirects=True) |
||
78 | # Reset buffer |
||
79 | self.buffer = {} |
||
80 | |||
81 | # Add current stat to the buffer |
||
82 | self.buffer[name] = dict(zip(columns, points)) |
||
83 |