Skip to content

Commit

Permalink
cleanup the monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ranguard committed May 16, 2016
1 parent c0f7664 commit 531eb1e
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 1 deletion.
47 changes: 47 additions & 0 deletions bin/munin/monitor_minion_workers.pl
@@ -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;


}
19 changes: 19 additions & 0 deletions bin/munin_queue_monitor.pl
@@ -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();

125 changes: 125 additions & 0 deletions lib/MetaCPAN/Queue/Monitor.pm
@@ -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;
3 changes: 2 additions & 1 deletion lib/MetaCPAN/Role/HasConfig.pm
@@ -1,7 +1,8 @@
package MetaCPAN::Role::HasConfig;

use Moose::Role;

use Config::JFDI;
use FindBin;
use MetaCPAN::Types qw(HashRef);

has config => (
Expand Down

0 comments on commit 531eb1e

Please sign in to comment.