|
@@ -3,6 +3,7 @@ package netstat
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
+ "fmt"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
"os"
|
|
@@ -97,6 +98,7 @@ func NewNetStatBuffer(recordCount int) (*NetStatBuffers, error) {
|
|
|
for {
|
|
|
select {
|
|
|
case <-n.StopChan:
|
|
|
+ fmt.Println("- Netstats listener stopped")
|
|
|
return
|
|
|
|
|
|
case <-ticker.C:
|
|
@@ -203,7 +205,12 @@ func GetNetworkInterfaceStats() (int64, int64, error) {
|
|
|
return 0, 0, err
|
|
|
}
|
|
|
|
|
|
- defer cmd.Process.Kill()
|
|
|
+ //Spawn a timer to terminate the cmd process if timeout
|
|
|
+ var timer *time.Timer
|
|
|
+ timer = time.AfterFunc(3*time.Second, func() {
|
|
|
+ timer.Stop()
|
|
|
+ cmd.Process.Kill()
|
|
|
+ })
|
|
|
|
|
|
//Filter out the first line
|
|
|
lines := strings.Split(strings.ReplaceAll(string(out), "\r\n", "\n"), "\n")
|
|
@@ -214,7 +221,7 @@ func GetNetworkInterfaceStats() (int64, int64, error) {
|
|
|
}
|
|
|
dataLine = strings.TrimSpace(dataLine)
|
|
|
info := strings.Split(dataLine, " ")
|
|
|
- if len(info) < 3 {
|
|
|
+ if len(info) != 3 {
|
|
|
return 0, 0, errors.New("Invalid wmic results")
|
|
|
}
|
|
|
rxString := info[0]
|
|
@@ -230,11 +237,10 @@ func GetNetworkInterfaceStats() (int64, int64, error) {
|
|
|
tx = s
|
|
|
}
|
|
|
|
|
|
- //log.Println(rx, tx)
|
|
|
+ log.Println(rx, tx)
|
|
|
return rx * 4, tx * 4, nil
|
|
|
} else {
|
|
|
//Invalid data
|
|
|
- log.Println("invalid wmic results: ", lines)
|
|
|
return 0, 0, errors.New("Invalid wmic results")
|
|
|
}
|
|
|
|