#!/usr/bin/perl # # # use strict; use warnings; use Anvil::Tools; $| = 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 remote_call { my $parameters = shift; my $debug = $parameters->{debug}; my @call_result = $anvil->Remote->call($parameters); my ($output, $error, $return_code) = @call_result; $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => { %$parameters, output => $output, error => $error, return_code => $return_code, } }); return @call_result; } sub system_call { my $parameters = shift; my $debug = $parameters->{debug}; my @call_result = $anvil->System->call($parameters); my ($output, $return_code) = @call_result; $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => { %$parameters, output => $output, return_code => $return_code, } }); return @call_result; } sub get_remote_call_requirements { my $parameters = shift; my $host_uuid = $parameters->{host_uuid}; my $debug = $parameters->{debug}; my $query = " SELECT ip_address_address FROM ip_addresses WHERE ip_address_gateway <> '' AND ip_address_note <> 'DELETED' AND ip_address_host_uuid = ".$anvil->Database->quote($host_uuid)." ;"; my $host_ip_address = $anvil->Database->query({ query => $query, source => $THIS_FILE, line => __LINE__, debug => $debug })->[0]->[0]; $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => { host_uuid => $host_uuid, host_ip_address => $host_ip_address, } }); return $host_ip_address; } sub get_server_screenshot { my $parameters = shift; my $server_uuid = $parameters->{server_uuid}; my $debug = $parameters->{debug}; my $resize_args = defined $parameters->{resize_args} ? $parameters->{resize_args} : ''; my $sh_domstate = $anvil->data->{path}{exe}{virsh}." domstate --domain ".$server_uuid; my ($sh_domstate_output, $sh_domstate_return_code) = system_call({ debug => $debug, shell_call => $sh_domstate }); if ( ($sh_domstate_return_code != 0) || ($sh_domstate_output ne "running") ) { return ""; } my $shell_call = $anvil->data->{path}{exe}{virsh}." --quiet screenshot --domain ".$server_uuid." --file ".$anvil->data->{path}{devices}{stdout}; if ($resize_args =~ /^\d+x\d+$/) { my ($resize_x, $resize_y) = split(/x/ , $resize_args); $shell_call .= " | ".$anvil->data->{path}{exe}{pamscale}." -quiet -xyfit ".$resize_x." ".$resize_y; } $shell_call .= " | ".$anvil->data->{path}{exe}{pamtopng}." -quiet | ".$anvil->data->{path}{exe}{base64}." --wrap 0"; my ($output, $return_code) = system_call({ debug => $debug, shell_call => $shell_call }); return $return_code == 0 ? $output : ""; } $anvil->Get->switches; my $debug = $anvil->data->{switches}{debug}; $anvil->Database->connect; $anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => $debug, 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 }); } # Try to get a job UUID if not given. if (not $anvil->data->{switches}{'job-uuid'}) { $anvil->data->{switches}{'job-uuid'} = $anvil->Job->get_job_uuid({ program => $THIS_FILE }); $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => { "switches::job-uuid" => $anvil->data->{switches}{'job-uuid'} } }); } # Handle this script as a job when job UUID is provided. if ($anvil->data->{switches}{'job-uuid'}) { $anvil->Job->clear(); $anvil->Job->get_job_details(); $anvil->Job->update_progress({ progress => 1, job_picked_up_by => $$, job_picked_up_at => time, message => "message_0263" }); foreach my $line (split/\n/, $anvil->data->{jobs}{job_data}) { if ($line =~ /server-uuid=(.*?)$/) { $anvil->data->{switches}{'server-uuid'} = $1; } if ($line =~ /resize=(.*?)$/) { $anvil->data->{switches}{'resize'} = $1; } if ($line =~ /request-host-uuid=(.*?)$/) { $anvil->data->{switches}{'request-host-uuid'} = $1; } if ($line =~ /out-file-id=(.*?)$/) { $anvil->data->{switches}{'out-file-id'} = $1; } } } my $server_uuid = $anvil->data->{switches}{'server-uuid'}; my $resize_args = $anvil->data->{switches}{resize}; my $request_host_uuid = $anvil->data->{switches}{'request-host-uuid'}; my $job_uuid = $anvil->data->{switches}{'job-uuid'}; my $out_file_id = defined $anvil->data->{switches}{'out-file-id'} ? $anvil->data->{switches}{'out-file-id'} : "0"; $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => { debug => $debug, job_uuid => $job_uuid, request_host_uuid => $request_host_uuid, resize_args => $resize_args, server_uuid => $server_uuid, } }); if ($server_uuid) { my $encoded_image = get_server_screenshot({ debug => $debug, server_uuid => $server_uuid, resize_args => $resize_args }); if ($request_host_uuid) { my $request_host_ip_address = get_remote_call_requirements({ debug => $debug, host_uuid => $request_host_uuid }); my $out_file_path = $anvil->data->{path}{directories}{tmp}."/".$server_uuid."_screenshot_".$out_file_id; my $remote_shell_call = $anvil->data->{path}{exe}{echo}." '".$encoded_image."' 1<>'".$out_file_path."' >'".$out_file_path."'"; $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => { request_host_ip_address => $request_host_ip_address } }); remote_call({ debug => $debug, shell_call => $remote_shell_call, target => $request_host_ip_address }); if (length($encoded_image)) { $anvil->Job->update_progress({ progress => 100, message => "message_0264" }); } else { $anvil->Job->update_progress({ progress => 100, message => "message_0265" }); $anvil->nice_exit({ exit_code => 1 }); } } else { print($encoded_image); } } else { $anvil->Job->update_progress({ progress => 100, message => "message_0266" }); $anvil->nice_exit({ exit_code => 1 }); } $anvil->nice_exit({ exit_code => 0 });