This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathifmon.go
163 lines (149 loc) · 3.42 KB
/
ifmon.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
package main
import (
"errors"
"log"
"os"
"path"
"strconv"
"sync"
)
const (
sysClassPath = `/sys/class/net/`
sysClassRxPath = `/statistics/rx_bytes`
sysClassTxPath = `/statistics/tx_bytes`
)
var (
ErrInvalidInterface = errors.New("Interface is invalid")
ErrClosed = errors.New("Interface Closed")
ErrInterfaceOpen = errors.New("Interface is already open")
ErrFailedSeek = errors.New("Failed to seek stat file")
ErrInvalidData = errors.New("Invalid data")
)
type Iface struct {
name string
alias string
fioSend *os.File
fioRecv *os.File
mtx *sync.Mutex
lastSend uint64
lastRecv uint64
open bool
}
func NewIfmon(name, alias string) (*Iface, error) {
iface := &Iface{
name: name,
alias: alias,
mtx: &sync.Mutex{},
open: true,
}
if err := iface.reopenInterfaces(); err != nil {
log.Printf("Failed to open %s, will keep trying: %v\n", name, err)
}
return iface, nil
}
//reopeninterfaces is NOT protected by the mutex, caller must hold it
func (iface *Iface) reopenInterfaces() error {
if iface.fioSend != nil || iface.fioRecv != nil {
return ErrInterfaceOpen
}
//open up both the file descriptors
fioRx, err := os.Open(path.Join(sysClassPath, iface.name, sysClassRxPath))
if err != nil {
return ErrInvalidInterface
}
fioTx, err := os.Open(path.Join(sysClassPath, iface.name, sysClassTxPath))
if err != nil {
fioRx.Close()
return ErrInvalidInterface
}
iface.fioSend = fioTx
iface.fioRecv = fioRx
return nil
}
//closeInterfaces tries to do a little cleanup, but is mainly for when an interface disapears
func (iface *Iface) closeInterfaces() {
//shutdown send
iface.fioSend.Close()
iface.fioSend = nil
iface.lastSend = 0
//shutdown recv
iface.fioRecv.Close()
iface.fioRecv = nil
iface.lastRecv = 0
}
func (iface *Iface) Close() error {
iface.mtx.Lock()
defer iface.mtx.Unlock()
if !iface.open {
return ErrClosed
}
if err := iface.fioSend.Close(); err != nil {
return err
}
if err := iface.fioRecv.Close(); err != nil {
return err
}
iface.open = false
iface.fioSend = nil
iface.fioRecv = nil
return nil
}
func (iface *Iface) getFioInt(fio *os.File) (uint64, error) {
bt := make([]byte, 64)
n, err := fio.Seek(0, 0)
if err != nil {
return 0, err
}
if n != 0 {
return 0, ErrFailedSeek
}
rn, err := fio.Read(bt)
if err != nil {
return 0, err
}
if bt[rn-1] != '\n' || rn < 2 {
return 0, ErrInvalidData
}
v := string(bt[0 : rn-1])
return strconv.ParseUint(v, 10, 64)
}
//getStats returns send bytes, recv bytes, and error
//returned data is the quantity of bytes sent/recv since last query
func (iface *Iface) GetStats() (uint64, uint64, error) {
iface.mtx.Lock()
defer iface.mtx.Unlock()
//check if interfaces are closed, if so try to reopen them
if iface.fioSend == nil || iface.fioRecv == nil {
if err := iface.reopenInterfaces(); err != nil {
//failed, return 0
return 0, 0, nil
}
}
rx, err := iface.getFioInt(iface.fioRecv)
if err != nil {
iface.closeInterfaces()
return 0, 0, nil
}
tx, err := iface.getFioInt(iface.fioSend)
if err != nil {
iface.closeInterfaces()
return 0, 0, nil
}
sendInt := tx - iface.lastSend
recvInt := rx - iface.lastRecv
if iface.lastSend == 0 {
sendInt = 0
}
if iface.lastRecv == 0 {
recvInt = 0
}
iface.lastSend = tx
iface.lastRecv = rx
return sendInt, recvInt, nil
}
func (iface Iface) Name() string {
if iface.alias == "" {
return iface.name
}
return iface.alias
}