diff --git a/restic_exporter/restic_exporter.py b/restic_exporter/restic_exporter.py index d42a180..04013bf 100644 --- a/restic_exporter/restic_exporter.py +++ b/restic_exporter/restic_exporter.py @@ -51,7 +51,7 @@ class ResticExporter: else: self._read_summary_from_systemd() - self._show_metrics() + self._write_metrics() def _read_summary_from_systemd(self,): self.summaries = () @@ -67,7 +67,7 @@ class ResticExporter: '').replace('.service', '') self.summaries.append(summary) - def _show_metrics(self): + def _write_metrics(self): for summary in self.summaries: labels = self._convert_labels(self.labels) counters = [ @@ -84,14 +84,19 @@ class ResticExporter: 'total_bytes_processed', 'total_duration' ] - print( - f"# HELP {summary['metric_name']}_{summary['job_name']} {self.metric_description}." + if self.config['output_file'] != "-": + 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: if counter in summary: - print( - f"{summary['metric_name']}_{counter}{labels} {float(summary[counter])}" + outfile.write( + f"{summary['metric_name']}_{counter}{labels} {float(summary[counter])}\n" ) 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: content = file_pointer.readlines() 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 = { "timestamp": time.time(), @@ -116,20 +125,31 @@ class ResticExporter: self.labels["snapshot_id"] = line_data['snapshot_id'] except json.decoder.JSONDecodeError as error: 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) summary['timestamp'] = round(file_stats.st_mtime * 1000) - self._log.debug(f"# Summary: {json.dumps(summary, indent=2)}") - self._log.debug(f"# Labels: {self.labels}") + self._log.debug("# Summary: {json.dumps(summary, indent=2)}") + self._log.debug( + "# Labels: %s", + self.labels + ) self.summaries = [ summary ] def _read_extra_labels(self, extra_labels): labels_ls = {} for pair in extra_labels.split(','): if '=' in pair: - k, v=pair.split('=', 1) - labels_ls[k] = v - self._log.debug(f"# Added extra label '{k}'='{v}'") + key, value=pair.split('=', 1) + labels_ls[key] = value + self._log.debug( + "# Added extra label '%s'='%s'", + key, + value + ) return labels_ls def _convert_labels(self, labels): @@ -138,9 +158,11 @@ class ResticExporter: labels_ls.append(f"{key}=\"{labels[key]}\"") text_labels = ','.join(labels_ls) labels_string = "{" + text_labels + "}" - self._log.debug(f"# Labels: {labels_string}") + self._log.debug( + "# Labels: %s", + labels_string + ) return labels_string - def _init_log(self): ''' Initialize log object ''' @@ -180,7 +202,7 @@ class ResticExporter: case_sensitive=False, ), help='Set the debug level for the standard output.') @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( '--job-name', '-n', required=True, @@ -207,6 +229,13 @@ class ResticExporter: default=False, 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() def __main__(**kwargs): return ResticExporter(**kwargs)