Extract data from MySQL using Perl for statistical analysis code

Source: Internet
Author: User

The requirements are as follows:

1. There is an sms_info table in database mydata1. The record format is as follows:

 
 
  1. + ----------- + ---------------- + ------ + -----
  2. | Field |
  3. + ----------- + ---------------- + ------ + -----
  4. | Id | 12345
  5. | Number | 136123456789 indicates the contact number of a person.
  6. | Service | check_ping nagios Monitoring service name
  7. | Message | ping failure .................. Nagios alarm information
  8. | Send_time | 13:25:16 SMS sending time
  9. | Is_send | y indicates that the SMS is successfully sent, and n indicates that the SMS is failed.
  10. + ----------- + ---------------- + ------ + -----

The purpose of this table is to save the service alert information, the alert time, the alert information, the contact person, and whether the SMS is successfully sent.

2. There is a contact table in database mydata2. The record format is as follows:

 
 
  1. + ----------- + ---------------- + ------ + -----
  2. Id 12345.
  3. Telephone 136123456789
  4. Contact_name Zhang San
  5. + ----------- + ---------------- + ------ + -----

This table records the O & M personnel with phone numbers.

To achieve this, you need to calculate the number of text messages each sent to related O & M personnel for each service in March.

A service may have multiple contacts during the alarm. During the alarm, it needs to send a text message to send an alarm, and each contact needs to send a text message. Therefore, my task is to count the number of messages sent to each contact of this service during the period of January. The number of successes and failures.

Sample:

 
 
  1. "Service Name Date: Number of successful O & M personnel failures
  2. Check_category 0:05:06 Zhang San: 187 ********** 739 11
  3. Check_category 0:05:08 Li Si: 186 ********** 750 2
  4. Check_update 0:05:23 Ma 6: 151 ********** 538 17
  5. Check_update 0:05:23 Zhang San: 187 ********** 539 16
  6. Www. blo-httpd 0:06:56 Old Five: 135 *********** 6 0
  7. Www. blo-httpd 0:09:56 Old Five: 135 *********** 9 1

The check_category service successfully sent 739 text messages to Michael Jacob in March and 11 failed messages.

Check_updateThe Service successfully sent 538 text messages to Ma 6th and failed 17 times in March.

The implementation code is as follows:

 
 
  1. #! /Usr/bin/perl-w
  2.  
  3. Use strict;
  4. Use DBI;
  5.  
  6.  
  7.  
  8. Open (OUTFILE ,">>Outfile ");
  9.  
  10. # Connecting to a database
  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. # Obtain all service names in January
  15. My $Something= $ Dbh->Prepare ("select service from data_info where SUBSTRING (send_time, 2012) = '2017-01 'group by service order by send_time ");
  16.  
  17. # Set the format. Otherwise, the data retrieved by perl will be garbled.
  18. $ Dbh->Do ("set names 'utf8 '");
  19. $ Dbh_contact_name->Do ("set names 'utf8 '");
  20. $ Something->Execute ();
  21.  
  22. # Output mode:
  23. Print OUTFILE ("service name \ t date \ t o & M personnel \ t \ t \ t successes \ t failures \ t \ n ");
  24.  
  25. # Retrieve data from the database and save it to a file
  26.  
  27. While (my $Service_name= $ Something->Fetchrow_array ()){
  28. My @Rows= (); # Save the data obtained from the database
  29.  
  30.  
  31. # Count the number of messages sent by each service, which is the number of successfully sent messages and the number of failed messages.
  32.  
  33. My $Service_sms= $ Dbh->Prepare ("select service, send_time, number, count (*) from sms_info where SUBSTRING (send_time, 2012) = '2017-01' andService='$ Service_name'AndIs_send='Y'Group by number order by send_time ;");
  34. $ Service_sms->Execute ();
  35.  
  36. # Obtain the total number of times each service sends messages
  37. # Retrieve the phone number, used to find the relevant person name in the mydata2 Database
  38.  
  39. My $Total= $ Dbh->Prepare ("select COUNT (*) from sms_info where SUBSTRING (send_time, 2012) = '2014-01 'andService='$ Service_name'Group by number order by send_time ");
  40. $ Total->Execute ();

  41. While ($ rows [0], $ rows [1], $ rows [2], $ rows [3]) = $ service_sms->Fetchrow_array ()){
  42. $ Rows [4] = $ total->Fetchrow_array ()-$ rows [3];
  43. My $Contact_name= $ Dbh_contact_name->Prepare ("select contact_name from contact wherePhone= $ Rows [2] ");
  44. $ Contact_name->Execute ();
  45.  
  46. $ Rows [5] = $ contact_name->Fetchrow_array ();
  47.  
  48. Print OUTFILE "$ rows [0] \ t $ rows [1] \ t $ rows [5]: $ rows [2] \ t $ rows [3] \ t $ rows [4] \ n ";
  49. }
  50. }
  51. Close (OUTFILE );
  52.  
  53. $ Dbh->Disconnect ();
  54. $ Dbh_contact_name->Disconnect ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.