| 1 | package Mail::TLSRPT::App::Command::prometheus; | ||
| 2 | # ABSTRACT: Process tlsrpt reports into prometheus | ||
| 3 | our $VERSION = '2.20261112'; # VERSION | ||
| 4 | use 5.20.0; | ||
| 5 | use Mail::TLSRPT::Pragmas; | ||
| 6 | use Mail::TLSRPT::App -command; | ||
| 7 | use Mail::TLSRPT::Report; | ||
| 8 | |||
| 9 | |||
| 10 | sub abstract { 'Process tlsrpt files into prometheus' } | ||
| 11 | sub description { 'Process tlsrpt files and output as prometheus' }; | ||
| 12 | sub usage_desc { "%c prometheus %o <FILE> <FILE> <FILE>" } | ||
| 13 | |||
| 14 | sub opt_spec { | ||
| 15 | return ( | ||
| 16 | [ 'shared=s', 'Use the supplied Prometheus::Tiny::Shared store' ], | ||
| 17 | [ 'output=s', 'Write results to filename (defaults to STDOUT)' ], | ||
| 18 | ); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub validate_args($self,$opt,$args) { | ||
| 22 | $self->usage_error ('Shared file not specified') if !$opt->{shared}; | ||
| 23 | # $self->usage_error('No files specified') if !@$args; # If no files are specified then we will output metrics from the shared file | ||
| 24 | } | ||
| 25 | |||
| 26 | sub execute($self,$opt,$args) { | ||
| 27 | |||
| 28 | my $tlsrpt; | ||
| 29 | |||
| 30 | eval { | ||
| 31 | require Prometheus::Tiny::Shared; | ||
| 32 | Prometheus::Tiny::Shared->VERSION(0.020) || die;; | ||
| 33 | }; | ||
| 34 | $self->usage_error('Prometheus::Tiny::Shared 0.020 not found') if $@; | ||
| 35 | |||
| 36 | my $prometheus = Prometheus::Tiny::Shared->new(filename=>$opt->shared); | ||
| 37 | |||
| 38 | foreach my $file ( $args->@* ) { | ||
| 39 | |||
| 40 | $self->usage_error("File $file does not exist") if ! -e $file; | ||
| 41 | |||
| 42 | open my $fh, '<', $file or die 'Could not open input file'; | ||
| 43 | my @file_contents = <$fh>; | ||
| 44 | close $fh; | ||
| 45 | my $payload = join('',@file_contents); | ||
| 46 | |||
| 47 | $tlsrpt = eval{ Mail::TLSRPT::Report->new_from_json($payload) }; | ||
| 48 | my $error = $@; | ||
| 49 | if ( $tlsrpt ) { | ||
| 50 | $tlsrpt->process_prometheus($prometheus); | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | warn "Could not parse file $file: $error"; | ||
| 54 | } | ||
| 55 | |||
| 56 | } | ||
| 57 | |||
| 58 | if ( $opt->output ) { | ||
| 59 | open my $fh, '>:encoding(UTF-8)', $opt->output or die 'Could not open output file'; | ||
| 60 | print $fh $prometheus->format; | ||
| 61 | close $fh; | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | say $prometheus->format; | ||
| 65 | } | ||
| 66 | |||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ |