运维短信网关统计脚本

实现需求: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

             },

   };


效果:

170025849.png

相关文章
相关标签/搜索