
# A10::Misc
# Jeremy Kister 2011.02.15
# Released under Perl's Artistic License
# http://jeremy.kister.net

package A10::Misc;
use strict;

sub new {
    my $class = shift;
    my %args;
    if( @_ % 2 ){
        my $host = shift;
        %args = @_;
        $args{host} = $host;
    }else{
        %args = @_;
    }

    die "missing CONF\n" unless($args{conf});

    my $self = bless(\%args, $class);
    $self->{class} = $class;

    return $self;
}

sub getcache {
    my $self = shift;
    my $colo = shift || 'all';
    my $zone = shift || 'all';

    # the worst caching mechanism in the world
    # some day, move data into sql
    
    my $file = "$self->{conf}{homedir}/data/cache/$colo.$zone";
    my ($size,$mtime) = (stat($file))[7,9];
    
    if( $size < 1261 ){
        warn "[lbconfig] cache probably invalid - ignoring\n";
    }elsif( time() - $mtime < 3600 ){
        if(open(my $fh, $file)){
            my @cache = <$fh>;       
            close $fh;
            return( \@cache );
        }else{
            warn "[lbconfig] could not read $file: $!\n";
        }
    }
    return 0;
}

sub getcolor {
    my $self = shift;
    my $input = shift;

    warn "[lbconfig] getcolor input undefined\n" unless defined $input;

    my $color = ($input == 4) ? 'FF99FF' :
                ($input == 1) ? '99FF99' :
                ($input eq 0) ? 'FFFFFF' :
                                'CCFF66' ;

    my $x = 'bgcolor="#' . $color . '"';
    return $x;
}

sub selector {
    my $self = shift;
    my $colo = shift;
    my $zone = shift;
   
    my (%colo,%zone);
    for my $host (keys %{$self->{conf}{hosts}}){
       $colo{ $self->{conf}{hosts}{$host}{colo} } = 1;
       $zone{ $self->{conf}{hosts}{$host}{zone} } = 1;
    }

    my $r = <<__EOS__
<form method="post" action="$ENV{'SCRIPT_NAME'}">
 <table border="0">
  <td>Colo:</td>
  <td>
   <select name="colo">
    <option value=""></option>
__EOS__
    ;

    for my $acolo (keys %colo){
        $r .= '<option value="' . $acolo . '"';
        $r .= ' selected' if( $colo eq $acolo );
        $r .= ">$acolo</option>\n";
    }
    
    $r .= <<__EOS__
   </select>
  </td>
  <td>&nbsp;</td>
  <td>Zone:</td>
  <td>
   <select name="zone">
    <option value=""></option>
__EOS__
    ;

    for my $azone (keys %zone){
        $r .= '<option value="' . $azone . '"';
        $r .= ' selected' if( $zone eq $azone );
        $r .= ">$azone</option>\n";
    }

    $r .= <<__EOS__
   </select>
  </td>
  <td>
   <input type="submit" name="submit" value="submit">
  </td>
</form>
</table>
<br>
 
__EOS__
    ;
}

sub banner {
    my $self = shift;

    return <<__EOB__
 <tr> 
  <td><b>VIP Name</b></td>
  <td><b>VIP Addr</b></td>
  <td><b>Status</b></td>
  <td><b>Port</b></td>
  <td><b>Group Name</b></td>
  <td><b>Group Status</b></td>
  <td><b>Type</b></td>
  <td><b>Group Health Method</b></td>
  <td><b>LB Method</b></td>
  <td><b>Member</b></td>
  <td><b>Addr</b></td>
  <td><b>Port</b></td>
  <td><b>Health Method</b></td>
  <td><b>Current Connections</b></td>
  <td><b>Status</b></td>
 </tr>
__EOB__
    ;
}

1;
