From 9fb16018c83208cb917c69fac5ba505ea385f7e1 Mon Sep 17 00:00:00 2001 From: Ramon Buckland Date: Sun, 24 Jan 2021 21:25:20 +1100 Subject: [PATCH] added flush debugging --- LICENSE | 21 +++++++++++++++++++++ sunspec_exporter/sunspec_exporter.py | 26 +++++++++++++++----------- 2 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..37c7715 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Ramon Buckland for Inosion Pty Ltd + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/sunspec_exporter/sunspec_exporter.py b/sunspec_exporter/sunspec_exporter.py index bd32bb2..2e800ed 100644 --- a/sunspec_exporter/sunspec_exporter.py +++ b/sunspec_exporter/sunspec_exporter.py @@ -117,9 +117,9 @@ def collect_data(sunspec_client, model_ids, filters): for model in sunspec_client.device.models_list: label = model_name(model) - print("# ---------------------") - print(f"# model: {label}") - print("# ---------------------") + print("# ---------------------", flush=True) + print(f"# model: {label}", flush=True) + print("# ---------------------", flush=True) for block in model.blocks: if block.index > 0: index = '%02d_' % (block.index) @@ -156,9 +156,12 @@ def collect_data(sunspec_client, model_ids, filters): if x.regex.match(metric_label): old_value = value value = x.fn(old_value) - print(f"# !! Filtered {metric_label}. {x.regex} matched. {old_value} -> {value}", flush=True) - - print(f"# {final_label}: {value}") + if old_value != value: + print(f"# !! Filtered {metric_label}. {x.regex} matched. {old_value} -> {value}", flush=True) + else: + print(f"# !! Filter no match {metric_label}. {x.regex} matched. {old_value} -> {value}", flush=True) + + print(f"# {final_label}: {value}", flush=True) results[f"{final_label}"] = { "value" : value, "metric_type": metric_type } return results @@ -192,7 +195,7 @@ def collect(self): m.add_metric([self.ip, str(self.port),str(self.target)], the_value) yield m else: - print(f"# metric from {self.ip}:{self.port}/{self.target} sunspec_{x} Value: {the_value} is a {type(the_value)}, Ignoring") + print(f"# metric from {self.ip}:{self.port}/{self.target} sunspec_{x} Value: {the_value} is a {type(the_value)}, Ignoring", flush=True) # https://stackoverflow.com/a/52676692/2150411 @@ -308,7 +311,7 @@ def sunspec_test(ip, port, address): if __name__ == '__main__': # Start up the server to expose the metrics. arguments = docopt(__doc__, version='sunspec-prometheus-exporter 1.0') - print(arguments) + print(arguments, flush=True) sunspec_ip = arguments["--sunspec_ip"] sunspec_port = int(arguments["--sunspec_port"]) @@ -340,17 +343,18 @@ def sunspec_test(ip, port, address): (filter_metric_regex, func_n_params, replacement) = f.split(" ") func_name, *parameters = func_n_params.split(":") func = FnMapping.filter_fn(eval(f"FnMapping.{func_name}"), replacement, *parameters) + print(f"# Added filter: {func_name}({func_n_params}) [{replacement}] -> {filter_metric_regex}", flush=True) filters.append(Filter(regex=re.compile(filter_metric_regex),fn=func)) - print("# !!! Enumerating all models, removing from future reads unwanted ones") + print("# !!! Enumerating all models, removing from future reads unwanted ones", flush=True) models = sunspec_client.device.models_list.copy() for model in models: name = model_name(model) if str(model.id) not in sunspec_model_ids: - print(f"# Removed [{name}]") + print(f"# Removed [{name}]", flush=True) sunspec_client.device.models_list.remove(model) else: - print(f"# Will collect [{name}]") + print(f"# Will collect [{name}]", flush=True) REGISTRY.register(SunspecCollector( sunspec_client,