« Authentication/App/Blocker/App/Command/delete.pm
↑ Summary
Authentication/Tester/HandlerTester.pm »
| 1 | package Mail::Milter::Authentication::Metric; | ||||||||||||||||||||||||||||
| 2 | 143 | ▶ | use 5.20.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 143 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 143 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 143 | ▶ | use Mail::Milter::Authentication::Pragmas; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Class for metrics generation | ||||||||||||||||||||||||||||
| 7 | 143 | our $VERSION = '4.20261116'; # VERSION | |||||||||||||||||||||||||||
| 8 | 143 | ▶ | use Mail::Milter::Authentication; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 143 | ▶ | use Mail::Milter::Authentication::HTDocs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 143 | ▶ | use Mail::Milter::Authentication::Metric::Grafana; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 143 | ▶ | use File::Temp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 143 | ▶ | use Prometheus::Tiny::Shared 0.020; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 143 | ▶ | use TOML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 143 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 143 | my ( $class, $thischild ) = @_; | |||||||||||||||||||||||||||
| 20 | 143 | my $self = {}; | |||||||||||||||||||||||||||
| 21 | 143 | $self->{counter} = {}; | |||||||||||||||||||||||||||
| 22 | 143 | $self->{help} = {}; | |||||||||||||||||||||||||||
| 23 | 143 | $self->{start_time} = time; | |||||||||||||||||||||||||||
| 24 | 143 | $self->{registered_metrics} = []; | |||||||||||||||||||||||||||
| 25 | 143 | $self->{thischild} = $thischild; | |||||||||||||||||||||||||||
| 26 | 143 | bless $self, $class; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 143 | $self->set_handler( undef ); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 143 | my $config = get_config(); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | $self->{enabled} = defined( $config->{metric_port} ) ? 1 | ||||||||||||||||||||||||||||
| 33 | 143 | ▶ | : defined( $config->{metric_connection} ) ? 1 | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 34 | : 0; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 143 | my $metric_tempfile; | |||||||||||||||||||||||||||
| 37 | 143 | ▶ | if ( defined( $config->{metric_tempfile} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | $metric_tempfile = $config->{metric_tempfile}; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 143 | ▶ | if ( ! $metric_tempfile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 143 | $metric_tempfile = $config->{lib_dir}.'/metrics'; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # If metric_tempfile is a regular file then we need to re-init with a directory | ||||||||||||||||||||||||||||
| 45 | # this is likely a restart after upgrade. | ||||||||||||||||||||||||||||
| 46 | 143 | ▶ | if ( -f $metric_tempfile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | unlink $metric_tempfile; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 143 | ▶ | if ( ! -d $metric_tempfile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 5 | mkdir $metric_tempfile, 0700; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 143 | $self->dbgout( 'Metrics', "Setup new metrics file $metric_tempfile", LOG_DEBUG ); | |||||||||||||||||||||||||||
| 54 | 143 | my $prom = eval{ Prometheus::Tiny::Shared->new(filename => $metric_tempfile.'/authmilter_metrics', init_file => 1) }; | |||||||||||||||||||||||||||
| 55 | 143 | $self->handle_exception($@); | |||||||||||||||||||||||||||
| 56 | 143 | ▶ | if ( $prom ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 143 | $self->{prom} = $prom; | |||||||||||||||||||||||||||
| 58 | 143 | $self->{metric_tempfile} = $metric_tempfile; | |||||||||||||||||||||||||||
| 59 | 143 | $prom->declare( 'authmilter_uptime_seconds_total', help => 'Number of seconds since server startup', type => 'counter' ); | |||||||||||||||||||||||||||
| 60 | 143 | $prom->declare( 'authmilter_processes_waiting', help => 'The number of authentication milter processes in a waiting state', type => 'gauge' ); | |||||||||||||||||||||||||||
| 61 | 143 | $prom->declare( 'authmilter_processes_processing', help => 'The number of authentication milter processes currently processing data', type => 'gauge' ); | |||||||||||||||||||||||||||
| 62 | 143 | $prom->declare( 'authmilter_version', help => 'Running versions', type => 'gauge' ); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||
| 65 | 0 | $self->dbgout( 'Metrics', "Failed to setup new metrics file $metric_tempfile", LOG_ERR ); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 143 | return $self; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 16753 | ▶ | sub set_handler { | ||||||||||||||||||||||||||
|
Subroutine
set_handler: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 16753 | my ( $self, $handler ) = @_; | |||||||||||||||||||||||||||
| 74 | 16753 | $self->{handler} = $handler; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4837 | ▶ | sub handle_exception { | ||||||||||||||||||||||||||
|
Subroutine
handle_exception: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 4837 | my ( $self, $exception ) = @_; | |||||||||||||||||||||||||||
| 80 | 4837 | ▶ | return if ! defined $exception; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 4837 | ▶ | return if ! defined $self->{handler}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 4483 | $self->{handler}->handle_exception($exception); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 4650 | ▶ | sub dbgout { | ||||||||||||||||||||||||||
|
Subroutine
dbgout: called CC=5 SCAR=21.7
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 4650 | my ( $self, $key, $value, $priority ) = @_; | |||||||||||||||||||||||||||
| 88 | 4650 | ▶ | if ( defined ( $self->{handler} ) ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 89 | 4483 | $self->{handler}->dbgout($key,$value,$priority); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | elsif ( $priority == LOG_DEBUG ) { | ||||||||||||||||||||||||||||
| 92 | 167 | $self->{thischild}->logdebug( "$key: $value" ); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | ▶ | elsif ( $priority == LOG_INFO || $priority == LOG_NOTICE ) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$priority == 6 or $priority == 5
MC/DC0%0/2
$priority == 6 or $priority == 5
$priority == 6
$priority == 5
Truth table0%0/3
A or B
A= $priority == 6B= $priority == 5
| |||||||||||||||||||||||||||||
| 95 | 0 | $self->{thischild}->loginfo( "$key: $value" ); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 0 | $self->{thischild}->logerror( "$key: $value" ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 13702 | ▶ | sub prom { | ||||||||||||||||||||||||||
|
Subroutine
prom: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 13702 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 105 | 13702 | return $self->{prom}; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 24 | ▶ | sub set_versions { | ||||||||||||||||||||||||||
|
Subroutine
set_versions: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 24 | my ( $self, $server ) = @_; | |||||||||||||||||||||||||||
| 111 | 24 | ▶ | return if ! $self->{enabled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 24 | ▶ | return if ! $self->prom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 24 | $self->dbgout( 'Metrics', "Setting up versioning metrics", LOG_DEBUG ); | |||||||||||||||||||||||||||
| 114 | 24 | $self->prom->set( 'authmilter_version', 1, { version => $Mail::Milter::Authentication::VERSION, module => 'core', ident => $self->clean_label( $Mail::Milter::Authentication::Config::IDENT ) }); | |||||||||||||||||||||||||||
| 115 | 24 | foreach my $Handler ( sort keys %{ $server->{handler} } ) { | |||||||||||||||||||||||||||
| 116 | 235 | ▶ | next if $Handler eq '_Handler'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 211 | eval{ $self->prom->set( 'authmilter_version', 1, { version => $server->{handler}->{ $Handler }->get_version(), module => $Handler, ident => $self->clean_label( $Mail::Milter::Authentication::Config::IDENT ) }) }; | |||||||||||||||||||||||||||
| 118 | 211 | $self->handle_exception($@); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 29 | ▶ | sub get_timeout { | ||||||||||||||||||||||||||
|
Subroutine
get_timeout: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 29 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 126 | 29 | my $config = get_config(); | |||||||||||||||||||||||||||
| 127 | 29 | ▶ | return $config->{metric_timeout} || 5; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'metric_timeout'} || 5
MC/DC100%1/1
$config->{'metric_timeout'} || 5
$config->{'metric_timeout'}
Truth table100%2/2
A
A= $config->{'metric_timeout'}
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 28600 | ▶ | sub clean_label { | ||||||||||||||||||||||||||
|
Subroutine
clean_label: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 28600 | my ( $self, $text ) = @_; | |||||||||||||||||||||||||||
| 133 | 28600 | $text = lc $text; | |||||||||||||||||||||||||||
| 134 | 28600 | $text =~ s/[^a-z0-9]/_/g; | |||||||||||||||||||||||||||
| 135 | 28600 | ▶ | if ( $text eq q{} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 71 | $text = '-none-'; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 28600 | return $text; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 8290 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 8290 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 144 | 8290 | ▶ | return if ! $self->{enabled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 4480 | ▶ | return if ! $self->prom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4480 | my $count_id = $args->{count_id}; | |||||||||||||||||||||||||||
| 148 | 4480 | my $labels = $args->{labels}; | |||||||||||||||||||||||||||
| 149 | 4480 | my $server = $args->{server}; | |||||||||||||||||||||||||||
| 150 | 4480 | my $count = $args->{count}; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 4480 | ▶ | $count = 1 if ! defined $count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 4480 | $count_id = $self->clean_label( $count_id ); | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 4480 | my $clean_labels = {}; | |||||||||||||||||||||||||||
| 157 | 4480 | ▶ | if ( $labels ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 4480 | foreach my $l ( sort keys %$labels ) { | |||||||||||||||||||||||||||
| 159 | 8852 | $clean_labels->{ $self->clean_label( $l ) } = $self->clean_label( $labels->{$l} ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 4480 | $clean_labels->{ident} = $self->clean_label( $Mail::Milter::Authentication::Config::IDENT ); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 4480 | $self->dbgout( 'Metrics', "Counting $count_id:$count:".join(',',map {"$_=".$clean_labels->{$_}} (sort keys %$clean_labels) ), LOG_DEBUG ); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 4480 | eval{ $self->prom->add( 'authmilter_' . $count_id, $count, $clean_labels ); }; | |||||||||||||||||||||||||||
| 168 | 4480 | $self->handle_exception($@); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 15 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | 15 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 174 | 15 | ▶ | return if ! $self->{enabled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 3 | ▶ | return if ! $self->prom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 3 | my $gauge_id = $args->{gauge_id}; | |||||||||||||||||||||||||||
| 178 | 3 | my $labels = $args->{labels}; | |||||||||||||||||||||||||||
| 179 | 3 | my $server = $args->{server}; | |||||||||||||||||||||||||||
| 180 | 3 | my $value = $args->{value}; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 3 | ▶ | die 'metric set must define value' if ! defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 3 | $gauge_id = $self->clean_label( $gauge_id ); | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 3 | my $clean_labels = {}; | |||||||||||||||||||||||||||
| 187 | 3 | ▶ | if ( $labels ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 3 | foreach my $l ( sort keys %$labels ) { | |||||||||||||||||||||||||||
| 189 | 6 | $clean_labels->{ $self->clean_label( $l ) } = $self->clean_label( $labels->{$l} ); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 3 | $clean_labels->{ident} = $self->clean_label( $Mail::Milter::Authentication::Config::IDENT ); | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 3 | $self->dbgout( 'Metrics', "Setting $gauge_id:$value:".join(',',map {"$_=".$clean_labels->{$_}} (sort keys %$clean_labels) ), LOG_DEBUG ); | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 3 | eval{ $self->prom->set( 'authmilter_' . $gauge_id, $value, $clean_labels ); }; | |||||||||||||||||||||||||||
| 198 | 3 | $self->handle_exception($@); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | sub send { ## no critic | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 203 | 0 | my ( $self, $server ) = @_; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 661 | ▶ | sub register_metrics { | ||||||||||||||||||||||||||
|
Subroutine
register_metrics: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 208 | 661 | my ( $self, $hash ) = @_; | |||||||||||||||||||||||||||
| 209 | 661 | ▶ | return if ! $self->{enabled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 415 | ▶ | return if ! $self->prom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 415 | push @{$self->{registered_metrics}}, $hash; | |||||||||||||||||||||||||||
| 212 | 415 | $self->_register_metrics( $hash ); | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 37 | ▶ | sub re_register_metrics { | ||||||||||||||||||||||||||
|
Subroutine
re_register_metrics: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 217 | 37 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 218 | 37 | ▶ | return if ! $self->{enabled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 37 | ▶ | return if ! $self->prom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 37 | foreach my $metric ( @{$self->{registered_metrics}} ) { | |||||||||||||||||||||||||||
| 221 | 423 | $self->_register_metrics( $metric ); | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 838 | ▶ | sub _register_metrics { | ||||||||||||||||||||||||||
|
Subroutine
_register_metrics: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 226 | 838 | my ( $self, $hash ) = @_; | |||||||||||||||||||||||||||
| 227 | 838 | ▶ | return if ! $self->{enabled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 838 | ▶ | return if ! $self->prom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 838 | foreach my $metric ( keys %$hash ) { | |||||||||||||||||||||||||||
| 231 | 1270 | my $data = $hash->{ $metric }; | |||||||||||||||||||||||||||
| 232 | 1270 | my $help; | |||||||||||||||||||||||||||
| 233 | 1270 | my $type = 'counter'; | |||||||||||||||||||||||||||
| 234 | 1270 | ▶ | if ( ref $data eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 134 | $help = $data->{help}; | |||||||||||||||||||||||||||
| 236 | 134 | $type = $data->{type}; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | else { | ||||||||||||||||||||||||||||
| 239 | 1136 | $help = $data; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 1270 | $self->prom->declare( 'authmilter_' . $metric, help => $help, type => $type); | |||||||||||||||||||||||||||
| 242 | 1270 | $self->prom->add( 'authmilter_' . $metric,0, { ident => $self->clean_label( $Mail::Milter::Authentication::Config::IDENT ) }); | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 201 | ▶ | sub parent_metric_update { | ||||||||||||||||||||||||||
|
Subroutine
parent_metric_update: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 248 | 201 | my ( $self, $server ) = @_; | |||||||||||||||||||||||||||
| 249 | 201 | ▶ | return if ! $self->{enabled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 201 | ▶ | return if ! $self->prom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 201 | eval { | |||||||||||||||||||||||||||
| 253 | 201 | foreach my $type ( qw { waiting processing } ) { | |||||||||||||||||||||||||||
| 254 | 402 | $self->prom->set('authmilter_processes_' . $type, $server->{server}->{tally}->{$type}, { ident => $self->clean_label( $Mail::Milter::Authentication::Config::IDENT ) }); | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | }; | ||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 14 | ▶ | sub is_config_http_enabled { | ||||||||||||||||||||||||||
|
Subroutine
is_config_http_enabled: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 261 | 14 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 262 | 14 | my $config = get_config(); | |||||||||||||||||||||||||||
| 263 | 14 | ▶ | return 0 if $config->{'metric_basic_http'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 10 | ▶ | return $config->{'metric_config_http'} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 29 | ▶ | sub child_handler { | ||||||||||||||||||||||||||
|
Subroutine
child_handler: called CC=26 SCAR=33.6
Pod
documented
| |||||||||||||||||||||||||||||
| 269 | 29 | my ( $self, $server ) = @_; | |||||||||||||||||||||||||||
| 270 | 29 | ▶ | return if ! $self->{enabled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 29 | my $config = get_config(); | |||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 29 | eval { | |||||||||||||||||||||||||||
| 275 | 0 | ▶ | local $SIG{ALRM} = sub{ die "Timeout\n" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 276 | 29 | alarm( $self->get_timeout() ); | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 29 | my $socket = $server->{server}->{client}; | |||||||||||||||||||||||||||
| 279 | 29 | my $req; | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 29 | $PROGRAM_NAME = $Mail::Milter::Authentication::Config::IDENT . ':metrics'; | |||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 29 | $req = <$socket>; | |||||||||||||||||||||||||||
| 284 | 29 | $req =~ s/[\n\r]+$//; | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 29 | ▶ | if (!defined($req) || $req !~ m{ ^\s*(GET|POST|PUT|DELETE|PUSH|HEAD|OPTIONS)\s+(.+)\s+(HTTP/1\.[01])\s*$ }ix) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $req and $req =~ m[ ^\s*(GET|POST|PUT|DELETE|PUSH|HEAD|OPTIONS)\s+(.+)\s+(HTTP/1\.[01])\s*$ ]iux
MC/DC100%2/2
defined $req and $req =~ m[ ^\s*(GET|POST|PUT|DELETE|PUSH|HEAD|OPTIONS)\s+(.+)\s+(HTTP/1\.[01])\s*$ ]iux
defined $req
$req =~ m[ ^\s*(GET|POST|PUT|DELETE|PUSH|HEAD|OPTIONS)\s+(.+)\s+(HTTP/1\.[01])\s*$ ]iux
Truth table100%3/3
A and B
A= defined $reqB= $req =~ m[ ^\s*(GET|POST|PUT|DELETE|PUSH|HEAD|OPTIONS)\s+(.+)\s+(HTTP/1\.[01])\s*$ ]iux
| |||||||||||||||||||||||||||||
| 287 | 2 | print $socket "HTTP/1.0 500 Server Error\n"; | |||||||||||||||||||||||||||
| 288 | 2 | print $socket "\n"; | |||||||||||||||||||||||||||
| 289 | 2 | print $socket "Invalid Request Error\n"; | |||||||||||||||||||||||||||
| 290 | 2 | return; | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 27 | my $request_method = uc $1; | |||||||||||||||||||||||||||
| 294 | 27 | my $request_uri = $2; | |||||||||||||||||||||||||||
| 295 | 27 | my $server_protocol = $3; | |||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 27 | ▶ | if ( $request_method ne 'GET' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 1 | print $socket "HTTP/1.0 500 Server Error\n"; | |||||||||||||||||||||||||||
| 299 | 1 | print $socket "\n"; | |||||||||||||||||||||||||||
| 300 | 1 | print $socket "Server Error\n"; | |||||||||||||||||||||||||||
| 301 | 1 | return; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | # Ignore the rest of the HTTP request | ||||||||||||||||||||||||||||
| 305 | 26 | ▶ | while ( $req = <$socket> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 26 | $req =~ s/[\n\r]+$//; | |||||||||||||||||||||||||||
| 307 | 26 | ▶ | last if $req eq q{}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 26 | ▶ | if ( $request_uri eq '/metrics' ) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 311 | 11 | ▶ | if ( $self->prom ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 11 | $server->{handler}->{_Handler}->top_metrics_callback(); | |||||||||||||||||||||||||||
| 313 | 11 | $self->prom->set( 'authmilter_uptime_seconds_total', time - $self->{start_time}, { ident => $self->clean_label( $Mail::Milter::Authentication::Config::IDENT ) }); | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 11 | print $socket "HTTP/1.0 200 OK\n"; | |||||||||||||||||||||||||||
| 317 | 11 | print $socket "Content-Type: text/plain\n"; | |||||||||||||||||||||||||||
| 318 | 11 | print $socket "\n"; | |||||||||||||||||||||||||||
| 319 | 11 | ▶ | if ( $self->prom ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | 11 | print $socket $self->prom->format(); | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | else { | ||||||||||||||||||||||||||||
| 323 | 0 | print $socket '# Metrics unavailable'; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | elsif ( $request_uri eq '/' ){ | ||||||||||||||||||||||||||||
| 328 | 5 | my $config = get_config(); | |||||||||||||||||||||||||||
| 329 | 5 | print $socket "HTTP/1.0 200 OK\n"; | |||||||||||||||||||||||||||
| 330 | 5 | print $socket "Content-Type: text/html\n"; | |||||||||||||||||||||||||||
| 331 | 5 | print $socket "\n"; | |||||||||||||||||||||||||||
| 332 | 5 | print $socket qq{ | |||||||||||||||||||||||||||
| 333 | <html> | ||||||||||||||||||||||||||||
| 334 | <head> | ||||||||||||||||||||||||||||
| 335 | <title>Authentication Milter</title> | ||||||||||||||||||||||||||||
| 336 | <link rel="stylesheet" href="/css/normalize.css" /> | ||||||||||||||||||||||||||||
| 337 | <link rel="stylesheet" href="/css/skeleton.css" /> | ||||||||||||||||||||||||||||
| 338 | <link rel="stylesheet" href="/css/authmilter.css" /> | ||||||||||||||||||||||||||||
| 339 | </head> | ||||||||||||||||||||||||||||
| 340 | <body> | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | <div class="container"> | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | <h1>Authentication Milter</h1> | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | <span class="versionBlock">Version: } . $Mail::Milter::Authentication::VERSION . qq{<br />Ident: } . $Mail::Milter::Authentication::Config::IDENT . qq{</span> | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | <h2>Installed Handlers</h2> | ||||||||||||||||||||||||||||
| 349 | <div class="spaceAfter">}; | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 5 | foreach my $Handler ( sort keys %{ $server->{handler} } ) { | |||||||||||||||||||||||||||
| 352 | 5 | ▶ | next if $Handler eq '_Handler'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | 0 | print $socket ' <span class="handler">' . $Handler . ' (' . $server->{handler}->{ $Handler }->get_version(). ')</span> '; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 5 | print $socket qq{ | |||||||||||||||||||||||||||
| 357 | </div> | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | <h2>Registered Callbacks</h2> | ||||||||||||||||||||||||||||
| 360 | <table class="callbacksTable">}; | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 5 | foreach my $stage ( qw{ setup connect helo envfrom envrcpt header eoh body eom abort close addheader } ) { | |||||||||||||||||||||||||||
| 363 | 60 | my $callbacks = $server->{handler}->{_Handler}->get_callbacks( $stage ); | |||||||||||||||||||||||||||
| 364 | 0 | print $socket "<tr><td>$stage</td><td>" . join( ' ', map{ "<span class=\"handler\">$_</span>" } @$callbacks ) . "</td></tr>"; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 5 | print $socket qq{</table> | |||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | <h2>Details</h2> | ||||||||||||||||||||||||||||
| 370 | <ul>}; | ||||||||||||||||||||||||||||
| 371 | 5 | print $socket '<li>Protocol: ' . $config->{protocol} . '</li>'; | |||||||||||||||||||||||||||
| 372 | # Copy, so the default connection is not added to the shared config | ||||||||||||||||||||||||||||
| 373 | 5 | ▶ | my %connections = %{ $config->{connections} // {} }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"connections"} // {}
MC/DC100%1/1
$config->{"connections"} // {}
$config->{"connections"}
Truth table100%2/2
A
A= $config->{"connections"}
| |||||||||||||||||||||||||||||
| 374 | 5 | $connections{default} = { connection => $config->{connection} }; | |||||||||||||||||||||||||||
| 375 | 5 | foreach my $connection ( sort keys %connections ) { | |||||||||||||||||||||||||||
| 376 | 9 | print $socket '<li>' . $connection . ': ' . $connections{ $connection }->{connection} . '</li>' | |||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | 5 | ▶ | print $socket qq{<li>Effective config (<a href="/config/toml">toml</a>/<a href="/config/json">json</a>)</li>} if $self->is_config_http_enabled(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | 5 | print $socket qq{ | |||||||||||||||||||||||||||
| 380 | </ul> | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | <h2>Metrics</h2> | ||||||||||||||||||||||||||||
| 383 | <ul> | ||||||||||||||||||||||||||||
| 384 | <li><a href="/metrics">Prometheus metrics endpoint</a></li> | ||||||||||||||||||||||||||||
| 385 | }; | ||||||||||||||||||||||||||||
| 386 | 5 | ▶ | print $socket qq{<li>Example <a href="/grafana">Grafana dashboard</a> for this setup</li>} if !$config->{'metric_basic_http'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | 5 | print $socket qq{ | |||||||||||||||||||||||||||
| 388 | </ul> | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | <hr /> | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | </div> | ||||||||||||||||||||||||||||
| 393 | </body> | ||||||||||||||||||||||||||||
| 394 | }; | ||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | ▶ | elsif ( $request_uri eq '/config/json' || $request_uri eq '/config' ) { | |||||||||||||||||||||||||||
|
Condition100%3/3
$request_uri eq "/config/json" or $request_uri eq "/config"
MC/DC100%2/2
$request_uri eq "/config/json" or $request_uri eq "/config"
$request_uri eq "/config/json"
$request_uri eq "/config"
Truth table100%3/3
A or B
A= $request_uri eq "/config/json"B= $request_uri eq "/config"
| |||||||||||||||||||||||||||||
| 397 | 6 | ▶ | if ( ! $self->is_config_http_enabled() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 4 | print $socket "HTTP/1.0 403 Denied\n"; | |||||||||||||||||||||||||||
| 399 | 4 | print $socket "Content-Type: text/plain\n\nDenied by config\n\n"; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | else { | ||||||||||||||||||||||||||||
| 402 | 2 | print $socket "HTTP/1.0 200 OK\n"; | |||||||||||||||||||||||||||
| 403 | 2 | print $socket "Content-Type: text/plain\n"; | |||||||||||||||||||||||||||
| 404 | 2 | print $socket "\n"; | |||||||||||||||||||||||||||
| 405 | 2 | my $json = JSON::XS->new(); | |||||||||||||||||||||||||||
| 406 | 2 | $json->canonical(); | |||||||||||||||||||||||||||
| 407 | 2 | $json->pretty(); | |||||||||||||||||||||||||||
| 408 | 2 | print $socket $json->encode( $config ); | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | elsif ( $request_uri eq '/config/toml' ) { | ||||||||||||||||||||||||||||
| 412 | 3 | ▶ | if ( ! $self->is_config_http_enabled() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | 2 | print $socket "HTTP/1.0 403 Denied\n"; | |||||||||||||||||||||||||||
| 414 | 2 | print $socket "Content-Type: text/plain\n\nDenied by config\n\n"; | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | else { | ||||||||||||||||||||||||||||
| 417 | 1 | print $socket "HTTP/1.0 200 OK\n"; | |||||||||||||||||||||||||||
| 418 | 1 | print $socket "Content-Type: text/plain\n"; | |||||||||||||||||||||||||||
| 419 | 1 | print $socket "\n"; | |||||||||||||||||||||||||||
| 420 | 1 | my $toml = TOML::to_toml( $config ); | |||||||||||||||||||||||||||
| 421 | 1 | $toml =~ s/\n\[/\n\n\[/g; | |||||||||||||||||||||||||||
| 422 | 1 | print $socket $toml; | |||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | elsif ( $request_uri eq '/grafana' ) { | ||||||||||||||||||||||||||||
| 426 | 0 | ▶ | if ( $config->{'metric_basic_http'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 427 | 0 | print $socket "HTTP/1.0 403 Denied\n"; | |||||||||||||||||||||||||||
| 428 | 0 | print $socket "Content-Type: text/plain\n\nDenied by config\n\n"; | |||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | else { | ||||||||||||||||||||||||||||
| 431 | 0 | print $socket "HTTP/1.0 200 OK\n"; | |||||||||||||||||||||||||||
| 432 | 0 | print $socket "Content-Type: application/json\n"; | |||||||||||||||||||||||||||
| 433 | 0 | print $socket "\n"; | |||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 0 | my $Grafana = Mail::Milter::Authentication::Metric::Grafana->new(); | |||||||||||||||||||||||||||
| 436 | 0 | print $socket $Grafana->get_dashboard( $server ); | |||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | else { | ||||||||||||||||||||||||||||
| 440 | 1 | my $htdocs = Mail::Milter::Authentication::HTDocs->new(); | |||||||||||||||||||||||||||
| 441 | 1 | my $result = $htdocs->get_file( $request_uri ); | |||||||||||||||||||||||||||
| 442 | 1 | ▶ | if ( $result ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 443 | 0 | print $socket $result; | |||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | else { | ||||||||||||||||||||||||||||
| 446 | 1 | print $socket "HTTP/1.0 404 Not Found\n"; | |||||||||||||||||||||||||||
| 447 | 1 | print $socket "Content-Type: text/plain\n"; | |||||||||||||||||||||||||||
| 448 | 1 | print $socket "\n"; | |||||||||||||||||||||||||||
| 449 | 1 | print $socket "Not Found\n"; | |||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | }; | ||||||||||||||||||||||||||||
| 453 | # Outside the eval, so a return or error within it does not leave the alarm pending | ||||||||||||||||||||||||||||
| 454 | 29 | alarm( 0 ); | |||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 143 | 1; | |||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | __END__ | ||||||||||||||||||||||||||||