#!/usr/bin/perl # # Prints JSON formatted data reporting the status of shared storage (a.k.a. storage groups/shared VGs) # 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_storage_groups { my $parameters = shift; my $anvil_uuid = $parameters->{anvil_uuid}; my $storage_groups = []; # Use DISTINCT ON (...) to limit the occurence of a storage group to 1 and # ORDER BY [free] ASC to only select the storage with lowest free space. my $query = " SELECT DISTINCT ON (sgr.storage_group_uuid) storage_group_uuid, sgr.storage_group_name, slv.scan_lvm_vg_size, slv.scan_lvm_vg_free FROM anvils anv, storage_groups sgr, storage_group_members sgm, scan_lvm_vgs slv WHERE anv.anvil_uuid = sgr.storage_group_anvil_uuid AND sgr.storage_group_uuid = sgm.storage_group_member_storage_group_uuid AND sgm.storage_group_member_vg_uuid = slv.scan_lvm_vg_internal_uuid AND anv.anvil_uuid = ".$anvil->Database->quote($anvil_uuid)." ORDER BY sgr.storage_group_uuid ASC, slv.scan_lvm_vg_free ASC ;"; $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}) { my $storage_group_uuid = $row->[0]; my $storage_group_name = $row->[1]; my $scan_lvm_vg_size = $row->[2]; my $scan_lvm_vg_free = $row->[3]; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { storage_group_uuid => $storage_group_uuid, storage_group_name => $storage_group_name, scan_lvm_vg_size => $scan_lvm_vg_size, scan_lvm_vg_free => $scan_lvm_vg_free }}); push(@{$storage_groups}, { storage_group_uuid => $storage_group_uuid, storage_group_name => $storage_group_name, storage_group_total => int($scan_lvm_vg_size), storage_group_free => int($scan_lvm_vg_free) }); } return $storage_groups; } $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}) { $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { message => "Valid ".$anvil_uuid_variable_name." received.", anvil_uuid => $anvil_uuid } }); $response_body->{storage_groups} = get_storage_groups({ anvil_uuid => $anvil_uuid }); } 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";