實現需求:html
因爲公司用的短信網關是收費的,在每一個月初都必須作一次統計,在統計的數據中也能夠看出每一個運維人員所屬業務告警的頻繁狀況,這樣能夠針對該些業務作對應的對策。爲此需求寫了一個運維短信統計腳本,其中運維人員信息和告警短信這兩張數據庫表都放在不一樣的數據庫,爲此引用了多個哈希,腳本以下:mysql
#!/usr/bin/perl -w use strict; use warnings; use DBI; use Data::Dumper; use MIME::Lite; use MIME::Base64; use Getopt::Long; my $debug ||= 0; my $result = GetOptions( "debug" => \$debug, ); my $num_hash; my $sms_hash; my $not_sms_hash; my $total_num_hash; my $start_time = `date -d "-1 month" "+%Y-%m-01"`; my $end_time = `date -d "\`date -d "now" "+%Y-%m-01"\` - 1days" "+%Y-%m-%d"`; chomp $start_time; chomp $end_time; my $output = ''; my $database = "xxxxx"; my $hostname = "xxxxxx"; my $port = "3306"; my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port"; my $user = "xxxxxx"; my $password = "xxxxxx"; my $dbh = DBI->connect($dsn, $user, $password); my $sth = $dbh->prepare("set names utf8"); $sth->execute; my $sql = sprintf("select contact_name,phone from contact where phone!=''"); $sth = $dbh->prepare($sql); $sth->execute; while (my $ref = $sth->fetchrow_hashref) { my $contact_name = $ref->{'contact_name'}; my $num = $ref->{'phone'}; $num_hash->{$contact_name} = $num; } print Dumper $num_hash if $debug; $database = "sms"; $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port"; $user = "xxxxx"; $password = "xxxxxx"; $dbh = DBI->connect($dsn, $user, $password); $sth = $dbh->prepare("set names utf8"); $sth->execute; $sql = sprintf("select number,is_send,service from sms_info where send_time>'$start_time' and send_time<'$end_time' order by send_time DESC"); $sth = $dbh->prepare($sql); $sth->execute; while (my $ref = $sth->fetchrow_hashref) { my $is_send = $ref->{'is_send'}; my $num = $ref->{'number'}; my $service = $ref->{'service'}; if (! defined $sms_hash->{$num}->{$service}) { $sms_hash->{$num}->{$service} = 0; } if (! defined $not_sms_hash->{$num}) { $not_sms_hash->{$num}->{$service} = 0; } if ($is_send eq 'y') { $sms_hash->{$num}->{$service} ++; } else { $not_sms_hash->{$num}->{$service} ++; } } print Dumper $sms_hash if $debug; foreach my $num( keys %{$sms_hash} ){ if(! defined $total_num_hash->{$num}){ $total_num_hash->{$num} = 0; } foreach my $service(keys %{$sms_hash->{$num}}){ foreach my $service_num($sms_hash->{$num}->{$service}){ $total_num_hash->{$num} += $service_num; } } } print Dumper $total_num_hash if $debug; my ($personal_succeed,$personal_failed); foreach my $num (sort { $total_num_hash->{$b} <=> $total_num_hash->{$a} } keys %{$total_num_hash} ) { foreach my $name (keys %{$num_hash}) { my $control = 0; if ($num == $num_hash->{$name}) { $personal_succeed = &personal_succeed($num); $personal_failed = &personal_failed($num); my @array = &sort_sms($num); foreach my $hashref (@array) { my ($service, $value) = each %$hashref; #print "$key =>; $value\n"; if($value == 0){ last; }else{ if ( $control == 0){ $output .= "<tr><td>" . $name . "</td><td>" . $num . "</td><td>" . $personal_failed . "</td><td>" . $personal_succeed . "</td><td>" .$service ." : ". $value . "</td></tr>"; }else{ $output .= "<tr><td></td><td></td><td></td><td></td><td>" .$service ." : ". $value. "</td></tr>"; } $control ++; } } } } } my $output_top = "<html><body><table border=1><tr><th>姓名</th><th>電話</th><th>發送失敗</th><th>發送成功</th><th>對應服務發送的條數</th>"; my $output_bottom = "</table></body></html>"; my $output_all = $output_top . $output . $output_bottom; my $subject = "[$start_time - $end_time]監控告警短信發送統計"; &send_mail($subject); sub send_mail{ my $subject = shift; my $msg=MIME::Lite->new( From => "xxxx\@xxx.com", To => 'xxx@xxx.com', Cc => 'xxx@xxx.com', # Subject => "=?UTF-8?B?" . encode_base64("監控告警短信發送統計","") . "?=", Subject => "=?UTF-8?B?" . encode_base64("$subject","") . "?=", Encoding => "base64", Type => 'text/html;charset=UTF-8', Data => "$output_all", ); MIME::Lite->send('smtp', 'mail.xxxx.com', AuthUser => 'xxxx@xxxx.com', AuthPass => 'xxxxxxxxxxx', debug => 0, ); $msg->send() or die "Send mail fail: $!\n"; } ##每一個人發送成功的短信條數統計## sub personal_succeed{ my $num = shift; my $succeed = 0; foreach my $service (keys %{$sms_hash->{$num}}){ foreach my $service_num($sms_hash->{$num}->{$service}){ $succeed += $service_num; } } return $succeed; } ##每一個人發送失敗的短信條數統計## sub personal_failed{ my $num = shift; my $failed = 0; foreach my $service (keys %{$not_sms_hash->{$num}}){ foreach my $service_num($not_sms_hash->{$num}->{$service}){ $failed += $service_num; } } return $failed; } ##發送成功的短信條數統計## sub send_succeed{ my $succeed = 0; foreach my $num (keys %{$sms_hash} ) { foreach my $name (keys %{$num_hash}) { if ($num == $num_hash->{$name}) { foreach my $service (keys %{$sms_hash->{$num}}){ foreach my $service_num ($sms_hash->{$num}->{$service}){ $succeed += $service_num; } } } } } return $succeed; } ##發送失敗的短信條數統計## sub send_failed{ my $failed = 0; foreach my $num (keys %{$not_sms_hash} ) { foreach my $name (keys %{$num_hash}) { if ($num == $num_hash->{$name}) { foreach my $service (keys %{$not_sms_hash->{$num}}){ foreach my $service_num($not_sms_hash->{$num}->{$service}){ $failed += $service_num; } } } } } return $failed; } ##對每一個人所屬服務告警短信條數哈希的值進行排序## sub sort_sms{ my $num = shift; my @array; print "---------------------------------------------------------------\n" if $debug; @array = map {{($_ => $sms_hash->{$num}->{$_})}} sort {$sms_hash->{$num}->{$b} <=> $sms_hash->{$num}->{$a} or $b cmp $a} keys %{$sms_hash->{$num}}; print Dumper @array if $debug; return @array; }
################################################
sql
$num_hash哈希:數據庫
$VAR1 = {運維
'張三' => 'xxxxxxxxxxx',ide
'李四' => 'xxxxxxxxxxx',fetch
};spa
發送短信成功的哈希$sms_hash和發送失敗的哈希$not_sms_hash:debug
$VAR1 = {code
'xxxxxxxxxxx' => {
'mfs_metadata' => 1
},
'xxxxxxxxxxx' => {
'news_substriber3_log' => 1
},
};
效果: