#!/usr/bin/perl use FiLeS; use strict; use warnings; use Data::Dumper; use processLogFiles; use Fcntl qw(:flock); use feature qw(state); use Net::OpenSSH::Parallel; my %WARNS = (); my %dir_all = (); my %sudo_passwords = (); my $confFile = "conf.ini"; my $dirFile = "directories.ini"; my $objectFiles = FiLeS->new( "confRef" , "dirRef" ); my $refHashDir = $objectFiles->getFiles( $dirFile ); my $refHashconf = $objectFiles->getFiles( $confFile ); my $refHashDirectories = $objectFiles->makeDirectories( $refHashDir ); my $objectProcessLogFiles = processLogFiles->new( "refLogFile" , "refArrayConversion" , "refPrintOutput" ); # Find the version Ubuntu or Crux my @data_files = getOsDistribution( $refHashconf , $refHashDir ); my $refhashProcessTimestampFile = $objectProcessLogFiles->processData( @data_files ); sub getOsDistribution { my ( $hashRefconf , $hashRefDir ) = @_; my @mps = sort keys ( $hashRefconf ); my $maximum_workers = @mps; my $maximum_connections = 2 * $maximum_workers; my $maximum_reconnections = 3; my %opts = ( workers => $maximum_workers, connections => $maximum_connections, reconnections => $maximum_reconnections ); my $pssh = Net::OpenSSH::Parallel->new(%opts); my $num = 0; my @stdout_fh = (); my @stderr_fh = (); my @log_files = (); foreach my $hash ( @mps ) { push (@log_files , "".$hashRefDir->{Directories}{log_dir}."/".$hashRefconf->{$hash}{log}.""); open $stdout_fh[$num] , '>' , "".$hashRefDir->{Directories}{log_dir}."/".$hashRefconf->{$hash}{log}."" or warn "unable to create file: $hashRefconf->{$hash}{log} - $!"; open $stderr_fh[$num], '>>', "".$hashRefDir->{Directories}{err_dir}."/".$hashRefconf->{$hash}{error}."" or warn "unable to create file: $hashRefconf->{$hash}{error} - $!"; $pssh->add_host( $hashRefconf->{$hash}{host} , user => $hashRefconf->{$hash}{user}, port => $hashRefconf->{$hash}{port}, password => $hashRefconf->{$hash}{psw}, default_stderr_fh => $stderr_fh[$num], default_stdout_fh => $stdout_fh[$num] ); $sudo_passwords{$hashRefconf->{$hash}{host}} = $hashRefconf->{$hash}{psw}; $num++; } # hostname -I another way to get IP $pssh->push('*', command => 'cat /etc/issue; echo %HOST%' ); $pssh->run; closeFH( @stdout_fh , @stderr_fh ); my $refhashProcessData = $objectProcessLogFiles->processData( @log_files ); my %version_hash = (); # Bind the IP's with the OS version foreach my $key ( keys $refhashProcessData ) { #print "$key: @{ $dev_info{$key} }\n"; $refhashProcessData->{$key}[0] = (split(/ /,$refhashProcessData->{$key}[0]))[0]; # Push the version into a hash pointing to the ip push( @{$version_hash{$refhashProcessData->{$key}[0]} } , $refhashProcessData->{$key}[1] ); } my $Crux_IPs = $version_hash{'CRUX'}; my $Ubuntu_IPs = $version_hash{'Ubuntu'}; my $refHashConvertArrayUbuntu = $objectProcessLogFiles->arrayConversion( $Ubuntu_IPs ); my $refHashConvertArrayCrux = $objectProcessLogFiles->arrayConversion( $Crux_IPs ); $pssh = Net::OpenSSH::Parallel->new(%opts); $num = 0; @stdout_fh = (); @stderr_fh = (); @log_files = (); foreach my $hash ( @mps ) { push (@log_files , "".$hashRefDir->{Directories}{log_dir}."/".$hashRefconf->{$hash}{log}.""); open $stdout_fh[$num] , '>' , "".$hashRefDir->{Directories}{log_dir}."/".$hashRefconf->{$hash}{log}."" or warn "unable to create file: $hashRefconf->{$hash}{log} - $!"; open $stderr_fh[$num], '>>', "".$hashRefDir->{Directories}{err_dir}."/".$hashRefconf->{$hash}{error}."" or warn "unable to create file: $hashRefconf->{$hash}{error} - $!"; $pssh->add_host( $hashRefconf->{$hash}{host} , user => $hashRefconf->{$hash}{user}, port => $hashRefconf->{$hash}{port}, password => $hashRefconf->{$hash}{psw}, default_stderr_fh => $stderr_fh[$num], default_stdout_fh => $stdout_fh[$num] ); $sudo_passwords{$hashRefconf->{$hash}{host}} = $hashRefconf->{$hash}{psw}; $num++; } if ( grep { defined($_) } $refHashConvertArrayUbuntu ) { my @Ubuntu_cmds = ( 'echo %HOST%' , "service ntp stop" , "ntpd -gq" , "service ntp start" ); sub ubuntu { my ($label , $ssh , @cmd) = @_; foreach my $c (@cmd) { $ssh->system( {stdin_data => "$sudo_passwords{$label}\n"} , 'sudo' , '-Skp' , '' , '--' , split " " , $c ); } } foreach my $Ubuntu_ip ( @{$refHashConvertArrayUbuntu} ) { $pssh->push($Ubuntu_ip , parsub => \&ubuntu , @Ubuntu_cmds); } } # End of if grep $refHashConvertArrayUbuntu if ( grep { defined($_) } $refHashConvertArrayCrux ) { foreach my $Crux_ip ( @{$refHashConvertArrayCrux} ) { $pssh->push($Crux_ip , command => 'echo %HOST%; /etc/rc.d/ntpd stop; ntpd -gq; /etc/rc.d/ntpd start;' ); } } $pssh->run; closeFH( @stdout_fh , @stderr_fh ); #/usr/local/dag/tools/dagclock -d dag0 none overin return @log_files; } sub closeFH { foreach my $file (@_) { close $file or warn $! ? "Error closing ".$file.": $!" : "Exit status $? from ".$file."\n"; } } local $SIG{__WARN__} = sub { my $message = shift; return if $WARNS{$message}++; logger('warning', $message); }; sub logger { my ( $level , $msg ) = @_; my $error = "$refHashDirectories->{Directories}{warn_dir}/error.err"; open my $out, '>>', "".$error."" or die "Could not open file: ".$error." - $!\n"; flock( $out , LOCK_SH ) or die "Could not lock '".$out."' - $!\n"; chomp $msg; my $datestring = localtime(); print $out "".$level." - ".$msg." - ".$datestring."\n"; close $out or die "Could not close file: ".$error." - $!\n"; }