-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (81 loc) · 1.79 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"strings"
"time"
)
func getTransmissionData(outPut bytes.Buffer) {
var ll []string
nl := 0
for {
nl++
line, err := outPut.ReadString('\n')
if err != nil {
break
}
ll = strings.Fields(line)
if nl == 5 {
fmt.Printf("%v,%v,%v\n", time.Now().Format("2006-01-02 15:04:05"), ll[1], ll[2])
}
}
}
func getPingData(outPut bytes.Buffer) {
var ll []string
nl := 0
for {
nl++
line, err := outPut.ReadString('\n')
if err != nil {
break
}
ll = strings.Fields(line)
if nl == 3 {
bytes := strings.Split(ll[3:][0], "=")[1]
timeInMS := strings.Split(ll[3:][1], "=")[1]
responseTime := strings.Split(timeInMS, "ms")[0]
ttl := strings.Split(ll[3:][2], "=")[1]
fmt.Printf("%v,%v,%v,%v\n", time.Now().Format("2006-01-02 15:04:05"), bytes, responseTime, ttl)
}
}
}
func main() {
transmission := flag.Bool("transmission", false, "Every second printing local network transmission, format: timestamp,received,sent")
ping := flag.String("ping", "", "Every second printing the latency from remote address, format: timestamp,bytes,time,ttl")
flag.Parse()
if len(os.Args) == 1 {
flag.Usage()
os.Exit(1)
}
if *transmission {
for {
cmd := exec.Command("netstat", "-e")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
fmt.Printf("%v,%v\n", time.Now().Format("2006-01-02 15:04:05"), err)
continue
}
getTransmissionData(out)
time.Sleep(time.Second * 1)
}
}
if *ping != "" {
for {
cmd := exec.Command("ping", "-n", "1", *ping)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
fmt.Printf("%v,not_resp\n", time.Now().Format("2006-01-02 15:04:05"))
continue
}
getPingData(out)
time.Sleep(time.Second * 1)
}
}
}