Browse Source

added support for loading metrics from JSON file

pull/1/head
sberk42 4 years ago
parent
commit
f0512c3ebc
  1. 186
      main.go
  2. 134
      metrics.json

186
main.go

@ -21,6 +21,8 @@ import (
"net/http"
"sync"
"time"
"encoding/json"
"io/ioutil"
"github.com/namsral/flag"
"github.com/prometheus/client_golang/prometheus"
@ -33,6 +35,7 @@ const serviceLoadRetryTime = 1 * time.Minute
var (
flag_test = flag.Bool("test", false, "print all available metrics to stdout")
flag_addr = flag.String("listen-address", "127.0.0.1:9042", "The address to listen on for HTTP requests.")
flag_metrics_file = flag.String("metrics-file", "metrics.json", "The JSON file with the metric definitions.")
flag_gateway_url = flag.String("gateway-url", "https://fritz.box:49443", "The URL of the FRITZ!Box")
flag_gateway_username = flag.String("username", "", "The user for the FRITZ!Box UPnP service")
@ -46,140 +49,28 @@ var (
})
)
type Metric struct {
Service string
Action string
Result string
OkValue string
type JSON_PromDesc struct {
FqName string `json:"fqName"`
Help string `json:"help"`
VarLabels []string `json:"varLabels"`
}
type Metric struct {
// initialized loading JSON
Service string `json:"service"`
Action string `json:"action"`
Result string `json:"result"`
OkValue string `json:"okValue"`
PromDesc JSON_PromDesc `json:"promDesc"`
PromType string `json:"promType"`
// initialized at startup
Desc *prometheus.Desc
MetricType prometheus.ValueType
}
var metrics = []*Metric{
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetTotalPacketsReceived",
Result: "TotalPacketsReceived",
Desc: prometheus.NewDesc(
"gateway_wan_packets_received",
"packets received on gateway WAN interface",
[]string{"gateway"},
nil,
),
MetricType: prometheus.CounterValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetTotalPacketsSent",
Result: "TotalPacketsSent",
Desc: prometheus.NewDesc(
"gateway_wan_packets_sent",
"packets sent on gateway WAN interface",
[]string{"gateway"},
nil,
),
MetricType: prometheus.CounterValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetAddonInfos",
Result: "TotalBytesReceived",
Desc: prometheus.NewDesc(
"gateway_wan_bytes_received",
"bytes received on gateway WAN interface",
[]string{"gateway"},
nil,
),
MetricType: prometheus.CounterValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetAddonInfos",
Result: "TotalBytesSent",
Desc: prometheus.NewDesc(
"gateway_wan_bytes_sent",
"bytes sent on gateway WAN interface",
[]string{"gateway"},
nil,
),
MetricType: prometheus.CounterValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetCommonLinkProperties",
Result: "Layer1UpstreamMaxBitRate",
Desc: prometheus.NewDesc(
"gateway_wan_layer1_upstream_max_bitrate",
"Layer1 upstream max bitrate",
[]string{"gateway"},
nil,
),
MetricType: prometheus.GaugeValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetCommonLinkProperties",
Result: "Layer1DownstreamMaxBitRate",
Desc: prometheus.NewDesc(
"gateway_wan_layer1_downstream_max_bitrate",
"Layer1 downstream max bitrate",
[]string{"gateway"},
nil,
),
MetricType: prometheus.GaugeValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetCommonLinkProperties",
Result: "PhysicalLinkStatus",
OkValue: "Up",
Desc: prometheus.NewDesc(
"gateway_wan_layer1_link_status",
"Status of physical link (Up = 1)",
[]string{"gateway"},
nil,
),
MetricType: prometheus.GaugeValue,
},
{
Service: "urn:schemas-upnp-org:service:WANIPConnection:1",
Action: "GetStatusInfo",
Result: "ConnectionStatus",
OkValue: "Connected",
Desc: prometheus.NewDesc(
"gateway_wan_connection_status",
"WAN connection status (Connected = 1)",
[]string{"gateway"},
nil,
),
MetricType: prometheus.GaugeValue,
},
{
Service: "urn:schemas-upnp-org:service:WANIPConnection:1",
Action: "GetStatusInfo",
Result: "Uptime",
Desc: prometheus.NewDesc(
"gateway_wan_connection_uptime_seconds",
"WAN connection uptime",
[]string{"gateway"},
nil,
),
MetricType: prometheus.GaugeValue,
},
{
Service: "urn:dslforum-org:service:WLANConfiguration:1",
Action: "GetTotalAssociations",
Result: "TotalAssociations",
Desc: prometheus.NewDesc(
"gateway_wlan_current_connections",
"current WLAN connections",
[]string{"gateway"},
nil,
),
MetricType: prometheus.GaugeValue,
},
}
var metrics []*Metric;
type FritzboxCollector struct {
Url string
@ -311,7 +202,7 @@ func test() {
res, err := a.Call()
if err != nil {
fmt.Errorf("unexpected error", err)
fmt.Printf(" %s unexpected error:", a.Name, err)
continue
}
@ -323,12 +214,25 @@ func test() {
}
}
func getValueType(vt string) prometheus.ValueType {
switch vt {
case "CounterValue":
return prometheus.CounterValue;
case "GaugeValue":
return prometheus.GaugeValue;
case "UntypedValue":
return prometheus.UntypedValue;
}
return prometheus.UntypedValue;
}
func main() {
flag.Parse()
u, err := url.Parse(*flag_gateway_url)
if err != nil {
fmt.Errorf("invalid URL", err)
fmt.Println("invalid URL:", err)
return
}
@ -337,6 +241,26 @@ func main() {
return
}
// read metrics
jsonData, err := ioutil.ReadFile(*flag_metrics_file)
if err != nil {
fmt.Println("error reading metric file:", err)
return
}
err = json.Unmarshal(jsonData, &metrics)
if err != nil {
fmt.Println("error parsing JSON:", err)
return
}
// init metrics
for _, m := range metrics {
pd := m.PromDesc
m.Desc = prometheus.NewDesc(pd.FqName, pd.Help, pd.VarLabels, nil)
m.MetricType = getValueType(m.PromType)
}
collector := &FritzboxCollector{
Url: *flag_gateway_url,
Gateway: u.Hostname(),

134
metrics.json

@ -0,0 +1,134 @@
[
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetTotalPacketsReceived",
"result": "TotalPacketsReceived",
"promDesc": {
"fqName": "gateway_wan_packets_received",
"help": "packets received on gateway WAN interface",
"varLabels": [
"gateway"
]
},
"promType": "CounterValue"
},
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetTotalPacketsSent",
"result": "TotalPacketsSent",
"promDesc": {
"fqName": "gateway_wan_packets_sent",
"help": "packets sent on gateway WAN interface",
"varLabels": [
"gateway"
]
},
"promType": "CounterValue"
},
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetAddonInfos",
"result": "TotalBytesReceived",
"promDesc": {
"fqName": "gateway_wan_bytes_received",
"help": "bytes received on gateway WAN interface",
"varLabels": [
"gateway"
]
},
"promType": "CounterValue"
},
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetAddonInfos",
"result": "TotalBytesSent",
"promDesc": {
"fqName": "gateway_wan_bytes_sent",
"help": "bytes sent on gateway WAN interface",
"varLabels": [
"gateway"
]
},
"promType": "CounterValue"
},
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetCommonLinkProperties",
"result": "Layer1UpstreamMaxBitRate",
"promDesc": {
"fqName": "gateway_wan_layer1_upstream_max_bitrate",
"help": "Layer1 upstream max bitrate",
"varLabels": [
"gateway"
]
},
"promType": "GaugeValue"
},
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetCommonLinkProperties",
"result": "Layer1DownstreamMaxBitRate",
"promDesc": {
"fqName": "gateway_wan_layer1_downstream_max_bitrate",
"help": "Layer1 downstream max bitrate",
"varLabels": [
"gateway"
]
},
"promType": "GaugeValue"
},
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetCommonLinkProperties",
"result": "PhysicalLinkStatus",
"okValue": "Up",
"promDesc": {
"fqName": "gateway_wan_layer1_link_status",
"help": "Status of physical link (Up = 1)",
"varLabels": [
"gateway"
]
},
"promType": "GaugeValue"
},
{
"service": "urn:schemas-upnp-org:service:WANIPConnection:1",
"action": "GetStatusInfo",
"result": "ConnectionStatus",
"okValue": "Connected",
"promDesc": {
"fqName": "gateway_wan_connection_status",
"help": "WAN connection status (Connected = 1)",
"varLabels": [
"gateway"
]
},
"promType": "GaugeValue"
},
{
"service": "urn:schemas-upnp-org:service:WANIPConnection:1",
"action": "GetStatusInfo",
"result": "Uptime",
"promDesc": {
"fqName": "gateway_wan_connection_uptime_seconds",
"help": "WAN connection uptime",
"varLabels": [
"gateway"
]
},
"promType": "GaugeValue"
},
{
"service": "urn:dslforum-org:service:WLANConfiguration:1",
"action": "GetTotalAssociations",
"result": "TotalAssociations",
"promDesc": {
"fqName": "gateway_wlan_current_connections",
"help": "current WLAN connections",
"varLabels": [
"gateway"
]
},
"promType": "GaugeValue"
}
]
Loading…
Cancel
Save