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. +--------------------+-------------+------------+---------------------+ 

 本文转自dongfang_09859 51CTO博客,原文链接:http://blog.51cto.com/hellosa/705854,如需转载请自行联系原作者

相关文章
|
4天前
|
Perl
Perl 教程 之 Perl POD 文档 1
Perl教程介绍了POD(Plain Old Documentation)——一种在Perl模块和脚本中嵌入的简单文档格式。POD文档由=head1开始,=cut结束,中间内容会被忽略。示例展示了如何使用POD注释,包括利用__END__或__DATA__来注释后续内容。当程序执行时,POD文档不会影响输出结果。
18 0
|
4天前
|
索引 Perl
Perl 教程 之 Perl POD 文档 2
Perl的POD文档是嵌入式简单标记语言,用于模块和脚本的文档编写。POD始于=head1,止于=cut,包含普通、原文和命令段落。命令如=pod, =head1(标题),=over/=back(列表),=encoding(编码)和=cut。可以用pod2html转换为HTML。例子展示了如何创建HTML头。通过pod2html命令,POD文档能转成HTML文件供浏览器查看。
8 0
|
4天前
|
索引 Perl
Perl 教程 之 Perl POD 文档 3
Perl教程介绍了POD(Plain Old Documentation),一种用于Perl模块和脚本的简单文档格式。POD始于=head1,止于=cut,包含普通、原文和命令段落。命令如=head1定义标题,=over和=item创建列表,=begin/html至=end/html允许嵌入HTML。通过pod2html命令可将POD转换为HTML。示例展示了如何创建HTML文档和链接。
23 6
|
应用服务中间件 测试技术 nginx
Python 实时获取任务请求对应的Nginx日志
Python 实时获取任务请求对应的Nginx日志
81 1
|
监控 固态存储 Linux
Python对系统数据进行采集监控——psutil
介绍一个可以获取当前系统信息的库——psutil
354 0
Python对系统数据进行采集监控——psutil