forked from amkay/snmp_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
222 lines (197 loc) · 5.27 KB
/
config.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"fmt"
"io/ioutil"
"strings"
"github.com/amkay/gosnmp"
"gopkg.in/yaml.v2"
)
func LoadFile(filename string) (*Config, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
cfg := &Config{}
err = yaml.Unmarshal(content, cfg)
if err != nil {
return nil, err
}
return cfg, nil
}
var (
DefaultModule = Module{
Version: 2,
}
DefaultAuth = Auth{
Community: "public",
SecurityLevel: "noAuthNoPriv",
AuthProtocol: "MD5",
PrivProtocol: "DES",
}
)
type Config map[string]*Module
type Module struct {
// A list of OIDs.
Walk []string `yaml:"walk"`
Metrics []*Metric `yaml:"metrics"`
Version int `yaml:"version,omitempty"`
Auth *Auth `yaml:"auth"`
XXX map[string]interface{} `yaml:",inline"`
}
func (c *Module) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultModule
type plain Module
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if err := checkOverflow(c.XXX, "module"); err != nil {
return err
}
if c.Auth == nil {
c.Auth = &DefaultAuth
}
if c.Version < 1 || c.Version > 3 {
return fmt.Errorf("SNMP version must be 1, 2 or 3. Got: %d", c.Version)
}
if c.Version == 3 {
if c.Auth.Username == "" {
return fmt.Errorf("Auth username is missing, required for SNMPv3")
}
if c.Auth.SecurityLevel != "authPriv" &&
c.Auth.SecurityLevel != "noAuthPriv" && c.Auth.SecurityLevel != "noAuthNoPriv" {
return fmt.Errorf("Security level must be one of authPriv, noAuthPriv or noAuthNoPriv")
}
if c.Auth.Password == "" && c.Auth.SecurityLevel != "noAuthNoPriv" {
return fmt.Errorf("Auth password is missing, required for SNMPv3 with auth.")
}
if c.Auth.AuthProtocol != "MD5" && c.Auth.AuthProtocol != "SHA" {
return fmt.Errorf("Auth protocol must be SHA or MD5.")
}
if c.Auth.PrivProtocol != "DES" && c.Auth.PrivProtocol != "AES" {
return fmt.Errorf("Priv protocol must be DES or AES.")
}
if c.Auth.PrivPassword == "" && c.Auth.SecurityLevel == "authPriv" {
return fmt.Errorf("Priv password is missing, required for SNMPv3 with priv.")
}
}
return nil
}
// configureSNMP sets the various version and auth settings.
func (c Module) configureSNMP(g *gosnmp.GoSNMP) {
switch c.Version {
case 1:
g.Version = gosnmp.Version1
case 2:
g.Version = gosnmp.Version2c
case 3:
g.Version = gosnmp.Version3
}
g.Community = c.Auth.Community
// v3 security settings.
g.SecurityModel = gosnmp.UserSecurityModel
switch c.Auth.SecurityLevel {
case "noAuthNoPriv":
g.MsgFlags = gosnmp.NoAuthNoPriv
case "authNoPriv":
g.MsgFlags = gosnmp.AuthNoPriv
case "authPriv":
g.MsgFlags = gosnmp.AuthPriv
}
usm := &gosnmp.UsmSecurityParameters{
UserName: c.Auth.Username,
AuthenticationPassphrase: c.Auth.Password,
PrivacyPassphrase: c.Auth.PrivPassword,
}
switch c.Auth.AuthProtocol {
case "SHA":
usm.AuthenticationProtocol = gosnmp.SHA
case "MD5":
usm.AuthenticationProtocol = gosnmp.MD5
}
switch c.Auth.PrivProtocol {
case "DES":
usm.PrivacyProtocol = gosnmp.DES
case "AES":
usm.PrivacyProtocol = gosnmp.AES
}
g.SecurityParameters = usm
}
type Metric struct {
Name string `yaml:"name"`
Oid string `yaml:"oid"`
Indexes []*Index `yaml:"indexes,omitempty"`
Lookups []*Lookup `yaml:"lookups,omitempty"`
XXX map[string]interface{} `yaml:",inline"`
}
func (c *Metric) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Metric
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if err := checkOverflow(c.XXX, "module"); err != nil {
return err
}
return nil
}
type Index struct {
Labelname string `yaml:"labelname"`
Type string `yaml:"type"`
XXX map[string]interface{} `yaml:",inline"`
}
func (c *Index) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Index
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if err := checkOverflow(c.XXX, "module"); err != nil {
return err
}
return nil
}
type Lookup struct {
Labels []string `yaml:"labels"`
Labelname string `yaml:"labelname"`
Oid string `yaml:"oid"`
XXX map[string]interface{} `yaml:",inline"`
}
func (c *Lookup) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Lookup
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if err := checkOverflow(c.XXX, "module"); err != nil {
return err
}
return nil
}
type Auth struct {
Community string `yaml:"community,omitempty"`
SecurityLevel string `yaml:"security_level,omitempty"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
AuthProtocol string `yaml:"auth_protocol,omitempty"`
PrivProtocol string `yaml:"priv_protocol,omitempty"`
PrivPassword string `yaml:"priv_password,omitempty"`
XXX map[string]interface{} `yaml:",inline"`
}
func (c *Auth) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultAuth
type plain Auth
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if err := checkOverflow(c.XXX, "module"); err != nil {
return err
}
return nil
}
func checkOverflow(m map[string]interface{}, ctx string) error {
if len(m) > 0 {
var keys []string
for k := range m {
keys = append(keys, k)
}
return fmt.Errorf("unknown fields in %s: %s", ctx, strings.Join(keys, ", "))
}
return nil
}