/usr/share/pyshared/pysnmp/v2/bulkrole.py is in python-pysnmp2 2.0.9-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | """
SNMP transport class.
Sends & receives SNMP messages to multiple destinations in bulk.
Written by Ilya Etingof <ilya@glas.net>, 1999-2002.
"""
import socket
# Import PySNMP components
import role
import v1, v2c
class Error(role.Error):
"""Base class for bulkrole module
"""
pass
class BadArgument(Error):
"""Bad argument given
"""
pass
class manager:
"""Send SNMP messages to multiple destinations and receive
replies.
"""
def __init__(self, iface=('0.0.0.0', 0)):
# Initialize defaults
self.iface = iface
self.clear()
# Set defaults to public attributes
self.retries = 3
self.timeout = 1
#
# Implement list interface
#
def __str__(self):
"""
"""
return str(self._requests)
def __repr__(self):
"""
"""
return repr(self._requests)
def clear(self):
"""Clear the list of sessions and prepare for next round
of append->dispatch->subscript cycle.
"""
self._requests = []
self._responses = []
self._durty = 0
# [Re-]create SNMP manager transport
self.transport = role.manager(None, self.iface)
def append(self, (dst, req)):
"""
append((dst, req))
Create transport session destined to "agent" (a tuple of (host, port)
where host is a string and port is an integer) and queue SNMP
"request" message (string) to be sent to "agent".
All queued request messages will be sent upon self.dispatch() method
invocation.
"""
if self._durty:
raise ValueError('List is not valid for update (try clear())')
if req['request_id'] in map(lambda (dst, req): \
req['request_id'], self._requests):
raise BadArgument('Duplicate request IDs in queue')
self._requests.append((dst, req))
def __setitem__(self, idx, (dst, req)):
"""
"""
if self._durty:
raise ValueError('List is not valid for update (try clear())')
if req['request_id'] in map(lambda (dst, req): \
req['request_id'], self._requests):
raise BadArgument('Duplicate request IDs in queue')
try:
self._requests[idx] = (dst, req)
except IndexError:
raise IndexError('Request index out of range')
def __getitem__(self, idx):
"""
"""
try:
return self._requests[idx]
except IndexError:
raise IndexError('Request index out of range')
def __len__(self):
"""
"""
return len(self._requests)
def count(self, val):
"""XXX
"""
return self._requests.count(val)
def index(self, (dst, req)):
"""
"""
if self._durty:
raise ValueError('List is not valid for update (try clear())')
try:
return self._requests.index((dst, req))
except ValueError:
raise ValueError('No such request in queue')
def insert(self, idx, (dst, req)):
"""
"""
if self._durty:
raise ValueError('List is not valid for update (try clear())')
if req['request_id'] in map(lambda (dst, req): \
req['request_id'], self._requests):
raise BadArgument('Duplicate request IDs in queue')
try:
return self._requests.insert(idx, (dst, req))
except IndexError:
raise IndexError('Request index out of range')
def remove(self, (dst, req)):
"""
"""
try:
return self._requests.remove((dst, req))
except ValueError:
raise ValueError('No such request in queue')
def pop(self, idx=-1):
"""
"""
try:
return self._requests.pop(idx)
except IndexError:
raise IndexError('Request index out of range')
#
# The main I/O method
#
def dispatch(self):
"""
dispatch()
Send pending SNMP requests and receive replies (or timeout).
"""
# Indicate that internal queue might change
self._durty = 1
# Resolve destination hostnames to IP numbers for later comparation
try:
self._requests = map(lambda (dst, req): \
((socket.gethostbyname(dst[0]), \
dst[1]), req),\
self._requests)
except socket.error, why:
raise BadArgument(why)
# Initialize a list of responses
self._responses = map(lambda (dst, req): (dst, None), self._requests)
pending = len(self._responses)
# Initialize retry counter
retries = self.retries
# Copy our timeout setting down to transport object
self.transport.timeout = self.timeout
while retries:
# Send out requests and prepare for waiting for replies
for idx in range(len(self._requests)):
# Skip completed session
(src, rsp) = self._responses[idx]
if rsp is not None:
continue
(dst, req) = self._requests[idx]
try:
self.transport.send(req.encode(), dst)
except role.Error:
# Ignore transport errors
pass
while pending:
# XXX Probably select() based multiplexing would better
# serve timeouts...
# Wait for response
(response, src) = self.transport.receive()
# Stop on timeout
if response is None:
retries = retries - 1
break
# Decode response
(rsp, rest) = v2c.decode(response)
# Try to match response message against pending
# request messages
for idx in range(len(self._requests)):
if (src, rsp) == self._requests[idx]:
self._responses[idx] = (src, rsp)
pending = pending - 1
break
if not pending:
break
# Replace list of requests with list of replies
self._requests = self._responses
|