#!/usr/bin/env perl use strict; use warnings; use LWP::Simple; use Data::Dumper; use RRD::Simple (); use LWP::UserAgent; use LWP::Protocol::socks; my $rrd_path = "/home/marius/appdrift"; my $score_url = "http://128.39.121.4/purser/scoreboard.txt"; my @group_colors = qw( 003300 003366 0000FF 66FF66 669999 666699 FF99FF 9900CC 660066 660033 FF0000 663300 FFCCCC 66FFFF 993333 000066 3366CC CC00CC 800000 CC0099 ); # HTTP get from url through proxy sub proxy_get { my $remote = shift; my $ua = new LWP::UserAgent(agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5'); $ua->proxy([ qw(http https) ] => 'socks://localhost:9090'); my $response = $ua->get($remote); return $response->content; } # Check if string is in array sub in_array { my $str = shift; my $arr = shift; for my $val (@{$arr}) { return 1 if ($str eq $val); } return 0; } my $groups = {}; my @content = split /^-+$/m, get($score_url); shift @content; for my $section (@content) { my ($position, $user, $balance, $endpoint, $time_up, $time_maint, $time_down); for my $line (split /\n/, $section) { next if (length $line < 1); if ($line =~ /Position: (.*)/i) { $position = $1; } elsif ($line =~ /User: (.*)/i) { $1 =~ /gruppe(\d+)/; $user = sprintf("gruppe%02d", $1); } elsif ($line =~ /Balance: (.*)/i) { $balance = $1; } elsif ($line =~ /Endpoint: (.*)/i) { $endpoint = $1; } elsif ($line =~ /time_up: (.*)/i) { $time_up = $1; } elsif ($line =~ /time_maint: (.*)/i) { $time_maint = $1; } elsif ($line =~ /time_down: (.*)/i) { $time_down = $1; } else { print STDERR "Unkown line: \"$line\""; } } $groups->{$user} = { position => $position, balance => $balance, server => $endpoint, time_up => $time_up, time_maint => $time_maint, time_down => $time_down, } } for my $group (sort keys %$groups) { my ($users, $posts, $comments) = (undef); my $content = proxy_get("http://".$groups->{$group}->{'server'}); for my $line (split /\n/, $content) { last if (defined($users) && defined($posts) && defined($comments)); last if ($line =~ /Last activity/i); next if (length $line < 1); if ($line =~ /Users:[^0-9]*(\d+)/i) { $users = $1; } elsif ($line =~ /Posts:[^0-9]*(\d+)/i) { $posts = $1; } elsif ($line =~ /Comments:[^0-9]*(\d+)/i) { $comments = $1; } } $groups->{$group}->{'users'} = $users; #defined($users) ? $users : 0; $groups->{$group}->{'posts'} = $posts; #defined($posts) ? $posts : 0; $groups->{$group}->{'comments'} = $comments; #defined($comments) ? $comments : 0; } my $group_info = { # rrd_groups => {}, # position => {}, # balance => {}, # uptime => {}, # downtime => {}, # maintenance => {}, # users => {}, # posts => {}, # comments => {}, }; my %g_rrd_info = ( destination => "$rrd_path/graph", periods => [ qw(day week month) ], interlaced => "", extended_legend => "true", timestamp => "both", ); for my $group (sort keys %$groups) { my %rrd_info = %g_rrd_info; $rrd_info{'destination'} = "$rrd_path/graph/$group"; my $uptime_rrd = RRD::Simple->new( file => "$rrd_path/rrd/$group-uptime.rrd" ); my $users_rrd = RRD::Simple->new( file => "$rrd_path/rrd/$group-users.rrd" ); unless ( -f "$rrd_path/rrd/$group-uptime.rrd" ) { $uptime_rrd->create( uptime => "GAUGE", maintenance => "GAUGE", downtime => "GAUGE", ); } unless ( -f "$rrd_path/rrd/$group-users.rrd" ) { $users_rrd->create( users => "GAUGE", maintenance => "GAUGE", downtime => "GAUGE", ); } $uptime_rrd->update( uptime => $groups->{$group}->{'time_up'}, maintenance => $groups->{$group}->{'time_maint'}, downtime => $groups->{$group}->{'time_down'}, ); $users_rrd->update( users => $groups->{$group}->{'users'}, posts => $groups->{$group}->{'posts'}, comments => $groups->{$group}->{'comments'}, ); unless ( -d "$rrd_path/graph/$group" ) { mkdir "$rrd_path/graph/$group"; } $uptime_rrd->graph( %rrd_info, basename => "all-times", sources => [ qw(uptime maintenance downtime) ], source_colors => [ qw(00CC00 0000FF FF0000) ], title => "Time stats group: $group", vertical_label => "Time/seconds", ); $uptime_rrd->graph( %rrd_info, basename => "uptime", sources => [ qw(uptime) ], source_colors => [ qw(00CC00) ], title => "Uptime stats group: $group", vertical_label => "Time/seconds", ); $uptime_rrd->graph( %rrd_info, basename => "maintenance", sources => [ qw(maintenance) ], source_colors => [ qw(0000FF) ], title => "Uptime stats group: $group", vertical_label => "Time/seconds", ); $uptime_rrd->graph( %rrd_info, basename => "downtime", sources => [ qw(downtime) ], source_colors => [ qw(FF0000) ], title => "Uptime stats group: $group", vertical_label => "Time/seconds", ); $users_rrd->graph( %rrd_info, basename => "all-user-stats", sources => [ qw(users posts comments) ], source_colors => [ qw(00CC00 0000FF FF0000) ], title => "Users/Posts/Comments for $group", vertical_label => "Count", ); $users_rrd->graph( %rrd_info, basename => "users", sources => [ qw(users) ], source_colors => [ qw(00CC00) ], title => "User count for $group", vertical_label => "Nr. of users", ); $users_rrd->graph( %rrd_info, basename => "posts", sources => [ qw(posts) ], source_colors => [ qw(0000FF) ], title => "Post count for $group", vertical_label => "Nr. of posts", ); $users_rrd->graph( %rrd_info, basename => "comments", sources => [ qw(comments) ], source_colors => [ qw(FF0000) ], title => "Comment count for $group", vertical_label => "Nr. of comments", ); $group_info->{'rrd_groups'}->{$group} = "GAUGE"; $group_info->{'position'}->{$group} = $groups->{$group}->{'position'}; $group_info->{'balance'}->{$group} = $groups->{$group}->{'balance'}; $group_info->{'uptime'}->{$group} = $groups->{$group}->{'time_up'}; $group_info->{'downtime'}->{$group} = $groups->{$group}->{'time_down'}; $group_info->{'maintenance'}->{$group} = $groups->{$group}->{'time_maint'}; $group_info->{'users'}->{$group} = $groups->{$group}->{'users'}; $group_info->{'posts'}->{$group} = $groups->{$group}->{'posts'}; $group_info->{'comments'}->{$group} = $groups->{$group}->{'comments'}; } ## Score related my $pos_rrd = "$rrd_path/rrd/positions.rrd"; my $bal_rrd = "$rrd_path/rrd/balance.rrd"; my $upt_rrd = "$rrd_path/rrd/uptime.rrd"; my $dwn_rrd = "$rrd_path/rrd/downtime.rrd"; my $mnt_rrd = "$rrd_path/rrd/maintenance.rrd"; my $position_rrd = RRD::Simple->new( file => "$pos_rrd" ); my $balance_rrd = RRD::Simple->new( file => "$bal_rrd" ); my $uptime_rrd = RRD::Simple->new( file => "$upt_rrd" ); my $downtime_rrd = RRD::Simple->new( file => "$dwn_rrd" ); my $maint_rrd = RRD::Simple->new( file => "$mnt_rrd" ); $position_rrd->create(%{$group_info->{'rrd_groups'}}) unless ( -f "$pos_rrd" ); $balance_rrd->create(%{$group_info->{'rrd_groups'}}) unless ( -f "$bal_rrd" ); $uptime_rrd->create(%{$group_info->{'rrd_groups'}}) unless ( -f "$upt_rrd" ); $downtime_rrd->create(%{$group_info->{'rrd_groups'}}) unless ( -f "$dwn_rrd" ); $maint_rrd->create(%{$group_info->{'rrd_groups'}}) unless ( -f "$mnt_rrd" ); my @pos_sources = $position_rrd->sources("$pos_rrd"); my @bal_sources = $balance_rrd->sources("$bal_rrd"); my @upt_sources = $uptime_rrd->sources("$upt_rrd"); my @dwn_sources = $downtime_rrd->sources("$dwn_rrd"); my @mnt_sources = $maint_rrd->sources("$mnt_rrd"); ## User related my $usr_rrd = "$rrd_path/rrd/users.rrd"; my $pst_rrd = "$rrd_path/rrd/posts.rrd"; my $cmt_rrd = "$rrd_path/rrd/comments.rrd"; my $user_rrd = RRD::Simple->new( file => "$usr_rrd" ); my $post_rrd = RRD::Simple->new( file => "$pst_rrd" ); my $comment_rrd = RRD::Simple->new( file => "$cmt_rrd" ); $user_rrd->create(%{$group_info->{'rrd_groups'}}) unless ( -f "$usr_rrd" ); $post_rrd->create(%{$group_info->{'rrd_groups'}}) unless ( -f "$pst_rrd" ); $comment_rrd->create(%{$group_info->{'rrd_groups'}}) unless ( -f "$cmt_rrd" ); my @usr_sources = $user_rrd->sources("$usr_rrd"); my @pst_sources = $user_rrd->sources("$pst_rrd"); my @cmt_sources = $comment_rrd->sources("$cmt_rrd"); for my $group (keys %{$group_info->{'rrd_groups'}}) { $position_rrd->add_source("$pos_rrd", $group => $group_info->{'rrd_groups'}->{$group}) unless (in_array($group, \@pos_sources)); $balance_rrd->add_source("$bal_rrd", $group => $group_info->{'rrd_groups'}->{$group}) unless (in_array($group, \@bal_sources)); $uptime_rrd->add_source("$upt_rrd", $group => $group_info->{'rrd_groups'}->{$group}) unless (in_array($group, \@upt_sources)); $downtime_rrd->add_source("$dwn_rrd", $group => $group_info->{'rrd_groups'}->{$group}) unless (in_array($group, \@dwn_sources)); $maint_rrd->add_source("$mnt_rrd", $group => $group_info->{'rrd_groups'}->{$group}) unless (in_array($group, \@mnt_sources)); $user_rrd->add_source("$usr_rrd", $group => $group_info->{'rrd_groups'}->{$group}) unless (in_array($group, \@usr_sources)); $post_rrd->add_source("$pst_rrd", $group => $group_info->{'rrd_groups'}->{$group}) unless (in_array($group, \@pst_sources)); $comment_rrd->add_source("$cmt_rrd", $group => $group_info->{'rrd_groups'}->{$group}) unless (in_array($group, \@cmt_sources)); } $position_rrd->update(%{$group_info->{'position'}}); $balance_rrd->update(%{$group_info->{'balance'}}); $uptime_rrd->update(%{$group_info->{'uptime'}}); $downtime_rrd->update(%{$group_info->{'downtime'}}); $maint_rrd->update(%{$group_info->{'maintenance'}}); $user_rrd->update(%{$group_info->{'users'}}); $post_rrd->update(%{$group_info->{'posts'}}); $comment_rrd->update(%{$group_info->{'comments'}}); my @group_names = sort(keys(%$groups)); $#group_colors = $#group_names; $g_rrd_info{'sources'} = [ @group_names ]; $g_rrd_info{'source_colors'} = [ @group_colors ]; $position_rrd->graph( %g_rrd_info, basename => "positions", title => "Positions", vertical_label => "Position", ); $balance_rrd->graph( %g_rrd_info, basename => "balance", title => "Balances", vertical_label => "Balance", ); $uptime_rrd->graph( %g_rrd_info, basename => "uptime", title => "Uptime per group", vertical_label => "Uptime/seconds", ); $downtime_rrd->graph( %g_rrd_info, basename => "downtime", title => "Downtime per group", vertical_label => "Downtime/seconds", ); $maint_rrd->graph( %g_rrd_info, basename => "maintenance", title => "Maintenance per group", vertical_label => "Maintenance/seconds", ); $uptime_rrd->graph( %g_rrd_info, basename => "uptime", title => "Uptime per group", vertical_label => "Uptime/seconds", ); $user_rrd->graph( %g_rrd_info, basename => "users", title => "Number of users per group", vertical_label => "Nr. of users", ); $post_rrd->graph( %g_rrd_info, basename => "posts", title => "Number of posts per group", vertical_label => "Nr. of posts", ); $comment_rrd->graph( %g_rrd_info, basename => "comments", title => "Number of comments per group", vertical_label => "Nr. of comments", ); delete $g_rrd_info{'sources'}; delete $g_rrd_info{'source_colors'}; for my $group (@group_names) { my %rrd_info = %g_rrd_info; $rrd_info{'destination'} = "$rrd_path/graph/$group"; $balance_rrd->graph( %rrd_info, basename => "balance", sources => [ $group ], source_colors => [ qw(00CC00) ], title => "Balance for $group", vertical_label => "Balance", ); $position_rrd->graph( %rrd_info, basename => "position", sources => [ $group ], source_colors => [ qw(00CC00) ], title => "Position for $group", vertical_label => "Position", ); }