#!/usr/bin/perl
use strict;
use IO::Socket::INET;
use YAML::XS ;


my $opts = { enable => \&enable, disable => \&disable, status => \&status } ;
my $maintfile = '/etc/sysconfig/maint';

my $continue = 0;
foreach(keys %{$opts}){
  if($ARGV[0] eq $_){
    $continue = 1;
    last;
  }
}

help() unless $continue;

$opts->{$ARGV[0]}->($ARGV[1]);



###################  subs  ##################

sub enable {
  my $host = shift;

  if($host) {#single host maintenance mode
    $host = hosttoip($host) if $host !~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
    if($host !~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
      print "dont' recognize '$host' as ip addr or host that I can resolve to an IP. Quitting\n";
      exit;
    }

    print "ENABLING MAINTENANCE MODE FOR $host\n";
    $maintfile = "$maintfile-$host";
  }
  else {#global maintenance mode
  
    print "ENABLING MAINTENANCE MODE\n";

  }
  
  if( -e $maintfile ){
    print "already enabled!\n";
    return 1;
  }
  
  open(FILE,">$maintfile") or die "could not open $maintfile: $^E\n";
  close FILE;

  if( -e $maintfile ){
    print "done\n";
    return 1;
  }
  else {
    print "failed!?!\n";
    return 0;
  }

}

sub disable {
  my $host = shift;

  if($host) {#single host maintenance mode
    $host = hosttoip($host) if $host !~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
    if($host !~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
      print "dont' recognize '$host' as ip addr or host that I can resolve to an IP. Quitting\n";
      exit;
    }

    print "DISABLING MAINTENANCE MODE FOR $host\n";
    $maintfile = "$maintfile-$host";
  }
  else {#global maintenance mode
  
    print "DISABLING MAINTENANCE MODE\n";

  }

  if(! -e $maintfile ){
    print "already disabled!\n";
    return 1; 
  }
  
  unlink $maintfile or die "could not remove $maintfile: $^E\n";

  if(! -e $maintfile ){
    print "done\n";
    return 1;
  }
  else {
    print "failed!?!\n";
    return 0;
  }

}

sub hosttoip {
  my $host = shift;
  my $ip = '';

  my $clients = hosthash();

  if(ref($clients) eq 'HASH'){
    foreach(sort keys %{$clients}) {
      if ($clients->{$_} eq $host) {
        $ip = $_ ;
        last;
      }
    }
  }
  else {
    print "unable to get hostname->ip mapping, use an IP instead\n";
  }

  return $ip if $ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;

  print "unable to translate '$host' into an ip, probably not in targets file. Use IP instead\n";
  return 0;

}

sub iptohost {
  my $ip = shift;
  my $host = '';

  my $clients = hosthash();

  if(ref($clients) eq 'HASH'){
    foreach(sort keys %{$clients}) {
      if ($_ eq $ip) {
        $host = $clients->{$_} ;
        last;
      }
    }
  }
  else {
    print "unable to get ip->hostname mapping\n";
  }

  $host = $ip if $host eq '';
  return $host ;
}

sub hosthash {
  my $cache = {};
  if(-e "/etc/hostcache") {
    open(FILE,"</etc/hostcache");
    while(<FILE>){
      my ($tempip,$temphost) = split(/\s+/,$_);
      $cache->{$tempip} = $temphost;
    }
    close FILE;
  }
  return $cache;

}

sub status {
  if(-e $maintfile){
    print "\nglobal maintenance is ENABLED\n\n";
  }
  else {
    print "\nglobal maintenance is DISABLED\n\n";
  }


  print "clients connected:\n";
  my @clients = split("\n",qx(netstat -atpn | grep 2049 | grep 'EST'|awk '{print \$5}'|cut -d: -f1|sort|uniq));
  foreach( sort @clients ) {
    if(-e "$maintfile-$_") {
      print "$_ <-- MAINTENANCE\n";
    }
    else{
      print "$_\n";
    }
  }

  print "\n";

  chdir("/etc/sysconfig") or die "could not chdir to /etc/sysconfig - $^E\n";
  opendir(DIR,".");

  foreach my $file(readdir DIR) {
    next unless -f $file;
    if($file =~ /^maint-(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/){
      my $ip = $1;
      my $host = iptohost($ip);
      print "$host is tagged MAINTENANCE\n";
    }
  }
  
  print "\n";
}


sub help {

print <<EOF;

USAGE:  maint [enable [hostname] | disable [hostname] | status]

	enable  - sets backup box to global maintenance mode, triggers stop iscsi on clients
                  if hostname or ip is given, sets just that client to maintenance
	disable - removes maintenance status from backup box, triggers iscsi start on clients
                  if hostname or ip is given, removes maintenance from that client only
	status  - prints maintenance status and lists connected clients

EOF

exit;   
}

