perl script to send download file from website - integration with Jenkins- using Machanize

#!/usr/bin/perl

#use strict;
#use warnings;
use WWW::Mechanize;
use LWP;
use JSON;
use Data::Dumper;
use Getopt::Long;
use POSIX qw/ strftime /;
$| = 1;
my $username = 'something@something.com';
my $password = 'xxxxxx';
my $loginurl= 'https://loginurl';
my $insighturl= 'https://loginurl';
my %region_Squeryurl= ( 'apac' => '/cloud/paths/download/prod/slow-query/9538?s=1767176',
        'na' => '/cloud/paths/download/prod/slow-query/8972?s=1767151',
        'emea' => '/cloud/paths/download/prod/slow-query/3664?s=670131');
my $region;
GetOptions(
        'region|r=s' => \$region  );
die 'usage: $0 -r [apac|na|emea]'  unless( $region_Squeryurl{$region} );


my $date = my $time = strftime("%Y%m%d", localtime());
#my $log_dir = "$ENV{JENKINS_HOME}/docroot/getlog/";
my $log_dir = "/mnt/getlog/";

my $fullpath= "$log_dir"."$region/"."$date/";


unless (-d $fullpath ){
print "creating $fullpath ";
    system( "sudo mkdir -p  $fullpath" )  ;
    system( "sudo chown -R www-data:www-data  $fullpath" )  ;

}




my $mech = WWW::Mechanize->new();
$mech -> cookie_jar(HTTP::Cookies->new());

download_file ($username,$password,$loginurl,$insighturl,$region_Squeryurl{$region},$date,$fullpath);

sub download_file {

    my ($username,$password,$loginurl,$insighturl,$region_Squeryurl,$date,$fullpath)=@_;
    $mech -> get("$loginurl");
    $mech -> form_id('user-login');
    $mech -> field ('name' => $username);
    $mech -> field ('pass' => $password);
    $mech -> click_button (value => 'Sign in');
    $mech -> get ("$insighturl$region_Squeryurl");
    my $jdata_1 =   decode_json($mech ->content());

    $jdata_1 = $jdata_1->[1]->{'url'};
    $jdata_1 =~ s/'//g;
    print "$insighturl$jdata\n";
    while (1){
    sleep(2);
     $mech -> get ("$insighturl$jdata_1");
    $jdata = decode_json($mech ->content());
     print "get url $insighturl$jdata_1\n";
        if ( $jdata->[3]->{'delay'} =~ /^0$/ ) {
         print "mnt url --> ".$jdata->[3]->{'url'}."\n";
         print "delay --->". $jdata->[3]->{'delay'}."\n";
        last;
        }

    }
    my $D_url =  $jdata->[3]->{'url'}."\n";

    $mech -> get ("$D_url");
    $mech->save_content( "/tmp/mysql-slow-log-$date.$$.log" );
    system("sudo mv /tmp/mysql-slow-log-$date.$$.log ${fullpath}mysql-slow-log-$date.$$.log" );
    print $mech->content_type()."\n";
    if ($mech ->status()){
    print "created successfully ".$fullpath."mysql-slow-log-$date.$$.log\n ";
    my $msql_slow_log_q=$fullpath."mysql-slow-log-$date.$$.log";
    system("/bin/bash $ENV{WORKSPACE}/scripts/pt-query-digest.sh $region $msql_slow_log_q ");
    }

}
#print $mech ->status();
#print $mech ->content();

#print $mech-> content();


No comments:

Other Articles

Enter your email address: