|
@ -21,8 +21,8 @@ import ( |
|
|
"github.com/namsral/flag" |
|
|
"github.com/namsral/flag" |
|
|
"github.com/prometheus/client_golang/prometheus" |
|
|
"github.com/prometheus/client_golang/prometheus" |
|
|
"github.com/prometheus/client_golang/prometheus/promhttp" |
|
|
"github.com/prometheus/client_golang/prometheus/promhttp" |
|
|
|
|
|
"github.com/sirupsen/logrus" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"log" |
|
|
|
|
|
"net/http" |
|
|
"net/http" |
|
|
"net/url" |
|
|
"net/url" |
|
|
"regexp" |
|
|
"regexp" |
|
@ -222,13 +222,13 @@ func (fc *FritzboxCollector) LoadServices() { |
|
|
for { |
|
|
for { |
|
|
root, err := upnp.LoadServices(fc.URL, fc.Username, fc.Password, fc.VerifyTls) |
|
|
root, err := upnp.LoadServices(fc.URL, fc.Username, fc.Password, fc.VerifyTls) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Printf("cannot load services: %s\n", err) |
|
|
logrus.Errorf("cannot load services: %s", err) |
|
|
|
|
|
|
|
|
time.Sleep(serviceLoadRetryTime) |
|
|
time.Sleep(serviceLoadRetryTime) |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fmt.Printf("services loaded\n") |
|
|
logrus.Info("services loaded") |
|
|
|
|
|
|
|
|
fc.Lock() |
|
|
fc.Lock() |
|
|
fc.Root = root |
|
|
fc.Root = root |
|
@ -248,7 +248,7 @@ func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric |
|
|
|
|
|
|
|
|
val, ok := result[m.Result] |
|
|
val, ok := result[m.Result] |
|
|
if !ok { |
|
|
if !ok { |
|
|
fmt.Printf("%s.%s has no result %s", m.Service, m.Action, m.Result) |
|
|
logrus.Debugf("%s.%s has no result %s", m.Service, m.Action, m.Result) |
|
|
collectErrors.Inc() |
|
|
collectErrors.Inc() |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
@ -270,7 +270,7 @@ func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric |
|
|
floatval = 0 |
|
|
floatval = 0 |
|
|
} |
|
|
} |
|
|
default: |
|
|
default: |
|
|
fmt.Println("unknown type", val) |
|
|
logrus.Warnf("unknown type: %s", val) |
|
|
collectErrors.Inc() |
|
|
collectErrors.Inc() |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
@ -282,7 +282,7 @@ func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric |
|
|
} else { |
|
|
} else { |
|
|
lval, ok := result[l] |
|
|
lval, ok := result[l] |
|
|
if !ok { |
|
|
if !ok { |
|
|
fmt.Printf("%s.%s has no resul for label %s", m.Service, m.Action, l) |
|
|
logrus.Warnf("%s.%s has no resul for label %s", m.Service, m.Action, l) |
|
|
lval = "" |
|
|
lval = "" |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -383,7 +383,7 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) { |
|
|
provRes, err := fc.getActionResult(m, aa.ProviderAction, nil) |
|
|
provRes, err := fc.getActionResult(m, aa.ProviderAction, nil) |
|
|
|
|
|
|
|
|
if err != 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()) |
|
|
logrus.Warnf("Error getting provider action %s result for %s.%s: %s", aa.ProviderAction, m.Service, m.Action, err.Error()) |
|
|
collectErrors.Inc() |
|
|
collectErrors.Inc() |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
@ -391,7 +391,7 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) { |
|
|
var ok bool |
|
|
var ok bool |
|
|
value, ok = provRes[aa.Value] // Value contains the result name for provider actions
|
|
|
value, ok = provRes[aa.Value] // Value contains the result name for provider actions
|
|
|
if !ok { |
|
|
if !ok { |
|
|
fmt.Printf("provider action %s for %s.%s has no result", m.Service, m.Action, aa.Value) |
|
|
logrus.Warnf("provider action %s for %s.%s has no result", m.Service, m.Action, aa.Value) |
|
|
collectErrors.Inc() |
|
|
collectErrors.Inc() |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
@ -428,7 +428,7 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) { |
|
|
result, err := fc.getActionResult(m, m.Action, actArg) |
|
|
result, err := fc.getActionResult(m, m.Action, actArg) |
|
|
|
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Println(err.Error()) |
|
|
logrus.Warnf("can not collect metrics: %s", err) |
|
|
collectErrors.Inc() |
|
|
collectErrors.Inc() |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
@ -542,7 +542,7 @@ func test() { |
|
|
panic(err) |
|
|
panic(err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var newEntry bool = false |
|
|
var newEntry = false |
|
|
var json bytes.Buffer |
|
|
var json bytes.Buffer |
|
|
json.WriteString("[\n") |
|
|
json.WriteString("[\n") |
|
|
|
|
|
|
|
@ -553,7 +553,7 @@ func test() { |
|
|
sort.Strings(serviceKeys) |
|
|
sort.Strings(serviceKeys) |
|
|
for _, k := range serviceKeys { |
|
|
for _, k := range serviceKeys { |
|
|
s := root.Services[k] |
|
|
s := root.Services[k] |
|
|
fmt.Printf("Service: %s (Url: %s)\n", k, s.ControlURL) |
|
|
logrus.Infof("Service: %s (Url: %s)\n", k, s.ControlURL) |
|
|
|
|
|
|
|
|
actionKeys := []string{} |
|
|
actionKeys := []string{} |
|
|
for l := range s.Actions { |
|
|
for l := range s.Actions { |
|
@ -562,14 +562,14 @@ func test() { |
|
|
sort.Strings(actionKeys) |
|
|
sort.Strings(actionKeys) |
|
|
for _, l := range actionKeys { |
|
|
for _, l := range actionKeys { |
|
|
a := s.Actions[l] |
|
|
a := s.Actions[l] |
|
|
fmt.Printf(" %s - arguments: variable [direction] (soap name, soap type)\n", a.Name) |
|
|
logrus.Debugf("%s - arguments: variable [direction] (soap name, soap type)", a.Name) |
|
|
for _, arg := range a.Arguments { |
|
|
for _, arg := range a.Arguments { |
|
|
sv := arg.StateVariable |
|
|
sv := arg.StateVariable |
|
|
fmt.Printf(" %s [%s] (%s, %s)\n", arg.RelatedStateVariable, arg.Direction, arg.Name, sv.DataType) |
|
|
logrus.Debugf("%s [%s] (%s, %s)", arg.RelatedStateVariable, arg.Direction, arg.Name, sv.DataType) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if !a.IsGetOnly() { |
|
|
if !a.IsGetOnly() { |
|
|
fmt.Printf(" %s - not calling, since arguments required or no output\n", a.Name) |
|
|
logrus.Debugf("%s - not calling, since arguments required or no output", a.Name) |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -592,16 +592,16 @@ func test() { |
|
|
json.WriteString("\"\n\t}") |
|
|
json.WriteString("\"\n\t}") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fmt.Printf(" %s - calling - results: variable: value\n", a.Name) |
|
|
logrus.Debugf("%s - calling - results: variable: value", a.Name) |
|
|
res, err := a.Call(nil) |
|
|
res, err := a.Call(nil) |
|
|
|
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Printf(" FAILED:%s\n", err.Error()) |
|
|
logrus.Warnf("FAILED:%s", err) |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for _, arg := range a.Arguments { |
|
|
for _, arg := range a.Arguments { |
|
|
fmt.Printf(" %s: %v\n", arg.RelatedStateVariable, res[arg.StateVariable.Name]) |
|
|
logrus.Debugf("%s: %v", arg.RelatedStateVariable, res[arg.StateVariable.Name]) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -611,7 +611,7 @@ func test() { |
|
|
if *flagJSONOut != "" { |
|
|
if *flagJSONOut != "" { |
|
|
err := ioutil.WriteFile(*flagJSONOut, json.Bytes(), 0644) |
|
|
err := ioutil.WriteFile(*flagJSONOut, json.Bytes(), 0644) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Printf("Failed writing JSON file '%s': %s\n", *flagJSONOut, err.Error()) |
|
|
logrus.Warnf("Failed writing JSON file '%s': %s\n", *flagJSONOut, err.Error()) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -669,7 +669,7 @@ func main() { |
|
|
|
|
|
|
|
|
u, err := url.Parse(*flagGatewayURL) |
|
|
u, err := url.Parse(*flagGatewayURL) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Println("invalid URL:", err) |
|
|
logrus.Errorf("invalid URL:", err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -686,13 +686,13 @@ func main() { |
|
|
// read metrics
|
|
|
// read metrics
|
|
|
jsonData, err := ioutil.ReadFile(*flagMetricsFile) |
|
|
jsonData, err := ioutil.ReadFile(*flagMetricsFile) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Println("error reading metric file:", err) |
|
|
logrus.Errorf("error reading metric file:", err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
err = json.Unmarshal(jsonData, &metrics) |
|
|
err = json.Unmarshal(jsonData, &metrics) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Println("error parsing JSON:", err) |
|
|
logrus.Errorf("error parsing JSON:", err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -822,14 +822,14 @@ func main() { |
|
|
prometheus.MustRegister(luaCollectErrors) |
|
|
prometheus.MustRegister(luaCollectErrors) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fmt.Println("collecting metrics via http") |
|
|
logrus.Infof("collecting metrics via http") |
|
|
|
|
|
|
|
|
// simulate HTTP request without starting actual http server
|
|
|
// simulate HTTP request without starting actual http server
|
|
|
writer := testResponseWriter{header: http.Header{}} |
|
|
writer := testResponseWriter{header: http.Header{}} |
|
|
request := http.Request{} |
|
|
request := http.Request{} |
|
|
promhttp.Handler().ServeHTTP(&writer, &request) |
|
|
promhttp.Handler().ServeHTTP(&writer, &request) |
|
|
|
|
|
|
|
|
fmt.Println(writer.String()) |
|
|
logrus.Infof("Response:\n\n%s", writer.String()) |
|
|
|
|
|
|
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
@ -850,11 +850,11 @@ func main() { |
|
|
healthChecks := createHealthChecks(*flagGatewayURL) |
|
|
healthChecks := createHealthChecks(*flagGatewayURL) |
|
|
|
|
|
|
|
|
http.Handle("/metrics", promhttp.Handler()) |
|
|
http.Handle("/metrics", promhttp.Handler()) |
|
|
fmt.Printf("metrics available at http://%s/metrics\n", *flagAddr) |
|
|
logrus.Infof("metrics available at http://%s/metrics", *flagAddr) |
|
|
http.HandleFunc("/ready", healthChecks.ReadyEndpoint) |
|
|
http.HandleFunc("/ready", healthChecks.ReadyEndpoint) |
|
|
fmt.Printf("readyness check available at http://%s/ready\n", *flagAddr) |
|
|
logrus.Infof("readyness check available at http://%s/ready\n", *flagAddr) |
|
|
http.HandleFunc("/live", healthChecks.LiveEndpoint) |
|
|
http.HandleFunc("/live", healthChecks.LiveEndpoint) |
|
|
fmt.Printf("liveness check available at http://%s/live\n", *flagAddr) |
|
|
logrus.Infof("liveness check available at http://%s/live", *flagAddr) |
|
|
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(*flagAddr, nil)) |
|
|
logrus.Error(http.ListenAndServe(*flagAddr, nil)) |
|
|
} |
|
|
} |
|
|