-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit aaf43bc
Showing
4 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package ifread | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/VividCortex/ewma" | ||
) | ||
|
||
type NicIO struct { | ||
name string | ||
rates ifaceRates | ||
ewma ifaceEWMA | ||
sync.RWMutex | ||
} | ||
type ifaceRates struct { | ||
rxb, txb, rxp, txp func(uint64) uint64 | ||
} | ||
|
||
type ifaceEWMA struct { | ||
rxb, txb, rxp, txp ewma.MovingAverage | ||
} | ||
|
||
// Delta is a small closure over the counters, returning the delta against previous | ||
// first = initial value | ||
func Delta(first uint64) func(uint64) uint64 { | ||
keep := first | ||
return func(delta uint64) uint64 { | ||
v := delta - keep | ||
keep = delta | ||
return v | ||
} | ||
} | ||
|
||
// MakeNic initalises the NicIO type | ||
// It'll be used in a receiver go channel | ||
func MakeNic(name string) (*NicIO, error) { | ||
var nic NicIO | ||
nic.name = name | ||
r, err := readvals(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
nic.Lock() | ||
defer nic.Unlock() | ||
nic.rates.rxb = Delta(r.rxb) | ||
nic.rates.txb = Delta(r.txb) | ||
nic.rates.rxp = Delta(r.rxp) | ||
nic.rates.txp = Delta(r.txp) | ||
nic.ewma.rxb = ewma.NewMovingAverage() | ||
nic.ewma.txb = ewma.NewMovingAverage() | ||
nic.ewma.rxp = ewma.NewMovingAverage() | ||
nic.ewma.txp = ewma.NewMovingAverage() | ||
return &nic, nil | ||
} | ||
|
||
// ReadNic put all NIC data in struct | ||
func (nic *NicIO) ReadNic() error { | ||
ifstat, err := readvals(nic.name) | ||
// fmt.Println("%+v", ifstat) | ||
if err == nil { | ||
// mind that calling nic.rate.xxx updates rate | ||
nic.ewma.rxb.Add(float64(nic.rates.rxb(ifstat.rxb))) | ||
nic.ewma.txb.Add(float64(nic.rates.txb(ifstat.txb))) | ||
nic.ewma.rxp.Add(float64(nic.rates.rxp(ifstat.rxp))) | ||
nic.ewma.txp.Add(float64(nic.rates.txp(ifstat.txp))) | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
// ListNics retuns the list of nics | ||
// TODO: make it regexp aware | ||
func ListNics() []string { | ||
var ifaces []string | ||
l, err := ioutil.ReadDir("/sys/class/net") | ||
if err != nil { | ||
errors.New("Can't read /sys/class/net. Is /sys mounted? Bailing...") | ||
os.Exit(1) | ||
} | ||
for _, iface := range l { | ||
ifaces = append(ifaces, iface.Name()) | ||
} | ||
return ifaces | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ifread | ||
|
||
import ( | ||
"io/ioutil" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type IfStat struct { | ||
rxb, txb, rxp, txp uint64 | ||
} | ||
|
||
func readval(path string) (uint64, error) { | ||
contents, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return 0, err | ||
} | ||
// we assume that's always ok ? | ||
val := strings.Split(string(contents), "\n") | ||
digit, _ := strconv.ParseUint(val[0], 10, 64) | ||
return digit, nil | ||
} | ||
|
||
func readvals(i string) (IfStat, error) { | ||
v := IfStat{} | ||
var err error | ||
// repeat .. fsck it only 4 | ||
if v.rxb, err = readval("/sys/class/net/" + i + "/statistics/rx_bytes"); err != nil { | ||
errfound = true | ||
} | ||
if v.txb, err = readval("/sys/class/net/" + i + "/statistics/tx_bytes"); err != nil { | ||
errfound = true | ||
} | ||
if v.rxp, err = readval("/sys/class/net/" + i + "/statistics/rx_packets"); err != nil { | ||
errfound = true | ||
} | ||
if v.txp, err = readval("/sys/class/net/" + i + "/statistics/tx_packets"); err != nil { | ||
errfound = true | ||
} | ||
if errfound { | ||
return nil, err | ||
} | ||
return v, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
fmt.Println("vim-go") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package squeezer | ||
|
||
import "github.com/delandtj/netork/ifread" | ||
|
||
func main() { | ||
fmt.Println("vim-go") | ||
} | ||
|
||
const ( | ||
_ = iota | ||
1000mbit | ||
500mbit | ||
200mbit | ||
100mbit | ||
50mbit | ||
20mbit | ||
5mbit | ||
1mbit | ||
200kbit | ||
20kbit | ||
) | ||
|
||
func SqueezeBW(name string, bw int) error { | ||
|
||
} |