Merge pull request #107 from Tsu-ba-me/issues/3-patch-endpoints-20210521

Web UI: patch endpoints (20210521)
main
Digimer 4 years ago committed by GitHub
commit bc0b4b1b62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      cgi-bin/get_anvils
  2. 10
      cgi-bin/get_cpu
  3. 19
      cgi-bin/get_memory
  4. 236
      cgi-bin/get_networks
  5. 20
      cgi-bin/get_servers
  6. 27
      cgi-bin/get_shared_storage
  7. 49
      cgi-bin/get_status
  8. 2
      cgi-bin/set_power

@ -33,6 +33,8 @@ if (not $anvil->data->{sys}{database}{connections})
# Read in any CGI variables, if needed.
$anvil->Get->cgi();
$anvil->Database->get_hosts();
$anvil->Database->get_anvils();
my $json_hash = {};
@ -40,13 +42,25 @@ $json_hash->{anvils} = [];
foreach my $anvil_uuid (keys %{$anvil->data->{anvils}{anvil_uuid}})
{
push @{$json_hash->{anvils}}, { uuid => $anvil_uuid };
my $anvil_name = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_name};
my $anvil_description = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_description};
my $anvil_password = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_password};
my $anvil_node1_host_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid};
my $anvil_node1_host_name = $anvil->data->{hosts}{host_uuid}{$anvil_node1_host_uuid}{short_host_name};
my $anvil_node2_host_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node2_host_uuid};
my $anvil_node2_host_name = $anvil->data->{hosts}{host_uuid}{$anvil_node2_host_uuid}{short_host_name};
my $anvil_dr1_host_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_dr1_host_uuid};
my $anvil_dr1_host_name = $anvil->data->{hosts}{host_uuid}{$anvil_dr1_host_uuid}{short_host_name};
push @{$json_hash->{anvils}}, {
anvil_name => $anvil_name,
anvil_uuid => $anvil_uuid,
hosts => [
{ host_name => $anvil_node1_host_name, host_uuid => $anvil_node1_host_uuid },
{ host_name => $anvil_node2_host_name, host_uuid => $anvil_node2_host_uuid },
{ host_name => $anvil_dr1_host_name, host_uuid => $anvil_dr1_host_uuid }
]
};
}
print $anvil->Template->get({file => "shared.html", name => "json_headers", show_name => 0})."\n";

@ -118,25 +118,27 @@ ORDER BY
scan_hardware_cpu_cores => $scan_hardware_cpu_cores,
scan_hardware_cpu_threads => $scan_hardware_cpu_threads,
}});
# Important: ensure the cores and threads are integers, which will be auto-transformed JSON number type.
if (not $hash->{cores})
{
$hash->{cores} = $scan_hardware_cpu_cores;
$hash->{cores} = int($scan_hardware_cpu_cores);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 'hash->cores' => $hash->{cores} }});
}
elsif ($scan_hardware_cpu_cores < $hash->{cores})
{
$hash->{cores} = $scan_hardware_cpu_cores;
$hash->{cores} = int($scan_hardware_cpu_cores);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 'hash->cores' => $hash->{cores} }});
}
if (not $hash->{threads})
{
$hash->{threads} = $scan_hardware_cpu_threads;
$hash->{threads} = int($scan_hardware_cpu_threads);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 'hash->threads' => $hash->{threads} }});
}
elsif ($scan_hardware_cpu_threads < $hash->{threads})
{
$hash->{threads} = $scan_hardware_cpu_threads;
$hash->{threads} = int($scan_hardware_cpu_threads);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 'hash->threads' => $hash->{threads} }});
}
}

@ -40,7 +40,7 @@ $anvil->Database->get_anvils();
print $anvil->Template->get({file => "shared.html", name => "json_headers", show_name => 0})."\n";
my $target = $anvil->Get->short_host_name();
my $hash = {};
my $hash = { reserved => 68719476736 };
my $anvil_uuid = "";
if ($anvil->data->{cgi}{anvil_uuid}{value})
{
@ -70,7 +70,7 @@ else
$hash->{total} = 0;
$hash->{allocated} = 0;
$hash->{nodes} = [];
$hash->{hosts} = [];
my @hosts = ($node1_uuid, $node2_uuid);
if ($dr1_uuid)
@ -118,24 +118,25 @@ AND
scan_hardware_swap_free => $anvil->Convert->add_commas({number => $scan_hardware_swap_free})." (".$anvil->Convert->bytes_to_human_readable({'bytes' => $scan_hardware_swap_free}).")",
}});
# Important: ensure all size values are integers, which will be auto-transformed to JSON number type.
if (not $hash->{total})
{
$hash->{total} = $scan_hardware_ram_total;
$hash->{total} = int($scan_hardware_ram_total);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 'hash->total' => $hash->{total} }});
}
elsif ($scan_hardware_ram_total < $hash->{total})
{
$hash->{total} = $scan_hardware_ram_total;
$hash->{total} = int($scan_hardware_ram_total);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 'hash->total' => $hash->{total} }});
}
push @{$hash->{nodes}}, {
push @{$hash->{hosts}}, {
host_uuid => $host_uuid,
total => $scan_hardware_ram_total,
free => $scan_hardware_memory_free,
swap_total => $scan_hardware_swap_total,
swap_used => $scan_hardware_swap_free,
total => int($scan_hardware_ram_total),
free => int($scan_hardware_memory_free),
swap_total => int($scan_hardware_swap_total),
swap_used => int($scan_hardware_swap_free),
};
}
}

@ -0,0 +1,236 @@
#!/usr/bin/perl
#
# This prints JSON formatted data about all networks within a specified anvil.
#
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 handle_invalid_uuid
{
my $parameters = shift;
my $name = $parameters->{name};
my $uuid = $parameters->{uuid};
$anvil->Log->entry({
source => $THIS_FILE,
line => __LINE__,
level => 0,
'print' => 1,
priority => "err",
key => "error_0160",
variables => { name => $name, uuid => $uuid }
});
$anvil->nice_exit({ exit_code => 1 });
}
sub get_anvil_networks
{
my $parameters = shift;
my $anvil_uuid = $parameters->{anvil_uuid};
my @host_uuids = (
$anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid},
$anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node2_host_uuid}
);
my $anvil_networks = [];
foreach (@host_uuids)
{
my $host_networks = get_host_networks({ host_uuid => $_ });
push(@$anvil_networks, $host_networks);
}
return $anvil_networks;
}
sub get_host_networks
{
my $parameters = shift;
my $host_uuid = $parameters->{host_uuid};
my $host_name = $anvil->data->{hosts}{host_uuid}{$host_uuid}{short_host_name};
# Note: the subprocess name matches its definition; there is a typo though.
$anvil->Network->load_interfces({ host_uuid => $host_uuid, host => $host_name });
my $host_networks_data = $anvil->data->{network}{$host_name};
my $interfaces = $host_networks_data->{interface};
my $bond_uuids = $host_networks_data->{bond_uuid};
my $bond_uuid_index_map = {};
my $host_networks = {
host_uuid => $host_uuid,
host_name => $host_name,
bonds => []
};
my $build_bond_link_hash = sub {
my $parameters = shift;
my $bond_data = $parameters->{bond_data};
my $link_name = $parameters->{link_name};
my $link_data = $parameters->{link_data};
my $known_links = $parameters->{known_links};
my $link_speed = $link_data->{speed};
my $link_state = $link_data->{operational} eq "up" ? "optimal" : "down";
my $degrade_link_state = sub {
my $current_link_state = shift;
return $current_link_state eq "optimal" ? "degraded" : $current_link_state;
};
# When at lease 1 link has already been processed, check this link against them.
if ($known_links)
{
my $known_links_index = 0;
my $known_links_length = scalar(@$known_links);
foreach (@$known_links)
{
# Degrade the link with lower speed.
if ($_->{link_speed} < $link_speed)
{
$_->{link_state} = &$degrade_link_state($_->{link_state});
}
elsif ($_->{link_state} > $link_speed)
{
$link_state = &$degrade_link_state($link_state);
}
$anvil->Log->variables({
source => $THIS_FILE,
line => __LINE__,
level => 2,
list => {
known_link_name => $_->{link_name},
known_link_speed => $_->{link_speed},
known_link_state => $_->{link_state},
link_speed => $link_speed,
link_name => $link_name,
link_state => $link_state
}
});
}
}
return {
link_name => $link_name,
link_uuid => $link_data->{uuid},
link_speed => $link_speed,
link_state => $link_state,
is_active => $link_name eq $bond_data->{active_interface} ? \1 : \0
};
};
foreach my $interface_name (sort { $a cmp $b } keys %$interfaces)
{
my $interface = $interfaces->{$interface_name};
# Only a bond's link will have the bond_uuid entry.
my $bond_uuid = $interface->{bond_uuid};
if ($bond_uuid)
{
my $bond_name = $bond_uuids->{$bond_uuid}{name};
my $bond_index = $bond_uuid_index_map->{$bond_uuid};
$anvil->Log->variables({
source => $THIS_FILE,
line => __LINE__,
level => 2,
list => {
bond_name => $bond_name,
bond_index => $bond_index
}
});
if (defined $bond_index)
{
my $bond = @{$host_networks->{bonds}}[$bond_index];
push(@{$bond->{links}}, &$build_bond_link_hash({
bond_data => $interfaces->{$bond_name},
link_name => $interface_name,
link_data => $interface,
known_links => $bond->{links}
}));
}
else
{
my $bond = { bond_name => $bond_name, bond_uuid => $bond_uuid, links => [] };
my $link = &$build_bond_link_hash({
bond_data => $interfaces->{$bond_name},
link_name => $interface_name,
link_data => $interface
});
push(@{$bond->{links}}, $link);
push(@{$host_networks->{bonds}}, $bond);
# Get the index of the most recently inserted item and map it to the bond's UUID.
$bond_uuid_index_map->{$bond_uuid} = $#{$host_networks->{bonds}};
}
}
}
return $host_networks;
}
$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 $anvil_uuid = exists $anvil->data->{cgi}{anvil_uuid}{value} ? $anvil->data->{cgi}{anvil_uuid}{value} : $anvil->data->{switches}{'anvil-uuid'};
my $anvil_uuid_variable_name = "anvil UUID";
my $response_body = {};
if ($anvil_uuid)
{
if (exists $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid})
{
my $anvil_networks = get_anvil_networks({ anvil_uuid => $anvil_uuid });
$response_body->{hosts} = $anvil_networks;
}
else
{
handle_invalid_uuid({ name => $anvil_uuid_variable_name, uuid => $anvil_uuid });
}
}
else
{
handle_invalid_uuid({ name => $anvil_uuid_variable_name, uuid => $anvil_uuid });
}
print JSON->new->utf8->encode($response_body)."\n";

@ -109,29 +109,11 @@ WHERE
# 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,
server_host_uuid => $server_state eq "shut off" ? undef : $server_host_uuid
};
}
}

@ -39,7 +39,7 @@ $anvil->Database->get_anvils();
print $anvil->Template->get({file => "shared.html", name => "json_headers", show_name => 0})."\n";
my $hash = {};
my $hash = { file_systems => [] };
my $anvil_uuid = "";
if ($anvil->data->{cgi}{anvil_uuid}{value})
{
@ -130,13 +130,14 @@ ORDER BY
}});
}
$anvil->data->{file_systems} = [];
foreach my $mount_point (sort {$a cmp $b} keys %{$anvil->data->{raw}{file_systems}})
{
# Make a FS hash, which will be pushed into the file_systems array in the response body.
my $file_system = {};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { mount_point => $mount_point }});
$hash->{mount_point} = $mount_point;
$hash->{nodes} = [];
$file_system->{mount_point} = $mount_point;
$file_system->{hosts} = [];
my $nodes = [$node1_uuid, $node2_uuid];
if ($dr1_uuid)
{
@ -151,25 +152,31 @@ ORDER BY
}});
if (exists $anvil->data->{raw}{file_systems}{$mount_point}{nodes}{$host_uuid})
{
push @{$hash->{nodes}}, {
push @{$file_system->{hosts}}, {
host_uuid => $host_uuid,
host_name => $anvil->data->{raw}{file_systems}{$mount_point}{nodes}{$host_uuid}{host_name},
is_mounted => 1,
total => $anvil->data->{raw}{file_systems}{$mount_point}{nodes}{$host_uuid}{total},
# \1 will be transformed to the JSON boolean "true" by the JSON module.
is_mounted => \1,
# FS total size is required by the spec to be an JSON number rather than string.
total => int($anvil->data->{raw}{file_systems}{$mount_point}{nodes}{$host_uuid}{total}),
free => $anvil->data->{raw}{file_systems}{$mount_point}{nodes}{$host_uuid}{free},
}
}
else
{
push @{$hash->{nodes}}, {
push @{$file_system->{hosts}}, {
host_uuid => $host_uuid,
host_name => $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_name},
is_mounted => 0,
# \0 will be transformed to the JSON boolean "false" by the JSON module.
is_mounted => \0,
total => 0,
free => 0,
}
}
}
# Push the filled FS has into the response body's file_systems array.
push(@{$hash->{file_systems}}, $file_system);
}
}

@ -82,15 +82,17 @@ else
# Array index 0 is node 1, index 1 is node 2.
$hash->{anvil_state} = "optimal";
$hash->{nodes}->[0] = {
host_uuid => $node1_uuid,
$hash->{hosts}->[0] = {
host_uuid => $node1_uuid,
host_name => $node1_host_name,
'state' => "offline",
state_percent => 0,
state_message => "message_0222,!!host_name!".$node1_host_name."!!",
maintenance_mode => 'undefined',
};
$hash->{nodes}->[1] = {
host_uuid => $node2_uuid,
$hash->{hosts}->[1] = {
host_uuid => $node2_uuid,
host_name => $node2_host_name,
'state' => "offline",
state_percent => 0,
state_message => "message_0222,!!host_name!".$node2_host_name."!!",
@ -99,7 +101,7 @@ else
foreach my $i (0..1)
{
my $host_uuid = $hash->{nodes}->[$i]->{host_uuid};
my $host_uuid = $hash->{hosts}->[$i]->{host_uuid};
my $short_host_name = $anvil->data->{hosts}{host_uuid}{$host_uuid}{short_host_name};
my $host_status = $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_status};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
@ -149,37 +151,38 @@ WHERE
scan_cluster_node_maintenance_mode => $scan_cluster_node_maintenance_mode,
}});
$hash->{nodes}->[$i]->{maintenance_mode} = $scan_cluster_node_maintenance_mode ? 1 : 0;
# Ensure the maintenance_mode is set to a value that can be converted to JSON booleans.
$hash->{hosts}->[$i]->{maintenance_mode} = $scan_cluster_node_maintenance_mode ? \1 : \0;
if ($scan_cluster_node_cluster_member)
{
$hash->{nodes}->[$i]->{'state'} = "online";
$hash->{nodes}->[$i]->{state_percent} = 100;
$hash->{nodes}->[$i]->{state_message} = "message_0223,!!host_name!".$node2_host_name."!!",
$hash->{hosts}->[$i]->{'state'} = "online";
$hash->{hosts}->[$i]->{state_percent} = 100;
$hash->{hosts}->[$i]->{state_message} = "message_0223,!!host_name!".$node2_host_name."!!",
}
elsif ($scan_cluster_node_crmd_member)
{
$hash->{nodes}->[$i]->{'state'} = "crmd";
$hash->{nodes}->[$i]->{state_percent} = 75;
$hash->{nodes}->[$i]->{state_message} = "message_0224,!!host_name!".$node2_host_name."!!",
$hash->{hosts}->[$i]->{'state'} = "crmd";
$hash->{hosts}->[$i]->{state_percent} = 75;
$hash->{hosts}->[$i]->{state_message} = "message_0224,!!host_name!".$node2_host_name."!!",
}
elsif ($scan_cluster_node_in_ccm)
{
$hash->{nodes}->[$i]->{'state'} = "in_ccm";
$hash->{nodes}->[$i]->{state_percent} = 50;
$hash->{nodes}->[$i]->{state_message} = "message_0225,!!host_name!".$node2_host_name."!!",
$hash->{hosts}->[$i]->{'state'} = "in_ccm";
$hash->{hosts}->[$i]->{state_percent} = 50;
$hash->{hosts}->[$i]->{state_message} = "message_0225,!!host_name!".$node2_host_name."!!",
}
else
{
$hash->{nodes}->[$i]->{'state'} = "booted";
$hash->{nodes}->[$i]->{state_percent} = 25;
$hash->{nodes}->[$i]->{state_message} = "message_0226,!!host_name!".$node2_host_name."!!",
$hash->{hosts}->[$i]->{'state'} = "booted";
$hash->{hosts}->[$i]->{state_percent} = 25;
$hash->{hosts}->[$i]->{state_message} = "message_0226,!!host_name!".$node2_host_name."!!",
}
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"hash->{nodes}->[".$i."]->{state}" => $hash->{nodes}->[$i]->{'state'},
"hash->{nodes}->[".$i."]->{state_percent}" => $hash->{nodes}->[$i]->{state_percent},
"hash->{nodes}->[".$i."]->{state_message}" => $hash->{nodes}->[$i]->{state_message},
"hash->{nodes}->[".$i."]->{maintenance_mode}" => $hash->{nodes}->[$i]->{maintenance_mode},
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"hash->{nodes}->[".$i."]->{state}" => $hash->{hosts}->[$i]->{'state'},
"hash->{nodes}->[".$i."]->{state_percent}" => $hash->{hosts}->[$i]->{state_percent},
"hash->{nodes}->[".$i."]->{state_message}" => $hash->{hosts}->[$i]->{state_message},
"hash->{nodes}->[".$i."]->{maintenance_mode}" => $hash->{hosts}->[$i]->{maintenance_mode},
}});
}
}

@ -1,6 +1,6 @@
#!/usr/bin/perl
#
# This prints JSON formated data reporting the status of an Anvil! system and it's member hosts.
# Accepts a HTTP PUT request to power ON or OFF a specified host.
#
use strict;

Loading…
Cancel
Save