Chinaunix首页 | 论坛 | 博客
  • 博客访问: 276216
  • 博文数量: 64
  • 博客积分: 1758
  • 博客等级: 上尉
  • 技术积分: 543
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-25 11:13
文章分类
文章存档

2011年(54)

2010年(10)

我的朋友

分类: Python/Ruby

2011-04-02 15:42:50

#!/usr/bin/perl
use strict;
use File::Copy;
use File::Find;
use POSIX qw( strftime);
my $time = strftime("%Y-%m-%d %H:%M:%S", localtime(time));
my $date = strftime("%Y-%m-%d", localtime(time));
my $logfile = "/home/scripts/logs/server_check_$date.log";
my $io_detail = `sar 1 1 | grep Average`;
my @meminfo = `free -m`;
my $network = `sar -n DEV 1 1 | grep bond0 | grep ^Average`;
if (! -e $logfile) {
 `touch $logfile`;
}
open(LOG, ">>", "$logfile") or die "cannot open the file $logfile $!\n";
#Average:          all      1.37      0.00      0.62      7.49      0.00     90.51
chomp $io_detail;
if($io_detail =~ /Average:\s+all\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)/){
 my ($user, $nice, $system, $iowait, $idle) = ($1, $2, $3, $4, $6);
 print LOG ("cpu: $time: user: $user\tnice: $nice\tsystem: $system\tiowait: $iowait\tidle: $idle\n");
}
foreach my $mem_detail(@meminfo){
        chomp($mem_detail);
        if ($mem_detail =~ /^Mem:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/) {
                my ($total, $used, $free, $shared, $buffers, $cached) = ($1, $2, $3, $4, $5, $6);
                my $pro_used = $used - $buffers - $cached;
                my $useable = $free + $buffers + $cached;
                print LOG ("mem: $time: total: $total\tused: $used\tfree: $free\tpro_used: $pro_used\tuseable: $useable\n");
        }
}
chomp $network;
#Average:        bond0   1485.86   2182.83 931281.82 2284077.78      0.00      0.00      0.00
if($network =~ /Average:\s+bond0\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+/){
 #print "$1,$2,$3,$4\n";
 my ($rxpck, $txpck, $rxbyte, $txbyte) = ($1, $2, $3, $4);
 print LOG ("net: $time: rxpck: $rxpck\ttxpck: $txpck\trxbyte: $rxbyte\ttxbyte: $txbyte\n");
}
close(LOG);
my $deltime = 90*24*3600;
my $logfolder = "/home/scripts/logs";
#my $backupfd = "/home/scripts/";
find(\&filehandler, "$logfolder");
#find(\&backupdel, "$backupfd");
sub filehandler {
        my $file = $File::Find::name;
        my $sfile = $_;
        return unless -f $file;
        my $mtime = (stat($file))[9];
        my $smtime = strftime("%y%m%d %H:%M",localtime($mtime));
        my $del = time -$mtime;
        if ($del >  $deltime){
                unlink $file;
                print "mtime $smtime deltime $file\n"
        }
        if ($file =~ /log$/) {
                my $del = time - $mtime;
                if ($del >  30*24*3600){
                        unlink $file;
                }
                if ($del >  7*24*3600){
                        if($file !~ /gz/){
                                `gzip -9 "$file"`;
                                print "mtime $smtime deltime $sfile\n";
                        }
                }
        }
}
sub backupdel {
        my $backup = $File::Find::name;
        my $sbackup = $_;
        return unless -f $sbackup;

        my $mtime = (stat($backup))[9];
        my $smtime = strftime("%y%m%d %H:%M",localtime($mtime));
        my $del = time -$mtime;
        if ($del >  $deltime){
                unlink $backup;
                print "mtime $smtime deltime $backup\n"
        }
}
阅读(1219) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~