#!/usr/bin/perl
# 
# This prints JSON formated data reporting the status of servers on an Anvil! system.
# 

use strict;
use warnings;
use Anvil::Tools;
use Data::Dumper;
use JSON;

$| = 1;

my $THIS_FILE           =  ($0 =~ /^.*\/(.*)$/)[0];
my $running_directory   =  ($0 =~ /^(.*?)\/$THIS_FILE$/)[0];
if (($running_directory =~ /^\./) && ($ENV{PWD}))
{
	$running_directory =~ s/^\./$ENV{PWD}/;
}

my $anvil = Anvil::Tools->new();

$anvil->Get->switches;

$anvil->Database->connect;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"});
if (not $anvil->data->{sys}{database}{connections})
{
	# No databases, exit.
	$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0003"});
	$anvil->nice_exit({exit_code => 1});
}

# Read in any CGI variables, if needed.
$anvil->Get->cgi();

$anvil->Database->get_hosts();
$anvil->Database->get_anvils();

print $anvil->Template->get({file => "shared.html", name => "json_headers", show_name => 0})."\n";

my $hash       = {};
my $anvil_uuid = "";
if ($anvil->data->{cgi}{anvil_uuid}{value})
{
	$anvil_uuid = $anvil->data->{cgi}{anvil_uuid}{value};
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { anvil_uuid => $anvil_uuid }});
}
elsif ($anvil->data->{switches}{'anvil-uuid'})
{
	$anvil_uuid = $anvil->data->{switches}{'anvil-uuid'};
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { anvil_uuid => $anvil_uuid }});
}
if ((not $anvil_uuid) or (not exists $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}))
{
	$anvil->data->{anvil_status}{anvil_name} = "!!invalid!anvil_uuid!!";
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 'anvil_status::anvil_name' => $anvil->data->{anvil_status}{anvil_name} }});
}
else
{
	my $node1_uuid      = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid};
	my $node1_host_name = $anvil->data->{hosts}{host_uuid}{$node1_uuid}{short_host_name};
	my $node2_uuid      = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node2_host_uuid};
	my $node2_host_name = $anvil->data->{hosts}{host_uuid}{$node2_uuid}{short_host_name};
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 
		node1_uuid      => $node1_uuid,
		node1_host_name => $node1_host_name, 
		node2_uuid      => $node2_uuid, 
		node2_host_name => $node2_host_name, 
	}});
	
	$hash->{servers} = [];
	
	# For each node, check to see if it's "online" and, if so, if the node is in the cluster.
	$anvil->Database->get_hosts_info();
	
	my $query = "
SELECT 
     server_uuid, 
     server_name, 
     server_state, 
     server_host_uuid  
FROM 
    servers 
WHERE 
    server_anvil_uuid = ".$anvil->Database->quote($anvil_uuid)."
";
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }});
	my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__});
	my $count   = @{$results};
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 
		results => $results, 
		count   => $count, 
	}});
	foreach my $row (@{$results})
	{
		# How online is it?
		my $server_uuid      = $row->[0];
		my $server_name      = $row->[1];
		my $server_state     = $row->[2];
		my $server_host_uuid = $row->[3];
		$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 
			server_uuid      => $server_uuid, 
			server_name      => $server_name, 
			server_state     => $server_state, 
			server_host_uuid => $server_host_uuid, 
		}});
		
		# Ignore deleted servers.
		next if $server_state eq "DELETED";
		
		my $server_host_index = undef;
		if ($server_state ne "shut off")
		{
			if ($server_host_uuid eq $node1_uuid)
			{
				$server_host_index = 0;
			}
			elsif ($server_host_uuid eq $node2_uuid)
			{
				$server_host_index = 1;
			}
			else
			{
				# Is it on DR?
			}
		}
		
		push @{$hash->{servers}}, {
			server_uuid       => $server_uuid, 
			server_name       => $server_name, 
			server_state      => $server_state, 
			server_host_uuid  => $server_state eq "shut off" ? undef : $server_host_uuid, 
			server_host_index => $server_host_index, 
		};
	}
}

print JSON->new->utf8->encode($hash)."\n";
 