Added Exporter Binary

master
Joachim Hummel 2 years ago
parent 45983239db
commit 7708d988c8

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

@ -0,0 +1,92 @@
# Fritz!Box Upnp statistics exporter for prometheus
This exporter exports some variables from an
[AVM Fritzbox](http://avm.de/produkte/fritzbox/)
to prometheus.
This exporter is tested with a Fritzbox 7590 software version 07.12, 07.20, 07.21 and 07.25.
The goal of the fork is:
- [x] allow passing of username / password using evironment variable
- [x] use https instead of http for communitcation with fritz.box
- [x] move config of metrics to be exported to config file rather then code
- [x] add config for additional metrics to collect (especially from TR-064 API)
- [x] create a grafana dashboard consuming the additional metrics
- [x] collect metrics from lua APIs not available in UPNP APIs
Other changes:
- replaced digest authentication code with own implementation
- improved error messages
- test mode prints details about all SOAP Actions and their parameters
- collect option to directly test collection of results
- additional metrics to collect details about connected hosts and DECT devices
- support to use results like hostname or MAC address as labels to metrics
- support for metrics from lua APIs (e.g. CPU temperature, utilization, ...)
## Building
go get github.com/sberk42/fritzbox_exporter/
cd $GOPATH/src/github.com/sberk42/fritzbox_exporter
go install
## Running
In the configuration of the Fritzbox the option "Statusinformationen über UPnP übertragen" in the dialog "Heimnetz >
Heimnetzübersicht > Netzwerkeinstellungen" has to be enabled.
Usage:
$GOPATH/bin/fritzbox_exporter -h
Usage of ./fritzbox_exporter:
-gateway-url string
The URL of the FRITZ!Box (default "http://fritz.box:49000")
-gateway-luaurl string
The URL of the FRITZ!Box UI (default "http://fritz.box")
-metrics-file string
The JSON file with the metric definitions. (default "metrics.json")
-lua-metrics-file string
The JSON file with the lua metric definitions. (default "metrics-lua.json")
-test
print all available SOAP calls and their results (if call possible) to stdout
-json-out string
store metrics also to JSON file when running test
-testLua
read luaTest.json file make all contained calls and dump results
-collect
collect metrics once print to stdout and exit
-nolua
disable collecting lua metrics
-username string
The user for the FRITZ!Box UPnP service
-password string
The password for the FRITZ!Box UPnP service
-listen-address string
The address to listen on for HTTP requests. (default "127.0.0.1:9042")
The password (needed for metrics from TR-064 API) can be passed over environment variables to test in shell:
read -rs PASSWORD && export PASSWORD && ./fritzbox_exporter -username <user> -test; unset PASSWORD
## Exported metrics
start exporter and run
curl -s http://127.0.0.1:9042/metrics
## Output of -test
The exporter prints all available Variables to stdout when called with the -test option.
These values are determined by parsing all services from http://fritz.box:49000/igddesc.xml and http://fritzbox:49000/tr64desc.xml (for TR64 username and password is needed!!!)
## Customizing metrics
The metrics to collect are no longer hard coded, but have been moved to the [metrics.json](metrics.json) and [metrics-lua.json](metrics-lua.json) files, so just adjust to your needs.
For a list of all the available metrics just execute the exporter with -test (username and password are needed for the TR-064 API!)
For lua metrics open UI in browser and check the json files used for the various screens.
For a list of all available metrics, see the dumps below (the format is the same as in the metrics.json file, so it can be used to easily add further metrics to retrieve):
- [FritzBox 7590 v7.12](all_available_metrics_7590_7.12.json)
- [FritzBox 7590 v7.20](all_available_metrics_7590_7.20.json)
- [FritzBox 7590 v7.25](all_available_metrics_7590_7.25.json)
## Grafana Dashboard
The dashboard is now also published on [Grafana](https://grafana.com/grafana/dashboards/12579).

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

@ -0,0 +1,110 @@
[
{
"path": "data.lua",
"params": "page=overview"
},
{
"path": "data.lua",
"params": "page=ipv6"
},
{
"path": "data.lua",
"params": "page=dnsSrv"
},
{
"path": "data.lua",
"params": "page=kidLis"
},
{
"path": "data.lua",
"params": "page=trafapp"
},
{
"path": "data.lua",
"params": "page=portoverview"
},
{
"path": "data.lua",
"params": "page=dslOv"
},
{
"path": "data.lua",
"params": "page=dialLi"
},
{
"path": "data.lua",
"params": "page=bookLi"
},
{
"path": "data.lua",
"params": "page=dectSet"
},
{
"path": "data.lua",
"params": "page=dectMon"
},
{
"path": "data.lua",
"params": "page=homeNet"
},
{
"path": "data.lua",
"params": "page=netDev"
},
{
"path": "data.lua",
"params": "page=netSet"
},
{
"path": "data.lua",
"params": "page=usbOv"
},
{
"path": "data.lua",
"params": "page=mServSet"
},
{
"path": "data.lua",
"params": "page=wSet"
},
{
"path": "data.lua",
"params": "page=chan"
},
{
"path": "data.lua",
"params": "page=sh_dev"
},
{
"path": "data.lua",
"params": "page=energy"
},
{
"path": "data.lua",
"params": "page=ecoStat"
},
{
"path": "GET:internet/inetstat_monitor.lua",
"params": "action=get_graphic&useajax=1"
},
{
"path": "GET:internet/internet_settings.lua",
"params": "multiwan_page=dsl&useajax=1"
},
{
"path": "GET:internet/dsl_stats_tab.lua",
"params": "update=mainDiv&useajax=1"
},
{
"path": "GET:net/network.lua",
"params": "useajax=1"
},
{
"path": "data.lua",
"params": "page=netCnt"
},
{
"path": "data.lua",
"params": "page=dslStat"
}
]

@ -0,0 +1,42 @@
[
{
"path": "data.lua",
"params": "page=overview"
},
{
"path": "data.lua",
"params": "page=dslOv"
},
{
"path": "data.lua",
"params": "page=dectMon"
},
{
"path": "data.lua",
"params": "page=netDev"
},
{
"path": "data.lua",
"params": "page=usbOv"
},
{
"path": "data.lua",
"params": "page=sh_dev"
},
{
"path": "data.lua",
"params": "page=energy"
},
{
"path": "data.lua",
"params": "page=ecoStat"
},
{
"path": "GET:webservices/homeautoswitch.lua",
"params": "switchcmd=getdevicelistinfos"
},
{
"path": "GET:webservices/homeautoswitch.lua",
"params": "switchcmd=getbasicdevicestats"
}
]

@ -0,0 +1,852 @@
package main
// Copyright 2016 Nils Decker
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/namsral/flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
lua "github.com/sberk42/fritzbox_exporter/fritzbox_lua"
upnp "github.com/sberk42/fritzbox_exporter/fritzbox_upnp"
)
const serviceLoadRetryTime = 1 * time.Minute
// minimum TTL for cached results in seconds
const minCacheTTL = 30
var (
flagTest = flag.Bool("test", false, "print all available metrics to stdout")
flagLuaTest = flag.Bool("testLua", false, "read luaTest.json file make all contained calls and dump results")
flagCollect = flag.Bool("collect", false, "print configured metrics to stdout and exit")
flagJSONOut = flag.String("json-out", "", "store metrics also to JSON file when running test")
flagAddr = flag.String("listen-address", "127.0.0.1:9042", "The address to listen on for HTTP requests.")
flagMetricsFile = flag.String("metrics-file", "metrics.json", "The JSON file with the metric definitions.")
flagDisableLua = flag.Bool("nolua", false, "disable collecting lua metrics")
flagLuaMetricsFile = flag.String("lua-metrics-file", "metrics-lua.json", "The JSON file with the lua metric definitions.")
flagGatewayURL = flag.String("gateway-url", "http://fritz.box:49000", "The URL of the FRITZ!Box")
flagGatewayLuaURL = flag.String("gateway-luaurl", "http://fritz.box", "The URL of the FRITZ!Box UI")
flagUsername = flag.String("username", "", "The user for the FRITZ!Box UPnP service")
flagPassword = flag.String("password", "", "The password for the FRITZ!Box UPnP service")
)
var (
collectErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "fritzbox_exporter_collectErrors",
Help: "Number of collection errors.",
})
)
var (
luaCollectErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "fritzbox_exporter_luaCollectErrors",
Help: "Number of lua collection errors.",
})
)
var collectLuaResultsCached = prometheus.NewCounter(prometheus.CounterOpts{
Name: "fritzbox_exporter_results_cached",
Help: "Number of results taken from cache.",
ConstLabels: prometheus.Labels{"Cache": "LUA"},
})
var collectUpnpResultsCached = prometheus.NewCounter(prometheus.CounterOpts{
Name: "fritzbox_exporter_results_cached",
Help: "Number of results taken from cache.",
ConstLabels: prometheus.Labels{"Cache": "UPNP"},
})
var collectLuaResultsLoaded = prometheus.NewCounter(prometheus.CounterOpts{
Name: "fritzbox_exporter_results_loaded",
Help: "Number of results loaded from fritzbox.",
ConstLabels: prometheus.Labels{"Cache": "LUA"},
})
var collectUpnpResultsLoaded = prometheus.NewCounter(prometheus.CounterOpts{
Name: "fritzbox_exporter_results_loaded",
Help: "Number of results loaded from fritzbox.",
ConstLabels: prometheus.Labels{"Cache": "UPNP"},
})
// JSONPromDesc metric description loaded from JSON
type JSONPromDesc struct {
FqName string `json:"fqName"`
Help string `json:"help"`
VarLabels []string `json:"varLabels"`
FixedLabels map[string]string `json:"fixedLabels"`
fixedLabelValues string // neeeded to create uniq lookup key when reporting
}
// ActionArg argument for upnp action
type ActionArg struct {
Name string `json:"Name"`
IsIndex bool `json:"IsIndex"`
ProviderAction string `json:"ProviderAction"`
Value string `json:"Value"`
}
// Metric upnp metric
type Metric struct {
// initialized loading JSON
Service string `json:"service"`
Action string `json:"action"`
ActionArgument *ActionArg `json:"actionArgument"`
Result string `json:"result"`
OkValue string `json:"okValue"`
PromDesc JSONPromDesc `json:"promDesc"`
PromType string `json:"promType"`
CacheEntryTTL int64 `json:"cacheEntryTTL"`
// initialized at startup
Desc *prometheus.Desc
MetricType prometheus.ValueType
}
// LuaTest JSON struct for API tests
type LuaTest struct {
Path string `json:"path"`
Params string `json:"params"`
}
// LuaLabelRename struct
type LuaLabelRename struct {
MatchRegex string `json:"matchRegex"`
RenameLabel string `json:"renameLabel"`
}
// LuaMetric struct
type LuaMetric struct {
// initialized loading JSON
Path string `json:"path"`
Params string `json:"params"`
ResultPath string `json:"resultPath"`
ResultKey string `json:"resultKey"`
OkValue string `json:"okValue"`
PromDesc JSONPromDesc `json:"promDesc"`
PromType string `json:"promType"`
CacheEntryTTL int64 `json:"cacheEntryTTL"`
// initialized at startup
Desc *prometheus.Desc
MetricType prometheus.ValueType
LuaPage lua.LuaPage
LuaMetricDef lua.LuaMetricValueDefinition
}
// LuaMetricsFile json struct
type LuaMetricsFile struct {
LabelRenames []LuaLabelRename `json:"labelRenames"`
Metrics []*LuaMetric `json:"metrics"`
}
type upnpCacheEntry struct {
Timestamp int64
Result *upnp.Result
}
type luaCacheEntry struct {
Timestamp int64
Result *map[string]interface{}
}
var metrics []*Metric
var luaMetrics []*LuaMetric
var upnpCache map[string]*upnpCacheEntry
var luaCache map[string]*luaCacheEntry
// FritzboxCollector main struct
type FritzboxCollector struct {
URL string
Gateway string
Username string
Password string
// support for lua collector
LuaSession *lua.LuaSession
LabelRenames *[]lua.LabelRename
sync.Mutex // protects Root
Root *upnp.Root
}
// simple ResponseWriter to collect output
type testResponseWriter struct {
header http.Header
statusCode int
body bytes.Buffer
}
func (w *testResponseWriter) Header() http.Header {
return w.header
}
func (w *testResponseWriter) Write(b []byte) (int, error) {
return w.body.Write(b)
}
func (w *testResponseWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
}
func (w *testResponseWriter) String() string {
return w.body.String()
}
// LoadServices tries to load the service information. Retries until success.
func (fc *FritzboxCollector) LoadServices() {
for {
root, err := upnp.LoadServices(fc.URL, fc.Username, fc.Password)
if err != nil {
fmt.Printf("cannot load services: %s\n", err)
time.Sleep(serviceLoadRetryTime)
continue
}
fmt.Printf("services loaded\n")
fc.Lock()
fc.Root = root
fc.Unlock()
return
}
}
// Describe describe metric
func (fc *FritzboxCollector) Describe(ch chan<- *prometheus.Desc) {
for _, m := range metrics {
ch <- m.Desc
}
}
func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric, result upnp.Result, dupCache map[string]bool) {
val, ok := result[m.Result]
if !ok {
fmt.Printf("%s.%s has no result %s", m.Service, m.Action, m.Result)
collectErrors.Inc()
return
}
var floatval float64
switch tval := val.(type) {
case uint64:
floatval = float64(tval)
case bool:
if tval {
floatval = 1
} else {
floatval = 0
}
case string:
if tval == m.OkValue {
floatval = 1
} else {
floatval = 0
}
default:
fmt.Println("unknown type", val)
collectErrors.Inc()
return
}
labels := make([]string, len(m.PromDesc.VarLabels))
for i, l := range m.PromDesc.VarLabels {
if l == "gateway" {
labels[i] = fc.Gateway
} else {
lval, ok := result[l]
if !ok {
fmt.Printf("%s.%s has no resul for label %s", m.Service, m.Action, l)
lval = ""
}
// convert hostname and MAC tolower to avoid problems with labels
if l == "HostName" || l == "MACAddress" {
labels[i] = strings.ToLower(fmt.Sprintf("%v", lval))
} else {
labels[i] = fmt.Sprintf("%v", lval)
}
}
}
// check for duplicate labels to prevent collection failure
key := m.PromDesc.FqName + ":" + m.PromDesc.fixedLabelValues + strings.Join(labels, ",")
if dupCache[key] {
fmt.Printf("%s.%s reported before as: %s\n", m.Service, m.Action, key)
collectErrors.Inc()
return
}
dupCache[key] = true
metric, err := prometheus.NewConstMetric(m.Desc, m.MetricType, floatval, labels...)
if err != nil {
fmt.Printf("Error creating metric %s.%s: %s", m.Service, m.Action, err.Error())
} else {
ch <- metric
}
}
func (fc *FritzboxCollector) getActionResult(metric *Metric, actionName string, actionArg *upnp.ActionArgument) (upnp.Result, error) {
key := metric.Service + "|" + actionName
// for calls with argument also add arguement name and value to key
if actionArg != nil {
key += "|" + actionArg.Name + "|" + fmt.Sprintf("%v", actionArg.Value)
}
now := time.Now().Unix()
cacheEntry := upnpCache[key]
if cacheEntry == nil {
cacheEntry = &upnpCacheEntry{}
upnpCache[key] = cacheEntry
} else if now-cacheEntry.Timestamp > metric.CacheEntryTTL {
cacheEntry.Result = nil
}
if cacheEntry.Result == nil {
service, ok := fc.Root.Services[metric.Service]
if !ok {
return nil, fmt.Errorf("service %s not found", metric.Service)
}
action, ok := service.Actions[actionName]
if !ok {
return nil, fmt.Errorf("action %s not found in service %s", actionName, metric.Service)
}
data, err := action.Call(actionArg)
if err != nil {
return nil, err
}
cacheEntry.Timestamp = now
cacheEntry.Result = &data
collectUpnpResultsCached.Inc()
} else {
collectUpnpResultsLoaded.Inc()
}
return *cacheEntry.Result, nil
}
// Collect collect upnp metrics
func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) {
fc.Lock()
root := fc.Root
fc.Unlock()
if root == nil {
// Services not loaded yet
return
}
// create cache for duplicate lookup, to prevent collection errors
var dupCache = make(map[string]bool)
for _, m := range metrics {
var actArg *upnp.ActionArgument
if m.ActionArgument != nil {
aa := m.ActionArgument
var value interface{}
value = aa.Value
if aa.ProviderAction != "" {
provRes, err := fc.getActionResult(m, aa.ProviderAction, nil)
if err != nil {
fmt.Printf("Error getting provider action %s result for %s.%s: %s\n", aa.ProviderAction, m.Service, m.Action, err.Error())
collectErrors.Inc()
continue
}
var ok bool
value, ok = provRes[aa.Value] // Value contains the result name for provider actions
if !ok {
fmt.Printf("provider action %s for %s.%s has no result", m.Service, m.Action, aa.Value)
collectErrors.Inc()
continue
}
}
if aa.IsIndex {
sval := fmt.Sprintf("%v", value)
count, err := strconv.Atoi(sval)
if err != nil {
fmt.Println(err.Error())
collectErrors.Inc()
continue
}
for i := 0; i < count; i++ {
actArg = &upnp.ActionArgument{Name: aa.Name, Value: i}
result, err := fc.getActionResult(m, m.Action, actArg)
if err != nil {
fmt.Println(err.Error())
collectErrors.Inc()
continue
}
fc.reportMetric(ch, m, result, dupCache)
}
continue
} else {
actArg = &upnp.ActionArgument{Name: aa.Name, Value: value}
}
}
result, err := fc.getActionResult(m, m.Action, actArg)
if err != nil {
fmt.Println(err.Error())
collectErrors.Inc()
continue
}
fc.reportMetric(ch, m, result, dupCache)
}
// if lua is enabled now also collect metrics
if fc.LuaSession != nil {
fc.collectLua(ch, dupCache)
}
}
func (fc *FritzboxCollector) collectLua(ch chan<- prometheus.Metric, dupCache map[string]bool) {
// create a map for caching results
now := time.Now().Unix()
for _, lm := range luaMetrics {
key := lm.Path + "_" + lm.Params
cacheEntry := luaCache[key]
if cacheEntry == nil {
cacheEntry = &luaCacheEntry{}
luaCache[key] = cacheEntry
} else if now-cacheEntry.Timestamp > lm.CacheEntryTTL {
cacheEntry.Result = nil
}
if cacheEntry.Result == nil {
pageData, err := fc.LuaSession.LoadData(lm.LuaPage)
if err != nil {
fmt.Printf("Error loading %s for %s.%s: %s\n", lm.Path, lm.ResultPath, lm.ResultKey, err.Error())
luaCollectErrors.Inc()
fc.LuaSession.SID = "" // clear SID in case of error, so force reauthentication
continue
}
var data map[string]interface{}
data, err = lua.ParseJSON(pageData)
if err != nil {
fmt.Printf("Error parsing JSON from %s for %s.%s: %s\n", lm.Path, lm.ResultPath, lm.ResultKey, err.Error())
luaCollectErrors.Inc()
continue
}
cacheEntry.Result = &data
cacheEntry.Timestamp = now
collectLuaResultsLoaded.Inc()
} else {
collectLuaResultsCached.Inc()
}
metricVals, err := lua.GetMetrics(fc.LabelRenames, *cacheEntry.Result, lm.LuaMetricDef)
if err != nil {
fmt.Printf("Error getting metric values for %s.%s: %s\n", lm.ResultPath, lm.ResultKey, err.Error())
luaCollectErrors.Inc()
cacheEntry.Result = nil // don't use invalid results for cache
continue
}
for _, mv := range metricVals {
fc.reportLuaMetric(ch, lm, mv, dupCache)
}
}
}
func (fc *FritzboxCollector) reportLuaMetric(ch chan<- prometheus.Metric, lm *LuaMetric, value lua.LuaMetricValue, dupCache map[string]bool) {
labels := make([]string, len(lm.PromDesc.VarLabels))
for i, l := range lm.PromDesc.VarLabels {
if l == "gateway" {
labels[i] = fc.Gateway
} else {
lval, ok := value.Labels[l]
if !ok {
fmt.Printf("%s.%s from %s?%s has no resul for label %s", lm.ResultPath, lm.ResultKey, lm.Path, lm.Params, l)
lval = ""
}
// convert hostname and MAC tolower to avoid problems with labels
if l == "HostName" || l == "MACAddress" {
labels[i] = strings.ToLower(fmt.Sprintf("%v", lval))
} else {
labels[i] = fmt.Sprintf("%v", lval)
}
}
}
// check for duplicate labels to prevent collection failure
key := lm.PromDesc.FqName + ":" + lm.PromDesc.fixedLabelValues + strings.Join(labels, ",")
if dupCache[key] {
fmt.Printf("%s.%s reported before as: %s\n", lm.ResultPath, lm.ResultPath, key)
luaCollectErrors.Inc()
return
}
dupCache[key] = true
metric, err := prometheus.NewConstMetric(lm.Desc, lm.MetricType, value.Value, labels...)
if err != nil {
fmt.Printf("Error creating metric %s.%s: %s", lm.ResultPath, lm.ResultPath, err.Error())
} else {
ch <- metric
}
}
func test() {
root, err := upnp.LoadServices(*flagGatewayURL, *flagUsername, *flagPassword)
if err != nil {
panic(err)
}
var newEntry bool = false
var json bytes.Buffer
json.WriteString("[\n")
serviceKeys := []string{}
for k := range root.Services {
serviceKeys = append(serviceKeys, k)
}
sort.Strings(serviceKeys)
for _, k := range serviceKeys {
s := root.Services[k]
fmt.Printf("Service: %s (Url: %s)\n", k, s.ControlURL)
actionKeys := []string{}
for l := range s.Actions {
actionKeys = append(actionKeys, l)
}
sort.Strings(actionKeys)
for _, l := range actionKeys {
a := s.Actions[l]
fmt.Printf(" %s - arguments: variable [direction] (soap name, soap type)\n", a.Name)
for _, arg := range a.Arguments {
sv := arg.StateVariable
fmt.Printf(" %s [%s] (%s, %s)\n", arg.RelatedStateVariable, arg.Direction, arg.Name, sv.DataType)
}
if !a.IsGetOnly() {
fmt.Printf(" %s - not calling, since arguments required or no output\n", a.Name)
continue
}
// only create JSON for Get
// TODO also create JSON templates for input actionParams
for _, arg := range a.Arguments {
// create new json entry
if newEntry {
json.WriteString(",\n")
} else {
newEntry = true
}
json.WriteString("\t{\n\t\t\"service\": \"")
json.WriteString(k)
json.WriteString("\",\n\t\t\"action\": \"")
json.WriteString(a.Name)
json.WriteString("\",\n\t\t\"result\": \"")
json.WriteString(arg.RelatedStateVariable)
json.WriteString("\"\n\t}")
}
fmt.Printf(" %s - calling - results: variable: value\n", a.Name)
res, err := a.Call(nil)
if err != nil {
fmt.Printf(" FAILED:%s\n", err.Error())
continue
}
for _, arg := range a.Arguments {
fmt.Printf(" %s: %v\n", arg.RelatedStateVariable, res[arg.StateVariable.Name])
}
}
}
json.WriteString("\n]")
if *flagJSONOut != "" {
err := ioutil.WriteFile(*flagJSONOut, json.Bytes(), 0644)
if err != nil {
fmt.Printf("Failed writing JSON file '%s': %s\n", *flagJSONOut, err.Error())
}
}
}
func testLua() {
jsonData, err := ioutil.ReadFile("luaTest.json")
if err != nil {
fmt.Println("error reading luaTest.json:", err)
return
}
var luaTests []LuaTest
err = json.Unmarshal(jsonData, &luaTests)
if err != nil {
fmt.Println("error parsing luaTest JSON:", err)
return
}
// create session struct and init params
luaSession := lua.LuaSession{BaseURL: *flagGatewayLuaURL, Username: *flagUsername, Password: *flagPassword}
for _, test := range luaTests {
fmt.Printf("TESTING: %s (%s)\n", test.Path, test.Params)
page := lua.LuaPage{Path: test.Path, Params: test.Params}
pageData, err := luaSession.LoadData(page)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(string(pageData))
}
fmt.Println()
fmt.Println()
}
}
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(*flagGatewayURL)
if err != nil {
fmt.Println("invalid URL:", err)
return
}
if *flagTest {
test()
return
}
if *flagLuaTest {
testLua()
return
}
// read metrics
jsonData, err := ioutil.ReadFile(*flagMetricsFile)
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
}
// create a map for caching results
upnpCache = make(map[string]*upnpCacheEntry)
var luaSession *lua.LuaSession
var luaLabelRenames *[]lua.LabelRename
if !*flagDisableLua {
jsonData, err := ioutil.ReadFile(*flagLuaMetricsFile)
if err != nil {
fmt.Println("error reading lua metric file:", err)
return
}
var lmf *LuaMetricsFile
err = json.Unmarshal(jsonData, &lmf)
if err != nil {
fmt.Println("error parsing lua JSON:", err)
return
}
// create a map for caching results
luaCache = make(map[string]*luaCacheEntry)
// init label renames
lblRen := make([]lua.LabelRename, 0)
for _, ren := range lmf.LabelRenames {
regex, err := regexp.Compile(ren.MatchRegex)
if err != nil {
fmt.Println("error compiling lua rename regex:", err)
return
}
lblRen = append(lblRen, lua.LabelRename{Pattern: *regex, Name: ren.RenameLabel})
}
luaLabelRenames = &lblRen
// init metrics
luaMetrics = lmf.Metrics
for _, lm := range luaMetrics {
pd := &lm.PromDesc
// make labels lower case
labels := make([]string, len(pd.VarLabels))
for i, l := range pd.VarLabels {
labels[i] = strings.ToLower(l)
}
// create fixed labels values
pd.fixedLabelValues = ""
for _, flv := range pd.FixedLabels {
pd.fixedLabelValues += flv + ","
}
lm.Desc = prometheus.NewDesc(pd.FqName, pd.Help, labels, pd.FixedLabels)
lm.MetricType = getValueType(lm.PromType)
lm.LuaPage = lua.LuaPage{
Path: lm.Path,
Params: lm.Params,
}
lm.LuaMetricDef = lua.LuaMetricValueDefinition{
Path: lm.ResultPath,
Key: lm.ResultKey,
OkValue: lm.OkValue,
Labels: pd.VarLabels,
}
// init TTL
if lm.CacheEntryTTL < minCacheTTL {
lm.CacheEntryTTL = minCacheTTL
}
}
luaSession = &lua.LuaSession{
BaseURL: *flagGatewayLuaURL,
Username: *flagUsername,
Password: *flagPassword,
}
}
// init metrics
for _, m := range metrics {
pd := &m.PromDesc
// make labels lower case
labels := make([]string, len(pd.VarLabels))
for i, l := range pd.VarLabels {
labels[i] = strings.ToLower(l)
}
// create fixed labels values
pd.fixedLabelValues = ""
for _, flv := range pd.FixedLabels {
pd.fixedLabelValues += flv + ","
}
m.Desc = prometheus.NewDesc(pd.FqName, pd.Help, labels, pd.FixedLabels)
m.MetricType = getValueType(m.PromType)
// init TTL
if m.CacheEntryTTL < minCacheTTL {
m.CacheEntryTTL = minCacheTTL
}
}
collector := &FritzboxCollector{
URL: *flagGatewayURL,
Gateway: u.Hostname(),
Username: *flagUsername,
Password: *flagPassword,
LuaSession: luaSession,
LabelRenames: luaLabelRenames,
}
if *flagCollect {
collector.LoadServices()
prometheus.MustRegister(collector)
prometheus.MustRegister(collectErrors)
if luaSession != nil {
prometheus.MustRegister(luaCollectErrors)
}
fmt.Println("collecting metrics via http")
// simulate HTTP request without starting actual http server
writer := testResponseWriter{header: http.Header{}}
request := http.Request{}
promhttp.Handler().ServeHTTP(&writer, &request)
fmt.Println(writer.String())
return
}
go collector.LoadServices()
prometheus.MustRegister(collector)
prometheus.MustRegister(collectErrors)
prometheus.MustRegister(collectUpnpResultsCached)
prometheus.MustRegister(collectUpnpResultsLoaded)
if luaSession != nil {
prometheus.MustRegister(luaCollectErrors)
prometheus.MustRegister(collectLuaResultsCached)
prometheus.MustRegister(collectLuaResultsLoaded)
}
http.Handle("/metrics", promhttp.Handler())
fmt.Printf("metrics available at http://%s/metrics\n", *flagAddr)
log.Fatal(http.ListenAndServe(*flagAddr, nil))
}

@ -0,0 +1,180 @@
{
"labelRenames": [
{
"matchRegex": "(?i)prozessor",
"renameLabel": "CPU"
},
{
"matchRegex": "(?i)system",
"renameLabel": "System"
},
{
"matchRegex": "(?i)DSL",
"renameLabel": "DSL"
},
{
"matchRegex": "(?i)FON",
"renameLabel": "Phone"
},
{
"matchRegex": "(?i)WLAN",
"renameLabel": "WLAN"
},
{
"matchRegex": "(?i)USB",
"renameLabel": "USB"
},
{
"matchRegex": "(?i)Speicher.*FRITZ",
"renameLabel": "Internal eStorage"
}
],
"metrics": [
{
"path": "data.lua",
"params": "page=energy",
"resultPath": "data.drain.*",
"resultKey": "actPerc",
"promDesc": {
"fqName": "gateway_data_energy_consumption",
"help": "percentage of energy consumed from data.lua?page=energy",
"varLabels": [
"gateway", "name"
]
},
"promType": "GaugeValue",
"cacheEntryTTL": 300
},
{
"path": "data.lua",
"params": "page=energy",
"resultPath": "data.drain.*.lan.*",
"resultKey": "class",
"okValue": "green",
"promDesc": {
"fqName": "gateway_data_energy_lan_status",
"help": "status of LAN connection from data.lua?page=energy (1 = up)",
"varLabels": [
"gateway", "name"
]
},
"promType": "GaugeValue",
"cacheEntryTTL": 300
},
{
"path": "data.lua",
"params": "page=ecoStat",
"resultPath": "data.cputemp.series.0",
"resultKey": "-1",
"promDesc": {
"fqName": "gateway_data_ecostat_cputemp",
"help": "cpu temperature from data.lua?page=ecoStat",
"varLabels": [
"gateway"
]
},
"promType": "GaugeValue",
"cacheEntryTTL": 300
},
{
"path": "data.lua",
"params": "page=ecoStat",
"resultPath": "data.cpuutil.series.0",
"resultKey": "-1",
"promDesc": {
"fqName": "gateway_data_ecostat_cpuutil",
"help": "percentage of cpu utilization from data.lua?page=ecoStat",
"varLabels": [
"gateway"
]
},
"promType": "GaugeValue",
"cacheEntryTTL": 300
},
{
"path": "data.lua",
"params": "page=ecoStat",
"resultPath": "data.ramusage.series.0",
"resultKey": "-1",
"promDesc": {
"fqName": "gateway_data_ecostat_ramusage",
"help": "percentage of RAM utilization from data.lua?page=energy",
"varLabels": [
"gateway"
],
"fixedLabels": {
"ram_type" : "Fixed"
}
},
"promType": "GaugeValue",
"cacheEntryTTL": 300
},
{
"path": "data.lua",
"params": "page=ecoStat",
"resultPath": "data.ramusage.series.1",
"resultKey": "-1",
"promDesc": {
"fqName": "gateway_data_ecostat_ramusage",
"help": "percentage of RAM utilization from data.lua?page=energy",
"varLabels": [
"gateway"
],
"fixedLabels": {
"ram_type" : "Dynamic"
}
},
"promType": "GaugeValue",
"cacheEntryTTL": 300
},
{
"path": "data.lua",
"params": "page=ecoStat",
"resultPath": "data.ramusage.series.2",
"resultKey": "-1",
"promDesc": {
"fqName": "gateway_data_ecostat_ramusage",
"help": "percentage of RAM utilization from data.lua?page=energy",
"varLabels": [
"gateway"
],
"fixedLabels": {
"ram_type" : "Free"
}
},
"promType": "GaugeValue",
"cacheEntryTTL": 300
},
{
"path": "data.lua",
"params": "page=usbOv",
"resultPath": "data.usbOverview.devices.*",
"resultKey": "partitions.0.totalStorageInBytes",
"promDesc": {
"fqName": "gateway_data_usb_storage_total",
"help": "total storage in bytes from data.lua?page=usbOv",
"varLabels": [
"gateway", "deviceType", "deviceName"
]
},
"promType": "GaugeValue",
"cacheEntryTTL": 300
},
{
"path": "data.lua",
"params": "page=usbOv",
"resultPath": "data.usbOverview.devices.*",
"resultKey": "partitions.0.usedStorageInBytes",
"promDesc": {
"fqName": "gateway_data_usb_storage_used",
"help": "used storage in bytes from data.lua?page=usbOv",
"varLabels": [
"gateway", "deviceType", "deviceName"
]
},
"promType": "GaugeValue",
"cacheEntryTTL": 300
}
]
}

@ -0,0 +1,549 @@
[
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetTotalPacketsReceived",
"result": "TotalPacketsReceived",
"promDesc": {
"fqName": "gateway_traffic",
"help": "traffic on gateway interface",
"varLabels": [
"gateway"
],
"fixedLabels": {
"direction": "Received",
"unit": "Packets",
"interface": "WAN"
}
},
"promType": "CounterValue"
},
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetTotalPacketsSent",
"result": "TotalPacketsSent",
"promDesc": {
"fqName": "gateway_traffic",
"help": "traffic on gateway interface",
"varLabels": [
"gateway"
],
"fixedLabels": {
"direction": "Sent",
"unit": "Packets",
"interface": "WAN"
}
},
"promType": "CounterValue"
},
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetAddonInfos",
"result": "TotalBytesReceived",
"promDesc": {
"fqName": "gateway_traffic",
"help": "traffic on gateway interface",
"varLabels": [
"gateway"
],
"fixedLabels": {
"direction": "Received",
"unit": "Bytes",
"interface": "WAN"
}
},
"promType": "CounterValue"
},
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetAddonInfos",
"result": "TotalBytesSent",
"promDesc": {
"fqName": "gateway_traffic",
"help": "traffic on gateway interface",
"varLabels": [
"gateway"
],
"fixedLabels": {
"direction": "Sent",
"unit": "Bytes",
"interface": "WAN"
}
},
"promType": "CounterValue"
},
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetAddonInfos",
"result": "ByteSendRate",
"promDesc": {
"fqName": "gateway_traffic_rate",
"help": "traffic rate on gateway interface",
"varLabels": [
"gateway"
],
"fixedLabels": {
"direction": "Sent",
"unit": "Bytes",
"interface": "WAN"
}
},
"promType": "GaugeValue"
},
{
"service": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
"action": "GetAddonInfos",
"result": "ByteReceiveRate",
"promDesc": {
"fqName": "gateway_traffic_rate",
"help": "traffic rate on gateway interface",
"varLabels": [
"gateway"
],