Default requests.post timeout and linter fixes

This commit is contained in:
Orsiris de Jong 2023-01-28 19:42:19 +01:00
parent d693b587c4
commit 6e9c8525e2

View file

@ -6,8 +6,8 @@ __intname__ = "restic_metrics"
__author__ = "Orsiris de Jong" __author__ = "Orsiris de Jong"
__copyright__ = "Copyright (C) 2022-2023 Orsiris de Jong - NetInvent" __copyright__ = "Copyright (C) 2022-2023 Orsiris de Jong - NetInvent"
__licence__ = "BSD-3-Clause" __licence__ = "BSD-3-Clause"
__version__ = "1.4.1" __version__ = "1.4.2"
__build__ = "2023012001" __build__ = "2023012801"
__description__ = ( __description__ = (
"Converts restic command line output to a text file node_exporter can scrape" "Converts restic command line output to a text file node_exporter can scrape"
) )
@ -70,7 +70,7 @@ def restic_output_2_metrics(restic_result, output, labels=None):
for line in output.splitlines(): for line in output.splitlines():
# for line in output: # for line in output:
matches = re.match( matches = re.match(
"Files:\s+(\d+)\snew,\s+(\d+)\schanged,\s+(\d+)\sunmodified", r"Files:\s+(\d+)\snew,\s+(\d+)\schanged,\s+(\d+)\sunmodified",
line, line,
re.IGNORECASE, re.IGNORECASE,
) )
@ -96,7 +96,7 @@ def restic_output_2_metrics(restic_result, output, labels=None):
errors = True errors = True
matches = re.match( matches = re.match(
"Dirs:\s+(\d+)\snew,\s+(\d+)\schanged,\s+(\d+)\sunmodified", r"Dirs:\s+(\d+)\snew,\s+(\d+)\schanged,\s+(\d+)\sunmodified",
line, line,
re.IGNORECASE, re.IGNORECASE,
) )
@ -122,7 +122,7 @@ def restic_output_2_metrics(restic_result, output, labels=None):
errors = True errors = True
matches = re.match( matches = re.match(
"Added to the repo.*:\s([-+]?(?:\d*\.\d+|\d+))\s(\w+)\s+\((.*)\sstored\)", r"Added to the repo.*:\s([-+]?(?:\d*\.\d+|\d+))\s(\w+)\s+\((.*)\sstored\)",
line, line,
re.IGNORECASE, re.IGNORECASE,
) )
@ -160,7 +160,7 @@ def restic_output_2_metrics(restic_result, output, labels=None):
errors = True errors = True
matches = re.match( matches = re.match(
"processed\s(\d+)\sfiles,\s([-+]?(?:\d*\.\d+|\d+))\s(\w+)\sin\s((\d+:\d+:\d+)|(\d+:\d+)|(\d+))", r"processed\s(\d+)\sfiles,\s([-+]?(?:\d*\.\d+|\d+))\s(\w+)\sin\s((\d+:\d+:\d+)|(\d+:\d+)|(\d+))",
line, line,
re.IGNORECASE, re.IGNORECASE,
) )
@ -199,14 +199,14 @@ def restic_output_2_metrics(restic_result, output, labels=None):
logger.warning("Cannot parse restic log for repo size: {}".format(exc)) logger.warning("Cannot parse restic log for repo size: {}".format(exc))
errors = True errors = True
matches = re.match( matches = re.match(
"Failure|Fatal|Unauthorized|no such host|s there a repository at the following location\?", r"Failure|Fatal|Unauthorized|no such host|s there a repository at the following location\?",
line, line,
re.IGNORECASE, re.IGNORECASE,
) )
if matches: if matches:
try: try:
logger.debug( logger.debug(
'Matcher found error: "{}\ in line "{}".'.format( 'Matcher found error: "{}" in line "{}".'.format(
matches.group(), line matches.group(), line
) )
) )
@ -234,7 +234,7 @@ def upload_metrics(destination, authentication, metrics):
data += "{}\n".format(metric) data += "{}\n".format(metric)
logger.debug("metrics:\n{}".format(data)) logger.debug("metrics:\n{}".format(data))
result = requests.post( result = requests.post(
destination, headers=headers, data=data, auth=authentication destination, headers=headers, data=data, auth=authentication, timeout=4
) )
if result.status_code == 200: if result.status_code == 200:
logger.info("Metrics pushed succesfully.") logger.info("Metrics pushed succesfully.")
@ -248,7 +248,7 @@ def upload_metrics(destination, authentication, metrics):
def write_metrics_file(metrics, filename): def write_metrics_file(metrics, filename):
with open(filename, "w") as file_handle: with open(filename, "w", encoding='utf-8') as file_handle:
for metric in metrics: for metric in metrics:
file_handle.write(metric + "\n") file_handle.write(metric + "\n")
@ -323,7 +323,7 @@ if __name__ == "__main__":
labels += ",{}".format(labels) labels += ",{}".format(labels)
destination_file = os.path.join(destination_dir, output_filename) destination_file = os.path.join(destination_dir, output_filename)
try: try:
with open(log_file, "r") as file_handle: with open(log_file, "r", encoding='utf-8') as file_handle:
errors, metrics = restic_output_2_metrics( errors, metrics = restic_output_2_metrics(
True, output=file_handle.readlines(), labels=labels True, output=file_handle.readlines(), labels=labels
) )