/
translate.go
77 lines (64 loc) · 1.59 KB
/
translate.go
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
package main
import (
"fmt"
"os"
"strings"
"github.com/bingoohuang/snmp/pkg/smi"
"github.com/bingoohuang/snmp/pkg/snmpp"
)
func (o *Options) translate() {
if !strings.Contains(o.Operate, "translate") {
return
}
arrow := StringStyle + "=>" + EndStyle
for _, v := range o.Oids {
if snmpp.IsSymbolName(v) {
if oid, err := o.mib.OID(v); err != nil {
fmt.Printf("%s %s error %v", v, arrow, err)
} else {
fmt.Printf("%s %s %s", v, arrow, oid)
if symbol, suffix := o.mib.Symbol(oid); symbol != nil {
o.printSymbolWithDescription(symbol, suffix, false)
}
}
} else {
oid, err := smi.ParseOID(v)
if err != nil {
fmt.Printf("%s %s error %v", v, arrow, err)
continue
}
if symbol, suffix := o.mib.Symbol(oid); symbol == nil {
fmt.Printf("%s %s unknown", v, arrow)
} else {
symbolName, f := o.printSymbolWithDescription(symbol, suffix, true)
fmt.Printf("%s %s %s", v, arrow, symbolName)
f()
}
}
fmt.Println()
}
os.Exit(0)
}
const (
KeyStyle = "\x1B[94m"
StringStyle = "\x1B[92m"
RedStyle = "\x1B[31m"
GrayStyle = "\x1B[36m"
EndStyle = "\x1B[0m"
)
func (o *Options) printSymbolWithDescription(symbol *smi.Symbol, suffix smi.OID, delay bool) (string, func()) {
symbolName, description := snmpp.SymbolString(symbol, suffix)
f := func() {}
if strings.Contains(o.Verbose, "desc") && description != "" {
f = func() {
if symbol.Unit != "" {
fmt.Printf(" Unit: %s", KeyStyle+symbol.Unit+EndStyle)
}
fmt.Printf(" Desc: %s", GrayStyle+description+EndStyle)
}
}
if !delay {
f()
}
return symbolName, f
}