xlfm22 发表于 2015-12-27 07:48:02

perl实现监控linux

  1、使用root用户telnet进入linux系统
  2、修改DNS以下两种方法
   A、通过setup命令配置dns
   B、通过在/etc目录下创建resolv.conf文件
  3、查看DNS是否配置成功
   # cat/etc/resolv.conf
  nameserver 202.107.117.11
  nameserver 202.107.117.12
  1、使用root用户telnet进入linux系统
  2、#提示符下,输入cpan
  提示:Are you ready for manual configuration?
  输入:no
  进入cpan>状态
  1、在cpan>提示下,输入o conf urllist push http://www.cpan.org/
  2、在cpan>提示下,输入o conf urllist push ftp://ftp.cuhk.edu.hk/pub/packages/perl/CPAN/
  3、执行过程中无任何输出后,在cpan>提示下,输入o conf commit,提示:commit: wrote /usr/lib/perl5/5.8.8/CPAN/Config.pm,表示成功
  1、在cpan>提示下,输入o conf http_proxy http://proxy.neusoft.com:8080
  2、在cpan>提示下,输入o conf proxy_user 用户名
  3、在cpan>提示下,输入o conf proxy_pass 密码
  4、执行过程中无任何输出后,在cpan>提示下,输入o conf commit,提示:commit: wrote /usr/lib/perl5/5.8.8/CPAN/Config.pm,表示成功
  在cpan>提示下,输入install Sys::Statistics::Linux
  Fetching with LWP:
  http://www.cpan.org/authors/01mailrc.txt.gz
  Going to read root/。。。。。。
  UNIVERSAL::require
  Shall I follow them and prepend them to the queue
  of modules we are processing right now? yes
  提示是否安装相关模块,请输入yes
  All tests successful.
  最后测试都通过表示安装成功
  1、将linuxperfstat.tar包拷贝到linux系统,并解压出LinuxDiskStat.pm和linuxperfstat文件
  2、设置可执行权限
  chmod +x linuxperfstat
  3、linuxperfstat -h - 打印帮助
  具体使用方法请看readme.doc
  more linuxperfstat
  #!/usr/bin/perl -w
   
use strict;
use warnings;
use Sys::Statistics::Linux;
use Data::Dumper;
use LinuxDiskStat;
  my $loop = 0;               # current loop number
my $PAGESIZE = 20;          # max lines per header
my $lines = $PAGESIZE;      # counter for lines printed
  #
#Process command line args
#
usage() if defined $ARGV and $ARGV =~ /^(-h|--help|0)$/;
  # process ],
my ($interval, $loop_max);
if (defined $ARGV) {
    $interval = $ARGV;
    $loop_max = defined $ARGV ? $ARGV : 2**32;
    usage() if $interval == 0;
}
else {
    $interval = 1;
    $loop_max = 1;
}
  sub sim_show
  {
my ($rh_siminfo) = @_;
my $str = "";
foreach (sort(keys(%$rh_siminfo)))
{
      $str .= "$_: $rh_siminfo->{$_}\n";
}
return $str;
}
  my $lxs = Sys::Statistics::Linux->new(
      cpustats => {
            init   => 1,
      },
      diskstats => {
            init   => 1,
      },
      netstats => {
            init   => 1,
      },
      memstats => {
            init   => 1,
      },
    );
my $lxs_diskstat = LinuxDiskStat->new;
$lxs_diskstat->init;
my $NIC_SPEED = 100_000_000;
  while (1) {
  ### Print header
    if ($lines++ >= $PAGESIZE) {
      $lines = 0;
      printf "%-20s %28s\n", "", "------ Utilisation ------";
      printf "%-20s %7s %6s %6s %6s\n", "Time", "%CPU",
               "%Mem", "%Disk", "%Net";
    }
    my $stat = $lxs->get;
    my $time = $lxs->gettime;
    my $disk_stat = $lxs_diskstat->get;
  #
    #Calculate utilisation
    #
    my $ucpu = fetch_cpu($stat);
    my $umem = fetch_mem($stat);
    my $udisk = fetch_disk($disk_stat);
    my $unet = fetch_net($stat);
  printf "%-20s %7s %6s %6s %6s\n",$time, $ucpu, $umem, $udisk, $unet;
    ### Check for end
    last if ++$loop == $loop_max;
    ### Interval
    sleep $interval;
}
  sub fetch_cpu
{
my ($rh_stat) = @_;
my $str_cpu = "";
if(defined($rh_stat->cpustats->{cpu}->{total}))
{
      $str_cpu = $rh_stat->cpustats->{cpu}->{total};
      $str_cpu = sprintf "%.2f", $str_cpu;
}
return $str_cpu;
}
sub fetch_mem
{
my ($rh_stat) = @_;
my $str_mem = "";
if(defined($rh_stat->memstats))
{
      $str_mem = 100-$rh_stat->memstats->{memfree}/$rh_stat->memstats->{memtotal}*100;
      $str_mem = sprintf "%.2f", $str_mem;
}
return $str_mem;
}
sub fetch_disk
{
my ($rh_stat) = @_;
my $str_disk = "";
if(defined($rh_stat->{hda}->{ticks}))
  
  {
      $str_disk = $rh_stat->{hda}->{ticks}/10;
      $str_disk = sprintf "%.2f", $str_disk;
}
elsif(defined($rh_stat->{sda}->{ticks}))
{
      $str_disk = $rh_stat->{sda}->{ticks}/10;
      $str_disk = sprintf "%.2f", $str_disk;
}
elsif(defined($rh_stat->{xvdb1}->{ticks}))
{
      $str_disk = $rh_stat->{xvdb1}->{ticks}/10;
      $str_disk = sprintf "%.2f", $str_disk;
}
return $str_disk;
}
sub fetch_net
{
my ($rh_stat) = @_;
my $str_net = "";
if(defined($rh_stat->netstats->{eth0}->{ttbyt}))
{
      $str_net = ($rh_stat->netstats->{eth0}->{ttbyt}*800)/$NIC_SPEED;
      $str_net = sprintf "%.2f", $str_net;
}
return $str_net;
}
# usage - print usage and exit.
#
  sub usage {
      print STDERR <<END;
USAGE: linuxperfstat [-h] | ]
   eg, linuxperfstat               # print summary since boot only
       linuxperfstat 5             # print continually every 5 seconds
       linuxperfstat 1 5         # print 5 times, every 1 second
END
      exit 1;
}
  
页: [1]
查看完整版本: perl实现监控linux