Issues (51)

glances/stats_client_snmp.py (1 issue)

1
#
2
# This file is part of Glances.
3
#
4
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]>
5
#
6
# SPDX-License-Identifier: LGPL-3.0-only
7
#
8
9
"""The stats manager."""
10
11
import re
12
13
from glances.logger import logger
14
from glances.stats import GlancesStats
15
16
# SNMP OID regexp pattern to short system name dict
17
oid_to_short_system_name = {
18
    '.*Linux.*': 'linux',
19
    '.*Darwin.*': 'mac',
20
    '.*BSD.*': 'bsd',
21
    '.*Windows.*': 'windows',
22
    '.*Cisco.*': 'cisco',
23
    '.*VMware ESXi.*': 'esxi',
24
    '.*NetApp.*': 'netapp',
25
}
26
27
28
class GlancesStatsClientSNMP(GlancesStats):
29
    """This class stores, updates and gives stats for the SNMP client."""
30
31
    def __init__(self, config=None, args=None):
32
        super().__init__()
33
34
        # Init the configuration
35
        self.config = config
36
37
        # Init the arguments
38
        self.args = args
39
40
        # OS name is used because OID is different between system
41
        self.os_name = None
42
43
        # Load AMPs, plugins and exports modules
44
        self.load_modules(self.args)
45
46
    def check_snmp(self):
47
        """Check if SNMP is available on the server."""
48
        # Import the SNMP client class
49
        from glances.snmp import GlancesSNMPClient
50
51
        # Create an instance of the SNMP client
52
        snmp_client = GlancesSNMPClient(
53
            host=self.args.client,
54
            port=self.args.snmp_port,
55
            version=self.args.snmp_version,
56
            community=self.args.snmp_community,
57
            user=self.args.snmp_user,
58
            auth=self.args.snmp_auth,
59
        )
60
61
        # If we cannot grab the hostname, then exit...
62
        ret = snmp_client.get_by_oid("1.3.6.1.2.1.1.5.0") != {}
63
        if ret:
64
            # Get the OS name (need to grab the good OID...)
65
            oid_os_name = snmp_client.get_by_oid("1.3.6.1.2.1.1.1.0")
66
            try:
67
                self.system_name = self.get_system_name(oid_os_name['1.3.6.1.2.1.1.1.0'])
68
                logger.info(f"SNMP system name detected: {self.system_name}")
69
            except KeyError:
70
                self.system_name = None
71
                logger.warning("Cannot detect SNMP system name")
72
73
        return ret
74
75
    def get_system_name(self, oid_system_name):
76
        """Get the short os name from the OS name OID string."""
77
        return (
78
            next((v for r, v in oid_to_short_system_name.items() if re.search(r, oid_system_name)), None)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable v does not seem to be defined.
Loading history...
79
            if oid_system_name
80
            else None
81
        )
82
83
    def update(self):
84
        """Update the stats using SNMP."""
85
        # For each plugins, call the update method
86
        for p in self._plugins:
87
            if self._plugins[p].is_disabled():
88
                # If current plugin is disable
89
                # then continue to next plugin
90
                continue
91
92
            # Set the input method to SNMP
93
            self._plugins[p].input_method = 'snmp'
94
            self._plugins[p].short_system_name = self.system_name
95
96
            # Update the stats...
97
            try:
98
                self._plugins[p].update()
99
            except Exception as e:
100
                logger.error(f"Update {p} failed: {e}")
101
            else:
102
                # ... the history
103
                self._plugins[p].update_stats_history()
104
                # ... and the views
105
                self._plugins[p].update_views()
106