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