Completed
Push — master ( bcff18...adb900 )
by Nicolas
01:15
created

glances.GlancesStats.get_plugin_list()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 3
rs 10
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
"""The stats manager."""
21
22
import collections
23
import os
24
import re
25
import sys
26
import threading
27
28
from glances.compat import iteritems
29
from glances.globals import exports_path, plugins_path, sys_path
30
from glances.logger import logger
31
32
# SNMP OID regexp pattern to short system name dict
33
oid_to_short_system_name = {'.*Linux.*': 'linux',
34
                            '.*Darwin.*': 'mac',
35
                            '.*BSD.*': 'bsd',
36
                            '.*Windows.*': 'windows',
37
                            '.*Cisco.*': 'cisco',
38
                            '.*VMware ESXi.*': 'esxi',
39
                            '.*NetApp.*': 'netapp'}
40
41
42
class GlancesStats(object):
43
44
    """This class stores, updates and gives stats."""
45
46
    def __init__(self, config=None, args=None):
47
        # Set the argument instance
48
        self.args = args
49
50
        # Set the config instance
51
        self.config = config
52
53
        # Load plugins and export modules
54
        self.load_plugins_and_exports(self.args)
55
56
        # Load the limits
57
        self.load_limits(config)
58
59
    def __getattr__(self, item):
60
        """Overwrite the getattr method in case of attribute is not found.
61
62
        The goal is to dynamically generate the following methods:
63
        - getPlugname(): return Plugname stat in JSON format
64
        """
65
        # Check if the attribute starts with 'get'
66
        if item.startswith('get'):
67
            # Get the plugin name
68
            plugname = item[len('get'):].lower()
69
            # Get the plugin instance
70
            plugin = self._plugins[plugname]
71
            if hasattr(plugin, 'get_stats'):
72
                # The method get_stats exist, return it
73
                return getattr(plugin, 'get_stats')
74
            else:
75
                # The method get_stats is not found for the plugin
76
                raise AttributeError(item)
77
        else:
78
            # Default behavior
79
            raise AttributeError(item)
80
81
    def load_plugins_and_exports(self, args):
82
        """Wrapper to load both plugins and export modules."""
83
        # Init the plugins dict
84
        self._plugins = collections.defaultdict(dict)
85
        # Load the plugins
86
        self.load_plugins(args=args)
87
88
        # Init the export modules dict
89
        self._exports = collections.defaultdict(dict)
90
        # Load the export modules
91
        self.load_exports(args=args)
92
93
        # Restoring system path
94
        sys.path = sys_path
95
96
    def load_plugins(self, args=None):
97
        """Load all plugins in the 'plugins' folder."""
98
        header = "glances_"
99
        for item in os.listdir(plugins_path):
100
            if (item.startswith(header) and
101
                    item.endswith(".py") and
102
                    item != (header + "plugin.py")):
103
                # Import the plugin
104
                plugin = __import__(os.path.basename(item)[:-3])
105
                # Add the plugin to the dictionary
106
                # The key is the plugin name
107
                # for example, the file glances_xxx.py
108
                # generate self._plugins_list["xxx"] = ...
109
                plugin_name = os.path.basename(item)[len(header):-3].lower()
110
                if plugin_name == 'help':
111
                    self._plugins[plugin_name] = plugin.Plugin(args=args, config=self.config)
112
                else:
113
                    self._plugins[plugin_name] = plugin.Plugin(args=args)
114
        # Log plugins list
115
        logger.debug("Available plugins list: {0}".format(self.getAllPlugins()))
116
117
    def load_exports(self, args=None):
118
        """Load all export modules in the 'exports' folder."""
119
        if args is None:
120
            return False
121
        header = "glances_"
122
        # Transform the arguments list into a dict
123
        # The aim is to chec if the export module should be loaded
124
        args_var = vars(locals()['args'])
125
        for item in os.listdir(exports_path):
126
            export_name = os.path.basename(item)[len(header):-3].lower()
127
            if (item.startswith(header) and
128
                    item.endswith(".py") and
129
                    item != (header + "export.py") and
130
                    item != (header + "history.py") and
131
                    args_var['export_' + export_name] is not None and
132
                    args_var['export_' + export_name] is not False):
133
                # Import the export module
134
                export_module = __import__(os.path.basename(item)[:-3])
135
                # Add the export to the dictionary
136
                # The key is the module name
137
                # for example, the file glances_xxx.py
138
                # generate self._exports_list["xxx"] = ...
139
                self._exports[export_name] = export_module.Export(args=args, config=self.config)
140
        # Log plugins list
141
        logger.debug("Available exports modules list: {0}".format(self.getExportList()))
142
        return True
143
144
    def getAllPlugins(self):
145
        """Return the plugins list."""
146
        return [p for p in self._plugins]
147
148
    def getExportList(self):
149
        """Return the exports modules list."""
150
        return [p for p in self._exports]
151
152
    def load_limits(self, config=None):
153
        """Load the stats limits."""
154
        # For each plugins, call the init_limits method
155
        for p in self._plugins:
156
            # logger.debug("Load limits for %s" % p)
157
            self._plugins[p].load_limits(config)
158
159
    def update(self):
160
        """Wrapper method to update the stats."""
161
        # For standalone and server modes
162
        # For each plugins, call the update method
163
        for p in self._plugins:
164
            # logger.debug("Update %s stats" % p)
165
            self._plugins[p].update()
166
167
    def export(self, input_stats=None):
168
        """Export all the stats.
169
170
        Each export module is ran in a dedicated thread.
171
        """
172
        # threads = []
173
        input_stats = input_stats or {}
174
175
        for e in self._exports:
176
            logger.debug("Export stats using the %s module" % e)
177
            thread = threading.Thread(target=self._exports[e].update,
178
                                      args=(input_stats,))
179
            # threads.append(thread)
180
            thread.start()
181
182
    def getAll(self):
183
        """Return all the stats (list)."""
184
        return [self._plugins[p].get_raw() for p in self._plugins]
185
186
    def getAllExports(self):
187
        """
188
        Return all the stats to be exported (list).
189
        Default behavor is to export all the stat
190
        """
191
        return [self._plugins[p].get_export() for p in self._plugins]
192
193
    def getAllAsDict(self):
194
        """Return all the stats (dict)."""
195
        # Python > 2.6
196
        # {p: self._plugins[p].get_raw() for p in self._plugins}
197
        ret = {}
198
        for p in self._plugins:
199
            ret[p] = self._plugins[p].get_raw()
200
        return ret
201
202
    def getAllLimits(self):
203
        """Return the plugins limits list."""
204
        return [self._plugins[p].limits for p in self._plugins]
205
206
    def getAllLimitsAsDict(self):
207
        """Return all the stats limits (dict)."""
208
        ret = {}
209
        for p in self._plugins:
210
            ret[p] = self._plugins[p].limits
211
        return ret
212
213
    def getAllViews(self):
214
        """Return the plugins views."""
215
        return [self._plugins[p].get_views() for p in self._plugins]
216
217
    def getAllViewsAsDict(self):
218
        """Return all the stats views (dict)."""
219
        ret = {}
220
        for p in self._plugins:
221
            ret[p] = self._plugins[p].get_views()
222
        return ret
223
224
    def get_plugin_list(self):
225
        """Return the plugin list."""
226
        return self._plugins
227
228
    def get_plugin(self, plugin_name):
229
        """Return the plugin name."""
230
        if plugin_name in self._plugins:
231
            return self._plugins[plugin_name]
232
        else:
233
            return None
234
235
    def end(self):
236
        """End of the Glances stats."""
237
        # Close export modules
238
        for e in self._exports:
239
            self._exports[e].exit()
240
        # Close plugins
241
        for p in self._plugins:
242
            self._plugins[p].exit()
243
244
245
class GlancesStatsServer(GlancesStats):
246
247
    """This class stores, updates and gives stats for the server."""
248
249
    def __init__(self, config=None):
250
        # Init the stats
251
        super(GlancesStatsServer, self).__init__(config)
252
253
        # Init the all_stats dict used by the server
254
        # all_stats is a dict of dicts filled by the server
255
        self.all_stats = collections.defaultdict(dict)
256
257
    def update(self, input_stats=None):
258
        """Update the stats."""
259
        input_stats = input_stats or {}
260
261
        # Force update of all the stats
262
        super(GlancesStatsServer, self).update()
263
264
        # Build all_stats variable (concatenation of all the stats)
265
        self.all_stats = self._set_stats(input_stats)
266
267
    def _set_stats(self, input_stats):
268
        """Set the stats to the input_stats one."""
269
        # Build the all_stats with the get_raw() method of the plugins
270
        ret = collections.defaultdict(dict)
271
        for p in self._plugins:
272
            ret[p] = self._plugins[p].get_raw()
273
        return ret
274
275
    def getAll(self):
276
        """Return the stats as a list."""
277
        return self.all_stats
278
279
    def getAllAsDict(self):
280
        """Return the stats as a dict."""
281
        # Python > 2.6
282
        # return {p: self.all_stats[p] for p in self._plugins}
283
        ret = {}
284
        for p in self._plugins:
285
            ret[p] = self.all_stats[p]
286
        return ret
287
288
289
class GlancesStatsClient(GlancesStats):
290
291
    """This class stores, updates and gives stats for the client."""
292
293
    def __init__(self, config=None, args=None):
294
        """Init the GlancesStatsClient class."""
295
        super(GlancesStatsClient, self).__init__()
296
297
        # Init the configuration
298
        self.config = config
299
300
        # Init the arguments
301
        self.args = args
302
303
        # Load plugins and exports
304
        self.load_plugins_and_exports(self.args)
305
306
    def set_plugins(self, input_plugins):
307
        """Set the plugin list according to the Glances server."""
308
        header = "glances_"
309
        for item in input_plugins:
310
            # Import the plugin
311
            plugin = __import__(header + item)
312
            # Add the plugin to the dictionary
313
            # The key is the plugin name
314
            # for example, the file glances_xxx.py
315
            # generate self._plugins_list["xxx"] = ...
316
            logger.debug("Server uses {0} plugin".format(item))
317
            self._plugins[item] = plugin.Plugin()
318
        # Restoring system path
319
        sys.path = sys_path
320
321
    def update(self, input_stats):
322
        """Update all the stats."""
323
        # For Glances client mode
324
        for p in input_stats:
325
            # Update plugin stats with items sent by the server
326
            self._plugins[p].set_stats(input_stats[p])
327
            # Update the views for the updated stats
328
            self._plugins[p].update_views()
329
330
331
class GlancesStatsClientSNMP(GlancesStats):
332
333
    """This class stores, updates and gives stats for the SNMP client."""
334
335
    def __init__(self, config=None, args=None):
336
        super(GlancesStatsClientSNMP, self).__init__()
337
338
        # Init the configuration
339
        self.config = config
340
341
        # Init the arguments
342
        self.args = args
343
344
        # OS name is used because OID is differents between system
345
        self.os_name = None
346
347
        # Load plugins and export modules
348
        self.load_plugins_and_exports(self.args)
349
350
    def check_snmp(self):
351
        """Chek if SNMP is available on the server."""
352
        # Import the SNMP client class
353
        from glances.snmp import GlancesSNMPClient
354
355
        # Create an instance of the SNMP client
356
        clientsnmp = GlancesSNMPClient(host=self.args.client,
357
                                       port=self.args.snmp_port,
358
                                       version=self.args.snmp_version,
359
                                       community=self.args.snmp_community,
360
                                       user=self.args.snmp_user,
361
                                       auth=self.args.snmp_auth)
362
363
        # If we cannot grab the hostname, then exit...
364
        ret = clientsnmp.get_by_oid("1.3.6.1.2.1.1.5.0") != {}
365
        if ret:
366
            # Get the OS name (need to grab the good OID...)
367
            oid_os_name = clientsnmp.get_by_oid("1.3.6.1.2.1.1.1.0")
368
            try:
369
                self.system_name = self.get_system_name(oid_os_name['1.3.6.1.2.1.1.1.0'])
370
                logger.info("SNMP system name detected: {0}".format(self.system_name))
371
            except KeyError:
372
                self.system_name = None
373
                logger.warning("Cannot detect SNMP system name")
374
375
        return ret
376
377
    def get_system_name(self, oid_system_name):
378
        """Get the short os name from the OS name OID string."""
379
        short_system_name = None
380
381
        if oid_system_name == '':
382
            return short_system_name
383
384
        # Find the short name in the oid_to_short_os_name dict
385
        for r, v in iteritems(oid_to_short_system_name):
386
            if re.search(r, oid_system_name):
387
                short_system_name = v
388
                break
389
390
        return short_system_name
391
392
    def update(self):
393
        """Update the stats using SNMP."""
394
        # For each plugins, call the update method
395
        for p in self._plugins:
396
            # Set the input method to SNMP
397
            self._plugins[p].input_method = 'snmp'
398
            self._plugins[p].short_system_name = self.system_name
399
            try:
400
                self._plugins[p].update()
401
            except Exception as e:
402
                logger.error("Update {0} failed: {1}".format(p, e))
403