|
@ -94,10 +94,11 @@ var collectUpnpResultsLoaded = prometheus.NewCounter(prometheus.CounterOpts{ |
|
|
|
|
|
|
|
|
// JSONPromDesc metric description loaded from JSON
|
|
|
// JSONPromDesc metric description loaded from JSON
|
|
|
type JSONPromDesc struct { |
|
|
type JSONPromDesc struct { |
|
|
FqName string `json:"fqName"` |
|
|
FqName string `json:"fqName"` |
|
|
Help string `json:"help"` |
|
|
Help string `json:"help"` |
|
|
VarLabels []string `json:"varLabels"` |
|
|
VarLabels []string `json:"varLabels"` |
|
|
FixedLabels map[string]string `json:"fixedLabels"` |
|
|
FixedLabels map[string]string `json:"fixedLabels"` |
|
|
|
|
|
fixedLabelValues string // neeeded to create uniq lookup key when reporting
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// ActionArg argument for upnp action
|
|
|
// ActionArg argument for upnp action
|
|
@ -242,7 +243,7 @@ func (fc *FritzboxCollector) Describe(ch chan<- *prometheus.Desc) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric, result upnp.Result, dupCache *map[string]bool) { |
|
|
func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric, result upnp.Result, dupCache map[string]bool) { |
|
|
|
|
|
|
|
|
val, ok := result[m.Result] |
|
|
val, ok := result[m.Result] |
|
|
if !ok { |
|
|
if !ok { |
|
@ -294,16 +295,13 @@ func (fc *FritzboxCollector) reportMetric(ch chan<- prometheus.Metric, m *Metric |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// check for duplicate labels to prevent collection failure
|
|
|
// check for duplicate labels to prevent collection failure
|
|
|
if dupCache != nil { |
|
|
key := m.PromDesc.FqName + ":" + m.PromDesc.fixedLabelValues + strings.Join(labels, ",") |
|
|
key := strings.Join(labels, ",") |
|
|
if dupCache[key] { |
|
|
if (*dupCache)[key] { |
|
|
fmt.Printf("%s.%s reported before as: %s\n", m.Service, m.Action, key) |
|
|
fmt.Printf("%s.%s reported before with labels: %s\n", m.Service, m.Action, key) |
|
|
collectErrors.Inc() |
|
|
collectErrors.Inc() |
|
|
return |
|
|
return |
|
|
|
|
|
} else { |
|
|
|
|
|
(*dupCache)[key] = true |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
dupCache[key] = true |
|
|
|
|
|
|
|
|
metric, err := prometheus.NewConstMetric(m.Desc, m.MetricType, floatval, labels...) |
|
|
metric, err := prometheus.NewConstMetric(m.Desc, m.MetricType, floatval, labels...) |
|
|
if err != nil { |
|
|
if err != nil { |
|
@ -370,6 +368,9 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// create cache for duplicate lookup, to prevent collection errors
|
|
|
|
|
|
var dupCache = make(map[string]bool) |
|
|
|
|
|
|
|
|
for _, m := range metrics { |
|
|
for _, m := range metrics { |
|
|
var actArg *upnp.ActionArgument |
|
|
var actArg *upnp.ActionArgument |
|
|
if m.ActionArgument != nil { |
|
|
if m.ActionArgument != nil { |
|
@ -404,7 +405,6 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) { |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var dupCache = make(map[string]bool) |
|
|
|
|
|
for i := 0; i < count; i++ { |
|
|
for i := 0; i < count; i++ { |
|
|
actArg = &upnp.ActionArgument{Name: aa.Name, Value: i} |
|
|
actArg = &upnp.ActionArgument{Name: aa.Name, Value: i} |
|
|
result, err := fc.getActionResult(m, m.Action, actArg) |
|
|
result, err := fc.getActionResult(m, m.Action, actArg) |
|
@ -415,7 +415,7 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) { |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fc.reportMetric(ch, m, result, &dupCache) |
|
|
fc.reportMetric(ch, m, result, dupCache) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
continue |
|
|
continue |
|
@ -432,16 +432,16 @@ func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) { |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fc.reportMetric(ch, m, result, nil) |
|
|
fc.reportMetric(ch, m, result, dupCache) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// if lua is enabled now also collect metrics
|
|
|
// if lua is enabled now also collect metrics
|
|
|
if fc.LuaSession != nil { |
|
|
if fc.LuaSession != nil { |
|
|
fc.collectLua(ch) |
|
|
fc.collectLua(ch, dupCache) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (fc *FritzboxCollector) collectLua(ch chan<- prometheus.Metric) { |
|
|
func (fc *FritzboxCollector) collectLua(ch chan<- prometheus.Metric, dupCache map[string]bool) { |
|
|
// create a map for caching results
|
|
|
// create a map for caching results
|
|
|
now := time.Now().Unix() |
|
|
now := time.Now().Unix() |
|
|
|
|
|
|
|
@ -489,12 +489,12 @@ func (fc *FritzboxCollector) collectLua(ch chan<- prometheus.Metric) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for _, mv := range metricVals { |
|
|
for _, mv := range metricVals { |
|
|
fc.reportLuaMetric(ch, lm, mv) |
|
|
fc.reportLuaMetric(ch, lm, mv, dupCache) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (fc *FritzboxCollector) reportLuaMetric(ch chan<- prometheus.Metric, lm *LuaMetric, value lua.LuaMetricValue) { |
|
|
func (fc *FritzboxCollector) reportLuaMetric(ch chan<- prometheus.Metric, lm *LuaMetric, value lua.LuaMetricValue, dupCache map[string]bool) { |
|
|
|
|
|
|
|
|
labels := make([]string, len(lm.PromDesc.VarLabels)) |
|
|
labels := make([]string, len(lm.PromDesc.VarLabels)) |
|
|
for i, l := range lm.PromDesc.VarLabels { |
|
|
for i, l := range lm.PromDesc.VarLabels { |
|
@ -516,6 +516,15 @@ 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) |
|
|
|
|
|
luaCollectErrors.Inc() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
dupCache[key] = true |
|
|
|
|
|
|
|
|
metric, err := prometheus.NewConstMetric(lm.Desc, lm.MetricType, value.Value, labels...) |
|
|
metric, err := prometheus.NewConstMetric(lm.Desc, lm.MetricType, value.Value, labels...) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Printf("Error creating metric %s.%s: %s", lm.ResultPath, lm.ResultPath, err.Error()) |
|
|
fmt.Printf("Error creating metric %s.%s: %s", lm.ResultPath, lm.ResultPath, err.Error()) |
|
@ -723,7 +732,7 @@ func main() { |
|
|
// init metrics
|
|
|
// init metrics
|
|
|
luaMetrics = lmf.Metrics |
|
|
luaMetrics = lmf.Metrics |
|
|
for _, lm := range luaMetrics { |
|
|
for _, lm := range luaMetrics { |
|
|
pd := lm.PromDesc |
|
|
pd := &lm.PromDesc |
|
|
|
|
|
|
|
|
// make labels lower case
|
|
|
// make labels lower case
|
|
|
labels := make([]string, len(pd.VarLabels)) |
|
|
labels := make([]string, len(pd.VarLabels)) |
|
@ -731,6 +740,12 @@ func main() { |
|
|
labels[i] = strings.ToLower(l) |
|
|
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.Desc = prometheus.NewDesc(pd.FqName, pd.Help, labels, pd.FixedLabels) |
|
|
lm.MetricType = getValueType(lm.PromType) |
|
|
lm.MetricType = getValueType(lm.PromType) |
|
|
|
|
|
|
|
@ -761,7 +776,7 @@ func main() { |
|
|
|
|
|
|
|
|
// init metrics
|
|
|
// init metrics
|
|
|
for _, m := range metrics { |
|
|
for _, m := range metrics { |
|
|
pd := m.PromDesc |
|
|
pd := &m.PromDesc |
|
|
|
|
|
|
|
|
// make labels lower case
|
|
|
// make labels lower case
|
|
|
labels := make([]string, len(pd.VarLabels)) |
|
|
labels := make([]string, len(pd.VarLabels)) |
|
@ -769,6 +784,12 @@ func main() { |
|
|
labels[i] = strings.ToLower(l) |
|
|
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.Desc = prometheus.NewDesc(pd.FqName, pd.Help, labels, pd.FixedLabels) |
|
|
m.MetricType = getValueType(m.PromType) |
|
|
m.MetricType = getValueType(m.PromType) |
|
|
|
|
|
|
|
|