@@ -44,13 +44,12 @@ var (
44
44
SecurityLevel : "noAuthNoPriv" ,
45
45
AuthProtocol : "MD5" ,
46
46
PrivProtocol : "DES" ,
47
+ Version : 2 ,
47
48
}
48
49
DefaultWalkParams = WalkParams {
49
- Version : 2 ,
50
50
MaxRepetitions : 25 ,
51
51
Retries : & defaultRetries ,
52
52
Timeout : time .Second * 5 ,
53
- Auth : DefaultAuth ,
54
53
UseUnconnectedUDPSocket : false ,
55
54
AllowNonIncreasingOIDs : false ,
56
55
}
@@ -63,14 +62,15 @@ var (
63
62
)
64
63
65
64
// Config for the snmp_exporter.
66
- type Config map [string ]* Module
65
+ type Config struct {
66
+ Auths map [string ]* Auth `yaml:"auths",omitempty"`
67
+ Modules map [string ]* Module `yaml:"modules",omitempty"`
68
+ }
67
69
68
70
type WalkParams struct {
69
- Version int `yaml:"version,omitempty"`
70
71
MaxRepetitions uint32 `yaml:"max_repetitions,omitempty"`
71
72
Retries * int `yaml:"retries,omitempty"`
72
73
Timeout time.Duration `yaml:"timeout,omitempty"`
73
- Auth Auth `yaml:"auth,omitempty"`
74
74
UseUnconnectedUDPSocket bool `yaml:"use_unconnected_udp_socket,omitempty"`
75
75
AllowNonIncreasingOIDs bool `yaml:"allow_nonincreasing_oids,omitempty"`
76
76
}
@@ -89,43 +89,11 @@ func (c *Module) UnmarshalYAML(unmarshal func(interface{}) error) error {
89
89
if err := unmarshal ((* plain )(c )); err != nil {
90
90
return err
91
91
}
92
-
93
- wp := c .WalkParams
94
-
95
- if wp .Version < 1 || wp .Version > 3 {
96
- return fmt .Errorf ("SNMP version must be 1, 2 or 3. Got: %d" , wp .Version )
97
- }
98
- if wp .Version == 3 {
99
- switch wp .Auth .SecurityLevel {
100
- case "authPriv" :
101
- if wp .Auth .PrivPassword == "" {
102
- return fmt .Errorf ("priv password is missing, required for SNMPv3 with priv" )
103
- }
104
- if wp .Auth .PrivProtocol != "DES" && wp .Auth .PrivProtocol != "AES" && wp .Auth .PrivProtocol != "AES192" && wp .Auth .PrivProtocol != "AES192C" && wp .Auth .PrivProtocol != "AES256" && wp .Auth .PrivProtocol != "AES256C" {
105
- return fmt .Errorf ("priv protocol must be DES or AES" )
106
- }
107
- fallthrough
108
- case "authNoPriv" :
109
- if wp .Auth .Password == "" {
110
- return fmt .Errorf ("auth password is missing, required for SNMPv3 with auth" )
111
- }
112
- if wp .Auth .AuthProtocol != "MD5" && wp .Auth .AuthProtocol != "SHA" && wp .Auth .AuthProtocol != "SHA224" && wp .Auth .AuthProtocol != "SHA256" && wp .Auth .AuthProtocol != "SHA384" && wp .Auth .AuthProtocol != "SHA512" {
113
- return fmt .Errorf ("auth protocol must be SHA or MD5" )
114
- }
115
- fallthrough
116
- case "noAuthNoPriv" :
117
- if wp .Auth .Username == "" {
118
- return fmt .Errorf ("auth username is missing, required for SNMPv3" )
119
- }
120
- default :
121
- return fmt .Errorf ("security level must be one of authPriv, authNoPriv or noAuthNoPriv" )
122
- }
123
- }
124
92
return nil
125
93
}
126
94
127
95
// ConfigureSNMP sets the various version and auth settings.
128
- func (c WalkParams ) ConfigureSNMP (g * gosnmp.GoSNMP ) {
96
+ func (c Auth ) ConfigureSNMP (g * gosnmp.GoSNMP ) {
129
97
switch c .Version {
130
98
case 1 :
131
99
g .Version = gosnmp .Version1
@@ -134,16 +102,16 @@ func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP) {
134
102
case 3 :
135
103
g .Version = gosnmp .Version3
136
104
}
137
- g .Community = string (c .Auth . Community )
138
- g .ContextName = c .Auth . ContextName
105
+ g .Community = string (c .Community )
106
+ g .ContextName = c .ContextName
139
107
140
108
// v3 security settings.
141
109
g .SecurityModel = gosnmp .UserSecurityModel
142
110
usm := & gosnmp.UsmSecurityParameters {
143
- UserName : c .Auth . Username ,
111
+ UserName : c .Username ,
144
112
}
145
113
auth , priv := false , false
146
- switch c .Auth . SecurityLevel {
114
+ switch c .SecurityLevel {
147
115
case "noAuthNoPriv" :
148
116
g .MsgFlags = gosnmp .NoAuthNoPriv
149
117
case "authNoPriv" :
@@ -155,8 +123,8 @@ func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP) {
155
123
priv = true
156
124
}
157
125
if auth {
158
- usm .AuthenticationPassphrase = string (c .Auth . Password )
159
- switch c .Auth . AuthProtocol {
126
+ usm .AuthenticationPassphrase = string (c .Password )
127
+ switch c .AuthProtocol {
160
128
case "SHA" :
161
129
usm .AuthenticationProtocol = gosnmp .SHA
162
130
case "SHA224" :
@@ -172,8 +140,8 @@ func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP) {
172
140
}
173
141
}
174
142
if priv {
175
- usm .PrivacyPassphrase = string (c .Auth . PrivPassword )
176
- switch c .Auth . PrivProtocol {
143
+ usm .PrivacyPassphrase = string (c .PrivPassword )
144
+ switch c .PrivProtocol {
177
145
case "DES" :
178
146
usm .PrivacyProtocol = gosnmp .DES
179
147
case "AES" :
@@ -245,6 +213,46 @@ type Auth struct {
245
213
PrivProtocol string `yaml:"priv_protocol,omitempty"`
246
214
PrivPassword Secret `yaml:"priv_password,omitempty"`
247
215
ContextName string `yaml:"context_name,omitempty"`
216
+ Version int `yaml:"version,omitempty"`
217
+ }
218
+
219
+ func (c * Auth ) UnmarshalYAML (unmarshal func (interface {}) error ) error {
220
+ * c = DefaultAuth
221
+ type plain Auth
222
+ if err := unmarshal ((* plain )(c )); err != nil {
223
+ return err
224
+ }
225
+
226
+ if c .Version < 1 || c .Version > 3 {
227
+ return fmt .Errorf ("SNMP version must be 1, 2 or 3. Got: %d" , c .Version )
228
+ }
229
+ if c .Version == 3 {
230
+ switch c .SecurityLevel {
231
+ case "authPriv" :
232
+ if c .PrivPassword == "" {
233
+ return fmt .Errorf ("priv password is missing, required for SNMPv3 with priv" )
234
+ }
235
+ if c .PrivProtocol != "DES" && c .PrivProtocol != "AES" && c .PrivProtocol != "AES192" && c .PrivProtocol != "AES192C" && c .PrivProtocol != "AES256" && c .PrivProtocol != "AES256C" {
236
+ return fmt .Errorf ("priv protocol must be DES or AES" )
237
+ }
238
+ fallthrough
239
+ case "authNoPriv" :
240
+ if c .Password == "" {
241
+ return fmt .Errorf ("auth password is missing, required for SNMPv3 with auth" )
242
+ }
243
+ if c .AuthProtocol != "MD5" && c .AuthProtocol != "SHA" && c .AuthProtocol != "SHA224" && c .AuthProtocol != "SHA256" && c .AuthProtocol != "SHA384" && c .AuthProtocol != "SHA512" {
244
+ return fmt .Errorf ("auth protocol must be SHA or MD5" )
245
+ }
246
+ fallthrough
247
+ case "noAuthNoPriv" :
248
+ if c .Username == "" {
249
+ return fmt .Errorf ("auth username is missing, required for SNMPv3" )
250
+ }
251
+ default :
252
+ return fmt .Errorf ("security level must be one of authPriv, authNoPriv or noAuthNoPriv" )
253
+ }
254
+ }
255
+ return nil
248
256
}
249
257
250
258
type RegexpExtract struct {
0 commit comments