mirror of
https://github.com/sberk42/fritzbox_exporter.git
synced 2026-08-26 23:57:16 +00:00
Uses logrus instead printf for logging and fixes creating error messages.
This commit is contained in:
@@ -5,5 +5,6 @@ go 1.15
|
||||
require (
|
||||
github.com/namsral/flag v1.7.4-pre
|
||||
github.com/prometheus/client_golang v1.10.0
|
||||
github.com/sirupsen/logrus v1.6.0
|
||||
golang.org/x/text v0.3.6
|
||||
)
|
||||
|
||||
@@ -40,6 +40,7 @@ github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfc
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
|
||||
@@ -139,10 +140,13 @@ github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8
|
||||
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
|
||||
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
|
||||
@@ -152,6 +156,7 @@ github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
|
||||
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
||||
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
|
||||
@@ -201,6 +206,7 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
|
||||
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
|
||||
@@ -240,6 +246,7 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
|
||||
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||
@@ -254,6 +261,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
|
||||
@@ -385,6 +393,7 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
|
||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
@@ -398,6 +407,7 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
||||
@@ -21,8 +21,8 @@ import (
|
||||
"github.com/namsral/flag"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
@@ -56,6 +56,8 @@ var (
|
||||
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")
|
||||
|
||||
flagLogLevel = flag.String("log-level", "info", "The logging level. Can be error, warn, info, debug or trace")
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -220,13 +222,13 @@ 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)
|
||||
logrus.Errorf("cannot load services: %s", err)
|
||||
|
||||
time.Sleep(serviceLoadRetryTime)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("services loaded\n")
|
||||
logrus.Info("services loaded")
|
||||
|
||||
fc.Lock()
|
||||
fc.Root = root
|
||||
@@ -246,7 +248,7 @@ func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric
|
||||
|
||||
val, ok := result[m.Result]
|
||||
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()
|
||||
return
|
||||
}
|
||||
@@ -268,7 +270,7 @@ func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric
|
||||
floatval = 0
|
||||
}
|
||||
default:
|
||||
fmt.Println("unknown type", val)
|
||||
logrus.Warnf("unknown type: %s", val)
|
||||
collectErrors.Inc()
|
||||
return
|
||||
}
|
||||
@@ -280,7 +282,7 @@ func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric
|
||||
} else {
|
||||
lval, ok := result[l]
|
||||
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 = ""
|
||||
}
|
||||
|
||||
@@ -296,7 +298,7 @@ func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric
|
||||
// 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)
|
||||
logrus.Debugf("%s.%s reported before as: %s\n", m.Service, m.Action, key)
|
||||
collectErrors.Inc()
|
||||
return
|
||||
}
|
||||
@@ -304,7 +306,7 @@ func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric
|
||||
|
||||
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())
|
||||
logrus.Errorf("Can not create metric %s.%s: %s", m.Service, m.Action, err.Error())
|
||||
} else {
|
||||
ch <- metric
|
||||
}
|
||||
@@ -381,7 +383,7 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
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())
|
||||
logrus.Warnf("Error getting provider action %s result for %s.%s: %s", aa.ProviderAction, m.Service, m.Action, err.Error())
|
||||
collectErrors.Inc()
|
||||
continue
|
||||
}
|
||||
@@ -389,7 +391,7 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
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)
|
||||
logrus.Warnf("provider action %s for %s.%s has no result", m.Service, m.Action, aa.Value)
|
||||
collectErrors.Inc()
|
||||
continue
|
||||
}
|
||||
@@ -399,7 +401,7 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
sval := fmt.Sprintf("%v", value)
|
||||
count, err := strconv.Atoi(sval)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
logrus.Warn(err.Error())
|
||||
collectErrors.Inc()
|
||||
continue
|
||||
}
|
||||
@@ -409,7 +411,7 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
result, err := fc.getActionResult(m, m.Action, actArg)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
logrus.Error("Can not get result for %s: %s", m.Action, err)
|
||||
collectErrors.Inc()
|
||||
continue
|
||||
}
|
||||
@@ -426,7 +428,7 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
result, err := fc.getActionResult(m, m.Action, actArg)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
logrus.Warnf("can not collect metrics: %s", err)
|
||||
collectErrors.Inc()
|
||||
continue
|
||||
}
|
||||
@@ -459,7 +461,7 @@ func (fc *FritzboxCollector) collectLua(ch chan<- prometheus.Metric, dupCache ma
|
||||
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())
|
||||
logrus.Errorf("Can not load %s for %s.%s: %s", lm.Path, lm.ResultPath, lm.ResultKey, err.Error())
|
||||
luaCollectErrors.Inc()
|
||||
fc.LuaSession.SID = "" // clear SID in case of error, so force reauthentication
|
||||
continue
|
||||
@@ -468,7 +470,7 @@ func (fc *FritzboxCollector) collectLua(ch chan<- prometheus.Metric, dupCache ma
|
||||
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())
|
||||
logrus.Errorf("Can not parse JSON from %s for %s.%s: %s", lm.Path, lm.ResultPath, lm.ResultKey, err.Error())
|
||||
luaCollectErrors.Inc()
|
||||
continue
|
||||
}
|
||||
@@ -483,7 +485,7 @@ func (fc *FritzboxCollector) collectLua(ch chan<- prometheus.Metric, dupCache ma
|
||||
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())
|
||||
logrus.Errorf("Can not get metric values for %s.%s: %s", lm.ResultPath, lm.ResultKey, err.Error())
|
||||
luaCollectErrors.Inc()
|
||||
cacheEntry.Result = nil // don't use invalid results for cache
|
||||
continue
|
||||
@@ -504,7 +506,7 @@ func (fc *FritzboxCollector) reportLuaMetric(ch chan<- prometheus.Metric, lm *Lu
|
||||
} 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)
|
||||
logrus.Warnf("%s.%s from %s?%s has no resul for label %s", lm.ResultPath, lm.ResultKey, lm.Path, lm.Params, l)
|
||||
lval = ""
|
||||
}
|
||||
|
||||
@@ -520,7 +522,7 @@ func (fc *FritzboxCollector) reportLuaMetric(ch chan<- prometheus.Metric, lm *Lu
|
||||
// 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)
|
||||
logrus.Errorf("%s.%s reported before as: %s\n", lm.ResultPath, lm.ResultPath, key)
|
||||
luaCollectErrors.Inc()
|
||||
return
|
||||
}
|
||||
@@ -528,7 +530,7 @@ func (fc *FritzboxCollector) reportLuaMetric(ch chan<- prometheus.Metric, lm *Lu
|
||||
|
||||
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())
|
||||
logrus.Errorf("Can not create metric %s.%s: %s", lm.ResultPath, lm.ResultPath, err.Error())
|
||||
} else {
|
||||
ch <- metric
|
||||
}
|
||||
@@ -540,7 +542,7 @@ func test() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var newEntry bool = false
|
||||
var newEntry = false
|
||||
var json bytes.Buffer
|
||||
json.WriteString("[\n")
|
||||
|
||||
@@ -551,7 +553,7 @@ func test() {
|
||||
sort.Strings(serviceKeys)
|
||||
for _, k := range serviceKeys {
|
||||
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{}
|
||||
for l := range s.Actions {
|
||||
@@ -560,14 +562,14 @@ func test() {
|
||||
sort.Strings(actionKeys)
|
||||
for _, l := range actionKeys {
|
||||
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 {
|
||||
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() {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -590,16 +592,16 @@ func test() {
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf(" FAILED:%s\n", err.Error())
|
||||
logrus.Warnf("FAILED:%s", err)
|
||||
continue
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -609,7 +611,7 @@ func test() {
|
||||
if *flagJSONOut != "" {
|
||||
err := ioutil.WriteFile(*flagJSONOut, json.Bytes(), 0644)
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -618,14 +620,14 @@ func testLua() {
|
||||
|
||||
jsonData, err := ioutil.ReadFile("luaTest.json")
|
||||
if err != nil {
|
||||
fmt.Println("error reading luaTest.json:", err)
|
||||
logrus.Error("Can not read luaTest.json: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
var luaTests []LuaTest
|
||||
err = json.Unmarshal(jsonData, &luaTests)
|
||||
if err != nil {
|
||||
fmt.Println("error parsing luaTest JSON:", err)
|
||||
logrus.Error("Can not parse luaTest JSON: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -633,19 +635,14 @@ func testLua() {
|
||||
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())
|
||||
logrus.Errorf("Testing %s (%s) failed: %s", test.Path, test.Params, err.Error())
|
||||
} else {
|
||||
fmt.Println(string(pageData))
|
||||
logrus.Infof("Testing %s(%s) successful: %s", test.Path, test.Params, string(pageData))
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -664,10 +661,16 @@ func getValueType(vt string) prometheus.ValueType {
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
level, e := logrus.ParseLevel(*flagLogLevel)
|
||||
if e != nil {
|
||||
logrus.Warnf("Can not parse log level: %s use INFO", e)
|
||||
level = logrus.InfoLevel
|
||||
}
|
||||
logrus.SetLevel(level)
|
||||
|
||||
u, err := url.Parse(*flagGatewayURL)
|
||||
if err != nil {
|
||||
fmt.Println("invalid URL:", err)
|
||||
logrus.Errorf("invalid URL:", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -684,13 +687,13 @@ func main() {
|
||||
// read metrics
|
||||
jsonData, err := ioutil.ReadFile(*flagMetricsFile)
|
||||
if err != nil {
|
||||
fmt.Println("error reading metric file:", err)
|
||||
logrus.Errorf("error reading metric file:", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(jsonData, &metrics)
|
||||
if err != nil {
|
||||
fmt.Println("error parsing JSON:", err)
|
||||
logrus.Errorf("error parsing JSON:", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -702,14 +705,14 @@ func main() {
|
||||
if !*flagDisableLua {
|
||||
jsonData, err := ioutil.ReadFile(*flagLuaMetricsFile)
|
||||
if err != nil {
|
||||
fmt.Println("error reading lua metric file:", err)
|
||||
logrus.Error("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)
|
||||
logrus.Error("error parsing lua JSON:", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -722,7 +725,7 @@ func main() {
|
||||
regex, err := regexp.Compile(ren.MatchRegex)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("error compiling lua rename regex:", err)
|
||||
logrus.Error("error compiling lua rename regex:", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -819,14 +822,14 @@ func main() {
|
||||
prometheus.MustRegister(luaCollectErrors)
|
||||
}
|
||||
|
||||
fmt.Println("collecting metrics via http")
|
||||
logrus.Infof("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())
|
||||
logrus.Infof("Response:\n\n%s", writer.String())
|
||||
|
||||
return
|
||||
}
|
||||
@@ -845,7 +848,7 @@ func main() {
|
||||
}
|
||||
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
fmt.Printf("metrics available at http://%s/metrics\n", *flagAddr)
|
||||
logrus.Infof("metrics available at http://%s/metrics", *flagAddr)
|
||||
|
||||
log.Fatal(http.ListenAndServe(*flagAddr, nil))
|
||||
logrus.Error(http.ListenAndServe(*flagAddr, nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user