Commit e53ab5f7 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

move munin-stats to a mojolicious command

parent 98822bdb
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -76,6 +76,8 @@ sub startup {
		$self->secrets( [ split( qr{:}, $ENV{TRAVELYNX_SECRETS} ) ] );
	}

	push( @{ $self->commands->namespaces }, 'Travelynx::Command' );

	$self->defaults( layout => 'default' );

	$self->config(
+52 −0
Original line number Diff line number Diff line
package Travelynx::Command::munin;
use Mojo::Base 'Mojolicious::Command';

use DateTime;

has description => 'Generate statistics for munin-node';

has usage => sub { shift->extract_usage };

sub query_to_munin {
	my ( $label, $query, @args ) = @_;

	$query->execute(@args);
	my $rows = $query->fetchall_arrayref;
	if ( @{$rows} ) {
		printf( "%s.value %d\n", $label, $rows->[0][0] );
	}
}

sub run {
	my ($self, $filename) = @_;

	my $dbh = $self->app->dbh;

	my $now = DateTime->now( time_zone => 'Europe/Berlin' );

	my $checkin_window_query
	= $dbh->prepare(
	qq{select count(*) from user_actions where action_id = 1 and action_time > ?;}
	);

	query_to_munin( 'reg_user_count',
		$dbh->prepare(qq{select count(*) from users where status = 1;}) );
	query_to_munin( 'checkins_24h', $checkin_window_query,
		$now->subtract( hours => 24 )->epoch );
	query_to_munin( 'checkins_7d', $checkin_window_query,
		$now->subtract( days => 7 )->epoch );
	query_to_munin( 'checkins_30d', $checkin_window_query,
		$now->subtract( days => 30 )->epoch );

	$dbh->disconnect;
}

1;

__END__

=head1 SYNOPSIS

  Usage: index.pl munin

  Write statistics for munin-node to stdout

munin-stats.pl

deleted100755 → 0
+0 −37
Original line number Diff line number Diff line
#!/usr/bin/env perl

use strict;
use warnings;
use 5.020;

use DateTime;
use DBI;

sub query_to_munin {
	my ( $label, $query, @args ) = @_;

	$query->execute(@args);
	my $rows = $query->fetchall_arrayref;
	if ( @{$rows} ) {
		printf( "%s.value %d\n", $label, $rows->[0][0] );
	}
}

my $dbname = $ENV{TRAVELYNX_DB_FILE} // 'travelynx.sqlite';
my $dbh = DBI->connect( "dbi:SQLite:dbname=${dbname}", q{}, q{} );

my $now = DateTime->now( time_zone => 'Europe/Berlin' );

my $checkin_window_query
  = $dbh->prepare(
qq{select count(*) from user_actions where action_id = 1 and action_time > ?;}
  );

query_to_munin( 'reg_user_count',
	$dbh->prepare(qq{select count(*) from users where status = 1;}) );
query_to_munin( 'checkins_24h', $checkin_window_query,
	$now->subtract( hours => 24 )->epoch );
query_to_munin( 'checkins_7d', $checkin_window_query,
	$now->subtract( days => 7 )->epoch );

$dbh->disconnect;