├── doc └── images │ └── demo.png ├── LICENSE ├── README.md └── ubee-prometheus.go /doc/images/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/berthubert/ubee-prometheus/main/doc/images/demo.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 berthubert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ubee-prometheus 2 | --------------- 3 | If you have a Ubee modem of the kind used by Ziggo in The Netherlands, this small tool may help you get its data to prometheus. 4 | 5 | It works at least on the Ubee UBC1318. 6 | 7 | ![Sample Grafana (through Prometheus)](doc/images/demo.png) 8 | 9 | To run, compile ('go build') and launch the binary. 10 | 11 | This will periodically poll the modem, which is assumed to be reachable on http://192.168.178.1/ and then keep the results ready for Prometheus. 12 | 13 | The URL we poll is: `/htdocs/cm_info_connection.php` 14 | 15 | ubee-prometheus is currently hardcoded to listen on port 10000. This will improve once I learn how to do configuration and argument parsing in Go. 16 | 17 | No matter how often prometheus polls, ubee-prometheus will only poll your Ubee modem once a minute. This is because a poll takes **20 seconds**, at least on my modem. The result of a Ubee poll is stored in memory, making sure that prometheus gets an anwer quickly. 18 | 19 | This also means that at startup, the prometheus URL will serve empty data, until the modem has delivered statistics. 20 | 21 | The statistics extracted are: 22 | 23 | * SNR for downstream 24 | * Power for up and downstream 25 | * Frequency for up and downstream 26 | * Width for up and downstream 27 | * Modulation for up and downstream 28 | * "Type" for upstream 29 | * Correctable errors for downstream 30 | * Uncorrectable errors for downstream 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /ubee-prometheus.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io/ioutil" 7 | "net/http" 8 | "strconv" 9 | "strings" 10 | "sync" 11 | "time" 12 | "log" 13 | ) 14 | 15 | type DSStatus struct { 16 | Ds_id string 17 | Ds_snr string 18 | Ds_power string 19 | Ds_correct string 20 | Ds_uncorrect string 21 | Ds_modulation string 22 | Ds_freq string 23 | Ds_width string 24 | } 25 | 26 | type USStatus struct { 27 | Us_status string 28 | Us_type string 29 | Us_id string 30 | Us_freq string 31 | Us_width string 32 | Us_power string 33 | Us_modulation string 34 | } 35 | 36 | type CMInfo struct { 37 | Cm_conn_ip_prov_mode string 38 | Cm_conn_wan_mode string 39 | Cm_conn_ds_gourpObj []DSStatus 40 | Cm_conn_us_gourpObj []USStatus 41 | } 42 | 43 | //! Create a prometheus line for DS 44 | func doDSField(value string, proname string, id int, description string, protype string, factor float64) string { 45 | ret := "" 46 | 47 | name := "cable_downstream_"+proname; 48 | ret += "# HELP "+name+" "+description+"\n" 49 | ret += "# TYPE "+name+" "+protype+"\n" 50 | 51 | if flval, err := strconv.ParseFloat(value, 32) ; err == nil { 52 | ret += fmt.Sprintf("%s{id=\"%d\"} %f\n", name, id, flval*factor) 53 | } else { 54 | log.Fatalln("Unable to convert value: "+err.Error()) 55 | return "" 56 | } 57 | return ret 58 | } 59 | 60 | 61 | //! Create a prometheus line for US 62 | func doUSField(value string, proname string, id int, description string, protype string) string { 63 | ret := "" 64 | 65 | name := "cable_upstream_"+proname; 66 | ret += "# HELP "+name+" "+description+"\n" 67 | ret += "# TYPE "+name+" "+protype+"\n" 68 | 69 | if flval, err := strconv.ParseFloat(value, 32) ; err == nil { 70 | ret += fmt.Sprintf("%s{id=\"%d\"} %f\n", name, id, flval) 71 | } else { 72 | log.Fatalln("Unable to convert value: "+err.Error()) 73 | return "" 74 | } 75 | return ret 76 | } 77 | 78 | 79 | func getPrometheus() string { 80 | var ret string 81 | 82 | resp, err := http.Get("http://192.168.178.1/htdocs/cm_info_connection.php") 83 | //resp, err := http.Get("https://berthub.eu/tmp/cm_info_connection.php") 84 | if err != nil { 85 | log.Fatalln("Error reading response from modem " + err.Error()) 86 | return "" 87 | } 88 | 89 | defer resp.Body.Close() 90 | 91 | content, err := ioutil.ReadAll(resp.Body) 92 | if err != nil { 93 | // panic(err) 94 | log.Fatalln("Error reading response from modem " + err.Error()) 95 | return "" 96 | } 97 | 98 | lines := strings.Split(strings.Replace(string(content), "\r\n", "\n", -1), "\n") 99 | 100 | for _, s := range lines { 101 | if strings.HasPrefix(s, "var cm_conn_json") { 102 | parts := strings.Split(s, "'") 103 | lejson := parts[1] 104 | 105 | var f CMInfo 106 | err := json.Unmarshal([]byte(lejson), &f) 107 | if err != nil { 108 | log.Fatalln("Error parsing JSON from modem " + err.Error()) 109 | return "" 110 | } 111 | 112 | for _, ds := range f.Cm_conn_ds_gourpObj { 113 | id, err := strconv.Atoi(ds.Ds_id) 114 | if(err != nil) { 115 | log.Fatalln("Error parsing downstream id "+ err.Error()) 116 | return "" 117 | } 118 | ret += doDSField(ds.Ds_snr, "snr", id, "Signal to noise ratio of this channel", "gauge", 0.1) 119 | ret += doDSField(ds.Ds_power, "power", id, "Power of this channel", "gauge", 1.0) 120 | ret += doDSField(ds.Ds_correct, "correct", id, "Correctable errors", "counter", 1.0) 121 | ret += doDSField(ds.Ds_uncorrect, "uncorrect", id, "Uncorrectable errors", "counter", 1.0) 122 | ret += doDSField(ds.Ds_freq, "freq", id, "Frequency of this channel", "gauge", 1.0) 123 | ret += doDSField(ds.Ds_width, "width", id, "Width of this channel", "gauge", 1.0) 124 | ret += doDSField(ds.Ds_modulation, "modulation", id, "Modulation of this channel", "gauge", 1.0) 125 | } 126 | 127 | for _, us := range f.Cm_conn_us_gourpObj { 128 | id, err := strconv.Atoi(us.Us_id) 129 | if(err != nil) { 130 | log.Fatalln("Error parsing upstream id "+ err.Error()) 131 | return "" 132 | } 133 | 134 | ret += doUSField(us.Us_power, "power", id, "Power of this channel", "gauge") 135 | ret += doUSField(us.Us_status, "status", id, "Status of this channel", "gauge") 136 | ret += doUSField(us.Us_type, "type", id, "Type of this channel", "gauge") 137 | ret += doUSField(us.Us_freq, "freq", id, "Frequency of this channel", "gauge") 138 | ret += doUSField(us.Us_width, "width", id, "Width of this channel", "gauge") 139 | ret += doUSField(us.Us_modulation, "modulation", id, "Modulation of this channel", "gauge") 140 | } 141 | } 142 | } 143 | return ret 144 | } 145 | 146 | var promMutex = &sync.Mutex{} 147 | var promStatus string 148 | 149 | func updateLoop() { 150 | for { 151 | tmp := getPrometheus() 152 | promMutex.Lock() 153 | promStatus = tmp 154 | promMutex.Unlock() 155 | time.Sleep(60 * time.Second) 156 | } 157 | } 158 | 159 | func handler(w http.ResponseWriter, r *http.Request) { 160 | var tmp string 161 | 162 | promMutex.Lock() 163 | tmp = promStatus 164 | promMutex.Unlock() 165 | 166 | fmt.Fprintf(w, "%s", tmp) 167 | } 168 | 169 | 170 | func main() { 171 | go updateLoop() 172 | http.HandleFunc("/metrics", handler) 173 | log.Fatal(http.ListenAndServe(":10000", nil)) 174 | 175 | } 176 | --------------------------------------------------------------------------------