From d193976a132fe96b4f1b3fe19219bb6726d4b668 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Fri, 12 Mar 2010 13:36:23 -0800 Subject: Don't give error if /var/run/vyatta doesn't exist yet clear counters might be run when /var/run/vyatta hasn't been created yet. Also cleanup some identation and declarations --- scripts/vyatta-show-interfaces.pl | 44 ++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/scripts/vyatta-show-interfaces.pl b/scripts/vyatta-show-interfaces.pl index 063eb98..1b662c8 100755 --- a/scripts/vyatta-show-interfaces.pl +++ b/scripts/vyatta-show-interfaces.pl @@ -88,26 +88,29 @@ sub get_clear_stats { foreach my $var (@rx_stat_vars, @tx_stat_vars) { $stats{$var} = 0; } - my $FILE; + my $filename = get_intf_statsfile($intf); - if (!open($FILE, '<', $filename)) { - return %stats; - } - my $magic = <$FILE>; chomp $magic; + open (my $f, '<', $filename) + or return %stats; + + my $magic = <$f>; + chomp $magic; if ($magic ne $clear_file_magic) { print "bad magic [$intf]\n"; return %stats; } - my $timestamp = <$FILE>; chomp $timestamp; + + my $timestamp = <$f>; + chomp $timestamp; $stats{'timestamp'} = $timestamp; - my ($var, $val); - while (<$FILE>) { + + while (<$f>) { chop; - ($var, $val) = split(/,/); + my ($var, $val) = split(/,/); $stats{$var} = $val; } - close($FILE); + close($f); return %stats; } @@ -276,18 +279,21 @@ sub run_clear_intf { foreach my $intf (@intfs) { my %stats = get_intf_stats($intf); - my $FILE; my $filename = get_intf_statsfile($intf); - if (!open($FILE, ">", $filename)) { - die "Couldn't open $filename [$!]\n"; - } + + mkdir $clear_stats_dir unless ( -d $clear_stats_dir ); + + open(my $f, '>', $filename) + or die "Couldn't open $filename [$!]\n"; + print "Clearing $intf\n"; - print $FILE $clear_file_magic, "\n", time(), "\n"; - my ($var, $val); - while (($var, $val) = each (%stats)) { - print $FILE $var, ",", $val, "\n"; + print $f $clear_file_magic, "\n", time(), "\n"; + + while (my ($var, $val) = each (%stats)) { + print $f $var, ",", $val, "\n"; } - close($FILE); + + close($f); } } -- cgit v1.2.3