为完成老大的要求,我写了一个代码完成这个工作。现在记录在博客上,跟大家分享一下。脚本水平很差,((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_category??2012-1-1????0:05:06?张三:187*********?739???????11?
- check_category??2012-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("select??service???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?("服务名ttttttt??日期ttttt?运维人员?ttttt?成功次数tt失败次数tn");?
- ?
- #从数据库中取出数据并保存到文件中?
- ?
- while(my?$service_name?=?$sth->fetchrow_array())?{?
- ?????
- ????my?@rows?=?();?#保存从数据库中取得的数据?
- ?
- ?
- ????#统计每个服务发送的信息的次数,分别为成功发送的次数和失败次数?
- ?
- ????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?;")?;?
- ????$service_sms->execute();????
- ????
- ?
- ????#获得每一个服务发送信息的总次数?
- ????#取出电话号码,用于在mydata2数据库中查找相关人名?
- ?
- ????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?")?;?
- ????$total->execute();?
-
?????
- while(($rows[0],$rows[1],$rows[2],$rows[3])?=?$service_sms->fetchrow_array()){?
- ????$rows[4]?=?$total->fetchrow_array()?-?$rows[3];?
- ????my?$contact_name?=?$dbh_contact_name->prepare("select?contact_name?from?contact?where?phone=$rows[2]");?
- ????$contact_name->execute();?
- ?
- ????$rows[5]?=??$contact_name->fetchrow_array();?
- ?
- ????print?OUTFILE?"?$rows[0]?t?$rows[1]?t?$rows[5]:$rows[2]?t?$rows[3]t?$rows[4]n";?
- }?
- }?
- close(OUTFILE);?
- ?
- $dbh->disconnect();?
- $dbh_contact_name->disconnect();?