You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
5.1 KiB
194 lines
5.1 KiB
#!/usr/bin/perl |
|
# |
|
# This prints JSON formated data reporting the status of an Anvil! system and it's member hosts. |
|
# |
|
|
|
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(); |
|
|
|
sub set_anvil_power |
|
{ |
|
# Expects the first element of @_ (argument array) to be a hash. |
|
my $parameters = shift; |
|
my $anvil_uuid = $parameters->{anvil_uuid}; |
|
my $on = $parameters->{on}; |
|
|
|
$anvil->Log->variables({ |
|
source => $THIS_FILE, |
|
line => __LINE__, |
|
level => 2, |
|
list => { |
|
anvil_uuid => $anvil_uuid, |
|
on => $on |
|
} |
|
}); |
|
|
|
# Use anvil-safe-* start/stop all components that belong to the given anvil. |
|
} |
|
|
|
sub set_host_power |
|
{ |
|
my $parameters = shift; |
|
my $host_uuid = $parameters->{host_uuid}; |
|
my $on = $parameters->{on}; |
|
|
|
$anvil->Log->variables({ |
|
source => $THIS_FILE, |
|
line => __LINE__, |
|
level => 2, |
|
list => { |
|
host_uuid => $host_uuid, |
|
on => $on |
|
} |
|
}); |
|
} |
|
|
|
$anvil->Get->switches; |
|
|
|
# Temporary; for debugging |
|
$anvil->Log->level({ |
|
set => 2 |
|
}); |
|
|
|
$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 = {}; |
|
|
|
# Decode request body (JSON string) to a hash |
|
my $request_body; |
|
|
|
$anvil->Log->variables({ |
|
source => $THIS_FILE, |
|
line => __LINE__, |
|
level => 2, |
|
list => { |
|
# Requests made with the PUT method won't have query params |
|
CGI => Dumper($anvil->data->{cgi}) |
|
} |
|
}); |
|
|
|
my $is_decode_json_success = eval { |
|
$request_body = decode_json( |
|
$anvil->data->{cgi}{PUTDATA}{value} |
|
); |
|
}; |
|
|
|
if (not $is_decode_json_success) |
|
{ |
|
$anvil->Log->entry({ |
|
source => $THIS_FILE, |
|
line => __LINE__, |
|
level => 0, |
|
'print' => 1, |
|
priority => "err", |
|
key => "error_0304", |
|
variables => { |
|
request_body_string => $anvil->data->{cgi}{PUTDATA}{value}, |
|
json_decode_error => $_ |
|
} |
|
}); |
|
$anvil->nice_exit({ |
|
exit_code => 1 |
|
}); |
|
} |
|
|
|
my $anvil_uuid = exists $request_body->{anvil_uuid} ? $request_body->{anvil_uuid} : ""; |
|
my $host_uuid = exists $request_body->{host_uuid} ? $request_body->{host_uuid} : ""; |
|
my $is_on = exists $request_body->{is_on} ? $request_body->{is_on} : 0; |
|
|
|
$anvil->Log->variables({ |
|
source => $THIS_FILE, |
|
line => __LINE__, |
|
level => 2, |
|
list => { |
|
anvil_uuid => $anvil_uuid, |
|
host_uuid => $host_uuid, |
|
is_on => $is_on |
|
} |
|
}); |
|
|
|
if ($anvil_uuid) |
|
{ |
|
if (exists $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}) |
|
{ |
|
set_anvil_power({ |
|
anvil_uuid => $anvil_uuid, |
|
on => $is_on |
|
}); |
|
} |
|
else |
|
{ |
|
$anvil->data->{anvil_status}{anvil_name} = "!!invalid!anvil_uuid!!"; |
|
} |
|
} |
|
elsif ($host_uuid) |
|
{ |
|
if (exists $anvil->data->{hosts}{host_uuid}{$anvil_uuid}) |
|
{ |
|
set_host_power({ |
|
host_uuid => $host_uuid, |
|
on => $is_on |
|
}); |
|
} |
|
else |
|
{ |
|
$anvil->data->{anvil_status}{anvil_name} = "!!invalid!host_uuid!!"; |
|
} |
|
} |
|
else |
|
{ |
|
$anvil->data->{anvil_status}{anvil_name} = "!!invalid!no_target!!"; |
|
} |
|
|
|
# Are we managing the power of a node or an Anvil! system? If the later, we're controlling both. |
|
|
|
# $anvil->data->{anvil_status}{anvil_name} = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_name}; |
|
# $anvil->data->{anvil_status}{anvil_description} = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_description}; |
|
# $anvil->data->{anvil_status}{timestamp} = time; |
|
# |
|
# my $node1_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid}; |
|
# my $node2_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node2_host_uuid}; |
|
# my $node2_status = $anvil->data->{hosts}{host_uuid}{$node2_uuid}{host_status} eq "online" ? 1 : 0; |
|
# |
|
# $anvil->data->{anvil_status}{nodes}{node1}{host_name} = $anvil->data->{hosts}{host_uuid}{$node1_uuid}{host_name}; |
|
# $anvil->data->{anvil_status}{nodes}{node1}{host_uuid} = $node1_uuid; |
|
# $anvil->data->{anvil_status}{nodes}{node1}{host_status} = $anvil->data->{hosts}{host_uuid}{$node1_uuid}{host_status} eq "online" ? 1 : 0; |
|
# $anvil->data->{anvil_status}{nodes}{node2}{host_name} = $anvil->data->{hosts}{host_uuid}{$node2_uuid}{host_name}; |
|
# $anvil->data->{anvil_status}{nodes}{node2}{host_uuid} = $node2_uuid; |
|
# $anvil->data->{anvil_status}{nodes}{node2}{host_status} = $anvil->data->{hosts}{host_uuid}{$node2_uuid}{host_status} eq "online" ? 1 : 0; |
|
# |
|
# $hash->{timestamp} = time; |
|
# $hash->{nodes} = []; |
|
# push @{$hash->{nodes}}, { on => $anvil->data->{anvil_status}{nodes}{node1}{host_status} }; |
|
# push @{$hash->{nodes}}, { on => $anvil->data->{anvil_status}{nodes}{node2}{host_status} }; |
|
|
|
print JSON->new->utf8->encode($hash)."\n";
|
|
|