perl monitor script积累

1. 监控中间件应用脚本

#!/bin/evn perl
$port = $ARGV[0];
$group = $ARGV[1];

$ret = `/home/a/bin/pat -s kfc_${port}_${group} -c|cut -d"\t" -f 3-`;
chomp($ret);
printf("%s\t%s", time, $ret);
(1) ARGV 与C语言里面的argv[]数组类似,用来接收脚本的参数列表

(2) /home/a/bin/pat  是一个perl脚本 后面的是此脚本的参数

(3) 一般取变量的值是$var即可,但是$var 前后有特殊符号,比如 _  时要用 ${var}形式

(4) cut -d"<ctrl>v<tab>" -f 3-  把前面脚本获取的数据用制表符(tab)为间隔; -f 3- 从第3个字段开始后面的字段都打印出来

(5). $var = `shell 命令`  把shell命令的结果赋给var 变量


2. nfs的监控

#!/usr/bin/perl
my $status = '';
my @options=('Size', 'Used', 'Avail', 'UsedRate');
$nfs_dir =  $ARGV[0];     #第一个参数


if(`df -h $nfs_dir 2>/dev/null|grep '/home/a/nfs'|wc -l` != 1) {
   $status=0;
} 
else {
    $status=1;
    $s1 = `df -h $nfs_dir|grep -v Filesystem`;  #grep -v *** 反向显示
    $s1 =~ s/\n//g;                             #这个正则表达式作用是删除换行 ---删除 \n;g--->指替换所有的\n
    @array = split(/\s+/, $s1);                 #把字符串$s1 按空格\s 进行分割,分割的字符串放到array中
}
#output
printf("%s\tnfs=%s ", time, $status);
for($i=0;$i<4;++$i){
   printf("$options[$i]=%s ",$array[$i+1]);    #$array[$i]   读数组array的值,用$array[$i]
}


3. nginx 监控

#!/usr/bin/perl
my $str = `tsar -i 1 --nginx|tail -5|head -1`;    #tsar这样的最好用绝对路径
my @data = split(/\s+/, $str);
splice @data,0,1;                                 #splice 是操作数组的函数,可以像数组中插入元素,也可以删除元素
for($i=0;$i<=$#data;$i++) {
    if($data[$i] =~ /(.+)K$/i) {                  #如果$data[$i]是以K结尾(20k),i--->不区分K的大小写
       $data[$i] *= 1024;
    }
    elsif($data[$i] =~ /(.+)M$/i) {
        $data[$i] *= 1024*1024;
    }
}
@title = qw(accept handle reqs active read write wait qps rt);  #qw是以空格为分隔符把字符串分为很多元素;等价于:spilt(/\s+/, $str)
printf("%s\t",time);
for($i=0;$i<=$#data;$i++){
   printf("%s=%s ",$title[$i],$data[$i]);
}

(1)tsar -i 1 --nginx 采集nginx的信息,-i 1 是采集间隔是1s

(2) split(/\s+/,$str) 用空格把字符串 str 进行分成多个字符串并放在数组 data中

(3) 正则表达式:split(/\s+/,$str) ;$str = ~ /(.+)K$/i    


4. 监控qps

#!/usr/bin/perl
my $file_conf = $ARGV[0];
my $file_qps = $ARGV[1];
my @ignore_networkids = `cat $file_conf 2>/dev/null`;
my $ignore_str = join('|', @ignore_networkids);
$ignore_str =~ s/\n//g;   
$ignore_str = '^$' if(!$ignore_str);
my $networkid=`cat $file_qps 2>/dev/null|egrep -v "$ignore_str"|sort -k 2n,2n|head -1|awk '{ print \$1; }'`;
my $qps=`cat $file_qps 2>/dev/null|egrep -v "$ignore_str"|sort -k 2n,2n|head -1|awk '{ print \$2; }'`;
chomp($networkid);
chomp($qps);
printf("%s\tnetworkid=%s qps=%s", time, $networkid,$qps);

(1). join('|',@ignore_networkids) 把数组里面的元素用 | 连接起来; 如果连接的字符串中有换行,则把换行符删除

(2). 不知道为什么 awk '{print  \$1}'  要加一个\




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值