2014-03-09 16:53:21 +01:00
|
|
|
/*
|
2017-05-22 14:58:47 +02:00
|
|
|
* Copyright (C) 2014-2017 struktur AG
|
|
|
|
* http://www.strukturag.com
|
|
|
|
*
|
|
|
|
* 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.
|
2014-03-09 16:53:21 +01:00
|
|
|
*/
|
2017-05-22 14:58:47 +02:00
|
|
|
|
2017-05-22 16:07:58 +02:00
|
|
|
package realtimetraffic
|
2014-03-09 16:53:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var grabkeys = []string{
|
|
|
|
"collisions",
|
|
|
|
"multicast",
|
|
|
|
"rx_bytes",
|
|
|
|
"rx_compressed",
|
|
|
|
"rx_crc_errors",
|
|
|
|
"rx_dropped",
|
|
|
|
"rx_errors",
|
|
|
|
"rx_fifo_errors",
|
|
|
|
"rx_frame_errors",
|
|
|
|
"rx_length_errors",
|
|
|
|
"rx_missed_errors",
|
|
|
|
"rx_over_errors",
|
|
|
|
"rx_packets",
|
|
|
|
"tx_aborted_errors",
|
|
|
|
"tx_bytes",
|
|
|
|
"tx_carrier_errors",
|
|
|
|
"tx_compressed",
|
|
|
|
"tx_dropped",
|
|
|
|
"tx_errors",
|
|
|
|
"tx_fifo_errors",
|
|
|
|
"tx_heartbeat_errors",
|
|
|
|
"tx_packets",
|
|
|
|
"tx_window_errors",
|
|
|
|
}
|
|
|
|
|
2017-05-22 16:07:58 +02:00
|
|
|
type Grabber struct {
|
2014-03-09 16:53:21 +01:00
|
|
|
path string
|
|
|
|
iface string
|
|
|
|
quit chan bool
|
|
|
|
running bool
|
|
|
|
count uint
|
|
|
|
}
|
|
|
|
|
2017-05-22 16:07:58 +02:00
|
|
|
func NewGrabber(iface string) *Grabber {
|
|
|
|
return &Grabber{
|
2014-03-09 16:53:21 +01:00
|
|
|
iface: iface,
|
|
|
|
path: fmt.Sprintf("/sys/class/net/%s/statistics", iface),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 16:07:58 +02:00
|
|
|
func (g *Grabber) grab() *Interfacedata {
|
|
|
|
data := &Interfacedata{}
|
2014-03-09 16:53:21 +01:00
|
|
|
statistics := map[string]interface{}{}
|
|
|
|
value := make([]byte, 100)
|
|
|
|
|
|
|
|
var file *os.File
|
|
|
|
var err error
|
|
|
|
for _, key := range grabkeys {
|
|
|
|
file, err = os.Open(path.Join(g.path, key))
|
|
|
|
if err == nil {
|
2017-05-22 16:07:58 +02:00
|
|
|
count, ferr := file.Read(value)
|
|
|
|
if ferr == nil {
|
|
|
|
statistics[key], ferr = strconv.ParseInt(strings.TrimSpace(string(value[:count])), 10, 64)
|
|
|
|
if ferr != nil {
|
|
|
|
log.Println("Failed to process data", ferr, value[:count], count)
|
2014-03-09 16:53:21 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-05-22 16:07:58 +02:00
|
|
|
log.Println("Failed to read data", ferr)
|
2014-03-09 16:53:21 +01:00
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
} else {
|
|
|
|
log.Println("Failed to load data", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data.set(g.iface, statistics)
|
|
|
|
|
2017-05-22 16:07:58 +02:00
|
|
|
return data
|
2014-03-09 16:53:21 +01:00
|
|
|
}
|
|
|
|
|
2017-05-22 16:07:58 +02:00
|
|
|
func (g *Grabber) Start(ch chan<- *Interfacedata) {
|
2014-03-09 16:53:21 +01:00
|
|
|
g.count++
|
|
|
|
if g.running {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
g.quit = make(chan bool)
|
|
|
|
g.running = true
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
fmt.Printf("Grabbing started: %s\n", g.iface)
|
|
|
|
ticker := time.NewTicker(1000 * time.Millisecond)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2017-05-22 16:07:58 +02:00
|
|
|
ch <- g.grab()
|
2014-03-09 16:53:21 +01:00
|
|
|
case <-g.quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-05-22 16:07:58 +02:00
|
|
|
func (g *Grabber) Stop() {
|
2014-03-09 16:53:21 +01:00
|
|
|
if !g.running {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
g.count--
|
|
|
|
if g.count == 0 {
|
|
|
|
g.running = false
|
|
|
|
close(g.quit)
|
|
|
|
fmt.Printf("Grabbing stopped: %s\n", g.iface)
|
|
|
|
}
|
|
|
|
}
|