forked from dynport/metrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_linux.go
74 lines (65 loc) · 1.9 KB
/
net_linux.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
package main
import (
"io/ioutil"
"strings"
)
const NET = "net"
func init() {
parser.Add(NET, "true", "Collect network metrics")
}
var mapStatMapping = map[string]string{
"Ip.InReceives": "ip.TotalPacketsReceived",
"Ip.ForwDatagrams": "ip.Forwarded",
"Ip.InDiscards": "ip.IncomingPacketsDiscarded",
"Ip.InDelivers": "ip.IncomingPacketsDelivered",
"Ip.OutRequests": "ip.RequestsSentOut",
"Tcp.ActiveOpens": "tcp.ActiveConnectionsOpenings",
"Tcp.PassiveOpens": "tcp.PassiveConnectionsOpenings",
"Tcp.AttemptFails": "tcp.FailedConnectionAttempts",
"Tcp.EstabResets": "tcp.ConnectionResetsReceived",
"Tcp.CurrEstab": "tcp.ConnectionsEstablished",
"Tcp.InSegs": "tcp.SegmentsReceived",
"Tcp.OutSegs": "tcp.SegmentsSendOut",
"Tcp.RetransSegs": "tcp.SegmentsTransmitted",
"Tcp.InErrs": "tcp.BadSegmentsReceived",
"Tcp.OutRsts": "tcp.ResetsSent",
"Udp.InDatagrams": "udp.PacketsReceived",
"Udp.NoPorts.": "udp.PacketsToUnknownPortRecived",
"Udp.InErrors": "udp.PacketReceiveErrors",
"Udp.OutDatagrams": "udp.PacketsSent",
"IpExt.InOctets": "ip.InOctets",
"IpExt.OutOctets": "ip.OutOctets",
}
type Net struct {
RawStatus []byte
}
func (self *Net) fetch(key string) (b []byte, e error) {
return ioutil.ReadFile(ProcRoot() + "/proc/net/" + key)
}
func (self *Net) Prefix() string {
return "net"
}
func (self *Net) collect2LineFile(c *MetricsCollection, name string) (e error) {
b, e := self.fetch(name)
if e != nil {
return e
}
raw := string(b)
lines := strings.Split(raw, "\n")
for i := 0; i < len(lines)-1; i += 2 {
for k, v := range parse2lines(lines[i], lines[i+1]) {
if mapped, ok := mapStatMapping[k]; ok {
c.Add(mapped, v)
}
}
}
return nil
}
func (self *Net) Collect(c *MetricsCollection) error {
for _, name := range []string{"snmp", "netstat"} {
if e := self.collect2LineFile(c, name); e != nil {
return e
}
}
return nil
}