Skip to content

Commit 4b2aaa5

Browse files
author
Lorenz Kästle
committed
Fix linter warnings
1 parent e969aee commit 4b2aaa5

File tree

2 files changed

+63
-63
lines changed

2 files changed

+63
-63
lines changed

snmp/file_handler.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ type FileHandler struct {
2121
gosnmp.Handler
2222
}
2323

24+
// Create a new file handler and initialize it with data from an io.Reader
25+
func NewFileHandler(r io.Reader) (h *FileHandler, err error) {
26+
h = &FileHandler{}
27+
err = h.ReadFromWalk(r)
28+
29+
return
30+
}
31+
32+
// Create a new file handler and initialize it with data by reading from a file
33+
func NewFileHandlerFromFile(filePath string) (h *FileHandler, err error) {
34+
fh, err := os.Open(filePath)
35+
if err != nil {
36+
err = fmt.Errorf("could not open SNMP data file: %s - %w", filePath, err)
37+
return
38+
}
39+
40+
defer fh.Close()
41+
42+
return NewFileHandler(fh)
43+
}
44+
2445
// Read data from a io.Reader and parse it for PDUs
2546
//
2647
// They will be stored in Data for later use.
@@ -127,24 +148,3 @@ func (h *FileHandler) BulkWalk(rootOid string, walkFn gosnmp.WalkFunc) error {
127148
func (h *FileHandler) BulkWalkAll(rootOid string) (results []gosnmp.SnmpPDU, err error) {
128149
panic("not implemented")
129150
}
130-
131-
// Create a new file handler and initialize it with data from an io.Reader
132-
func NewFileHandler(r io.Reader) (h *FileHandler, err error) {
133-
h = &FileHandler{}
134-
err = h.ReadFromWalk(r)
135-
136-
return
137-
}
138-
139-
// Create a new file handler and initialize it with data by reading from a file
140-
func NewFileHandlerFromFile(filePath string) (h *FileHandler, err error) {
141-
fh, err := os.Open(filePath)
142-
if err != nil {
143-
err = fmt.Errorf("could not open SNMP data file: %s - %w", filePath, err)
144-
return
145-
}
146-
147-
defer fh.Close()
148-
149-
return NewFileHandler(fh)
150-
}

snmp/table.go

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -39,48 +39,6 @@ func (t *Table) Walk() error {
3939
return nil
4040
}
4141

42-
// addWalkValue parses the PDU and stored result in an indexed way
43-
//
44-
// The OID part below the table is something like:
45-
// 1.X.Y.Y
46-
//
47-
// 1 entry OID, just a construct to represent the row
48-
// X value OID
49-
// Y.Y actual index part (can be a longer chain)
50-
//
51-
// TODO: this might not apply to all tables
52-
func (t *Table) addWalkValue(data gosnmp.SnmpPDU) error {
53-
subOid := GetSubOid(data.Name, t.Oid)
54-
if subOid == "" {
55-
// other data in walk, ignoring it
56-
return nil
57-
}
58-
59-
parts := strings.Split(subOid, ".")
60-
61-
if len(parts) < 3 {
62-
return fmt.Errorf("could not identify entry, column and id in oid: %s", data.Name)
63-
}
64-
65-
// entry := parts[0]
66-
column := parts[1]
67-
id := strings.Join(parts[2:], ".")
68-
69-
if _, ok := t.Values[id]; !ok {
70-
t.Values[id] = TableColumns{}
71-
}
72-
73-
// store data in indexed tree
74-
t.Values[id][column] = data
75-
76-
// keep list of existing columns
77-
if _, ok := t.Columns[column]; !ok {
78-
t.Columns[column] = column
79-
}
80-
81-
return nil
82-
}
83-
8442
func (t *Table) GetValue(id string, oid string) (interface{}, error) {
8543
parts := strings.Split(oid, ".")
8644
column := parts[len(parts)-1]
@@ -145,3 +103,45 @@ func SortOIDs(list []string) []string {
145103

146104
return list
147105
}
106+
107+
// addWalkValue parses the PDU and stored result in an indexed way
108+
//
109+
// The OID part below the table is something like:
110+
// 1.X.Y.Y
111+
//
112+
// 1 entry OID, just a construct to represent the row
113+
// X value OID
114+
// Y.Y actual index part (can be a longer chain)
115+
//
116+
// TODO: this might not apply to all tables
117+
func (t *Table) addWalkValue(data gosnmp.SnmpPDU) error {
118+
subOid := GetSubOid(data.Name, t.Oid)
119+
if subOid == "" {
120+
// other data in walk, ignoring it
121+
return nil
122+
}
123+
124+
parts := strings.Split(subOid, ".")
125+
126+
if len(parts) < 3 {
127+
return fmt.Errorf("could not identify entry, column and id in oid: %s", data.Name)
128+
}
129+
130+
// entry := parts[0]
131+
column := parts[1]
132+
id := strings.Join(parts[2:], ".")
133+
134+
if _, ok := t.Values[id]; !ok {
135+
t.Values[id] = TableColumns{}
136+
}
137+
138+
// store data in indexed tree
139+
t.Values[id][column] = data
140+
141+
// keep list of existing columns
142+
if _, ok := t.Columns[column]; !ok {
143+
t.Columns[column] = column
144+
}
145+
146+
return nil
147+
}

0 commit comments

Comments
 (0)