Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
# Munin runs this as metacpan user, but with root's env | ||
# it's only for production so path is hard coded | ||
|
||
my $config_mode = 0; | ||
$config_mode = 1 if $ARGV[0] && $ARGV[0] eq 'config'; | ||
|
||
if($config_mode) { | ||
|
||
# Dump this (though we supported dynamic below) so it's faster | ||
print <<'EOF'; | ||
graph_title Minion Queue worker stats | ||
graph_vlabel count | ||
graph_category metacpan_api | ||
graph_info Minion Queue workers | ||
workers_inactive.label Inactive workers | ||
workers_active.label Active workers | ||
EOF | ||
|
||
exit; | ||
} | ||
|
||
# Get the stats | ||
my $stats_report = `/home/metacpan/bin/metacpan-api-carton-exec bin/queue.pl minion job -s`; | ||
|
||
my @lines = split("\n", $stats_report); | ||
|
||
for my $line (@lines) { | ||
my ($label, $num) = split ':', $line; | ||
|
||
$num =~ s/\D//g; | ||
|
||
my $key = lc($label); # Was 'Inactive workers' | ||
next unless $key =~ /workers/; | ||
|
||
# Swap type and status around so inactive_workers becomes workers_inactive | ||
$key =~ s/(\w+)\s+(\w+)/$2_$1/g; | ||
|
||
# results | ||
print "${key}.value $num\n" if $num; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use MetaCPAN::Queue::Monitor; | ||
|
||
# Show the jobs | ||
|
||
my $monitor = MetaCPAN::Queue::Monitor->new({ | ||
|
||
graph_title => 'Active Workers', | ||
graph_info => "What's happening in the Minion queue", | ||
fields => [ 'inactive_workers', 'active_workers' ], | ||
|
||
}); | ||
|
||
$monitor->run(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package MetaCPAN::Queue::Monitor; | ||
|
||
use Moo; | ||
|
||
extends 'Mojolicious'; | ||
|
||
use CHI; | ||
use feature qw( say ); | ||
|
||
use MetaCPAN::Queue::Helper; | ||
use Minion; | ||
|
||
=head1 NAME | ||
MetaCPAN::Queue::Monitor | ||
=head1 SYNOPSIS | ||
use MetaCPAN::Queue::Monitor; | ||
my $monitor = MetaCPAN::Queue::Monitor->new({ | ||
graph_title => 'Active Workers', | ||
graph_info => "What's happening in the Minion queue", | ||
fields => [ 'inactive_workers', 'active_workers' ], | ||
}); | ||
$monitor->run(); | ||
=head1 DESCRIPTION | ||
This spits out the relevant information for a munin monitoring | ||
if the 1st ARGV is 'config' then the config is displayed, | ||
otherwise the actual data | ||
=cut | ||
|
||
has graph_title => ( | ||
is => 'rw', | ||
required => 1, | ||
); | ||
|
||
has graph_info => ( | ||
is => 'rw', | ||
required => 1, | ||
); | ||
|
||
has fields => ( | ||
is => 'rw', | ||
required => 1, | ||
); | ||
|
||
sub cache { | ||
CHI->new( | ||
driver => 'File', | ||
root_dir => '/tmp/', | ||
); | ||
} | ||
|
||
has config_mode => ( | ||
is => 'rw', | ||
default => sub { defined $ARGV[0] && $ARGV[0] eq 'config' ? 1 : 0 }, | ||
); | ||
|
||
sub run { | ||
my ( $self, $args ) = @_; | ||
|
||
if ( $self->config_mode ) { | ||
|
||
# Tell Munin how to config this output | ||
say sprintf 'graph_title %s', $self->graph_title; | ||
say sprintf 'graph_info %s', $self->graph_info; | ||
say 'graph_vlabel count'; | ||
say 'graph_category metacpan_api'; | ||
|
||
for my $field ( @{ $self->fields } ) { | ||
my $label = $field; | ||
$label =~ s/_/ /g; | ||
say sprintf '%s.label %s', $field, ucfirst($label); | ||
} | ||
|
||
exit; | ||
|
||
} | ||
else { | ||
|
||
# Print out the results | ||
my $stats = $self->_queue_stats(); | ||
|
||
for my $field ( @{ $self->fields } ) { | ||
|
||
my $value = $stats->{$field} // 0; | ||
say sprintf '%s.value %s', $field, $value; | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
# Fetch the stats from the cache or DB | ||
sub _queue_stats { | ||
my $self = $_[0]; | ||
|
||
my $stats_cache = 'stats_cache'; | ||
|
||
my $stats; | ||
unless ( $stats = $self->cache->get($stats_cache) ) { | ||
|
||
my $helper = MetaCPAN::Queue::Helper->new; | ||
my $backend = $helper->backend; | ||
|
||
my $minion = Minion->new( %{$backend} ); | ||
my $stats = $minion->stats; | ||
|
||
$self->cache->set( $stats_cache, $stats, 0 ); # 1 min cache | ||
|
||
} | ||
|
||
return $stats; | ||
|
||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters