Skip to content

Commit

Permalink
Skip parsing of not present devices
Browse files Browse the repository at this point in the history
Devices which are not present do not return valid (last) data in all
cases. As there is no real use in parsing them (last valid data has most
likely been scraped already anyways when the device was last present),
skip parsing of not present devices.
  • Loading branch information
jayme-github committed Feb 15, 2022
1 parent 4d38ca7 commit 9a485ca
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
13 changes: 9 additions & 4 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ func (fc *fritzCollector) Collect(ch chan<- prometheus.Metric) {
dev.Name,
)

if dev.Present == 1 && dev.CanMeasureTemp() {
// Not present devices do not carry any more metrics
if dev.Present != 1 {
continue
}

if dev.CanMeasureTemp() {
if err := mustStringToFloatMetric(ch, fc.Temperature, dev.Temperature.FmtCelsius(), &dev); err != nil {
log.Printf("Unable to parse temperature data of \"%s\" : %v\n", dev.Name, err)
}
Expand All @@ -138,7 +143,7 @@ func (fc *fritzCollector) Collect(ch chan<- prometheus.Metric) {
}
}

if dev.Present == 1 && dev.CanMeasurePower() {
if dev.CanMeasurePower() {
if err := mustStringToFloatMetric(ch, fc.EnergyWh, dev.Powermeter.FmtEnergyWh(), &dev); err != nil {
log.Printf("Unable to parse energy data of \"%s\" : %v\n", dev.Name, err)
}
Expand All @@ -147,13 +152,13 @@ func (fc *fritzCollector) Collect(ch chan<- prometheus.Metric) {
}
}

if dev.Present == 1 && dev.CanMeasureHumidity() {
if dev.CanMeasureHumidity() {
if err := mustStringToFloatMetric(ch, fc.Humidity, dev.Humidity.FmtRelativeHumidity(), &dev); err != nil {
log.Printf("Unable to parse humidity data of \"%s\" : %v\n", dev.Name, err)
}
}

if dev.Present == 1 && dev.IsBatteryPowered() {
if dev.IsBatteryPowered() {
if err := mustStringToFloatMetric(ch, fc.BatteryChargeLevel, dev.BatteryChargeLevel, &dev); err != nil {
log.Printf("Unable to parse battery charge level of \"%s\" : %v\n", dev.Name, err)
}
Expand Down
4 changes: 0 additions & 4 deletions test/devicelist.metrics
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,21 @@ fritzbox_power{device_id="12524 2211244",device_name="SWITCH_3",device_type="FRI
fritzbox_switch_boxlock{device_id="12324 2131421",device_name="SWITCH_1",device_type="FRITZ!DECT 200"} 0
fritzbox_switch_boxlock{device_id="12324 2211244",device_name="SWITCH_2",device_type="FRITZ!DECT 200"} 0
fritzbox_switch_boxlock{device_id="12524 2211244",device_name="SWITCH_3",device_type="FRITZ!DECT 200"} 0
fritzbox_switch_boxlock{device_id="ALWAYS FAILZ",device_name="SWITCH_4_FAILING",device_type="FRITZ!DECT 200"} 1
# HELP fritzbox_switch_devicelock Switching via device disabled 1/0, -1 if not known or error
# TYPE fritzbox_switch_devicelock gauge
fritzbox_switch_devicelock{device_id="12324 2131421",device_name="SWITCH_1",device_type="FRITZ!DECT 200"} 0
fritzbox_switch_devicelock{device_id="12324 2211244",device_name="SWITCH_2",device_type="FRITZ!DECT 200"} 0
fritzbox_switch_devicelock{device_id="12524 2211244",device_name="SWITCH_3",device_type="FRITZ!DECT 200"} 0
fritzbox_switch_devicelock{device_id="ALWAYS FAILZ",device_name="SWITCH_4_FAILING",device_type="FRITZ!DECT 200"} 1
# HELP fritzbox_switch_mode Switch mode 1/0 (manual/automatic), -1 if not known or error
# TYPE fritzbox_switch_mode gauge
fritzbox_switch_mode{device_id="12324 2131421",device_name="SWITCH_1",device_type="FRITZ!DECT 200"} 1
fritzbox_switch_mode{device_id="12324 2211244",device_name="SWITCH_2",device_type="FRITZ!DECT 200"} 1
fritzbox_switch_mode{device_id="12524 2211244",device_name="SWITCH_3",device_type="FRITZ!DECT 200"} 1
fritzbox_switch_mode{device_id="ALWAYS FAILZ",device_name="SWITCH_4_FAILING",device_type="FRITZ!DECT 200"} 1
# HELP fritzbox_switch_state Switch state 1/0 (on/off), -1 if not known or error
# TYPE fritzbox_switch_state gauge
fritzbox_switch_state{device_id="12324 2131421",device_name="SWITCH_1",device_type="FRITZ!DECT 200"} 0
fritzbox_switch_state{device_id="12324 2211244",device_name="SWITCH_2",device_type="FRITZ!DECT 200"} 1
fritzbox_switch_state{device_id="12524 2211244",device_name="SWITCH_3",device_type="FRITZ!DECT 200"} 0
fritzbox_switch_state{device_id="ALWAYS FAILZ",device_name="SWITCH_4_FAILING",device_type="FRITZ!DECT 200"} 0
# HELP fritzbox_temperature Temperature measured at the device sensor in units of 0.1 °C
# TYPE fritzbox_temperature gauge
fritzbox_temperature{device_id="12324 2131421",device_name="SWITCH_1",device_type="FRITZ!DECT 200"} 21
Expand Down
2 changes: 2 additions & 0 deletions test/devicelist_729.metrics
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fritzbox_device_info{device_id="12345 0000004",device_name="HKR_3",device_type="
fritzbox_device_info{device_id="12345 0000005",device_name="HKR_4",device_type="Comet DECT",functionbitmask="320",fw_version="03.68",internal_id="24",manufacturer="AVM"} 1
fritzbox_device_info{device_id="12345 0000006",device_name="SW_2",device_type="FRITZ!DECT 210",functionbitmask="35712",fw_version="04.17",internal_id="25",manufacturer="AVM"} 1
fritzbox_device_info{device_id="12345 0000007",device_name="BUTTON_1",device_type="FRITZ!DECT 440",functionbitmask="1048864",fw_version="05.21",internal_id="26",manufacturer="AVM"} 1
fritzbox_device_info{device_id="12345 0000008",device_name="SW_3",device_type="FRITZ!DECT 200",functionbitmask="35712",fw_version="04.16",internal_id="27",manufacturer="AVM"} 1
# HELP fritzbox_device_present Device connected (1) or not (0)
# TYPE fritzbox_device_present gauge
fritzbox_device_present{device_id="12345 0000001",device_name="SW_1",device_type="FRITZ!DECT 200"} 1
Expand All @@ -30,6 +31,7 @@ fritzbox_device_present{device_id="12345 0000004",device_name="HKR_3",device_typ
fritzbox_device_present{device_id="12345 0000005",device_name="HKR_4",device_type="Comet DECT"} 1
fritzbox_device_present{device_id="12345 0000006",device_name="SW_2",device_type="FRITZ!DECT 210"} 1
fritzbox_device_present{device_id="12345 0000007",device_name="BUTTON_1",device_type="FRITZ!DECT 440"} 1
fritzbox_device_present{device_id="12345 0000008",device_name="SW_3",device_type="FRITZ!DECT 200"} 0
# HELP fritzbox_energy Absolute energy consumption (in Wh) since the device started operating
# TYPE fritzbox_energy gauge
fritzbox_energy{device_id="12345 0000001",device_name="SW_1",device_type="FRITZ!DECT 200"} 339584
Expand Down
23 changes: 23 additions & 0 deletions test/devicelist_729.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,27 @@
<lastpressedtimestamp></lastpressedtimestamp>
</button>
</device>
<device identifier="12345 0000008" id="27" functionbitmask="35712" fwversion="04.16" manufacturer="AVM" productname="FRITZ!DECT 200">
<present>0</present>
<txbusy>0</txbusy>
<name>SW_3</name>
<switch>
<state></state>
<mode></mode>
<lock></lock>
<devicelock></devicelock>
</switch>
<simpleonoff>
<state></state>
</simpleonoff>
<powermeter>
<voltage></voltage>
<power></power>
<energy></energy>
</powermeter>
<temperature>
<celsius></celsius>
<offset></offset>
</temperature>
</device>
</devicelist>

0 comments on commit 9a485ca

Please sign in to comment.