-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmodelstat.go
82 lines (68 loc) · 1.69 KB
/
modelstat.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
78
79
80
81
82
package sw
import (
"log"
"strings"
"time"
"github.com/ulricqin/gosnmp"
)
func SysModel(ip, community string, retry int, timeout int) (string, error) {
vendor, err := SysVendor(ip, community, retry, timeout)
method := "get"
var oid string
defer func() {
if r := recover(); r != nil {
log.Println("Recovered in sw.modelstat.go SysModel", r)
}
}()
switch vendor {
case "Cisco_NX", "Cisco", "Cisco_old", "Cisco_IOS_XR", "Cisco_IOS_XE", "Ruijie":
method = "getnext"
oid = "1.3.6.1.2.1.47.1.1.1.1.13"
case "Huawei_ME60", "Huawei_V5.70", "Huawei_V5.130", "Huawei_V3.10":
method = "getnext"
oid = "1.3.6.1.2.1.47.1.1.1.1.2"
case "H3c_V3.10", "H3C_S9500", "H3C", "H3C_V5", "H3C_V7", "Cisco_ASA":
oid = "1.3.6.1.2.1.47.1.1.1.1.13"
return getSwmodle(ip, community, oid, timeout, retry)
case "Linux":
return "Linux", nil
default:
return "", err
}
snmpPDUs, err := RunSnmp(ip, community, oid, method, timeout)
if err == nil {
for _, pdu := range snmpPDUs {
return pdu.Value.(string), err
}
}
return "", err
}
func getSwmodle(ip, community, oid string, timeout, retry int) (value string, err error) {
defer func() {
if r := recover(); r != nil {
log.Println(ip+" Recovered in CPUtilization", r)
}
}()
method := "getnext"
oidnext := oid
var snmpPDUs []gosnmp.SnmpPDU
for {
for i := 0; i < retry; i++ {
snmpPDUs, err = RunSnmp(ip, community, oidnext, method, timeout)
if len(snmpPDUs) > 0 {
break
}
time.Sleep(100 * time.Millisecond)
}
oidnext = snmpPDUs[0].Name
if strings.Contains(oidnext, oid) {
if snmpPDUs[0].Value.(string) != "" {
value = snmpPDUs[0].Value.(string)
break
}
} else {
break
}
}
return value, err
}