1
# -*- encoding: utf-8 -*-
3
# Copyright © 2014 ZHAW SoE
5
# Authors: Lucas Graf <graflu0@students.zhaw.ch>
6
# Toni Zehnder <zehndton@students.zhaw.ch>
8
# Licensed under the Apache License, Version 2.0 (the "License"); you may
9
# not use this file except in compliance with the License. You may obtain
10
# a copy of the License at
12
# http://www.apache.org/licenses/LICENSE-2.0
14
# Unless required by applicable law or agreed to in writing, software
15
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
# License for the specific language governing permissions and limitations
19
"""Inspector for collecting data over SNMP"""
23
from ceilometer.hardware.inspector import base
24
from pysnmp.entity.rfc3413.oneliner import cmdgen
27
class SNMPException(Exception):
31
def parse_snmp_return(ret):
32
"""Check the return value of snmp operations
34
:param ret: a tuple of (errorIndication, errorStatus, errorIndex, data)
36
:return: a tuple of (err, data)
37
err: True if error found, or False if no error found
38
data: a string of error description if error found, or the
39
actual return data of the snmp operation
42
(errIndication, errStatus, errIdx, varBinds) = ret
46
data = "%s at %s" % (errStatus.prettyPrint(),
47
errIdx and varBinds[int(errIdx) - 1] or "?")
54
class SNMPInspector(base.Inspector):
56
_cpu_1_min_load_oid = "1.3.6.1.4.1.2021.10.1.3.1"
57
_cpu_5_min_load_oid = "1.3.6.1.4.1.2021.10.1.3.2"
58
_cpu_15_min_load_oid = "1.3.6.1.4.1.2021.10.1.3.3"
60
_memory_total_oid = "1.3.6.1.4.1.2021.4.5.0"
61
_memory_used_oid = "1.3.6.1.4.1.2021.4.6.0"
63
_disk_index_oid = "1.3.6.1.4.1.2021.9.1.1"
64
_disk_path_oid = "1.3.6.1.4.1.2021.9.1.2"
65
_disk_device_oid = "1.3.6.1.4.1.2021.9.1.3"
66
_disk_size_oid = "1.3.6.1.4.1.2021.9.1.6"
67
_disk_used_oid = "1.3.6.1.4.1.2021.9.1.8"
68
#Network Interface OIDs
69
_interface_index_oid = "1.3.6.1.2.1.2.2.1.1"
70
_interface_name_oid = "1.3.6.1.2.1.2.2.1.2"
71
_interface_bandwidth_oid = "1.3.6.1.2.1.2.2.1.5"
72
_interface_mac_oid = "1.3.6.1.2.1.2.2.1.6"
73
_interface_ip_oid = "1.3.6.1.2.1.4.20.1.2"
74
_interface_received_oid = "1.3.6.1.2.1.2.2.1.10"
75
_interface_transmitted_oid = "1.3.6.1.2.1.2.2.1.16"
76
_interface_error_oid = "1.3.6.1.2.1.2.2.1.20"
77
#Default port and security name
79
_security_name = 'public'
82
super(SNMPInspector, self).__init__()
83
self._cmdGen = cmdgen.CommandGenerator()
85
def _get_or_walk_oid(self, oid, host, get=True):
87
func = self._cmdGen.getCmd
88
ret_func = lambda x: x[0][1]
90
func = self._cmdGen.nextCmd
91
ret_func = lambda x: x
92
ret = func(cmdgen.CommunityData(self._get_security_name(host)),
93
cmdgen.UdpTransportTarget((host.hostname,
94
host.port or self._port)),
96
(error, data) = parse_snmp_return(ret)
98
raise SNMPException("An error occurred, oid %(oid)s, "
99
"host %(host)s, %(err)s" % dict(oid=oid,
100
host=host.hostname, err=data))
102
return ret_func(data)
104
def _get_value_from_oid(self, oid, host):
105
return self._get_or_walk_oid(oid, host, True)
107
def _walk_oid(self, oid, host):
108
return self._get_or_walk_oid(oid, host, False)
110
def inspect_cpu(self, host):
113
str(self._get_value_from_oid(self._cpu_1_min_load_oid, host))
116
str(self._get_value_from_oid(self._cpu_5_min_load_oid, host))
119
str(self._get_value_from_oid(self._cpu_15_min_load_oid, host))
121
yield base.CPUStats(cpu_1_min=float(cpu_1_min_load),
122
cpu_5_min=float(cpu_5_min_load),
123
cpu_15_min=float(cpu_15_min_load))
125
def inspect_memory(self, host):
127
total = self._get_value_from_oid(self._memory_total_oid, host)
129
used = self._get_value_from_oid(self._memory_used_oid, host)
131
yield base.MemoryStats(total=int(total), used=int(used))
133
def inspect_disk(self, host):
134
disks = self._walk_oid(self._disk_index_oid, host)
137
for object_name, value in disk:
138
path_oid = "%s.%s" % (self._disk_path_oid, str(value))
139
path = self._get_value_from_oid(path_oid, host)
140
device_oid = "%s.%s" % (self._disk_device_oid, str(value))
141
device = self._get_value_from_oid(device_oid, host)
142
size_oid = "%s.%s" % (self._disk_size_oid, str(value))
143
size = self._get_value_from_oid(size_oid, host)
144
used_oid = "%s.%s" % (self._disk_used_oid, str(value))
145
used = self._get_value_from_oid(used_oid, host)
147
disk = base.Disk(device=str(device),
149
stats = base.DiskStats(size=int(size),
154
def inspect_network(self, host):
155
net_interfaces = self._walk_oid(self._interface_index_oid, host)
157
for interface in net_interfaces:
158
for object_name, value in interface:
159
ip = self._get_ip_for_interface(host, value)
160
name_oid = "%s.%s" % (self._interface_name_oid,
162
name = self._get_value_from_oid(name_oid, host)
163
mac_oid = "%s.%s" % (self._interface_mac_oid,
165
mac = self._get_value_from_oid(mac_oid, host)
166
bw_oid = "%s.%s" % (self._interface_bandwidth_oid,
169
bandwidth = self._get_value_from_oid(bw_oid, host) / 8
170
rx_oid = "%s.%s" % (self._interface_received_oid,
172
rx_bytes = self._get_value_from_oid(rx_oid, host)
173
tx_oid = "%s.%s" % (self._interface_transmitted_oid,
175
tx_bytes = self._get_value_from_oid(tx_oid, host)
176
error_oid = "%s.%s" % (self._interface_error_oid,
178
error = self._get_value_from_oid(error_oid, host)
180
adapted_mac = mac.prettyPrint().replace('0x', '')
181
interface = base.Interface(name=str(name),
184
stats = base.InterfaceStats(bandwidth=int(bandwidth),
185
rx_bytes=int(rx_bytes),
186
tx_bytes=int(tx_bytes),
188
yield (interface, stats)
190
def _get_security_name(self, host):
191
options = urlparse.parse_qs(host.query)
192
return options.get('security_name', [self._security_name])[-1]
194
def _get_ip_for_interface(self, host, interface_id):
195
ip_addresses = self._walk_oid(self._interface_ip_oid, host)
196
for ip in ip_addresses:
197
for name, value in ip:
198
if value == interface_id:
199
return str(name).replace(self._interface_ip_oid + ".", "")