-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathubee-prometheus.go
175 lines (145 loc) · 4.46 KB
/
ubee-prometheus.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"sync"
"time"
"log"
)
type DSStatus struct {
Ds_id string
Ds_snr string
Ds_power string
Ds_correct string
Ds_uncorrect string
Ds_modulation string
Ds_freq string
Ds_width string
}
type USStatus struct {
Us_status string
Us_type string
Us_id string
Us_freq string
Us_width string
Us_power string
Us_modulation string
}
type CMInfo struct {
Cm_conn_ip_prov_mode string
Cm_conn_wan_mode string
Cm_conn_ds_gourpObj []DSStatus
Cm_conn_us_gourpObj []USStatus
}
//! Create a prometheus line for DS
func doDSField(value string, proname string, id int, description string, protype string, factor float64) string {
ret := ""
name := "cable_downstream_"+proname;
ret += "# HELP "+name+" "+description+"\n"
ret += "# TYPE "+name+" "+protype+"\n"
if flval, err := strconv.ParseFloat(value, 32) ; err == nil {
ret += fmt.Sprintf("%s{id=\"%d\"} %f\n", name, id, flval*factor)
} else {
log.Fatalln("Unable to convert value: "+err.Error())
return ""
}
return ret
}
//! Create a prometheus line for US
func doUSField(value string, proname string, id int, description string, protype string) string {
ret := ""
name := "cable_upstream_"+proname;
ret += "# HELP "+name+" "+description+"\n"
ret += "# TYPE "+name+" "+protype+"\n"
if flval, err := strconv.ParseFloat(value, 32) ; err == nil {
ret += fmt.Sprintf("%s{id=\"%d\"} %f\n", name, id, flval)
} else {
log.Fatalln("Unable to convert value: "+err.Error())
return ""
}
return ret
}
func getPrometheus() string {
var ret string
resp, err := http.Get("http://192.168.178.1/htdocs/cm_info_connection.php")
//resp, err := http.Get("https://berthub.eu/tmp/cm_info_connection.php")
if err != nil {
log.Fatalln("Error reading response from modem " + err.Error())
return ""
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
// panic(err)
log.Fatalln("Error reading response from modem " + err.Error())
return ""
}
lines := strings.Split(strings.Replace(string(content), "\r\n", "\n", -1), "\n")
for _, s := range lines {
if strings.HasPrefix(s, "var cm_conn_json") {
parts := strings.Split(s, "'")
lejson := parts[1]
var f CMInfo
err := json.Unmarshal([]byte(lejson), &f)
if err != nil {
log.Fatalln("Error parsing JSON from modem " + err.Error())
return ""
}
for _, ds := range f.Cm_conn_ds_gourpObj {
id, err := strconv.Atoi(ds.Ds_id)
if(err != nil) {
log.Fatalln("Error parsing downstream id "+ err.Error())
return ""
}
ret += doDSField(ds.Ds_snr, "snr", id, "Signal to noise ratio of this channel", "gauge", 0.1)
ret += doDSField(ds.Ds_power, "power", id, "Power of this channel", "gauge", 1.0)
ret += doDSField(ds.Ds_correct, "correct", id, "Correctable errors", "counter", 1.0)
ret += doDSField(ds.Ds_uncorrect, "uncorrect", id, "Uncorrectable errors", "counter", 1.0)
ret += doDSField(ds.Ds_freq, "freq", id, "Frequency of this channel", "gauge", 1.0)
ret += doDSField(ds.Ds_width, "width", id, "Width of this channel", "gauge", 1.0)
ret += doDSField(ds.Ds_modulation, "modulation", id, "Modulation of this channel", "gauge", 1.0)
}
for _, us := range f.Cm_conn_us_gourpObj {
id, err := strconv.Atoi(us.Us_id)
if(err != nil) {
log.Fatalln("Error parsing upstream id "+ err.Error())
return ""
}
ret += doUSField(us.Us_power, "power", id, "Power of this channel", "gauge")
ret += doUSField(us.Us_status, "status", id, "Status of this channel", "gauge")
ret += doUSField(us.Us_type, "type", id, "Type of this channel", "gauge")
ret += doUSField(us.Us_freq, "freq", id, "Frequency of this channel", "gauge")
ret += doUSField(us.Us_width, "width", id, "Width of this channel", "gauge")
ret += doUSField(us.Us_modulation, "modulation", id, "Modulation of this channel", "gauge")
}
}
}
return ret
}
var promMutex = &sync.Mutex{}
var promStatus string
func updateLoop() {
for {
tmp := getPrometheus()
promMutex.Lock()
promStatus = tmp
promMutex.Unlock()
time.Sleep(60 * time.Second)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
var tmp string
promMutex.Lock()
tmp = promStatus
promMutex.Unlock()
fmt.Fprintf(w, "%s", tmp)
}
func main() {
go updateLoop()
http.HandleFunc("/metrics", handler)
log.Fatal(http.ListenAndServe(":10000", nil))
}