forked from bpicode/fritzctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_inetstats.go
60 lines (51 loc) · 1.77 KB
/
list_inetstats.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
package cmd
import (
"fmt"
"strconv"
"strings"
"github.com/coolduke/fritzctl/fritz"
"github.com/coolduke/fritzctl/logger"
"github.com/spf13/cobra"
)
var listInetstatsCmd = &cobra.Command{
Use: "inetstats",
Short: "Get recent internet statistics",
Long: "Get recent internet upstream/downstream statistics from the FRITZ!Box.",
Example: "fritzctl list inetstats",
RunE: listInetstats,
}
func init() {
listCmd.AddCommand(listInetstatsCmd)
}
func listInetstats(_ *cobra.Command, _ []string) error {
c := clientLogin()
f := fritz.NewInternal(c)
stats, err := f.InternetStats()
assertNoErr(err, "cannot obtain internet stats")
logger.Success("Recent upstream/downstream time series:\n")
printTrafficData(stats)
return nil
}
func printTrafficData(data *fritz.TrafficMonitoringData) {
kbps := data.KiloBitsPerSecond()
printSlice("Downstream/internet [kb/s]: ", kbps.DownstreamInternet)
printSlice("Downstream/media [kb/s]: ", kbps.DownStreamMedia)
printSlice("Downstream/guest [kb/s]: ", kbps.DownStreamGuest)
printSlice("Upstream/low priority [kb/s]: ", kbps.UpstreamLowPriority)
printSlice("Upstream/default priority [kb/s]: ", kbps.UpstreamDefaultPriority)
printSlice("Upstream/high priority [kb/s]: ", kbps.UpstreamHighPriority)
printSlice("Upstream/realtime [kb/s]: ", kbps.UpstreamRealtime)
printSlice("Upstream/guest [kb/s]: ", kbps.UpstreamGuest)
}
func printSlice(pre string, data []float64) {
strs := float64Slice(data).formatFloats('f', 2)
fmt.Println(pre, strings.Join(strs, " "))
}
type float64Slice []float64
func (fs float64Slice) formatFloats(format byte, prec int) []string {
var strs []string
for _, f := range fs {
strs = append(strs, strconv.FormatFloat(f, format, prec, 64))
}
return strs
}