use Expect::Simple;
use Getopt::Long;
use Time::Local;
use Data::Dumper;
use POSIX;
use DBI;
use Net::OpenSSH;
$option = GetOptions ("n|ne=s" => \$ne, "debug" => \$debug, "expect_debug" => \$exp_debug);
$expect_debug = 0;
$expect_verbose = 0;
if($exp_debug eq 1) {
$expect_debug = 4;
$expect_verbose = 4;
}
debug('Check if ITP/STP is in the database list');
$ip = '';
$prompt_name = $ne;
$user = '';
$password_login = '';
$password_enable = '';
$is_ssh = 0;
#check if ITP/STP available in the database list
$dsn = "DBI:mysql:database=server_in;host=10.23.yy.xxx;port=3306";
$dbh = DBI->connect($dsn,'wedus','kadal');
$sql = "SELECT * FROM list_server WHERE prompt_name=" . $dbh->quote($prompt_name);
debug('SQL: ' . $sql);
$sth = $dbh->prepare($sql);
$sth->execute;
while(my $ref = $sth->fetchrow_hashref()) {
$ip = $ref->{'ip'};
$user = $ref->{'user'};
$password_login = $ref->{'password_login'};
$password_enable = $ref->{'password_enable'};
$is_ssh = $ref->{'is_ssh'};
debug('ITP/STP exists, IP address ' . $ip);
if ($is_ssh) {
debug('ITP/STP conection using SSH');
} else {
debug('ITP/STP conection using TELNET');
}
}
$sth->finish;
$sth = undef;
$dbh->disconnect();
$dbh = undef;
if ($ip eq '') {
debug('ITP/STP not found in database, exiting...');
quit(0);
}
if ($is_ssh == 2)
{ #Switch_SPS
$t = new Expect::Simple(
{
Cmd => ['ssh', $user . '@' . $ip ],
Prompt => ['Are you sure you want to continue connecting (yes/no)? ', -re => '.*\'s password:', $prompt_name.'>','Password:', $prompt_name.'#'],
DisconnectCmd => 'exit',
Timeout => 60,
Verbose => $expect_verbose,
Debug => $expect_debug
}
);
if($t->match_idx eq 1)
{
$t->send('yes');
}
debug('Connecting SSH to NE : ' . $ne . ' [' . $ip . ']');
my $ssh = &initSSH($prompt_name, $ip, $user, $password_login);
debug('Login to NE : ' . $ne . ' [' . $ip . ']');
$now = time();
%MONTH = (
"Jan"=> 1,
"Feb"=> 2,
"Mar"=> 3,
"Apr"=> 4,
"May"=> 5,
"Jun"=> 6,
"Jul"=> 7,
"Aug"=> 8,
"Sep"=> 9,
"Oct"=> 10,
"Nov"=> 11,
"Dec"=> 12
);
#$work_dir = $WORKDIR . $str_date . '/';
#$str_date = strftime('%Y%m%d_%H%M', localtime($now));
%config = ();
%current = ();
%maxi = ();
@data = ();
$str_date = strftime('%Y-%m-%d %H:%M:%S', localtime($now));
#my $cmd = "date";
my $cmd = "/IN/service_packages/SMS/check.sh.NCC "; #----->commandnya
my $cmd2= "date '+%Y-%m-%d %H:%M:%S'";
my @command = ();
my $rmtCmd = sprintf "%s", $cmd;
#$t->send($rmtCmd);
my ($out, $err) = $ssh->capture2($rmtCmd);
chomp $err;
warn sprintf "[%s] ERROR: %s\n", $prompt_name, $err if $ssh->error;
my @output = split /\n/, $out;
undef @copy_data;
$cran=0;
$data_true=0;
foreach (@output)
{
if (m/KAPASITAS/)
{
$data_true=$cran;
}
#debug($cran);
push(@copy_data, $_);
#printf "[%s] %s\n", $_;
$cran++;
}
my $rmtCmd2 = sprintf "%s", $cmd2;
#$t->send($rmtCmd);
my ($out2, $err2) = $ssh->capture2($rmtCmd2);
chomp $err2;
warn sprintf "[%s] ERROR: %s\n", $prompt_name, $err2 if $ssh->error;
my @output2 = split /\n/, $out2;
undef @copy_data2;
foreach (@output2)
{
push(@copy_data2, $_);
printf "[%s] %s\n", $_;
}
#0 1 2 3 4 5 6 7 8 9 10 11 12
#echo "$host | $DISK | $MEMORY | $SWAP | $DEBUG | $HARDWARE | $VERITAS | $LOG | $SLEE | $RESOURCES | $LINK_STATUS | $CAPACITY |DISK USAGE=$DUSAGE, MEMORY FREE=$MEMFREE, SWAP FREE=$SWAPFREE, sua_if_SMS=$DEBUGSMS, sua_if_V=$DEBUGV, sua_if_USSD=$DEBUGUSSD, eax TRACE=$EAXTRACE, APPDEBUG=$APPDEBUG, DISK OFFLINE=$OFFLINE, error log $ERROR times, slee process not started $SLEEPROCESS, $SLEERESOURCES, $LINK, KAPASITAS = $SLP "
#0 1 2 3 4 5 6 7 8 9 10 11 12
#ncc-plb-fda01 | OK | OK | OK | OK | OK | OK | OK | OK | OK | NOK | OK |DISK USAGE=50, MEMORY FREE=18, SWAP FREE=100, sua_if_SMS=false, sua_if_V=, sua_if_USSD=, eax TRACE=, APPDEBUG=0, DISK OFFLINE=0, error log 0 times, slee process not started 0, SLEE RESOURCES UNKNOWN: NAGIOS_BASE not set. Check Nagios user profile., igb0 NOK igb1 NOK igb2 NOK nxge0 NOK nxge1 NOK nxge2 NOK, KAPASITAS = 96
$split_pagar = @copy_data[$data_true];
@pisah_pagar = split('\|', $split_pagar);
undef @hasil_pagar;
foreach(@pisah_pagar)
{
push(@hasil_pagar,$_);
}
#0 1 2 3 4 5 6 7 8 9 10 11 12 13
#DISK USAGE=$DUSAGE, MEMORY FREE=$MEMFREE, SWAP FREE=$SWAPFREE, sua_if_SMS=$DEBUGSMS, sua_if_V=$DEBUGV, sua_if_USSD=$DEBUGUSSD, eax TRACE=$EAXTRACE, APPDEBUG=$APPDEBUG, DISK OFFLINE=$OFFLINE, error log $ERROR times, slee process not started $SLEEPROCESS, $SLEERESOURCES, $LINK, KAPASITAS = $SLP "
#0 1 2 3 4 5 6 7 8 9 10 11 12 13
#DISK USAGE=50, MEMORY FREE=18, SWAP FREE=100, sua_if_SMS=false, sua_if_V=, sua_if_USSD=, eax TRACE=, APPDEBUG=0, DISK OFFLINE=0, error log 0 times, slee process not started 0, SLEE RESOURCES UNKNOWN: NAGIOS_BASE not set. Check Nagios user profile., igb0 NOK igb1 NOK igb2 NOK nxge0 NOK nxge1 NOK nxge2 NOK, KAPASITAS = 96
#printf "kadal.' '.'-'.%s %s\n", @hasil_pagar[12];
$split_koma = $hasil_pagar[12];
@pisah_koma = split(',', $split_koma);
undef @hasil_koma;
foreach(@pisah_koma)
{
debug($_);
push(@hasil_koma,$_);
}
$ulangane = 7;
for (my $i = 0; $i < $ulangane; $i++)
{
if ($i>3 && $i<6){$k=$i+3;}
elsif ($i>5){$k=$i+7;}
else {$k=$i;}
undef @hasil_dusage;
@cari_angka = split(/=/, $hasil_koma[$k]);
foreach(@cari_angka)
{
push(@hasil_dusage,$_);
}
$normalisasi = 0;
push(@data_0_3,@hasil_dusage[$normalisasi+1]);
}
@eror_log = split(' ', $hasil_koma[9]);
foreach(@eror_log)
{
debug($_);
push(@hasil_eror_log,$_);
}
@slee_process = split(' ', $hasil_koma[10]);
foreach(@slee_process)
{
debug($_);
push(@hasil_sleeprocess,$_);
}
$dusage = @data_0_3[0];
$memfree = @data_0_3[1];
$swapfree = @data_0_3[2];
$debugsms = @data_0_3[3];
$hardware = @hasil_pagar[5];
$link = @hasil_pagar[10];
$appdebug = @data_0_3[4];
$offline = @data_0_3[5];
$slp = @data_0_3[6];
$error_times = @hasil_eror_log[2];
$sleeprocess = @hasil_sleeprocess[4];
for (my $i = 0; $i < $ulangane; $i++)
{
if ($i>3 && $i<6){$k=$i+3;}
elsif ($i>5){$k=$i+7;}
else {$k=$i;}
printf "kadal.' '.$i.'-'.%s %s\n", @data_0_3[$i];
}
printf "kadal.' '.7.'-'.%s %s\n", $error_times;
printf "kadal.' '.8.'-'.%s %s\n", $sleeprocess;
printf "kadal.' '.9.'-'.%s %s\n", $hardware;
printf "kadal.' '.10.'-'.%s %s\n", $link;
printf "kadal.' '.11.'-'.%s %s\n", @copy_data2[0];
}
#STTORE DATA KE MYSQL
$str_insert = strftime('%Y-%m-%d %H:%M:%S', localtime($now));
$str_date_update = UTC2LocalString(@copy_data2[0]);
debug($str_date_update);
$dbh = DBI->connect($dsn,'ttmsc','ttmsc_core');
$sql = "INSERT INTO audit_in VALUES ( '" .
$ne . "', '" . $str_date_update . "', '" . $str_insert . "'," .
$dusage . ", " . $memfree .",".
$swapfree . ",'" . $debugsms . "'," . $appdebug . "," .
$offline . "," . $error_times .",".
$sleeprocess . ",'" . $link ."','".
$hardware . "'," . $slp .
")";
debug('SQL: ' . $sql);
$dbh->do($sql);
$dbh->disconnect();
undef $dbh;
debug('Disconnecting from NE : ' . $ne . ' [' . $ip . ']');
sub debug {
my $str = shift;
if($debug eq 1) {
$str =~ s/\s+$//g;
if($exp_debug eq 1)
{
print STDERR "\nDEBUG: $str\n";
} else
{
print STDERR "DEBUG: $str\n";
}
}
}
sub initSSH()
{
my ($prompt_name, $ip, $user, $password_login) = @_;
my $ssh = Net::OpenSSH->new($ip,
user => $user,
passwd => $password_login,
timeout => 200,
master_opts => ['-q'],
);
die sprintf "Box: %s\t%s\n", $prompt_name, $ssh->error if $ssh->error;
return $ssh;
}
sub UTC2LocalString
{
my $t = shift;
my ($datehour, $rest) = split(/:/,$t,2);
my ($year, $month, $day, $hour) =
$datehour =~ /(\d+)-(\d\d)-(\d\d)\s+(\d\d)/;
# proto: $time = timegm($sec,$min,$hour,$mday,$mon,$year);
my $epoch = timegm (0,0,$hour,$day,$month-1,$year);
# proto: ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
# localtime(time);
my ($lyear,$lmonth,$lday,$lhour,$isdst) =
(localtime($epoch))[5,4,3,2,-1];
$lyear += 1900; # year is 1900 based
$lmonth++; # month number is zero based
#print "isdst: $isdst\n"; #debug flag day-light-savings time
return ( sprintf("%04d-%02d-%02d %02d:%s",
$lyear,$lmonth,$lday,$lhour,$rest) );
}