-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmib.go
More file actions
161 lines (133 loc) · 2.97 KB
/
Copy pathmib.go
File metadata and controls
161 lines (133 loc) · 2.97 KB
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
package mibs
import (
"fmt"
"github.com/qmsk/snmpbot/snmp"
"regexp"
)
func makeMIB(name string, oid snmp.OID) MIB {
var oids snmp.OIDSet
if oid != nil {
oids = snmp.MakeOIDSet(oid)
} else {
// MIB without any OID has an empty OID set
oids = snmp.MakeOIDSet()
}
return MIB{
Name: name,
OID: oid,
OIDs: oids,
registry: makeRegistry(),
objects: make(map[IDKey]*Object),
tables: make(map[IDKey]*Table),
}
}
type MIB struct {
Name string
OID snmp.OID
OIDs snmp.OIDSet
registry
objects map[IDKey]*Object
tables map[IDKey]*Table
}
func (mib *MIB) String() string {
return mib.Name
}
func (mib *MIB) registerObject(object Object) *Object {
mibRegistry.registerOID(object.ID)
mib.registry.register(object.ID)
mib.objects[object.ID.Key()] = &object
return &object
}
func (mib *MIB) registerTable(table Table) *Table {
mibRegistry.registerOID(table.ID)
mib.registry.register(table.ID)
mib.tables[table.ID.Key()] = &table
return &table
}
/* Resolve MIB-relative ID by human-readable name:
".1.0"
"sysDescr"
"sysDescr.0"
*/
var mibResolveRegexp = regexp.MustCompile("^([^.]+?)?([.][0-9.]+)?$")
func (mib *MIB) Resolve(name string) (ID, error) {
var id = ID{MIB: mib, OID: mib.OID}
var nameID, nameOID string
if matches := mibResolveRegexp.FindStringSubmatch(name); matches == nil {
return id, fmt.Errorf("Invalid syntax: %v", name)
} else {
nameID = matches[1]
nameOID = matches[2]
}
if nameID == "" {
} else if resolveID, err := mib.ResolveName(nameID); err != nil {
return id, err
} else {
id = resolveID
}
if nameOID == "" {
} else if oid, err := snmp.ParseOID(nameOID); err != nil {
return id, err
} else {
if id.OID == nil {
id.OID = oid
} else {
id.OID = id.OID.Extend(oid...)
}
}
return id, nil
}
func (mib *MIB) ResolveName(name string) (ID, error) {
if id, ok := mib.registry.getName(name); !ok {
return ID{MIB: mib, Name: name}, fmt.Errorf("%v name not found: %v", mib.Name, name)
} else {
return id, nil
}
}
func (mib *MIB) Lookup(oid snmp.OID) ID {
if id, ok := mib.registry.getOID(oid); !ok {
return ID{MIB: mib, OID: oid}
} else {
return id
}
}
func (mib *MIB) Walk(f func(ID)) {
mib.registry.walk(f)
}
func (mib *MIB) Object(id ID) *Object {
if object, ok := mib.objects[id.Key()]; !ok {
return nil
} else {
return object
}
}
func (mib *MIB) ResolveObject(name string) *Object {
if id, err := mib.Resolve(name); err != nil {
return nil
} else {
return mib.Object(id)
}
}
func (mib *MIB) Table(id ID) *Table {
if table, ok := mib.tables[id.Key()]; !ok {
return nil
} else {
return table
}
}
func (mib *MIB) ResolveTable(name string) *Table {
if id, err := mib.Resolve(name); err != nil {
return nil
} else {
return mib.Table(id)
}
}
func (mib *MIB) FormatOID(oid snmp.OID) string {
if index := mib.OID.Index(oid); index == nil {
return oid.String()
} else if len(index) == 0 {
return mib.Name
} else {
return fmt.Sprintf("%s%s", mib.Name, snmp.OID(index).String())
}
}