blob: 5f232215bd72b37075aaf8e225fd160d40f32326 (
plain) (
blame)
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
|
package api
import (
"encoding/json"
"fmt"
"log"
"net"
"github.com/cilium/cilium/pkg/mac"
"github.com/dustin/go-humanize"
"sinanmohd.com/redq/usage"
)
type BandwidthStat struct {
Ingress string `json:"ingress"`
Egress string `json:"egress"`
}
type BandwidthResp map[string]BandwidthStat
func handleBandwidth(conn net.Conn, u *usage.Usage) {
resp := make(BandwidthResp)
u.Mutex.RLock()
for key, value := range u.Data {
m := mac.Uint64MAC(key)
resp[m.String()] = BandwidthStat{
Ingress: fmt.Sprintf("%s/s", humanize.Bytes(value.BandwidthIngress)),
Egress: fmt.Sprintf("%s/s", humanize.Bytes(value.BandwidthEgress)),
}
}
u.Mutex.RUnlock()
buf, err := json.Marshal(resp)
if err != nil {
log.Printf("marshaling json: %s", err)
return
}
conn.Write(buf)
}
|