wslhs 发表于 2018-8-31 12:17:31

用perl 从mysql取出数据做统计分析代码

  为完成老大的要求,我写了一个代码完成这个工作。现在记录在博客上,跟大家分享一下。脚本水平很差,((o(´∀`)o))。
  要求如下:
  1、数据库mydata1中有个sms_info表,表中记录格式如下:
  


[*]+-----------+------------------+------+-----
[*]| Field   |
[*]+-----------+------------------+------+-----
[*]| id      |12345
[*]| number    |136123456789                  表示人员电话联系号码
[*]| service   |check_ping                  nagios监控服务名
[*]| message   |ping不通………………               nagios报警信息
[*]| send_time |2011-09-12 13:25:16         短信发送时间
[*]| is_send   |y                           y表示成功发送短信,n表示发送失败
[*]+-----------+------------------+------+-----
  

  这个表的作用是,保存 nagios 报警时,哪个服务报的警,在什么时间,报警信息,联系了谁,以及短信是否发送成功。
  2、数据库mydata2中有个contact表,表中记录格式如下:
  


[*]+-----------+------------------+------+-----
[*]   id         12345
[*]   phone      136123456789
[*]   contact_name 张三
[*]+-----------+------------------+------+-----
  

  这个表记录有电话号码的所属运维人员。
  实现需要实现的是,统计出各个服务在1月份各发送了多少短信给相关运维人员。
  一个服务,在报警的时候,可能有多个联系人,在报警的时候,它需要发送短信去报警,每个联系人都需要发送一次短信。所以我的任务是,统计出这个服务在1月份这个时间段里,发送给它各个联系人的短信次数有多少次。成功的次数,失败的次数。
  样本如下:
  


[*]"服务名                  日期         运维人员       成功次数失败次数
[*]check_category2012-1-1    0:05:06 张三:187********* 739       11
[*]check_category2012-1-1    0:05:08 李四:186********* 750      2
[*]check_update    2012-1-1    0:05:23 马六:151********* 538       17
[*]check_update    2012-1-1    0:05:23 张三:187********* 539       16
[*]www.blo-httpd   2012-1-1    0:06:56 老五:135********* 6          0
[*]www.blo-httpd   2012-1-1    0:09:56 老五:135********* 9          1
  

  check_category服务在1月份 成功发送给张三 739次短信,失败11次。
  check_update服务在1月份 成功发送给马六 538次短信,失败17次。
  实现代码如下:
  


[*]#!/usr/bin/perl -w
[*]
[*]use strict;
[*]use DBI;
[*]
[*]
[*]
[*]open (OUTFILE,">>outfile");
[*]
[*]#连接数据库
[*]my $dbh = DBI->connect("DBI:mysql:database=mydata1;host=localhost", "root", "123456", {'RaiseError' => 1});
[*]my $dbh_contact_name = DBI->connect("DBI:mysql:database=mydata2;host=localhost","root","123456",{'RaiseError' => 1});
[*]
[*]# 取得1月份所有的服务名
[*]my $sth = $dbh->prepare("selectservice   from data_info where SUBSTRING(send_time,1,7)='2012-01'group by service order by send_time ");
[*]
[*]#设置格式,否则perl取出的数据会乱码
[*]$dbh->do("SET NAMES 'utf8'");
[*]$dbh_contact_name->do("SET NAMES 'utf8'");
[*]$sth->execute();
[*]
[*]# 输出 模式为:
[*]print OUTFILE ("服务名\t\t\t\t\t\t\t日期\t\t\t\t\t 运维人员 \t\t\t\t\t 成功次数\t\t失败次数\t\n");
[*]
[*]#从数据库中取出数据并保存到文件中
[*]
[*]while(my $service_name = $sth->fetchrow_array()) {
[*]
[*]    my @rows = (); #保存从数据库中取得的数据
[*]
[*]
[*]    #统计每个服务发送的信息的次数,分别为成功发送的次数和失败次数
[*]
[*]    my $service_sms = $dbh->prepare("selectservice,send_time,number,count(*)   from sms_info where SUBSTRING(send_time,1,7)='2012-01' and service='$service_name' and is_send='y' group by number order by send_time ;") ;
[*]    $service_sms->execute();
[*]
[*]
[*]    #获得每一个服务发送信息的总次数
[*]    #取出电话号码,用于在mydata2数据库中查找相关人名
[*]
[*]    my $total = $dbh->prepare("selectCOUNT(*)   from sms_info where SUBSTRING(send_time,1,7)='2012-01' and service='$service_name'group by numberorder by send_time ") ;
[*]    $total->execute();
[*]
[*]while(($rows,$rows,$rows,$rows) = $service_sms->fetchrow_array()){
[*]    $rows = $total->fetchrow_array() - $rows;
[*]    my $contact_name = $dbh_contact_name->prepare("select contact_name from contact where phone=$rows");
[*]    $contact_name->execute();
[*]
[*]    $rows =$contact_name->fetchrow_array();
[*]
[*]    print OUTFILE " $rows \t $rows \t $rows:$rows \t $rows\t $rows\n";
[*]}
[*]}
[*]close(OUTFILE);
[*]
[*]$dbh->disconnect();
[*]$dbh_contact_name->disconnect();
  



页: [1]
查看完整版本: 用perl 从mysql取出数据做统计分析代码