ssh_crawler/stat/network.go

147 lines
3.2 KiB
Go
Raw Normal View History

2017-10-23 05:31:04 +00:00
package stat
import (
"bufio"
2017-10-23 09:08:19 +00:00
"fmt"
"io"
2017-10-23 05:31:04 +00:00
"regexp"
"strings"
2017-10-24 09:23:56 +00:00
"strconv"
"git.loafle.net/overflow/ssh_crawler/util"
2017-10-23 05:31:04 +00:00
)
type NetDevStat struct {
2017-10-24 07:09:17 +00:00
Iface,
RecvByte,
RecvPacket,
RecvErr,
RecvDrop,
RecvFifo,
RecvFrame,
RecvCompressed,
RecvMulticast,
TransByte,
TransPacket,
TransErr,
TransDrop,
TransFifo,
TransFrame,
TransCompressed,
TransMulticast string
2017-10-23 05:31:04 +00:00
}
func (net *NetDevStat) Command() string {
return "cat /proc/net/dev"
}
2017-10-25 08:37:32 +00:00
func (net *NetDevStat) Read(r io.Reader, keys []string) (interface{}, error) {
2017-10-23 05:31:04 +00:00
var (
scanner = bufio.NewScanner(r)
procNetDevFieldSep = regexp.MustCompile("[ :] *")
2017-10-24 09:23:56 +00:00
stats = []NetDevStat{}
2017-10-23 05:31:04 +00:00
)
scanner.Scan()
scanner.Scan()
parts := strings.Split(scanner.Text(), "|")
if len(parts) != 3 { // interface + receive + transmit
return nil, fmt.Errorf("invalid header line in net/dev: %s",
scanner.Text())
}
header := strings.Fields(parts[1])
for scanner.Scan() {
line := strings.TrimLeft(scanner.Text(), " ")
parts := procNetDevFieldSep.Split(line, -1)
if len(parts) != 2*len(header)+1 {
return nil, fmt.Errorf("invalid line in net/dev: %s", scanner.Text())
}
dev := parts[0][:len(parts[0])]
2017-10-24 09:23:56 +00:00
stats = append(stats, NetDevStat{
2017-10-24 07:09:17 +00:00
Iface: dev,
RecvByte: parts[1],
RecvPacket: parts[2],
RecvErr: parts[3],
RecvDrop: parts[4],
RecvFifo: parts[5],
RecvFrame: parts[6],
RecvCompressed: parts[7],
RecvMulticast: parts[8],
TransByte: parts[9],
TransPacket: parts[10],
TransErr: parts[11],
TransDrop: parts[12],
TransFifo: parts[13],
TransFrame: parts[14],
TransCompressed: parts[15],
TransMulticast: parts[16],
2017-10-23 05:31:04 +00:00
})
}
2017-10-24 09:23:56 +00:00
2017-10-25 08:37:32 +00:00
if keys == nil {
return &stats, nil
}
2017-10-24 09:23:56 +00:00
res, err := net.parse(keys, stats)
if err != nil {
return nil, err
}
return &res, scanner.Err()
2017-10-23 09:08:19 +00:00
}
2017-10-24 09:23:56 +00:00
func (net *NetDevStat) parse(keys []string, data []NetDevStat) (map[string]string, error) {
resMap := make(map[string]string)
for _, key := range keys {
t := strings.Split(key, ".")
suffix := t[len(t)-1]
ext := util.ExtractInBracket(key)
idx, _ := strconv.Atoi(ext)
2017-11-08 02:43:25 +00:00
var regex = regexp.MustCompile(`\[(.*?)\]`)
rk := regex.ReplaceAllString(key, ``)
2017-10-24 09:23:56 +00:00
switch suffix {
case "iface":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].Iface
2017-10-24 09:23:56 +00:00
case "recv_byte":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].RecvByte
2017-10-24 09:23:56 +00:00
case "recv_packet":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].RecvPacket
2017-10-24 09:23:56 +00:00
case "recv_err":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].RecvErr
2017-10-24 09:23:56 +00:00
case "recv_drop":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].RecvDrop
2017-10-24 09:23:56 +00:00
case "recv_fifo":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].RecvFifo
2017-10-24 09:23:56 +00:00
case "recv_frame":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].RecvFrame
2017-10-24 09:23:56 +00:00
case "recv_compressed":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].RecvCompressed
2017-10-24 09:23:56 +00:00
case "recv_multicast":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].RecvMulticast
2017-10-24 09:23:56 +00:00
case "send_byte":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].TransByte
2017-10-24 09:23:56 +00:00
case "send_packet":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].TransPacket
2017-10-24 09:23:56 +00:00
case "send_err":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].TransErr
2017-10-24 09:23:56 +00:00
case "send_drop":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].TransDrop
2017-10-24 09:23:56 +00:00
case "send_fifo":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].TransFifo
2017-10-24 09:23:56 +00:00
case "send_frame":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].TransFrame
2017-10-24 09:23:56 +00:00
case "send_compressed":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].TransCompressed
2017-10-24 09:23:56 +00:00
case "send_multicast":
2017-11-08 02:43:25 +00:00
resMap[rk] = data[idx].TransMulticast
2017-10-24 09:23:56 +00:00
default:
}
}
return resMap, nil
}