File Coverage

lib/App/Tel/Color/Cisco.pm
Criterion Covered Total %
statement 83 116 71.5
branch 22 44 50.0
condition 8 24 33.3
subroutine 12 13 92.3
pod 1 1 100.0
total 126 198 63.6


line stmt bran cond sub pod time code
1             package App::Tel::Color::Cisco;
2 2     2   2322 use parent 'App::Tel::Color::Base';
  2         283  
  2         11  
3 2     2   120 use Term::ANSIColor;
  2         2  
  2         94  
4 2     2   10 use strict;
  2         3  
  2         36  
5 2     2   9 use warnings;
  2         6  
  2         2506  
6              
7             =head1 NAME
8              
9             App::Tel::Cisco - Colors for show interface and other commands
10              
11             =head2 METHODS
12              
13             =cut
14              
15             sub _c {
16             # if not a number then return the original text
17 5     5   152 my $val = shift;
18 5 100       52 return $val if ($val =~ /\D/);
19 4 100       19 if ($val > 0) {
20 1         5 return colored($val, 'red');
21             }
22 3         17 return colored($val, 'green');
23             }
24              
25              
26             # not kidding, this will be crazy.
27             # it simulates s/blah (\d+) blah/sprintf("blah %s blah", c($1))/e;
28             sub _crazy {
29 4     4   19 my @strings = @_;
30 4         7 my $evils;
31              
32 4         11 foreach my $s (@strings) {
33 56         114 my $substring = $s;
34             # (?<!\\)(?!\\) are funny things that mean look behind and look ahead
35             # for \\ (the escape \ before a parenthesis)
36 56         365 my $count = $substring =~ s/(?<!\\)(?!\\)\(.*?\)/%s/g;
37              
38 56         100 my $args;
39 56         105 map { $args .= ",_c(\$$_)"; } 1..$count;
  136         427  
40 56         205 $evils .= "s/$s/sprintf(\"$substring\"$args)/e;";
41             }
42              
43 4         25 return $evils;
44             }
45              
46             sub _uspwr {
47 1     1   4 my $pwr = shift;
48 1         3 my $color = 'red';
49 1 50       11 if ( $pwr < 30 ) { $color = 'red'; }
  0         0  
50 1 50 33     12 if ( $pwr >= 30 && $pwr <= 33 ) { $color = 'yellow'; }
  0         0  
51 1 50 33     11 if ( $pwr >= 33 && $pwr <= 45 ) { $color = 'green'; }
  1         4  
52 1 50 33     6 if ( $pwr >= 45 && $pwr <= 50 ) { $color = 'yellow'; }
  0         0  
53 1 50       5 if ( $pwr > 50 ) { $color = 'red'; }
  0         0  
54 1         6 return colored($pwr, $color);
55             }
56              
57             sub _ussnr {
58 1     1   48 my $pwr = shift;
59 1         4 my $color = 'red';
60 1 50       7 if ( $pwr < 20 ) { $color = 'red'; }
  0         0  
61 1 50 33     12 if ( $pwr >= 20 && $pwr <= 25 ) { $color = 'yellow'; };
  0         0  
62 1 50       9 if ( $pwr > 25 ) { $color = 'green'; }
  1         3  
63 1         6 return colored($pwr, $color);
64             }
65              
66             sub _dspwr {
67 1     1   43 my $input = shift;
68 1         3 my $pwr = $input;
69 1         7 $pwr =~ s/ //g; # remove all spaces, leaving possible negative sign and value
70 1         3 my $color = 'red';
71 1 50       13 if ( $pwr < -15 ) { $color = 'red'; }
  0         0  
72 1 50 33     12 if ( $pwr >= -15 && $pwr <= -9 ) { $color = 'yellow'; }
  0         0  
73 1 50 33     12 if ( $pwr >= -9 && $pwr <= 9 ) { $color = 'green'; }
  1         3  
74 1 50 33     8 if ( $pwr >= 9 && $pwr <= 15 ) { $color = 'yellow'; }
  0         0  
75 1 50       6 if ( $pwr > 15 ) { $color = 'red'; }
  0         0  
76 1         5 return colored($input, $color);
77             }
78              
79             sub _dssnr {
80 1     1   44 my $pwr = shift;
81 1         4 my $color = 'red';
82 1 50 33     20 if ( $pwr eq '-----' ) { $color = 'yellow'; }
  0 50       0  
    50          
    50          
83 0         0 elsif ( $pwr < 35 ) { $color = 'red'; }
84 0         0 elsif ( $pwr >= 35 && $pwr <= 35 ) { $color = 'yellow'; }
85 1         4 elsif ( $pwr > 35 ) { $color = 'green'; }
86 1         5 return colored($pwr, $color);
87             }
88              
89             sub _cpu {
90 0     0   0 my $cpu = shift;
91 0         0 my $color = 'green';
92 0 0       0 if ($cpu > 0) { $color = 'yellow'; }
  0         0  
93 0 0       0 if ($cpu > 1) { $color = 'red'; }
  0         0  
94 0         0 return colored($cpu, $color);
95             }
96              
97             sub _interface {
98             # without knowing syntax, this will automatically handle err-disable and
99             # any other weird corner cases by defaulting to red.
100 2     2   55 my $color = 'red';
101 2 50       7 if ($_[0] eq 'up') {
102 0         0 $color = 'green';
103             }
104 2         7 return colored($_[0], $color);
105             }
106              
107             =head2 colorize
108              
109             my $output = $self->colorize($input);
110              
111             Given a line of text from a cisco router, this will try to colorize it.
112              
113             =cut
114              
115             sub colorize {
116 4     4 1 10 my $self = shift;
117 4         11 $_ = shift;
118              
119 4         16 s/(\S+) is (.*), line protocol is (\S+)/sprintf("%s is %s, line protocol is %s", colored($1, 'magenta'),
  1         4  
120             _interface($2), _interface($3))/eg;
121              
122             # sh cable modem phy
123 4         49 s#([a-f0-9\.]+ C\d+/\d+/U\d+\s+\d+\s+)([\d\.]+)(\s+)([\d\.]+)(\s+\!?\d+)([\s\-]+[\d\.]+)(\s+)([\d\.\-]+)#
124 1         7 sprintf("%s%s%s%s%s%s%s%s", $1, _uspwr($2), $3, _ussnr($4), $5, _dspwr($6), $7, _dssnr($8))#eg;
125              
126             # more show interface
127 4         58 s/Full-duplex/colored('Full-duplex', 'green')/eg;
  0         0  
128 4         9 s/Half-duplex/colored('Half-duplex', 'yellow')/eg;
  0         0  
129              
130             # sh proc cpu
131 4         10 s#(\s+\d+\s+\d+\s+\d+\s+\d+\s+)([\d\.]+)(%\s+)([\d\.]+)(%\s+)([\d\.]+)#
132 0         0 sprintf("%s%s%s%s%s%s", $1, _cpu($2), $3, _cpu($4), $5, _cpu($6))#eg;
133              
134             # parts of sh run
135 4         9 s/\n(ip route [^\n]+)/sprintf("\n%s", colored($1,'yellow'))/eg;
  0         0  
136 4         10 s/\n(ipv6 route [^\n]+)/sprintf("\n%s", colored($1,'yellow'))/eg;
  0         0  
137 4         9 s/\n(aaa [^\n]+)/sprintf("\n%s", colored($1,'green'))/eg;
  0         0  
138 4         10 s/\n(access-list [^\n]+)/sprintf("\n%s", colored($1,'cyan'))/eg;
  0         0  
139 4         8 s/\n(snmp-server [^\n]+)/sprintf("\n%s", colored($1,'bright_white'))/eg;
  0         0  
140 4         9 s/\n(tacacs-server [^\n]+)/sprintf("\n%s", colored($1,'magenta'))/eg;
  0         0  
141 4         8 s/\n(no tacacs-server [^\n]+)/sprintf("\n%s", colored($1,'magenta'))/eg;
  0         0  
142 4         9 s/\n(radius-server [^\n]+)/sprintf("\n%s", colored($1,'magenta'))/eg;
  0         0  
143 4         10 s/\n(ntp [^\n]+)/sprintf("\n%s", colored($1,'magenta'))/eg;
  0         0  
144              
145 4         15 my $regexp = _crazy('(\d+) runts, (\d+) giants, (\d+) throttles',
146             '(\d+) input errors, (\d+) CRC, (\d+) frame, (\d+) overrun, (\d+) ignored',
147             '(\d+) input packets with dribble condition detected',
148             'Total output drops: (\d+)',
149             '(\d+) output errors, (\d+) interface resets',
150             '(\d+) output errors, (\d+) collisions, (\d+) interface resets',
151             '(\d+) output buffer failures, (\d+) output buffers swapped out',
152             '(\d+) carrier transitions',
153             'Output queue (\S+), (\d+) drops; input queue (\S+), (\d+) drops',
154             '(\d+)\/(\d+) \(size\/max\/drops\/flushes\)\;',
155             '(\d+) (pause input|watchdog|underruns|no buffer|pause output|abort)',
156             '(\d+) output errors, (\d+) collisions, (\d+) interface resets',
157             '(\d+) babbles, (\d+) late collision, (\d+) deferred',
158             '(\d+) lost carrier, (\d+) no carrier',
159             );
160              
161             # the rest of show interface is in this eval
162 4         3170 eval $regexp; ## no critic
163 4         23 return $_;
164             }
165              
166             1;