summaryrefslogtreecommitdiff
path: root/scripts/vyatta-show-interfaces.pl
blob: 36428cb90245247048ecc7cd492ffeb7c476831a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/perl
#
# Module: vyatta-show-interfaces.pl
# 
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
# All Rights Reserved.
# 
# Author: Stig Thormodsrud
# Date: February 2008
# Description: Script to display interface information
# 
# **** End License ****
#

use lib "/opt/vyatta/share/perl5/";

use Vyatta::Interface;
use Vyatta::Misc;
use Getopt::Long;
use POSIX;
use NetAddr::IP;

use strict;
use warnings;

#
# valid actions
#
my %action_hash = (
    'show'       => \&run_show_intf,
    'show-brief' => \&run_show_intf_brief,
    'show-count' => \&run_show_counters,
    'clear'      => \&run_clear_intf,
    'reset'      => \&run_reset_intf,
    );


my $clear_stats_dir = '/var/run/vyatta';
my $clear_file_magic = 'XYZZYX';

my @rx_stat_vars = 
    qw/rx_bytes rx_packets rx_errors rx_dropped rx_over_errors multicast/; 
my @tx_stat_vars = 
    qw/tx_bytes tx_packets tx_errors tx_dropped tx_carrier_errors collisions/;

sub get_intf_description {
    my $name = shift;
    my $description = interface_description($name);

    return "" unless $description;
    return $description;
}

sub get_intf_stats {
    my $intf = shift;
    
    my %stats = ();
    foreach my $var (@rx_stat_vars, @tx_stat_vars) {
	$stats{$var} = get_sysfs_value($intf, "statistics/$var");
    }
    return %stats;
}

sub get_intf_statsfile {
    my $intf = shift;

    return "$clear_stats_dir/$intf.stats";
}

sub get_clear_stats {
   my $intf = shift;

   my %stats = ();
   foreach my $var (@rx_stat_vars, @tx_stat_vars) {
       $stats{$var} = 0;
   }

   my $filename = get_intf_statsfile($intf);

   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 = <$f>;
   chomp $timestamp;
   $stats{'timestamp'} = $timestamp;

   while (<$f>) {
       chop;
       my ($var, $val) = split(/,/);
       $stats{$var} = $val;
   }
   close($f);
   return %stats;
}

sub get_ipaddr {
    my $name = shift;
    
    # Skip IPV6 default Link-local
    return grep { !/^fe80/ } Vyatta::Misc::getIP($name);
}

sub get_state_link {
    my $name = shift;
    my $intf = new Vyatta::Interface($name);
    my $state;
    my $link = 'down';

    if ($intf->up()) {
	$state = 'up'; 
	$link = "up" if ($intf->running());
    } else {
	$state = "admin down";
    }

    return ($state, $link);
}

sub is_valid_intf {
    my $name = shift;
    return unless $name;

    my $intf = new Vyatta::Interface($name);
    return unless $intf;

    return $intf->exists();
}

sub get_intf_for_type {
    my $type = shift;
    my @interfaces = getInterfaces();
    my @list = ();
    foreach my $name (@interfaces) {
	if ($type) {
	    my $intf = new Vyatta::Interface($name);
	    next unless $intf;				# unknown type
	    next if ($type ne $intf->type());
	}
	push @list, $name;
    }

    return @list;
}

# This function has to deal with both 32 and 64 bit counters
sub get_counter_val {
    my ($clear, $now) = @_;

    return $now if $clear == 0;

    # device is using 64 bit values assume they never wrap
    my $value = $now - $clear;
    return $value if ($now >> 32) != 0;

    # The counter has rolled.  If the counter has rolled
    # multiple times since the clear value, then this math
    # is meaningless.
    $value = (4294967296 - $clear) + $now
	if ($value < 0);

    return $value;
}

#
# The "action" routines
#

sub run_show_intf {
    my @intfs = @_;

    foreach my $intf (@intfs) {
	my %clear = get_clear_stats($intf);
	my $description = get_intf_description($intf);
	my $timestamp = $clear{'timestamp'};

	my $line = `ip addr show $intf | sed 's/^[0-9]*: //'`;
	chomp $line;

	if ($line =~ /link\/tunnel6/) {
	    my $estat = `ip -6 tun show $intf | sed 's/.*encap/encap/'`;
	    $line =~ s%    link/tunnel6%    $estat$&%;
	}

	print "$line\n";
	if (defined $timestamp and $timestamp ne "") {
	    my $time_str = strftime("%a %b %d %R:%S %Z %Y", 
				    localtime($timestamp));
	    print "    Last clear: $time_str\n";
	}
	if (defined $description and $description ne "") {
	    print "    Description: $description\n";
	}
	print "\n";

	my %stats = get_intf_stats($intf);

	my $fmt = "    %10s %10s %10s %10s %10s %10s\n";

	printf($fmt,
	       "RX:  bytes", "packets", "errors", "dropped", "overrun", "mcast");
	printf($fmt,
	       map { get_counter_val($clear{$_}, $stats{$_}) } @rx_stat_vars);

	printf($fmt,
	       "TX:  bytes", "packets", "errors", "dropped", "carrier", "collisions");
	printf($fmt,
	       map { get_counter_val($clear{$_}, $stats{$_}) } @tx_stat_vars);
    }
}

sub conv_brief_code {
  my $state = pop(@_);
  $state = 'u' if ($state eq 'up');
  $state = 'D' if ($state eq 'down');
  $state = 'A' if ($state eq 'admin down');
  return $state;
}

sub conv_descriptions {
  my $description = pop @_;
  my @descriptions;
  my $line = '';
  foreach my $elem (split(' ', $description)){
    if ((length($line) + length($elem)) >= 24){
      push(@descriptions, $line);
      $line = "$elem ";
    } else {
      $line .= "$elem ";
    }
  }
  push(@descriptions, $line);
  return @descriptions;
}

sub run_show_intf_brief {
    my @intfs = @_;
    my $format = "%-16s %-33s %-4s %s\n";
    my $format2 = "%-16s %s\n";
    print "Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down\n";
    printf($format, "Interface","IP Address","S/L","Description");
    printf($format, "---------","----------","---","-----------");
    foreach my $intf (@intfs) {
      next if ($intf =~ /gre0/);
      next if ($intf =~ /sit0/);
      next if ($intf =~ /tunl0/);
      next if ($intf =~ /ip6tnl0/);
      my @ip_addr = get_ipaddr($intf);
      my ($state, $link) = get_state_link($intf);
      $state = conv_brief_code($state);
      $link = conv_brief_code($link);
      my $description = get_intf_description($intf);
      my @descriptions = conv_descriptions($description);
      if (scalar(@ip_addr) == 0) {
        my $desc = '';
        $desc = shift @descriptions if (scalar(@descriptions) > 0 );
        printf($format, $intf, "-", "$state/$link", $desc);
        foreach my $descrip (@descriptions){
          printf($format, '', '', '', $descrip);
        }
      } else {
        my $tmpip = shift(@ip_addr);
        my $desc = '';
        if (length($tmpip) < 33){
          $desc = shift @descriptions if (scalar(@descriptions) > 0 );
          printf($format, $intf, $tmpip , "$state/$link", $desc);
          foreach my $descrip (@descriptions){
            printf($format, '', '', '', $descrip);
          }
          foreach my $ip (@ip_addr) {
            printf($format2, '', $ip) if (defined $ip);
          }
        } else {
          $desc = shift @descriptions if (scalar(@descriptions) > 0 );
          printf($format2, $intf, $tmpip);
          my $printed_desc = 0;
          foreach my $ip (@ip_addr) {
            if (length($ip) >= 33) {
              printf($format2, '', $ip) if (defined $ip);
            } else {
              if (!$printed_desc){
                printf($format, '', $ip, "$state/$link", $desc);
                $printed_desc = 1;
                foreach my $descrip (@descriptions){
                  printf($format, '', '', '', $descrip);
                }
              } else {
                printf($format2, '', $ip);
              }
            }
          }
          if (!$printed_desc){
            printf($format, '', '', "$state/$link", $desc);
            foreach my $descrip (@descriptions){
              printf($format, '', '', '', $descrip);
            }
          }
        }
      }
   }
}

sub run_show_counters {
    my @intfs = @_;

    printf("%-12s %10s %10s     %10s %10s\n",
	   "Interface","Rx Packets","Rx Bytes","Tx Packets","Tx Bytes");

    foreach my $intf (@intfs) {
	my ($state, $link) = get_state_link($intf);
	next if $state ne 'up';
	my %clear = get_clear_stats($intf);
	my %stats = get_intf_stats($intf);

	printf("%-12s %10s %10s     %10s %10s\n", $intf,
	       get_counter_val($clear{rx_packets}, $stats{rx_packets}),
	       get_counter_val($clear{rx_bytes},   $stats{rx_bytes}),
	       get_counter_val($clear{tx_packets}, $stats{tx_packets}),
	       get_counter_val($clear{tx_bytes},   $stats{tx_bytes})
	    );
    }
}

sub run_clear_intf {
    my @intfs = @_;

    foreach my $intf (@intfs) {
	my %stats = get_intf_stats($intf);
	my $filename = get_intf_statsfile($intf);

	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 $f $clear_file_magic, "\n", time(), "\n";

	while (my ($var, $val) = each (%stats)) {
	    print $f $var, ",", $val, "\n";
	}

	close($f);
    }
}

sub run_reset_intf {
    my @intfs = @_;
    
    foreach my $intf (@intfs) {
	my $filename = get_intf_stats($intf);
	system("rm -f $filename");
    }
}

sub alphanum_split {
    my ($str) = @_;
    my @list = split m/(?=(?<=\D)\d|(?<=\d)\D)/, $str;
    return @list;
}

sub natural_order {
    my ($a, $b) = @_;
    my @a = alphanum_split($a);
    my @b = alphanum_split($b);
  
    while (@a && @b) {
	my $a_seg = shift @a;
	my $b_seg = shift @b;
	my $val;
	if (($a_seg =~ /\d/) && ($b_seg =~ /\d/)) {
	    $val = $a_seg <=> $b_seg;
	} else {
	    $val = $a_seg cmp $b_seg;
	}
	if ($val != 0) {
	    return $val;
	}
    }
    return @a <=> @b;
}

sub intf_sort {
    my @a = @_;
    my @new_a = sort { natural_order($a,$b) } @a;
    return @new_a;
}

sub usage {
    print "Usage: $0 [intf=NAME|intf-type=TYPE] action=ACTION\n";
    print "  NAME = ", join(' | ', get_intf_for_type()), "\n";
    print "  TYPE = ", join(' | ', Vyatta::Interface::interface_types()), "\n";
    print "  ACTION = ", join(' | ', keys %action_hash), "\n";
    exit 1;
}

#
# main
#
my @intf_list = ();
my ($intf_type, $intf);
my $action = 'show';

GetOptions("intf-type=s" => \$intf_type,
	   "intf=s"      => \$intf,
	   "action=s"    => \$action,
) or usage();

if ($intf) {
    die "Invalid interface [$intf]\n" 
	unless is_valid_intf($intf);

    push @intf_list, $intf;
} elsif ($intf_type) {
    @intf_list = get_intf_for_type($intf_type);
} else {
    # get all interfaces
    @intf_list = get_intf_for_type();
}

@intf_list = intf_sort(@intf_list);

my $func;
if (defined $action_hash{$action}) {
    $func = $action_hash{$action};
} else {
    print "Invalid action [$action]\n";
    usage();
}

#
# make it so...
#
&$func(@intf_list);