Skip to content

Commit

Permalink
Net error (#41)
Browse files Browse the repository at this point in the history
* change Maxrete and Rate type to uint64

* print output

* update test

* change int to uint64

* change uint64 to int64

Co-authored-by: Stephane Beuret <[email protected]>
  • Loading branch information
saphoooo and de13 authored Oct 3, 2020
1 parent 15e95e6 commit 09711b3
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 140 deletions.
37 changes: 0 additions & 37 deletions gauges.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,43 +107,6 @@ var (
},
)

// RRD switch gauges
// as switch database seems to be broken, this one is not used at this time
/*
rx1Gauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "freebox_switch_rx1_bytes",
Help: "Receive rate on port 1 (in byte/s)",
})
tx1Gauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "freebox_switch_tx1_bytes",
Help: "Transmit on port 1 (in byte/s)",
})
rx2Gauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "freebox_switch_rx2_bytes",
Help: "Receive rate on port 2 (in byte/s)",
})
tx2Gauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "freebox_switch_tx2_bytes",
Help: "Transmit on port 2 (in byte/s)",
})
rx3Gauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "freebox_switch_rx3_bytes",
Help: "Receive rate on port 3 (in byte/s)",
})
tx3Gauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "freebox_switch_tx3_bytes",
Help: "Transmit on port 3 (in byte/s)",
})
rx4Gauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "freebox_switch_rx4_bytes",
Help: "Receive rate on port 4 (in byte/s)",
})
tx4Gauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "freebox_switch_tx4_bytes",
Help: "Transmit on port 4 (in byte/s)",
})
*/

// RRD net gauges
bwUpGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "freebox_net_bw_up_bytes",
Expand Down
96 changes: 48 additions & 48 deletions getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func getConnectionXdsl(authInf *authInfo, pr *postRequest, xSessionToken *string
}

// getDsl get dsl statistics
func getDsl(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int, error) {
func getDsl(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int64, error) {
d := &database{
DB: "dsl",
Fields: []string{"rate_up", "rate_down", "snr_up", "snr_down"},
Expand All @@ -117,63 +117,63 @@ func getDsl(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int, e

freeboxToken, err := setFreeboxToken(authInf, xSessionToken)
if err != nil {
return []int{}, err
return []int64{}, err
}
client := http.Client{}
r, err := json.Marshal(*d)
if err != nil {
return []int{}, err
return []int64{}, err
}
buf := bytes.NewReader(r)
req, err := http.NewRequest(pr.method, pr.url, buf)
if err != nil {
return []int{}, err
return []int64{}, err
}
req.Header.Add(pr.header, *xSessionToken)
resp, err := client.Do(req)
if err != nil {
return []int{}, err
return []int64{}, err
}
if resp.StatusCode == 404 {
return []int{}, errors.New(resp.Status)
return []int64{}, errors.New(resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []int{}, err
return []int64{}, err
}
rrdTest := rrd{}
err = json.Unmarshal(body, &rrdTest)
if err != nil {
if debug {
log.Println(string(body))
}
return []int{}, err
return []int64{}, err
}

if rrdTest.ErrorCode == "auth_required" {
*xSessionToken, err = getSessToken(freeboxToken, authInf, xSessionToken)
if err != nil {
return []int{}, err
return []int64{}, err
}
}

if rrdTest.ErrorCode != "" && rrdTest.ErrorCode != "auth_required" {
if rrdTest.status().Error() == "Unknown return code from the API" {
fmt.Println("getDsl")
}
return []int{}, rrdTest.status()
return []int64{}, rrdTest.status()
}

if len(rrdTest.Result.Data) == 0 {
return []int{}, nil
return []int64{}, nil
}

result := []int{rrdTest.Result.Data[0]["rate_up"], rrdTest.Result.Data[0]["rate_down"], rrdTest.Result.Data[0]["snr_up"], rrdTest.Result.Data[0]["snr_down"]}
result := []int64{rrdTest.Result.Data[0]["rate_up"], rrdTest.Result.Data[0]["rate_down"], rrdTest.Result.Data[0]["snr_up"], rrdTest.Result.Data[0]["snr_down"]}
return result, nil
}

// getTemp get temp statistics
func getTemp(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int, error) {
func getTemp(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int64, error) {
d := &database{
DB: "temp",
Fields: []string{"cpum", "cpub", "sw", "hdd", "fan_speed"},
Expand All @@ -183,63 +183,63 @@ func getTemp(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int,

freeboxToken, err := setFreeboxToken(authInf, xSessionToken)
if err != nil {
return []int{}, err
return []int64{}, err
}

client := http.Client{}
r, err := json.Marshal(*d)
if err != nil {
return []int{}, err
return []int64{}, err
}
buf := bytes.NewReader(r)
req, err := http.NewRequest(pr.method, fmt.Sprintf(pr.url), buf)
if err != nil {
return []int{}, err
return []int64{}, err
}
req.Header.Add(pr.header, *xSessionToken)
resp, err := client.Do(req)
if err != nil {
return []int{}, err
return []int64{}, err
}
if resp.StatusCode == 404 {
return []int{}, errors.New(resp.Status)
return []int64{}, errors.New(resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []int{}, err
return []int64{}, err
}
rrdTest := rrd{}
err = json.Unmarshal(body, &rrdTest)
if err != nil {
if debug {
log.Println(string(body))
}
return []int{}, err
return []int64{}, err
}

if rrdTest.ErrorCode == "auth_required" {
*xSessionToken, err = getSessToken(freeboxToken, authInf, xSessionToken)
if err != nil {
return []int{}, err
return []int64{}, err
}
}

if rrdTest.ErrorCode != "" && rrdTest.ErrorCode != "auth_required" {
if rrdTest.status().Error() == "Unknown return code from the API" {
fmt.Println("getTemp")
}
return []int{}, rrdTest.status()
return []int64{}, rrdTest.status()
}

if len(rrdTest.Result.Data) == 0 {
return []int{}, nil
return []int64{}, nil
}

return []int{rrdTest.Result.Data[0]["cpum"], rrdTest.Result.Data[0]["cpub"], rrdTest.Result.Data[0]["sw"], rrdTest.Result.Data[0]["hdd"], rrdTest.Result.Data[0]["fan_speed"]}, nil
return []int64{rrdTest.Result.Data[0]["cpum"], rrdTest.Result.Data[0]["cpub"], rrdTest.Result.Data[0]["sw"], rrdTest.Result.Data[0]["hdd"], rrdTest.Result.Data[0]["fan_speed"]}, nil
}

// getNet get net statistics
func getNet(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int, error) {
func getNet(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int64, error) {
d := &database{
DB: "net",
Fields: []string{"bw_up", "bw_down", "rate_up", "rate_down", "vpn_rate_up", "vpn_rate_down"},
Expand All @@ -249,63 +249,63 @@ func getNet(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int, e

freeboxToken, err := setFreeboxToken(authInf, xSessionToken)
if err != nil {
return []int{}, err
return []int64{}, err
}

client := http.Client{}
r, err := json.Marshal(*d)
if err != nil {
return []int{}, err
return []int64{}, err
}
buf := bytes.NewReader(r)
req, err := http.NewRequest(pr.method, pr.url, buf)
if err != nil {
return []int{}, err
return []int64{}, err
}
req.Header.Add(pr.header, *xSessionToken)
resp, err := client.Do(req)
if err != nil {
return []int{}, err
return []int64{}, err
}
if resp.StatusCode == 404 {
return []int{}, errors.New(resp.Status)
return []int64{}, errors.New(resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []int{}, err
return []int64{}, err
}
rrdTest := rrd{}
err = json.Unmarshal(body, &rrdTest)
if err != nil {
if debug {
log.Println(string(body))
}
return []int{}, err
return []int64{}, err
}

if rrdTest.ErrorCode == "auth_required" {
*xSessionToken, err = getSessToken(freeboxToken, authInf, xSessionToken)
if err != nil {
return []int{}, err
return []int64{}, err
}
}

if rrdTest.ErrorCode != "" && rrdTest.ErrorCode != "auth_required" {
if rrdTest.status().Error() == "Unknown return code from the API" {
fmt.Println("getNet")
}
return []int{}, rrdTest.status()
return []int64{}, rrdTest.status()
}

if len(rrdTest.Result.Data) == 0 {
return []int{}, nil
return []int64{}, nil
}

return []int{rrdTest.Result.Data[0]["bw_up"], rrdTest.Result.Data[0]["bw_down"], rrdTest.Result.Data[0]["rate_up"], rrdTest.Result.Data[0]["rate_down"], rrdTest.Result.Data[0]["vpn_rate_up"], rrdTest.Result.Data[0]["vpn_rate_down"]}, nil
return []int64{rrdTest.Result.Data[0]["bw_up"], rrdTest.Result.Data[0]["bw_down"], rrdTest.Result.Data[0]["rate_up"], rrdTest.Result.Data[0]["rate_down"], rrdTest.Result.Data[0]["vpn_rate_up"], rrdTest.Result.Data[0]["vpn_rate_down"]}, nil
}

// getSwitch get switch statistics
func getSwitch(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int, error) {
func getSwitch(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int64, error) {
d := &database{
DB: "switch",
Fields: []string{"rx_1", "tx_1", "rx_2", "tx_2", "rx_3", "tx_3", "rx_4", "tx_4"},
Expand All @@ -315,59 +315,59 @@ func getSwitch(authInf *authInfo, pr *postRequest, xSessionToken *string) ([]int

freeboxToken, err := setFreeboxToken(authInf, xSessionToken)
if err != nil {
return []int{}, err
return []int64{}, err
}

client := http.Client{}
r, err := json.Marshal(*d)
if err != nil {
return []int{}, err
return []int64{}, err
}
buf := bytes.NewReader(r)
req, err := http.NewRequest(pr.method, pr.url, buf)
if err != nil {
return []int{}, err
return []int64{}, err
}
req.Header.Add(pr.header, *xSessionToken)
resp, err := client.Do(req)
if err != nil {
return []int{}, err
return []int64{}, err
}
if resp.StatusCode == 404 {
return []int{}, errors.New(resp.Status)
return []int64{}, errors.New(resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []int{}, err
return []int64{}, err
}
rrdTest := rrd{}
err = json.Unmarshal(body, &rrdTest)
if err != nil {
if debug {
log.Println(string(body))
}
return []int{}, err
return []int64{}, err
}

if rrdTest.ErrorCode == "auth_required" {
*xSessionToken, err = getSessToken(freeboxToken, authInf, xSessionToken)
if err != nil {
return []int{}, err
return []int64{}, err
}
}

if rrdTest.ErrorCode != "" && rrdTest.ErrorCode != "auth_required" {
if rrdTest.status().Error() == "Unknown return code from the API" {
fmt.Println("getSwitch")
}
return []int{}, rrdTest.status()
return []int64{}, rrdTest.status()
}

if len(rrdTest.Result.Data) == 0 {
return []int{}, nil
return []int64{}, nil
}

return []int{rrdTest.Result.Data[0]["rx_1"], rrdTest.Result.Data[0]["tx_1"], rrdTest.Result.Data[0]["rx_2"], rrdTest.Result.Data[0]["tx_2"], rrdTest.Result.Data[0]["rx_3"], rrdTest.Result.Data[0]["tx_3"], rrdTest.Result.Data[0]["rx_4"], rrdTest.Result.Data[0]["tx_4"]}, nil
return []int64{rrdTest.Result.Data[0]["rx_1"], rrdTest.Result.Data[0]["tx_1"], rrdTest.Result.Data[0]["rx_2"], rrdTest.Result.Data[0]["tx_2"], rrdTest.Result.Data[0]["rx_3"], rrdTest.Result.Data[0]["tx_3"], rrdTest.Result.Data[0]["rx_4"], rrdTest.Result.Data[0]["tx_4"]}, nil
}

// getLan get lan statistics
Expand Down
Loading

0 comments on commit 09711b3

Please sign in to comment.