-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for debugging, use a walk file to return fake results (#712)
- Loading branch information
Showing
5 changed files
with
104 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/kentik/ktranslate/pkg/eggs/logger" | ||
|
||
"github.com/gosnmp/gosnmp" | ||
) | ||
|
||
// Load a snmpwalk response from the given file and use it for debugging. | ||
func LoadFromWalk(ctx context.Context, file string, log logger.ContextL) error { | ||
data, err := os.ReadFile(file) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
res := map[string]gosnmp.SnmpPDU{} | ||
for _, line := range strings.Split(string(data), "\n") { | ||
pts := strings.SplitN(line, " = ", 2) | ||
if len(pts) != 2 { | ||
continue | ||
} | ||
oid := strings.TrimSpace(pts[0]) | ||
if oid[0:1] == "." { // Strip off leading dot. | ||
oid = oid[1:] | ||
} | ||
pp := strings.SplitN(pts[1], ": ", 2) | ||
if len(pp) != 2 { | ||
continue | ||
} | ||
val := pp[1] | ||
|
||
switch strings.ToLower(pp[0]) { | ||
case "string": | ||
res[oid] = gosnmp.SnmpPDU{Value: []byte(val), Type: gosnmp.OctetString, Name: oid} | ||
case "integer": | ||
res[oid] = gosnmp.SnmpPDU{Value: val, Type: gosnmp.Integer, Name: oid} | ||
case "counter64": | ||
res[oid] = gosnmp.SnmpPDU{Value: val, Type: gosnmp.Counter64, Name: oid} | ||
case "counter32": | ||
res[oid] = gosnmp.SnmpPDU{Value: val, Type: gosnmp.Counter32, Name: oid} | ||
default: | ||
log.Errorf("Skipping unknown walk type: %s", pp[0]) | ||
} | ||
} | ||
|
||
walkCacheMap = res | ||
log.Infof("Loaded up %d entries to the walk cache map", len(res)) | ||
|
||
return nil | ||
} | ||
|
||
func useCachedMap(oid string) ([]gosnmp.SnmpPDU, error) { | ||
res := []gosnmp.SnmpPDU{} | ||
|
||
if oid[0:1] == "." { // Strip off leading dot. | ||
oid = oid[1:] | ||
} | ||
for k, v := range walkCacheMap { | ||
if strings.HasPrefix(k, oid) { | ||
res = append(res, v) | ||
} | ||
} | ||
|
||
if len(res) == 0 { | ||
return nil, fmt.Errorf("Nothing to see") | ||
} | ||
return res, nil | ||
} |