-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmikrotik.go
144 lines (123 loc) · 2.98 KB
/
mikrotik.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
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
package dmc
import (
"net"
"regexp"
"strings"
"time"
"github.com/soniah/gosnmp"
)
type MikroTik struct {
Community string
}
func (m *MikroTik) Name() string {
return "MikroTik"
}
func (m *MikroTik) MatchString(s string) bool {
return regexp.MustCompile("^MikroTik").MatchString(s)
}
func (m *MikroTik) Groups() []ModelType {
return []ModelType{RadioModel, RouterModel}
}
func (m *MikroTik) Group(g ModelType) bool {
switch g {
case RadioModel:
return true
case RouterModel:
return true
default:
return false
}
}
func (m *MikroTik) Identify(orig string, ip net.IP, timeout time.Duration, retries int) (*State, error) {
community := env(m.Community, "MIKROTIK_COMMUNITY", "public")
var snmp = &gosnmp.GoSNMP{
Target: ip.String(),
Port: 161,
Community: community,
Version: gosnmp.Version2c,
Timeout: timeout,
Retries: retries,
}
if err := snmp.Connect(); err != nil {
return nil, err
}
defer snmp.Conn.Close()
oid, err := sysObjectID(snmp)
if oid == nil || err != nil {
return nil, err
}
// not a mikrotik ...
if (*oid) != ".1.3.6.1.4.1.14988.1" {
return nil, nil
}
// default model ...
label := "MikroTik RouterOS"
// check interfaces ...
w, err := snmp.WalkAll(".1.3.6.1.2.1.2.2.1.3")
if err != nil {
return nil, err
}
// check for wlan interfaces ...
for _, v := range w {
switch {
case !(v.Type == gosnmp.Integer):
case !(strings.HasPrefix(v.Name, ".1.3.6.1.2.1.2.2.1.3")):
case v.Value.(int) == 71:
label = "MikroTik Routerboard"
}
}
// state ....
s := State{Values: make(map[string]interface{})}
s.Values["model"] = label
// status ...
oids := []string{
".1.3.6.1.2.1.1.1.0",
".1.3.6.1.2.1.1.5.0",
".1.3.6.1.2.1.1.6.0",
".1.3.6.1.4.1.14988.1.1.4.1.0",
".1.3.6.1.4.1.14988.1.1.4.4.0",
".1.3.6.1.4.1.14988.1.1.7.3.0",
".1.3.6.1.4.1.14988.1.1.7.4.0",
}
// check for optional status values
if r, err := snmp.Get(oids); err == nil {
for _, v := range r.Variables {
switch v.Type {
case gosnmp.OctetString:
switch v.Name {
case ".1.3.6.1.2.1.1.1.0":
if f := strings.Fields((string)(v.Value.([]byte))); len(f) > 0 {
if a := strings.Join(f[1:], " "); a != "" {
s.Values["version"] = a
}
}
case ".1.3.6.1.2.1.1.5.0":
if a := (string)(v.Value.([]byte)); a != "" {
s.Values["name"] = a
}
case ".1.3.6.1.2.1.1.6.0":
if a := (string)(v.Value.([]byte)); a != "" {
s.Values["location"] = a
}
case ".1.3.6.1.4.1.14988.1.1.7.3.0":
if a := (string)(v.Value.([]byte)); a != "" {
s.Values["serial"] = a
}
case ".1.3.6.1.4.1.14988.1.1.7.4.0":
if a := (string)(v.Value.([]byte)); a != "" {
s.Values["firmware"] = a
}
case ".1.3.6.1.4.1.14988.1.1.4.4.0":
if a := (string)(v.Value.([]byte)); a != "" {
s.Values["software"] = a
}
case ".1.3.6.1.4.1.14988.1.1.4.1.0":
if a := (string)(v.Value.([]byte)); a != "" {
s.Values["license"] = a
}
}
}
}
}
return &s, nil
}