summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Command/munin.pm
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2019-03-22 21:18:03 +0100
committerDaniel Friesel <derf@finalrewind.org>2019-03-22 21:18:03 +0100
commite53ab5f78164e5637b02b7930825e53f0689ee74 (patch)
treebb5d4b4a120a1d79d1eeda7deeb570f286387144 /lib/Travelynx/Command/munin.pm
parent98822bdbf75ce21f0c54e4be47e1d03bba057e07 (diff)
move munin-stats to a mojolicious command
Diffstat (limited to 'lib/Travelynx/Command/munin.pm')
-rw-r--r--lib/Travelynx/Command/munin.pm52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/Travelynx/Command/munin.pm b/lib/Travelynx/Command/munin.pm
new file mode 100644
index 0000000..23ed747
--- /dev/null
+++ b/lib/Travelynx/Command/munin.pm
@@ -0,0 +1,52 @@
+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