Skip to content

Commit

Permalink
git - Merge pull request #90 from DinoTools/improve_interface_cookie_…
Browse files Browse the repository at this point in the history
…filenames

Improve interface cookie filenames
  • Loading branch information
phibos authored Jul 15, 2024
2 parents 9836f60 + 3c4edf5 commit 8fee057
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions routeros_check/check/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,23 +298,28 @@ def interface_names(self):
return tuple(self.fetch_data().keys())

def probe(self):
def get_cookie_filename(name: str) -> str:
format_values = {
"name": escape_filename(name),
}
for n in ["host", "hostname"]:
cmd_option_value = self._cmd_options.get(n)
format_values[n] = escape_filename(str(cmd_option_value))
return self.cookie_filename.format(
**format_values
)

routeros_metrics = []
data = self.fetch_data()

if self.single_interface:
if len(self.interface_names) == 1:
cookie_filename = self.cookie_filename.format(
name=escape_filename(self.interface_names[0])
)
with nagiosplugin.Cookie(cookie_filename) as cookie:
with nagiosplugin.Cookie(get_cookie_filename(self.interface_names[0])) as cookie:
routeros_metrics += self.get_routeros_metric_item(data[self.interface_names[0]], cookie=cookie)
self._add_contexts(name=self.interface_names[0], values=data[self.interface_names[0]])
else:
for name in self.interface_names:
cookie_filename = self.cookie_filename.format(
name=escape_filename(name)
)
with nagiosplugin.Cookie(cookie_filename) as cookie:
with nagiosplugin.Cookie(get_cookie_filename(name)) as cookie:
routeros_metrics += self.get_routeros_metric_item(data[name], name_prefix=f"{name} ", cookie=cookie)
self._add_contexts(name=name, values=data[name], metric_prefix="{name} ")

Expand Down Expand Up @@ -385,9 +390,11 @@ def evaluate(self, metric, resource: InterfaceResource):
"cookie_filename",
default="/tmp/check_routeros_interface_{name}.data",
help=(
"The filename to use to store the information to calculate the rate. '{name}' will be replaced with an "
"internal uniq id. It Will create one file per interface."
"(Default: /tmp/check_routeros_interface_{name}.data)"
"The filename to use to store the information to calculate the rate. '{name}' will be replaced with the "
"interface name. Also '{host}' and '{hostname}' will be replaced with the values provided as commandline "
"options. You must create uniq filenames to get the correct rate. "
"(Default: /tmp/check_routeros_interface_{name}.data) "
"If multiple devices are checked use something like: /tmp/check_routeros_interface_{host}_{name}.data"
),
)
@click.option(
Expand Down

0 comments on commit 8fee057

Please sign in to comment.