#!/usr/bin/perl
use strict;
use warnings;

use FindBin;
use lib "$FindBin::RealBin/../lib";
use BackupServer;
use YAML qw(LoadFile DumpFile);
use Sys::Hostname;
use Directory::Queue::Normal;
use Getopt::Long qw(:config gnu_getopt);
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 0;

my $state_folder = '/opt/eig_linux/data/obj-backups/';

my $opts = {
    'debug' => 0,
    'no-cache' => 0,
    'verbose' => 0,
};
usage() if(!GetOptions(\%{$opts},
        'verbose|v',
        'no-cache|nocache',
        'debug|d',
        'submit',
    )
);

# create backupserver object and prep variables
my $bs_args->{debug_stdout} = $opts->{'debug'} ? 1 : 0;
my $bk = BackupServer->new($bs_args);
my $dr_cboxes = $bk->get_gcp_dr_clients();
my $cboxes = $bk->get_clients();
my $server_data;
my $stats;
my @stats_keys = qw(server-count completed no-dr-setup total-users eligible-users full-backups 30d-old-full-backups completed-backups 7d-old-backups  queue);
my @server_stats_keys = qw(completed no-backups total-eligible-users total-users full-backups 30d-old-full-backups completed-backups 7d-old-backups);
foreach my $key (@stats_keys){ $stats->{$key} = 0 };
my $diskusage_threshold = $bk->get_config_value('obj_max_user_size') || 15;

# Collect stats for all the userboxes
foreach my $cbox (keys %$cboxes) {
    # Use Cached data if available for server stats (12 hour cache)
    my $cache_file = "/opt/eig_linux/data/obj-backups/$cbox/$cbox.yaml";
    if (! $opts->{'no-cache'} && -f $cache_file  && ((stat $cache_file)[9]) >= time - (60*60*12)){
        $server_data->{$cbox} = LoadFile("$cache_file");
        print "Using cached data for $cbox\n" if $opts->{'verbose'};
        next;
    }

    foreach my $key (@server_stats_keys){ $server_data->{$cbox}->{$key} = 0 };
    if (! $dr_cboxes->{$cbox}) {
        $server_data->{$cbox}->{'no-dr-setup'} = 1;
        $stats->{'no-dr-setup'}++;
        next;
    }

    # Find the user info file and load it
    my $userinfo;
    foreach my $part (@{$cboxes->{$cbox}->{'partitions'}}) {
        next if ! -f "/home/$cbox/$part->{'cmount'}/dirs.files/cpbackup/daily/userinfo";
        $userinfo = LoadFile("/home/$cbox/$part->{'cmount'}/dirs.files/cpbackup/daily/userinfo");
    }
    if (!$userinfo) {
        $bk->logger('LOG_ERR', "unable to find userinfo file for $cbox");
        $server_data->{$cbox}->{'no-dr-setup'} = 1;
        $stats->{'no-dr-setup'}++;
        next;
    }
    $server_data->{$cbox}->{'total-users'} = int(keys %$userinfo);
    $server_data->{$cbox}->{'total-eligible-users'} = int(keys %$userinfo);

    # Loop through the users and gather stats
    foreach my $user (keys %$userinfo) {
        if (defined $userinfo->{$user}->{'diskusage'} && $userinfo->{$user}->{'diskusage'} >= $diskusage_threshold){
            $server_data->{$cbox}->{'total-eligible-users'}--;
            next;
        }

        # Load up the users file
        if (! -f "$state_folder/$cbox/$user.yaml"){
            print "$user on $cbox does not have a state file\n" if $opts->{'verbose'};
            $server_data->{$cbox}->{'no-backups'}++;
            next;
        }
        my $userstate = eval{ LoadFile("$state_folder/$cbox/$user.yaml"); };

        # Check status of full backup
        if (! defined $userstate || ! defined $userstate->{'last_full_backup'} || ! defined $userstate->{'last_full_backup'}->{'time'}) {
            print "$user on $cbox does not have a full backup completed\n" if $opts->{'verbose'};
            $server_data->{$cbox}->{'no-backups'}++;
            next;
        } else {
            $server_data->{$cbox}->{'full-backups'}++;
            $server_data->{$cbox}->{'30d-old-full-backups'}++ if int($userstate->{'last_full_backup'}->{'time'}) < time - (60 * 60 * 24 * 30);
        }

        # Check status of incremental backup
        if (! defined $userstate->{'last_backup'} || ! defined $userstate->{'last_backup'}->{'time'}) {
            print "$user on $cbox does not have a last backup completed\n" if $opts->{'verbose'};
            next;
        } else {
            int($userstate->{'last_full_backup'}->{'time'}) < time - (60 * 60 * 24 * 7) ? $server_data->{$cbox}->{'7d-old-backups'}++ : $server_data->{$cbox}->{'completed-backups'}++;
        }
    }
    DumpFile($cache_file,$server_data->{$cbox});
}

# Condense into digestible box stats and print if needed
$stats->{'server-count'} = int(keys %$server_data);
my $threshold = .95;
my $header = "Hostname," . join(',',@server_stats_keys);
print "$header\n" if $opts->{'verbose'};
foreach my $cbox (keys %$server_data) {
    $stats->{'full-backups'} += $server_data->{$cbox}->{'full-backups'};
    $stats->{'30d-old-full-backups'} += $server_data->{$cbox}->{'30d-old-full-backups'};
    $stats->{'completed-backups'} += $server_data->{$cbox}->{'completed-backups'};
    $stats->{'7d-old-backups'} += $server_data->{$cbox}->{'7d-old-backups'};
    $stats->{'eligible-users'} += $server_data->{$cbox}->{'total-eligible-users'};
    $stats->{'total-users'} += $server_data->{$cbox}->{'total-users'};
    my $threshold_users = POSIX::ceil($threshold * $server_data->{$cbox}->{'total-eligible-users'});
    if ($server_data->{$cbox}->{'full-backups'} && $server_data->{$cbox}->{'full-backups'}  >= $threshold_users){
        $server_data->{$cbox}->{'completed'} = 1;
        $stats->{'completed'}++;
    }
    my $out = "$cbox";
    foreach my $key (@server_stats_keys) { $out .= ",$server_data->{$cbox}->{$key}" }
    print "$out\n" if $opts->{'verbose'};
}


# Get the queue size
my $obj_backup_queue_schema = { "userstring" => "string", "userdata" => "table?" };
my $obj_backup_queue_dir = $bk->get_config_value('obj_backup_queue_dir') ||  "/var/spool/obj-backups/queue";
my $obj_dir_queue = Directory::Queue::Normal->new( path => $obj_backup_queue_dir, schema => $obj_backup_queue_schema );
$stats->{'queue'} = $obj_dir_queue->count();

# Submit the stats
submit_graphite() if $opts->{'submit'};

sub submit_graphite {
    die "Unable to open collectd conf" if ( !-f '/etc/collectd.conf');
    open my $fh, '<', '/etc/collectd.conf' or die "Unable to open file\n";
    my $content = join("\n", <$fh>);
    close $fh;
    my $now = time();
    my $statname = hostname;
    $statname =~ s/\./_/g;
    $content =~ m/Prefix\s+"([\w\.]+)"/xms;
    my $header = $1;
    $content =~ m/(50\.\d{1,3}\.\d{1,3}\.\d{1,3})/xms;
    my $ip = $1;
    my $prefix = sprintf("%s%s.obj-backups",
                        $header,
                        $statname
                        );
    #we need to send it someplace
    my $socket = new IO::Socket::INET (
            PeerHost => $ip,
            PeerPort => '2003',
            Proto => 'udp',
    );
    #send the data
    my $buffer = '';
    my $limit = 1024;
    foreach my $key (@stats_keys){
        my $nextline = sprintf("%s.%s %d %d\n",$prefix,$key, $stats->{$key},$now);
        print $nextline;
        #see if this message puts us over the limit
        if( length($buffer) + length($nextline) > $limit ){
            $socket->send($buffer);
            $buffer = '';
        }
        $buffer .= $nextline;
    }
    if(length($buffer) > 0){
        $socket->send($buffer);
    }
}

sub usage {
    my $spacing = 3;
    my $spaces = " "x($spacing);
    print("\n");
    printf("  $0 [options]\n");
    printf("  OPTIONS:\n");
    printf("\t--%-29s$spaces%s\n", "debug", "Enable debug output");
    printf("\t--%-29s$spaces%s\n", "verbose", "Print stats and to stdout");
    printf("\t--%-29s$spaces%s\n", "submit", "Submit data to graphite");
    printf("\t--%-29s$spaces%s\n", "no-cache", "Collect all data fresh");
    print("\n");
    exit(33);
}
