%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /scripts/
Upload File :
Create Path :
Current File : //scripts/gather-update-logs

#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/gather-update-logs              Copyright 2020 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;
use warnings;

#UpdateGatherer lives here
use lib '/var/cpanel/perl/';

use Getopt::Long ();

use Cpanel::Config::LoadCpConf ();
use Cpanel::LoadModule::Custom ();
use Cpanel::Logger             ();

# update analysis includes sensitive files which should not be world readable
umask(077);

my $logger = Cpanel::Logger->new();
my $list;
my $send_existing;
my $skip_if_same_version;
my $logfile;
my $version_before;
my $upload = 1;
my $help;
my $gather;

print_usage_and_exit('Required parameters omitted.') if !scalar @ARGV;

Getopt::Long::GetOptions(
    'list!'                 => \$list,
    'send=s'                => \$send_existing,
    'skip-if-same-version!' => \$skip_if_same_version,
    'logfile=s'             => \$logfile,
    'version-before=s'      => \$version_before,
    'upload!'               => \$upload,
    'gather'                => \$gather,
    'help'                  => \$help,
);

print_usage_and_exit() if $help;
print_usage_and_exit('send and upload are mutually exclusive!')
  if $send_existing && !$upload;

if ($list) {
    list_available_tarballs();
    exit;
}

print_usage_and_exit('Required parameters omitted.') unless $send_existing || $logfile || $gather;
print_usage_and_exit('A upcp log must be specified w/--logfile') if ( $logfile && $logfile =~ m/^\d+$/ );

my $gatherer;
my $mod = 'Cpanel::UpdateGatherer::Gatherer';

if ( eval { Cpanel::LoadModule::Custom::load_perl_module($mod); 1 } ) {
    $gatherer = $mod->new(
        {
            'update_log_file' => $logfile,
            'version_before'  => $version_before,
        }
    );
}

$logger->die("Could not load UpdateGatherer! Have you opted into cpanel analytics?") unless $gatherer;

if ($gather) {
    $gatherer->compile();
    $logger->info( "Done gathering tarball (" . $gatherer->update_analysis_dir() . '/' . $gatherer->_tarball() . ")." );
    exit 0;
}

# If the user has disabled all collection, don't even bother compiling anything,
# since all that would be sent is an empty hash.
if ($send_existing) {
    local $SIG{'ALRM'} = sub {
        $logger->die('Timed out attempting to send data to cPanel');
    };

    -e $gatherer->update_analysis_dir() . "/$send_existing" || $logger->die('Specified analysis tarball does not exist');

    my $show_output = 0;
    $gatherer->send_tarball( $send_existing, $show_output ) || $logger->die('Failed to send data to cPanel');
}
else {
    gather_send_cleanup($gatherer);
}

$gatherer->cleanup() || $logger->info('Cleanup incomplete');

sub list_available_tarballs {
    my $dh;
    return if !opendir $dh, '/usr/local/cpanel/logs/update_analysis';

    print "Available update tarballs:\n";
    for my $entry ( sort readdir $dh ) {
        next if $entry !~ m/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\.tar\.gz$/;
        print "    $entry\n";
    }
    closedir $dh;
    return 1;
}

sub gather_send_cleanup {
    my ($gatherer) = @_;
    my $version_after = $gatherer->version_after();
    if ( $skip_if_same_version && $version_before eq $version_after ) {
        exit;
    }

    $gatherer->compile();

    if ($upload) {
        my $cpconf = Cpanel::Config::LoadCpConf::loadcpconf();
        $gatherer->send_tarball() || $logger->info('Failed to send data to cPanel');
    }
    elsif ( -e $gatherer->_working_dir() . '.tar.gz' ) {
        $logger->info( "Update tarball created, but not uploaded: " . $gatherer->_working_dir() . '.tar.gz' );
    }
    else {
        $logger->info( 'Failed to generate tarball, ' . $gatherer->_working_dir() . ".tar.gz: $!" );
    }
}

sub print_usage_and_exit {
    my ($error) = @_;

    my %options = (

        ## internal usage parameters
        #'skip-if-same-version' => 'Skip run if previous version matches current version',
        #'version-before'       => 'Version before the most recent update',
        #'logfile'    => provide the current upcp log file name

        'help'   => 'Brief help message',
        'list'   => 'List existing tarballs available to send',
        'send'   => 'Specify an existing tarball to send',
        'upload' => 'Upload the tarball; defaults to true',
        'gather' => 'Make an upload tarball based on current system state',
    );

    if ( defined $error ) {
        print $error, "\n\n";
    }

    print "Usage: $0 ";
    print "[options]\n\n";
    print "    Options:\n";

    while ( my ( $opt, $desc ) = each %options ) {
        print "      --$opt";
        my $space = 12 - length $opt;
        ( 0 < $space ) ? print ' ' x $space : print '  ';
        print "$desc\n";
    }

    exit 1 if defined $error;
    exit;
}

Zerion Mini Shell 1.0