|
@@ -33,6 +33,7 @@ type NetStatBuffers struct {
|
|
|
PreviousStat *RawFlowStat //The value of the last instance of netstats
|
|
|
Stats []*FlowStat //Statistic of the flow
|
|
|
StopChan chan bool //Channel to stop the ticker
|
|
|
+ EventTicker *time.Ticker //Ticker for event logging
|
|
|
}
|
|
|
|
|
|
// Get a new network statistic buffers
|
|
@@ -58,7 +59,6 @@ func NewNetStatBuffer(recordCount int) (*NetStatBuffers, error) {
|
|
|
|
|
|
//Setup a timer to get the value from NIC accumulation stats
|
|
|
ticker := time.NewTicker(time.Second)
|
|
|
- defer ticker.Stop()
|
|
|
|
|
|
//Setup a stop channel
|
|
|
stopCh := make(chan bool)
|
|
@@ -68,6 +68,7 @@ func NewNetStatBuffer(recordCount int) (*NetStatBuffers, error) {
|
|
|
PreviousStat: &currnetNetSpec,
|
|
|
Stats: initialStats,
|
|
|
StopChan: stopCh,
|
|
|
+ EventTicker: ticker,
|
|
|
}
|
|
|
|
|
|
// Update the buffer every second
|
|
@@ -102,8 +103,10 @@ func NewNetStatBuffer(recordCount int) (*NetStatBuffers, error) {
|
|
|
TX: tx,
|
|
|
}
|
|
|
|
|
|
- copy(initialStats, initialStats[1:])
|
|
|
- initialStats = append(initialStats, newStat)
|
|
|
+ newStats := n.Stats[1:]
|
|
|
+ newStats = append(newStats, newStat)
|
|
|
+
|
|
|
+ n.Stats = newStats
|
|
|
}
|
|
|
}
|
|
|
}(&thisNetBuffer)
|
|
@@ -112,12 +115,37 @@ func NewNetStatBuffer(recordCount int) (*NetStatBuffers, error) {
|
|
|
}
|
|
|
|
|
|
func (n *NetStatBuffers) HandleGetBufferedNetworkInterfaceStats(w http.ResponseWriter, r *http.Request) {
|
|
|
- js, _ := json.Marshal(n.Stats)
|
|
|
- utils.SendJSONResponse(w, string(js))
|
|
|
+ arr, _ := utils.GetPara(r, "array")
|
|
|
+ if arr == "true" {
|
|
|
+ //Restructure it into array
|
|
|
+ rx := []int{}
|
|
|
+ tx := []int{}
|
|
|
+
|
|
|
+ for _, state := range n.Stats {
|
|
|
+ rx = append(rx, int(state.RX))
|
|
|
+ tx = append(tx, int(state.TX))
|
|
|
+ }
|
|
|
+
|
|
|
+ type info struct {
|
|
|
+ Rx []int
|
|
|
+ Tx []int
|
|
|
+ }
|
|
|
+
|
|
|
+ js, _ := json.Marshal(info{
|
|
|
+ Rx: rx,
|
|
|
+ Tx: tx,
|
|
|
+ })
|
|
|
+ utils.SendJSONResponse(w, string(js))
|
|
|
+ } else {
|
|
|
+ js, _ := json.Marshal(n.Stats)
|
|
|
+ utils.SendJSONResponse(w, string(js))
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
func (n *NetStatBuffers) Close() {
|
|
|
n.StopChan <- true
|
|
|
+ n.EventTicker.Stop()
|
|
|
}
|
|
|
|
|
|
func HandleGetNetworkInterfaceStats(w http.ResponseWriter, r *http.Request) {
|