1 | # -*- coding: utf-8 -*- |
||
2 | # |
||
3 | # This file is part of Glances. |
||
4 | # |
||
5 | # Copyright (C) 2015 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 | """JMS interface class.""" |
||
21 | |||
22 | import datetime |
||
23 | import socket |
||
24 | import sys |
||
25 | from numbers import Number |
||
26 | |||
27 | from glances.compat import NoOptionError, NoSectionError, range |
||
28 | from glances.logger import logger |
||
29 | from glances.exports.glances_export import GlancesExport |
||
30 | |||
31 | # Import pika for RabbitMQ |
||
32 | import pika |
||
33 | |||
34 | |||
35 | class Export(GlancesExport): |
||
36 | |||
37 | """This class manages the rabbitMQ export module.""" |
||
38 | |||
39 | View Code Duplication | def __init__(self, config=None, args=None): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
40 | """Init the RabbitMQ export IF.""" |
||
41 | super(Export, self).__init__(config=config, args=args) |
||
42 | |||
43 | # Load the rabbitMQ configuration file |
||
44 | self.rabbitmq_host = None |
||
45 | self.rabbitmq_port = None |
||
46 | self.rabbitmq_user = None |
||
47 | self.rabbitmq_password = None |
||
48 | self.rabbitmq_queue = None |
||
49 | self.hostname = socket.gethostname() |
||
50 | self.export_enable = self.load_conf() |
||
51 | if not self.export_enable: |
||
52 | sys.exit(2) |
||
53 | |||
54 | # Init the rabbitmq client |
||
55 | self.client = self.init() |
||
56 | |||
57 | def load_conf(self, section="rabbitmq"): |
||
58 | """Load the rabbitmq configuration in the Glances configuration file.""" |
||
59 | if self.config is None: |
||
60 | return False |
||
61 | try: |
||
62 | self.rabbitmq_host = self.config.get_value(section, 'host') |
||
63 | self.rabbitmq_port = self.config.get_value(section, 'port') |
||
64 | self.rabbitmq_user = self.config.get_value(section, 'user') |
||
65 | self.rabbitmq_password = self.config.get_value(section, 'password') |
||
66 | self.rabbitmq_queue = self.config.get_value(section, 'queue') |
||
67 | except NoSectionError: |
||
68 | logger.critical("No rabbitmq configuration found") |
||
69 | return False |
||
70 | except NoOptionError as e: |
||
71 | logger.critical("Error in the RabbitM configuration (%s)" % e) |
||
72 | return False |
||
73 | else: |
||
74 | logger.debug("Load RabbitMQ from the Glances configuration file") |
||
75 | return True |
||
76 | |||
77 | def init(self): |
||
78 | """Init the connection to the rabbitmq server.""" |
||
79 | if not self.export_enable: |
||
80 | return None |
||
81 | try: |
||
82 | parameters = pika.URLParameters( |
||
83 | 'amqp://' + self.rabbitmq_user + |
||
84 | ':' + self.rabbitmq_password + |
||
85 | '@' + self.rabbitmq_host + |
||
86 | ':' + self.rabbitmq_port + '/') |
||
87 | connection = pika.BlockingConnection(parameters) |
||
88 | channel = connection.channel() |
||
89 | return channel |
||
90 | except Exception as e: |
||
91 | logger.critical("Connection to rabbitMQ failed : %s " % e) |
||
92 | return None |
||
93 | |||
94 | def export(self, name, columns, points): |
||
95 | """Write the points in RabbitMQ.""" |
||
96 | data = ('hostname=' + self.hostname + ', name=' + name + |
||
97 | ', dateinfo=' + datetime.datetime.utcnow().isoformat()) |
||
98 | for i in range(len(columns)): |
||
99 | if not isinstance(points[i], Number): |
||
100 | continue |
||
101 | else: |
||
102 | data += ", " + columns[i] + "=" + str(points[i]) |
||
103 | logger.debug(data) |
||
104 | try: |
||
105 | self.client.basic_publish(exchange='', routing_key=self.rabbitmq_queue, body=data) |
||
106 | except Exception as e: |
||
107 | logger.error("Can not export stats to RabbitMQ (%s)" % e) |
||
108 |