加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 00:28:49 所属栏目:大数据 来源:网络整理
导读:为完成老大的要求,我写了一个代码完成这个工作。现在记录在博客上,跟大家分享一下。脚本水平很差,((o(′?`)o))。 要求如下: 1、数据库mydata1中有个sms_info表,表中记录格式如下: ? ? +-----------+------------------+------+-----?? |?Field?????|?

为完成老大的要求,我写了一个代码完成这个工作。现在记录在博客上,跟大家分享一下。脚本水平很差,((o(′?`)o))。

要求如下:

1、数据库mydata1中有个sms_info表,表中记录格式如下:

?

?

 
 
  1. +-----------+------------------+------+-----??
  2. |?Field?????|?????????
  3. +-----------+------------------+------+-----??
  4. |?id????????|??12345?????
  5. |?number????|??136123456789? 表示人员电话联系号码
  6. |?service???|??check_ping????? nagios监控服务名
  7. |?message???|??ping不通………………????????? nagios报警信息
  8. |?send_time?|??2011-09-12?13:25:16???????? 短信发送时间
  9. |?is_send???|??y??????????????????????????? y表示成功发送短信,n表示发送失败
  10. +-----------+------------------+------+-----

这个表的作用是,保存 nagios 报警时,哪个服务报的警,在什么时间,报警信息,联系了谁,以及短信是否发送成功。

2、数据库mydata2中有个contact表,表中记录格式如下:

?

 
 
  1. +-----------+------------------+------+-----???
  2. ???id???????????12345?
  3. ???phone????????136123456789?
  4. ???contact_name?张三?
  5. +-----------+------------------+------+-----??

这个表记录有电话号码的所属运维人员。

实现需要实现的是,统计出各个服务在1月份各发送了多少短信给相关运维人员。

一个服务,在报警的时候,可能有多个联系人,在报警的时候,它需要发送短信去报警,每个联系人都需要发送一次短信。所以我的任务是,统计出这个服务在1月份这个时间段里,发送给它各个联系人的短信次数有多少次。成功的次数,失败的次数。

样本如下:

?

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

check_category服务在1月份 成功发送给张三 739次短信,失败11次。

check_update服务在1月份 成功发送给马六 538次短信,失败17次。

?

实现代码如下:

?

?

 
 
  1. #!/usr/bin/perl?-w?
  2. ?
  3. use?strict;?
  4. use?DBI;?
  5. ?
  6. ?
  7. ?
  8. open?(OUTFILE,">>outfile");?
  9. ?
  10. #连接数据库?
  11. my?$dbh?=?DBI->connect("DBI:mysql:database=mydata1;host=localhost",?"root",?"123456",?{'RaiseError'?=>?1});?
  12. my?$dbh_contact_name?=?DBI->connect("DBI:mysql:database=mydata2;host=localhost","root","123456",{'RaiseError'?=>?1});?
  13. ?
  14. #?取得1月份所有的服务名?
  15. my?$sth?=?$dbh->prepare("select??service???from?data_info?where?SUBSTRING(send_time,1,7)='2012-01'??group?by?service?order?by?send_time?");?
  16. ?
  17. #设置格式,否则perl取出的数据会乱码?
  18. $dbh->do("SET?NAMES?'utf8'");?
  19. $dbh_contact_name->do("SET?NAMES?'utf8'");?
  20. $sth->execute();?
  21. ?
  22. #?输出?模式为:?
  23. print?OUTFILE?("服务名ttttttt??日期ttttt?运维人员?ttttt?成功次数tt失败次数tn");?
  24. ?
  25. #从数据库中取出数据并保存到文件中?
  26. ?
  27. while(my?$service_name?=?$sth->fetchrow_array())?{?
  28. ?????
  29. ????my?@rows?=?();?#保存从数据库中取得的数据?
  30. ?
  31. ?
  32. ????#统计每个服务发送的信息的次数,分别为成功发送的次数和失败次数?
  33. ?
  34. ????my?$service_sms?=?$dbh->prepare("select??service,send_time,number,count(*)???from?sms_info?where?SUBSTRING(send_time,7)='2012-01'?and?service='$service_name'?and?is_send='y'?group?by?number?order?by?send_time?;")?;?
  35. ????$service_sms->execute();????
  36. ????
  37. ?
  38. ????#获得每一个服务发送信息的总次数?
  39. ????#取出电话号码,用于在mydata2数据库中查找相关人名?
  40. ?
  41. ????my?$total?=?$dbh->prepare("select??COUNT(*)???from?sms_info?where?SUBSTRING(send_time,7)='2012-01'?and?service='$service_name'??group?by?number??order?by?send_time?")?;?
  42. ????$total->execute();?
  43. ?????
  44. while(($rows[0],$rows[1],$rows[2],$rows[3])?=?$service_sms->fetchrow_array()){?
  45. ????$rows[4]?=?$total->fetchrow_array()?-?$rows[3];?
  46. ????my?$contact_name?=?$dbh_contact_name->prepare("select?contact_name?from?contact?where?phone=$rows[2]");?
  47. ????$contact_name->execute();?
  48. ?
  49. ????$rows[5]?=??$contact_name->fetchrow_array();?
  50. ?
  51. ????print?OUTFILE?"?$rows[0]?t?$rows[1]?t?$rows[5]:$rows[2]?t?$rows[3]t?$rows[4]n";?
  52. }?
  53. }?
  54. close(OUTFILE);?
  55. ?
  56. $dbh->disconnect();?
  57. $dbh_contact_name->disconnect();?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读