perl 獲取監控數據(續)

上次的腳本只是將監控的數據展示在終端上,這次增加了一個功能:數據展示在終端的同時,存入到數據庫中

code:

  1. #!/usr/bin/perl -w 
  2.  
  3. use strict; 
  4. use utf8; 
  5. use LWP::Simple; 
  6. use DBI; 
  7.  
  8. my $dsn = 'DBI:mysql:database=monitor;host=localhost;port=3306'
  9. my $dbh = DBI->connect($dsn,'root','test',{PrintError=>0,RaiseError=>1}) 
  10.                         or die "Can't connect to mysql" . DBI->errstr; 
  11.  
  12. my $table = qq/CREATE TABLE IF NOT EXISTS monitor ( 
  13.                room char(20) not null, 
  14.                in_bytes varchar(100) not null, 
  15.                out_bytes varchar(100) not null, 
  16.                date timestamp not null 
  17.                ) 
  18.               /; 
  19.  
  20. my $sth = $dbh->prepare($table); 
  21.    $sth->execute(); 
  22.    $sth->finish(); 
  23.  
  24. while (1) { 
  25.        my $url = shift || "http://xxx.xxx.xxx.xxx"; 
  26.        my $content = get $url; 
  27.        my @url = split /\n/,$content; 
  28.        my ( $i,$str ) = ( 0,\@url ); 
  29.        print scalar localtime,"\n"; 
  30.  
  31.        while ( $i < scalar @$str ) { 
  32.  
  33.              if ( @$str[$i++] =~ /杭州機房1|杭州機房2|上海機房1|上海機房2/ ) { 
  34.                  binmode STDOUT,'encoding(utf8)'; 
  35.                  my $room = $&; 
  36.                  print STDOUT "$&\n"; 
  37.                  $i += 16; 
  38.                  my ($in) = @$str[$i++] =~ /(?:.*)\>(\S*\s*\S*).*\<\/td\>/; 
  39.                  printf '%s',"\tCurrent In: $1\t"; 
  40.                  $i += 5; 
  41.                  my ($out) = @$str[$i++] =~ /(?:.*)\>(\S*\s*\S*).*\<\/td\>/; 
  42.                  printf '%s',"Current Out: $1\n\n"; 
  43.                  my ($sec,$min,$hour,$day,$month,$year) = (localtime(time))[0..5]; 
  44.                  $month++;$year+=1900; 
  45.                  my $date = sprintf "%04d-%02d-%02d %d:%d:%d",$year,$month,$day,$hour,$min,$sec; 
  46.  
  47.                  my $data = qq/INSERT INTO monitor VALUES 
  48.                               ("$room","$in","$out","$date")/; 
  49.                     $sth = $dbh->do($data); 
  50.              } 
  51.  
  52.        } 
  53.  
  54. print '-' x 70,"\n"; 
  55. sleep 900; 
  56.  
  57.  
  58. $dbh->disconnect(); 

代碼有些改動,主要是之前的一些寫法不太好。

數據庫中的圖

  1. +--------------------+-------------+------------+---------------------+ 
  2. | room               | in_bytes    | out_bytes  | date                | 
  3. +--------------------+-------------+------------+---------------------+ 
  4. | xxxx        | 85.9 Mb/s   | 99.5 Mb/s  | 2011-11-04 10:29:49 | 
  5. | xxxx        | 14.1 Mb/s   | 80.7 Mb/s  | 2011-11-04 10:29:49 | 
  6. | xxxx       | 190.3 Mb/s  | 332.6 Mb/s | 2011-11-04 10:29:49 | 
  7. | xxxx       | 8968.4 kb/s | 119.9 Mb/s | 2011-11-04 10:29:49 | 
  8. | xxxx        | 384.0 b/s   | 576.0 b/s  | 2011-11-04 10:29:49 | 
  9. +--------------------+-------------+------------+---------------------+ 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章