aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/prometheus/procfs/proc_snmp.go
blob: b9d2cf642a7455e712c2bfc89d9821228d0617a6 (plain) (blame)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2022 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package procfs

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"strconv"
	"strings"

	"github.com/prometheus/procfs/internal/util"
)

// ProcSnmp models the content of /proc/<pid>/net/snmp.
type ProcSnmp struct {
	// The process ID.
	PID int
	Ip
	Icmp
	IcmpMsg
	Tcp
	Udp
	UdpLite
}

type Ip struct { // nolint:revive
	Forwarding      *float64
	DefaultTTL      *float64
	InReceives      *float64
	InHdrErrors     *float64
	InAddrErrors    *float64
	ForwDatagrams   *float64
	InUnknownProtos *float64
	InDiscards      *float64
	InDelivers      *float64
	OutRequests     *float64
	OutDiscards     *float64
	OutNoRoutes     *float64
	ReasmTimeout    *float64
	ReasmReqds      *float64
	ReasmOKs        *float64
	ReasmFails      *float64
	FragOKs         *float64
	FragFails       *float64
	FragCreates     *float64
}

type Icmp struct { // nolint:revive
	InMsgs           *float64
	InErrors         *float64
	InCsumErrors     *float64
	InDestUnreachs   *float64
	InTimeExcds      *float64
	InParmProbs      *float64
	InSrcQuenchs     *float64
	InRedirects      *float64
	InEchos          *float64
	InEchoReps       *float64
	InTimestamps     *float64
	InTimestampReps  *float64
	InAddrMasks      *float64
	InAddrMaskReps   *float64
	OutMsgs          *float64
	OutErrors        *float64
	OutDestUnreachs  *float64
	OutTimeExcds     *float64
	OutParmProbs     *float64
	OutSrcQuenchs    *float64
	OutRedirects     *float64
	OutEchos         *float64
	OutEchoReps      *float64
	OutTimestamps    *float64
	OutTimestampReps *float64
	OutAddrMasks     *float64
	OutAddrMaskReps  *float64
}

type IcmpMsg struct {
	InType3  *float64
	OutType3 *float64
}

type Tcp struct { // nolint:revive
	RtoAlgorithm *float64
	RtoMin       *float64
	RtoMax       *float64
	MaxConn      *float64
	ActiveOpens  *float64
	PassiveOpens *float64
	AttemptFails *float64
	EstabResets  *float64
	CurrEstab    *float64
	InSegs       *float64
	OutSegs      *float64
	RetransSegs  *float64
	InErrs       *float64
	OutRsts      *float64
	InCsumErrors *float64
}

type Udp struct { // nolint:revive
	InDatagrams  *float64
	NoPorts      *float64
	InErrors     *float64
	OutDatagrams *float64
	RcvbufErrors *float64
	SndbufErrors *float64
	InCsumErrors *float64
	IgnoredMulti *float64
}

type UdpLite struct { // nolint:revive
	InDatagrams  *float64
	NoPorts      *float64
	InErrors     *float64
	OutDatagrams *float64
	RcvbufErrors *float64
	SndbufErrors *float64
	InCsumErrors *float64
	IgnoredMulti *float64
}

func (p Proc) Snmp() (ProcSnmp, error) {
	filename := p.path("net/snmp")
	data, err := util.ReadFileNoStat(filename)
	if err != nil {
		return ProcSnmp{PID: p.PID}, err
	}
	procSnmp, err := parseSnmp(bytes.NewReader(data), filename)
	procSnmp.PID = p.PID
	return procSnmp, err
}

// parseSnmp parses the metrics from proc/<pid>/net/snmp file
// and returns a map contains those metrics (e.g. {"Ip": {"Forwarding": 2}}).
func parseSnmp(r io.Reader, fileName string) (ProcSnmp, error) {
	var (
		scanner  = bufio.NewScanner(r)
		procSnmp = ProcSnmp{}
	)

	for scanner.Scan() {
		nameParts := strings.Split(scanner.Text(), " ")
		scanner.Scan()
		valueParts := strings.Split(scanner.Text(), " ")
		// Remove trailing :.
		protocol := strings.TrimSuffix(nameParts[0], ":")
		if len(nameParts) != len(valueParts) {
			return procSnmp, fmt.Errorf("%w: mismatch field count mismatch in %s: %s",
				ErrFileParse, fileName, protocol)
		}
		for i := 1; i < len(nameParts); i++ {
			value, err := strconv.ParseFloat(valueParts[i], 64)
			if err != nil {
				return procSnmp, err
			}
			key := nameParts[i]

			switch protocol {
			case "Ip":
				switch key {
				case "Forwarding":
					procSnmp.Ip.Forwarding = &value
				case "DefaultTTL":
					procSnmp.Ip.DefaultTTL = &value
				case "InReceives":
					procSnmp.Ip.InReceives = &value
				case "InHdrErrors":
					procSnmp.Ip.InHdrErrors = &value
				case "InAddrErrors":
					procSnmp.Ip.InAddrErrors = &value
				case "ForwDatagrams":
					procSnmp.Ip.ForwDatagrams = &value
				case "InUnknownProtos":
					procSnmp.Ip.InUnknownProtos = &value
				case "InDiscards":
					procSnmp.Ip.InDiscards = &value
				case "InDelivers":
					procSnmp.Ip.InDelivers = &value
				case "OutRequests":
					procSnmp.Ip.OutRequests = &value
				case "OutDiscards":
					procSnmp.Ip.OutDiscards = &value
				case "OutNoRoutes":
					procSnmp.Ip.OutNoRoutes = &value
				case "ReasmTimeout":
					procSnmp.Ip.ReasmTimeout = &value
				case "ReasmReqds":
					procSnmp.Ip.ReasmReqds = &value
				case "ReasmOKs":
					procSnmp.Ip.ReasmOKs = &value
				case "ReasmFails":
					procSnmp.Ip.ReasmFails = &value
				case "FragOKs":
					procSnmp.Ip.FragOKs = &value
				case "FragFails":
					procSnmp.Ip.FragFails = &value
				case "FragCreates":
					procSnmp.Ip.FragCreates = &value
				}
			case "Icmp":
				switch key {
				case "InMsgs":
					procSnmp.Icmp.InMsgs = &value
				case "InErrors":
					procSnmp.Icmp.InErrors = &value
				case "InCsumErrors":
					procSnmp.Icmp.InCsumErrors = &value
				case "InDestUnreachs":
					procSnmp.Icmp.InDestUnreachs = &value
				case "InTimeExcds":
					procSnmp.Icmp.InTimeExcds = &value
				case "InParmProbs":
					procSnmp.Icmp.InParmProbs = &value
				case "InSrcQuenchs":
					procSnmp.Icmp.InSrcQuenchs = &value
				case "InRedirects":
					procSnmp.Icmp.InRedirects = &value
				case "InEchos":
					procSnmp.Icmp.InEchos = &value
				case "InEchoReps":
					procSnmp.Icmp.InEchoReps = &value
				case "InTimestamps":
					procSnmp.Icmp.InTimestamps = &value
				case "InTimestampReps":
					procSnmp.Icmp.InTimestampReps = &value
				case "InAddrMasks":
					procSnmp.Icmp.InAddrMasks = &value
				case "InAddrMaskReps":
					procSnmp.Icmp.InAddrMaskReps = &value
				case "OutMsgs":
					procSnmp.Icmp.OutMsgs = &value
				case "OutErrors":
					procSnmp.Icmp.OutErrors = &value
				case "OutDestUnreachs":
					procSnmp.Icmp.OutDestUnreachs = &value
				case "OutTimeExcds":
					procSnmp.Icmp.OutTimeExcds = &value
				case "OutParmProbs":
					procSnmp.Icmp.OutParmProbs = &value
				case "OutSrcQuenchs":
					procSnmp.Icmp.OutSrcQuenchs = &value
				case "OutRedirects":
					procSnmp.Icmp.OutRedirects = &value
				case "OutEchos":
					procSnmp.Icmp.OutEchos = &value
				case "OutEchoReps":
					procSnmp.Icmp.OutEchoReps = &value
				case "OutTimestamps":
					procSnmp.Icmp.OutTimestamps = &value
				case "OutTimestampReps":
					procSnmp.Icmp.OutTimestampReps = &value
				case "OutAddrMasks":
					procSnmp.Icmp.OutAddrMasks = &value
				case "OutAddrMaskReps":
					procSnmp.Icmp.OutAddrMaskReps = &value
				}
			case "IcmpMsg":
				switch key {
				case "InType3":
					procSnmp.IcmpMsg.InType3 = &value
				case "OutType3":
					procSnmp.IcmpMsg.OutType3 = &value
				}
			case "Tcp":
				switch key {
				case "RtoAlgorithm":
					procSnmp.Tcp.RtoAlgorithm = &value
				case "RtoMin":
					procSnmp.Tcp.RtoMin = &value
				case "RtoMax":
					procSnmp.Tcp.RtoMax = &value
				case "MaxConn":
					procSnmp.Tcp.MaxConn = &value
				case "ActiveOpens":
					procSnmp.Tcp.ActiveOpens = &value
				case "PassiveOpens":
					procSnmp.Tcp.PassiveOpens = &value
				case "AttemptFails":
					procSnmp.Tcp.AttemptFails = &value
				case "EstabResets":
					procSnmp.Tcp.EstabResets = &value
				case "CurrEstab":
					procSnmp.Tcp.CurrEstab = &value
				case "InSegs":
					procSnmp.Tcp.InSegs = &value
				case "OutSegs":
					procSnmp.Tcp.OutSegs = &value
				case "RetransSegs":
					procSnmp.Tcp.RetransSegs = &value
				case "InErrs":
					procSnmp.Tcp.InErrs = &value
				case "OutRsts":
					procSnmp.Tcp.OutRsts = &value
				case "InCsumErrors":
					procSnmp.Tcp.InCsumErrors = &value
				}
			case "Udp":
				switch key {
				case "InDatagrams":
					procSnmp.Udp.InDatagrams = &value
				case "NoPorts":
					procSnmp.Udp.NoPorts = &value
				case "InErrors":
					procSnmp.Udp.InErrors = &value
				case "OutDatagrams":
					procSnmp.Udp.OutDatagrams = &value
				case "RcvbufErrors":
					procSnmp.Udp.RcvbufErrors = &value
				case "SndbufErrors":
					procSnmp.Udp.SndbufErrors = &value
				case "InCsumErrors":
					procSnmp.Udp.InCsumErrors = &value
				case "IgnoredMulti":
					procSnmp.Udp.IgnoredMulti = &value
				}
			case "UdpLite":
				switch key {
				case "InDatagrams":
					procSnmp.UdpLite.InDatagrams = &value
				case "NoPorts":
					procSnmp.UdpLite.NoPorts = &value
				case "InErrors":
					procSnmp.UdpLite.InErrors = &value
				case "OutDatagrams":
					procSnmp.UdpLite.OutDatagrams = &value
				case "RcvbufErrors":
					procSnmp.UdpLite.RcvbufErrors = &value
				case "SndbufErrors":
					procSnmp.UdpLite.SndbufErrors = &value
				case "InCsumErrors":
					procSnmp.UdpLite.InCsumErrors = &value
				case "IgnoredMulti":
					procSnmp.UdpLite.IgnoredMulti = &value
				}
			}
		}
	}
	return procSnmp, scanner.Err()
}