82 lines
1.5 KiB
Go
82 lines
1.5 KiB
Go
package sysdig
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
RX = iota
|
|
TX
|
|
)
|
|
|
|
type Record struct {
|
|
//Host, Container, Process string
|
|
//Pid, Tid uint64
|
|
Proto string
|
|
ServerIp net.IP
|
|
ServerPort uint16
|
|
ClientIp net.IP
|
|
ClientPort uint16
|
|
Direction int
|
|
Bytes uint64
|
|
}
|
|
|
|
func ParseOutput(result string) ([]Record, error) {
|
|
r := csv.NewReader(strings.NewReader(result))
|
|
r.Comma = '\t'
|
|
|
|
var records []Record
|
|
for {
|
|
record, err := r.Read()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
sip := net.ParseIP(record[1])
|
|
if sip == nil {
|
|
return nil, fmt.Errorf("invalid ip '%s'", record[1])
|
|
}
|
|
sport, err := strconv.ParseUint(record[2], 10, 16)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid port %s: %v", record[2], err)
|
|
}
|
|
cip := net.ParseIP(record[3])
|
|
if cip == nil {
|
|
return nil, fmt.Errorf("invalid ip '%s'", record[3])
|
|
}
|
|
cport, err := strconv.ParseUint(record[4], 10, 16)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid port %s: %v", record[4], err)
|
|
}
|
|
var direction int
|
|
if record[5] == "rx" {
|
|
direction = RX
|
|
} else {
|
|
direction = TX
|
|
}
|
|
|
|
bytes, err := strconv.ParseUint(record[6], 10, 64)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid transmission size %s: %v", record[11], err)
|
|
}
|
|
records = append(records, Record{
|
|
record[0],
|
|
sip,
|
|
uint16(sport),
|
|
cip,
|
|
uint16(cport),
|
|
direction,
|
|
bytes,
|
|
})
|
|
}
|
|
return records, nil
|
|
}
|