• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

perl实现监控linux

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1、使用root用户telnet进入linux系统

2、修改DNS以下两种方法

A、通过setup命令配置dns

B、通过在/etc目录下创建resolv.conf文件

3、查看DNS是否配置成功

[root@BL360-25]# 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? [yes]

输入: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 

提示是否安装相关模块,请输入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[0] and $ARGV[0] =~ /^(-h|--help|0)$/;

# process [interval [count]],
my ($interval, $loop_max);
if (defined $ARGV[0]) {
$interval = $ARGV[0];
$loop_max = defined $ARGV[1] ? $ARGV[1] : 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] | [interval [count]]
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;
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Perl中的字符串操作函数发布时间:2022-07-22
下一篇:
perl中的map和grep发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap