#!/usr/bin/perl
########################
#  Written by anpipi
#  2008-08-22
########################

use strict;
my $time = FormatTime(0);
my $LOADFILE = "/proc/loadavg";
my $MEMFILE = "/proc/meminfo";
my $logfile = "/var/log/load_log";
my @now=qw();
open (LOAD,"$LOADFILE") || die "Can't open this file!!";
my $load = <LOAD>;
close(LOAD);
my @j=split(/ /,$load);
for(my $i=0;$i<3;$i++)
{ @now[$i]=@j[$i];}
open (LOG,">> $logfile") || die "Can't open this file!!";
print LOG " $time  LOAD : @now[0]  @now[1] @now[2]\n";
close (LOG);

sub FormatTime
{
        my $format = shift || 0;
        my ($sec, $min, $hour, $day, $mon, $year) = localtime(time);

        $year = sprintf("%d", $year + 1900);
        $mon = sprintf("%02d", $mon + 1);
        $day = sprintf("%02d", $day);
        $hour = sprintf("%02d", $hour);
        $min = sprintf("%02d", $min);
        $sec = sprintf("%02d", $sec);

        SWITCH:
        {
                if ($format == 0) {
                                return sprintf("%s-%s-%s %s:%s:%s", $year, $mon, $day, $hour, $min, $sec);
                                last SWITCH;
                        }
                if ($format == 1) {
                                return sprintf("%s-%s-%s", $year, $mon, $day);
                                last SWITCH;
                        }
                if ($format == 2) {
                                return sprintf("%s:%s:%s", $hour, $min, $sec);
                                last SWITCH;
                        }
                if ($format == 3) {
                                return sprintf("%s%s%s", $year, $mon, $day);
                                last SWITCH;
                        }
        }
}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()