perl 采集入库脚本分享
程序员文章站
2022-04-29 19:13:07
#!/usr/bin/perl -w
use dbi;
use posix qw(strftime);
my $dbh = dbi->connect(“...
#!/usr/bin/perl -w use dbi; use posix qw(strftime); my $dbh = dbi->connect(“dbi:mysql:shencan:111.1.32.153:3306″,”shencan”,”shencan”); #my $sql = “select * from shencan.lvs”; #my $sth = $dbh->prepare($sql); #$sth->execute(); my $hostname=`hostname`; chomp($hostname); my $type; my $hostinfo; #while($hostname = $sth->fetchrow_hashref()) #{ # print “$hostname->{hostname} $hostname->{hostip}\n”; #} #my @value; #while(@value = $sth->fetchrow_array()) #{ # print “@value\n”; #} #my $sql = “update shencan.lvs set node='cbn_cq' where hostname='cbn-cq-1-3z2′”; #my $sth = $dbh->prepare($sql); #$sth->execute(); #####mysql update sub start ##### sub update{ my $sql = shift @_; my $mysqlupdate = $dbh->prepare($sql); $mysqlupdate->execute(); } #####mysql update sub end ##### #sub scselect { # my $sql = shift @_; # my $myselect = $dbh->prepare($sql); # $myselect->execute(); # while($hostinfo = $myselect->fetchrow_hashref()) { print “$type\n” ;print “$hostinfo->{$type} \n” ;} #} #####mysql select sub start##### sub shencan { my @shencan = @_; foreach $type (@shencan){ my $sql = “select * from shencan.fc where hostname='$hostname'”; my $myselect = $dbh->prepare($sql); $myselect->execute(); while($hostinfo = $myselect->fetchrow_hashref()) { return “$hostinfo->{$type}” ; } } } #####mysql select sub end###### #####mysql select start ####### my $fc =shencan “fcversion”; chomp($fc); my $os = shencan “os”; chomp($os); my $arch = shencan “arch”; chomp($arch); my $fdns =shencan “fdnsversion”; chomp($fdns); my $ng = shencan “ngversion”; chomp($ng); my $amr =shencan “amrversion”; chomp($amr); my $oh =shencan “ohversion”; chomp($oh); my $dm = shencan “dmversion”; chomp($dm); my $cpisbin = shencan “cpisbinversion”; chomp($cpisbin); my $node = shencan “node”; chomp($node); my $type1 =shencan “type”; chomp($type1); #####mysql select end###### ####check ng version start##### my $ng =`rpm -q ng`; chomp($ng); ####check ng version end##### ####check amr version start##### my $amr =`rpm -q amr`; chomp($amr); ####check amr version end##### ####check oh version start##### my $oh =`rpm -q oh`; chomp($oh); ####check oh version end##### ####check dm version start##### my $dm =`rpm -q dm`; chomp($dm); ####check dm version end##### ####check mftt version start##### my $mftt =`rpm -q mftt`; chomp($mftt); ####check mftt version end##### ####check cpisbin version start##### my $cpisbin = `rpm -q cpisbin`; chomp($cpisbin); ####check cpisbin version end##### ####check node start##### my @node=split(/-/,$hostname); $node = “$node[0]_$node[1]“; ####check node end##### ####check arch star ##### my $arch=`arch`; chomp($arch); #####check arch end ##### ####check ep53 status and update start##### my $ep53 =`dig \@127.0.0.1 www.qq.com >/dev/null && echo ok||echo bad`; chomp($ep53); update “update shencan.fc set ep53='$ep53′ where hostname='$hostname'”; ####check ep53 status and update end##### ####check ep80 status and update start##### my $gw=`route -n |awk ‘\$1==”0.0.0.0″{print \$2}'|awk -f”.” ‘{print \$1″.”\$2″.”\$3}'`; chomp($gw); my $ip=`ip a|grep inet|grep $gw|awk -f”[ /]” ‘{print\$6}'`; chomp($ip); my $ep80 =`curl -h “host:www.qq.com” -o /dev/null -s -w “%{http_code}” http://$ip`; chomp($ep80); update “update shencan.fc set ep80='$ep80′ where hostname='$hostname'”; ####check ep80 status and update end##### print “ng: $ng arm: $amr oh: $oh dm: $dm mftt:$mftt cpisbin: $cpisbin\n”; print “$hostname node: $node os: $os arch: $arch fc: $fc fdns: $fdns ng: $ng\n”; #####check os start###### sub os { open (os,”</etc/issue”); @line =<os>; $line= $line[0]; chomp($line); my @os =split(/\s+|\(|\)/,$line); if($os[0] ge “red”){ $sc = “rehl$os[6].$os[10]“; }else{ $sc = “$os[0]$os[2]“; } } os; close(os); #####check os end###### ######check fdns or name version start###### open (fdns,”</etc/chinacache/app.d/cpisbm.amr”); while(<fdns>){ if(/restartcmd/){ my @fc1 =split(/\s+/,$_); my $fc =”$fc1[2]“; if($fc ge “named”){ $fcver=`/usr/sbin/named -v`; chomp($fcver); my @fc1 =split(/\s+/,$fcver); $fcver1=”$fc1[0]$fc1[1]“; }else{ $fcver=`/flexidns/application/sbin/fdns -v`; chomp($fcver); my @fc1 =split(/\s+/,$fcver); $fcver1=”$fc1[0]$fc1[1]“; } } } close(fdns); ######check fdns or name version end###### #####check fcversion and type start###### open (fc,”</etc/chinacache/app.d/cpisfc.amr”); while(<fc>){ if(/restartcmd/){ my @squid =split(/\s+/,$_); my $squid =”$squid[2]“; if($squid ge “squid”){ $squidver=`/usr/local/squid/sbin/squid -v|head -n 1 |awk ‘{print “v”\$4}'`; chomp($squidver); $type=”fc5″; }else { $squidver=`/usr/local/flexicache/sbin/flexicache -v|awk ‘{print \$3}'`; chomp($squidver); $type=”fc6″; } } } close(fc); #####check fcversion and type end###### ####change mysql ####### if($squidver eq $fc){ }else{ update “update shencan.fc set fcversion='$squidver' where hostname='$hostname'”; } if($ng eq $ng){ }else{ update “update shencan.fc set ngversion='$ng' where hostname='$hostname'”; } if($dm eq $dm){ }else{ update “update shencan.fc set dmversion='$dm' where hostname='$hostname'”; } if($amr eq $amr){ }else{ update “update shencan.fc set amrversion='$amr' where hostname='$hostname'”; } if($oh eq $oh){ }else{ update “update shencan.fc set ohversion='$oh' where hostname='$hostname'”; } if($cpisbin eq $cpisbin){ }else{ update “update shencan.fc set cpisbinversion='$cpisbin' where hostname='$hostname'”; } if($node eq $node){ }else{ update “update shencan.fc set node='$node' where hostname='$hostname'”; } if($type eq $type1){ }else{ update “update shencan.fc set type='$type' where hostname='$hostname'”; } if($fcver1 eq $fdns){ }else{ update “update shencan.fc set fdnsversion='$fcver1′ where hostname='$hostname'”; } if($sc eq $os){ }else{ update “update shencan.fc set os='$sc' where hostname='$hostname'”; } if($arch eq $arch){ }else{ update “update shencan.fc set arch='$arch' where hostname='$hostname'”; } my $time = strftime “%y-%m-%d_%h:%m:%s”, localtime; chomp($time); update “update shencan.fc set lmtime='$time' where hostname='$hostname'”;