/usr/share/pyshared/pysnmp/smi/mibs/SNMPv2-TC.py is in python-pysnmp4 4.2.5-1.
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | import sys
from pysnmp.smi import error
from pysnmp import debug
OctetString, Integer, ObjectIdentifier = mibBuilder.importSymbols(
'ASN1', 'OctetString', 'Integer', 'ObjectIdentifier'
)
( NamedValues, ) = mibBuilder.importSymbols("ASN1-ENUMERATION", "NamedValues")
( ConstraintsIntersection, ConstraintsUnion, SingleValueConstraint,
ValueRangeConstraint, ValueSizeConstraint, ) = mibBuilder.importSymbols(
"ASN1-REFINEMENT", "ConstraintsIntersection", "ConstraintsUnion",
"SingleValueConstraint", "ValueRangeConstraint", "ValueSizeConstraint"
)
Counter32, Unsigned32, TimeTicks, Counter64 = mibBuilder.importSymbols(
'SNMPv2-SMI', 'Counter32', 'Unsigned32', 'TimeTicks', 'Counter64'
)
class TextualConvention:
displayHint = ''
status = 'current'
description = ''
reference = ''
bits = ()
__integer = Integer()
__counter32 = Counter32()
__unsigned32 = Unsigned32()
__timeticks = TimeTicks()
__counter64 = Counter64()
__octetString = OctetString()
__objectIdentifier = ObjectIdentifier()
def getDisplayHint(self): return self.displayHint
def getStatus(self): return self.status
def getDescription(self): return self.description
def getReference(self): return self.reference
def getValue(self): return self.clone()
def setValue(self, value): return self.clone(value)
def prettyOut(self, value): # override asn1 type method
"""Implements DISPLAY-HINT evaluation"""
if self.displayHint and (
self.__integer.isSuperTypeOf(self) or
self.__unsigned32.isSuperTypeOf(self) or
self.__timeticks.isSuperTypeOf(self) or
self.__counter32.isSuperTypeOf(self) or
self.__counter64.isSuperTypeOf(self)
):
_ = lambda t, f=0: (t, f)
t, f = _(*self.displayHint.split('-'))
if t == 'x':
return '0x%x' % value
elif t == 'd':
try:
return '%.*f' % (int(f), float(value)/pow(10, int(f)))
except Exception:
raise error.SmiError(
'float num evaluation error: %s' % sys.exc_info()[1]
)
elif t == 'o':
return '0%o' % value
elif t == 'b':
v = value; r = ['B']
while v:
r.insert(0, '%d' % (v&0x01))
v = v>>1
return ''.join(r)
else:
raise error.SmiError(
'Unsupported numeric type spec: %s' % t
)
elif self.displayHint and self.__octetString.isSuperTypeOf(self):
r = ''
v = self.__class__(value).asNumbers()
d = self.displayHint
while v and d:
# 1
if d[0] == '*':
repeatIndicator = repeatCount = v[0]
d = d[1:]; v = v[1:]
else:
repeatCount = 1; repeatIndicator = None
# 2
octetLength = ''
while d and d[0] in '0123456789':
octetLength = octetLength + d[0]
d = d[1:]
try:
octetLength = int(octetLength)
except Exception:
raise error.SmiError(
'Bad octet length: %s' % octetLength
)
if not d:
raise error.SmiError(
'Short octet length: %s' % self.displayHint
)
# 3
displayFormat = d[0]
d = d[1:]
# 4
if d and d[0] not in '0123456789' and d[0] != '*':
displaySep = d[0]
d = d[1:]
else:
displaySep = ''
# 5
if d and displaySep and repeatIndicator is not None:
repeatTerminator = d[0]
displaySep = ''
d = d[1:]
else:
repeatTerminator = None
while repeatCount:
repeatCount = repeatCount - 1
# 't' stands for UTF-8, does it need any special support?
if displayFormat == 'a' or displayFormat == 't':
r = r + ''.join([ chr(x) for x in v[:octetLength] ])
elif displayFormat in ('x', 'd', 'o'):
n = 0; vv = v[:octetLength]
while vv:
n = n << 8
try:
n = n | vv[0]
vv = vv[1:]
except Exception:
raise error.SmiError(
'Display format eval failure: %s: %s'
% (vv, sys.exc_info()[1])
)
if displayFormat == 'x':
r = r + '%02x' % n
elif displayFormat == 'o':
r = r + '%03o' % n
else:
r = r + '%d' % n
else:
raise error.SmiError(
'Unsupported display format char: %s' % \
displayFormat
)
if v and repeatTerminator:
r = r + repeatTerminator
v = v[octetLength:]
if v and displaySep:
r = r + displaySep
if not d:
d = self.displayHint
# if d:
# raise error.SmiError(
# 'Unparsed display hint left: %s' % d
# )
return r
elif self.__objectIdentifier.isSuperTypeOf(self):
return self.__objectIdentifier.prettyOut(value)
elif self.__octetString.isSuperTypeOf(self):
return self.__octetString.prettyOut(value)
else:
return str(value)
# elif self.bits:
# try:
# return self.bits[value]
# except Exception:
# raise error.SmiError(
# 'Enumeratin resolution failure for %s: %s' % (self, sys.exc_info()[1])
# )
# XXX
# def prettyIn(self, value):
# # XXX parse TC syntax
# return str(value)
class DisplayString(TextualConvention, OctetString):
subtypeSpec = OctetString.subtypeSpec+ValueSizeConstraint(0,255)
displayHint = "255a"
class PhysAddress(TextualConvention, OctetString):
displayHint = "1x:"
class MacAddress(TextualConvention, OctetString):
subtypeSpec = OctetString.subtypeSpec+ValueSizeConstraint(6,6)
displayHint = "1x:"
fixedLength = 6
class TruthValue(Integer, TextualConvention):
subtypeSpec = Integer.subtypeSpec+SingleValueConstraint(1, 2)
namedValues = NamedValues(('true', 1), ('false', 2))
class TestAndIncr(Integer, TextualConvention):
subtypeSpec = Integer.subtypeSpec+ValueRangeConstraint(0, 2147483647)
defaultValue = 0
def setValue(self, value):
if value is not None:
if value != self:
raise error.InconsistentValueError()
value = value + 1
if value > 2147483646:
value = 0
return self.clone(self, value)
class AutonomousType(ObjectIdentifier, TextualConvention): pass
class InstancePointer(ObjectIdentifier, TextualConvention):
status = 'obsolete'
class VariablePointer(ObjectIdentifier, TextualConvention): pass
class RowPointer(ObjectIdentifier, TextualConvention): pass
class RowStatus(Integer, TextualConvention):
"""A special kind of scalar MIB variable responsible for
MIB table row creation/destruction.
"""
subtypeSpec = Integer.subtypeSpec+SingleValueConstraint(0, 1, 2, 3, 4, 5, 6)
namedValues = NamedValues(
('notExists', 0), ('active', 1), ('notInService', 2), ('notReady', 3),
('createAndGo', 4), ('createAndWait', 5), ('destroy', 6)
)
# Known row states
stNotExists, stActive, stNotInService, stNotReady, \
stCreateAndGo, stCreateAndWait, stDestroy = list(range(7))
# States transition matrix (see RFC-1903)
stateMatrix = {
# (new-state, current-state) -> (error, new-state)
( stCreateAndGo, stNotExists ): (
error.RowCreationWanted, stActive
),
( stCreateAndGo, stNotReady ): (
error.InconsistentValueError, stNotReady
),
( stCreateAndGo, stNotInService ): (
error.InconsistentValueError, stNotInService
),
( stCreateAndGo, stActive ): (
error.InconsistentValueError, stActive
),
#
( stCreateAndWait, stNotExists ): (
error.RowCreationWanted, stActive
),
( stCreateAndWait, stNotReady ): (
error.InconsistentValueError, stNotReady
),
( stCreateAndWait, stNotInService ): (
error.InconsistentValueError, stNotInService
),
( stCreateAndWait, stActive ): (
error.InconsistentValueError, stActive
),
#
( stActive, stNotExists ): (
error.InconsistentValueError, stNotExists
),
( stActive, stNotReady ): (
error.InconsistentValueError, stNotReady
),
( stActive, stNotInService ): (
None, stActive
),
( stActive, stActive ): (
None, stActive
),
#
( stNotInService, stNotExists ): (
error.InconsistentValueError, stNotExists
),
( stNotInService, stNotReady ): (
error.InconsistentValueError, stNotReady
),
( stNotInService, stNotInService ): (
None, stNotInService
),
( stNotInService, stActive ): (
None, stActive
),
#
( stDestroy, stNotExists ): (
error.RowDestructionWanted, stNotExists
),
( stDestroy, stNotReady ): (
error.RowDestructionWanted, stNotExists
),
( stDestroy, stNotInService ): (
error.RowDestructionWanted, stNotExists
),
( stDestroy, stActive ): (
error.RowDestructionWanted, stNotExists
),
# This is used on instantiation
( stNotExists, stNotExists ): (
None, stNotExists
)
}
defaultValue = stNotExists
def setValue(self, value):
value = self.clone(value)
# Run through states transition matrix,
# resolve new instance value
excValue, newState = self.stateMatrix.get(
(value, self),
(error.MibOperationError, None)
)
newState = self.clone(newState)
debug.logger & debug.flagIns and debug.logger('RowStatus state change from %r to %s produced new state %r, error indication %r' % (self, value, newState, excValue))
if excValue is not None:
excValue = excValue(
msg='Exception at row state transition from %s to %s yields state %s and exception' % (self, value, newState), syntax=newState
)
raise excValue
return newState
class TimeStamp(TimeTicks, TextualConvention): pass
class TimeInterval(Integer, TextualConvention):
subtypeSpec = Integer.subtypeSpec+ValueRangeConstraint(0, 2147483647)
class DateAndTime(TextualConvention, OctetString):
subtypeSpec = OctetString.subtypeSpec+ValueSizeConstraint(8, 11)
displayHint = "2d-1d-1d,1d:1d:1d.1d,1a1d:1d"
class StorageType(Integer, TextualConvention):
subtypeSpec = Integer.subtypeSpec+SingleValueConstraint(1, 2, 3, 4, 5)
namedValues = NamedValues(
('other', 1), ('volatile', 2), ('nonVolatile', 3),
('permanent', 4), ('readOnly', 5)
)
class TDomain(ObjectIdentifier, TextualConvention): pass
class TAddress(OctetString, TextualConvention):
subtypeSpec = OctetString.subtypeSpec+ValueSizeConstraint(1, 255)
mibBuilder.exportSymbols(
'SNMPv2-TC', TextualConvention=TextualConvention, DisplayString=DisplayString,
PhysAddress=PhysAddress, MacAddress=MacAddress, TruthValue=TruthValue,
TestAndIncr=TestAndIncr, AutonomousType=AutonomousType,
InstancePointer=InstancePointer, VariablePointer=VariablePointer,
RowPointer=RowPointer, RowStatus=RowStatus, TimeStamp=TimeStamp,
TimeInterval=TimeInterval, DateAndTime=DateAndTime, StorageType=StorageType,
TDomain=TDomain, TAddress=TAddress
)
|