Add outout file

This commit is contained in:
Antonio J. Delgado 2024-11-03 14:58:18 +02:00
parent bffdd74e47
commit 88d7fcd7df

View file

@ -51,7 +51,7 @@ class ResticExporter:
else: else:
self._read_summary_from_systemd() self._read_summary_from_systemd()
self._show_metrics() self._write_metrics()
def _read_summary_from_systemd(self,): def _read_summary_from_systemd(self,):
self.summaries = () self.summaries = ()
@ -67,7 +67,7 @@ class ResticExporter:
'').replace('.service', '') '').replace('.service', '')
self.summaries.append(summary) self.summaries.append(summary)
def _show_metrics(self): def _write_metrics(self):
for summary in self.summaries: for summary in self.summaries:
labels = self._convert_labels(self.labels) labels = self._convert_labels(self.labels)
counters = [ counters = [
@ -84,14 +84,19 @@ class ResticExporter:
'total_bytes_processed', 'total_bytes_processed',
'total_duration' 'total_duration'
] ]
print( if self.config['output_file'] != "-":
f"# HELP {summary['metric_name']}_{summary['job_name']} {self.metric_description}." outfile = open(self.config['output_file'], 'w', encoding='utf-8')
else:
outfile = sys.stdout
outfile.write(
f"# HELP {summary['metric_name']}_{summary['job_name']} \
{self.metric_description}.\n"
) )
print(f"# TYPE {summary['metric_name']}_{summary['job_name']} counter") outfile.write(f"# TYPE {summary['metric_name']}_{summary['job_name']} counter\n")
for counter in counters: for counter in counters:
if counter in summary: if counter in summary:
print( outfile.write(
f"{summary['metric_name']}_{counter}{labels} {float(summary[counter])}" f"{summary['metric_name']}_{counter}{labels} {float(summary[counter])}\n"
) )
def _read_summary_from_json(self, json_file): def _read_summary_from_json(self, json_file):
@ -99,7 +104,11 @@ class ResticExporter:
with open(json_file, 'r', encoding='utf-8') as file_pointer: with open(json_file, 'r', encoding='utf-8') as file_pointer:
content = file_pointer.readlines() content = file_pointer.readlines()
except Exception as error: except Exception as error:
self._log.error(f"# Error reading file '{json_file}'. Check permissions. {error}") self._log.error(
"# Error reading file '%s'. Check permissions. %s",
json_file,
error
)
summary = { summary = {
"timestamp": time.time(), "timestamp": time.time(),
@ -116,20 +125,31 @@ class ResticExporter:
self.labels["snapshot_id"] = line_data['snapshot_id'] self.labels["snapshot_id"] = line_data['snapshot_id']
except json.decoder.JSONDecodeError as error: except json.decoder.JSONDecodeError as error:
fixed_line = line.replace('\n', '') fixed_line = line.replace('\n', '')
self._log.error(f"# Error decoding line '{fixed_line}'. {error}") self._log.error(
"# Error decoding line '%s'. %s",
fixed_line,
error
)
file_stats = os.stat(json_file) file_stats = os.stat(json_file)
summary['timestamp'] = round(file_stats.st_mtime * 1000) summary['timestamp'] = round(file_stats.st_mtime * 1000)
self._log.debug(f"# Summary: {json.dumps(summary, indent=2)}") self._log.debug("# Summary: {json.dumps(summary, indent=2)}")
self._log.debug(f"# Labels: {self.labels}") self._log.debug(
"# Labels: %s",
self.labels
)
self.summaries = [ summary ] self.summaries = [ summary ]
def _read_extra_labels(self, extra_labels): def _read_extra_labels(self, extra_labels):
labels_ls = {} labels_ls = {}
for pair in extra_labels.split(','): for pair in extra_labels.split(','):
if '=' in pair: if '=' in pair:
k, v=pair.split('=', 1) key, value=pair.split('=', 1)
labels_ls[k] = v labels_ls[key] = value
self._log.debug(f"# Added extra label '{k}'='{v}'") self._log.debug(
"# Added extra label '%s'='%s'",
key,
value
)
return labels_ls return labels_ls
def _convert_labels(self, labels): def _convert_labels(self, labels):
@ -138,9 +158,11 @@ class ResticExporter:
labels_ls.append(f"{key}=\"{labels[key]}\"") labels_ls.append(f"{key}=\"{labels[key]}\"")
text_labels = ','.join(labels_ls) text_labels = ','.join(labels_ls)
labels_string = "{" + text_labels + "}" labels_string = "{" + text_labels + "}"
self._log.debug(f"# Labels: {labels_string}") self._log.debug(
"# Labels: %s",
labels_string
)
return labels_string return labels_string
def _init_log(self): def _init_log(self):
''' Initialize log object ''' ''' Initialize log object '''
@ -180,7 +202,7 @@ class ResticExporter:
case_sensitive=False, case_sensitive=False,
), help='Set the debug level for the standard output.') ), help='Set the debug level for the standard output.')
@click.option('--log-file', '-l', help="File to store all debug messages.") @click.option('--log-file', '-l', help="File to store all debug messages.")
@click.option("--json-file", "-j", required=True, help='JSON file containing the output of restic') @click.option("--json-file", "-j", help='JSON file containing the output of restic')
@click.option( @click.option(
'--job-name', '-n', '--job-name', '-n',
required=True, required=True,
@ -207,6 +229,13 @@ class ResticExporter:
default=False, default=False,
help='Get JSON data from Systemd units', help='Get JSON data from Systemd units',
) )
@click.option(
'--output-file',
'-o',
default='-',
help='File to write the metrics so node-exporter can read it. Use "-" \
to write to standard output (default).'
)
@click_config_file.configuration_option() @click_config_file.configuration_option()
def __main__(**kwargs): def __main__(**kwargs):
return ResticExporter(**kwargs) return ResticExporter(**kwargs)