| 1 | package App::Tel::Color::Cisco; | ||||||||||||||||||||||||||
| 2 | 2 | ▶ | use parent 'App::Tel::Color::Base'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Term::ANSIColor; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Scalar::Util qw ( looks_like_number ); | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||
| 10 | App::Tel::Cisco - Colors for show interface and other commands | ||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||
| 12 | =head2 METHODS | ||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||
| 16 | 7 | ▶ | sub _c { | ||||||||||||||||||||||||
|
Subroutine
_c: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||
| 17 | # if not a number then return the original text | ||||||||||||||||||||||||||
| 18 | 7 | my $val = shift; | |||||||||||||||||||||||||
| 19 | 7 | ▶ | return $val if (!looks_like_number($val)); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 20 | 5 | ▶ | if ($val > 0) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 21 | 1 | return colored($val, 'red'); | |||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||
| 23 | 4 | return colored($val, 'green'); | |||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||
| 27 | # not kidding, this will be crazy. | ||||||||||||||||||||||||||
| 28 | # it simulates s/blah (\d+) blah/sprintf("blah %s blah", c($1))/e; | ||||||||||||||||||||||||||
| 29 | 24 | ▶ | sub _crazy { | ||||||||||||||||||||||||
|
Subroutine
_crazy: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||
| 30 | 24 | my $text = shift; | |||||||||||||||||||||||||
| 31 | 24 | my @strings = @_; | |||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||
| 33 | 24 | foreach my $s (@strings) { | |||||||||||||||||||||||||
| 34 | 336 | my $substring = $s; | |||||||||||||||||||||||||
| 35 | # (?<!\\)(?!\\) are funny things that mean look behind and look ahead | ||||||||||||||||||||||||||
| 36 | # for \\ (the escape \ before a parenthesis) | ||||||||||||||||||||||||||
| 37 | 336 | my $count = $substring =~ s/(?<!\\)(?!\\)\(.*?\)/%s/g; | |||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||
| 39 | 336 | my $args = ''; | |||||||||||||||||||||||||
| 40 | 336 | for (1..$count) { $args .= ",_c(\$$_)" } | |||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||
| 42 | 336 | my $eval = 'sprintf("'.$substring.'"'.$args.')'; | |||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||
| 44 | # in theory this is safer than the old external eval. The reason | ||||||||||||||||||||||||||
| 45 | # being all the evaluated data is part of the defined strings passed | ||||||||||||||||||||||||||
| 46 | # to the _crazy function. That means no data coming from a router can | ||||||||||||||||||||||||||
| 47 | # be evaluated. | ||||||||||||||||||||||||||
| 48 | 336 | $text =~ s/$s/eval $eval/e; | |||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||
| 51 | 24 | return $text; | |||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||
| 54 | 6 | ▶ | sub _uspwr { | ||||||||||||||||||||||||
|
Subroutine
_uspwr: called CC=9 SCAR=22.8
| |||||||||||||||||||||||||||
| 55 | 6 | my $pwr = shift; | |||||||||||||||||||||||||
| 56 | 6 | my $color = 'red'; | |||||||||||||||||||||||||
| 57 | 0 | ▶ | if ( $pwr < 30 ) { $color = 'red'; } | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 58 | 1 | ▶ | elsif ( $pwr >= 30 && $pwr <= 33 ) { $color = 'yellow'; } | ||||||||||||||||||||||||
|
Condition66%2/3
$pwr >= 30 and $pwr <= 33
MC/DC50%1/2
$pwr >= 30 and $pwr <= 33
$pwr >= 30
$pwr <= 33
Truth table66%2/3
A and B
A= $pwr >= 30B= $pwr <= 33
| |||||||||||||||||||||||||||
| 59 | 1 | ▶ | elsif ( $pwr >= 33 && $pwr <= 45 ) { $color = 'green'; } | ||||||||||||||||||||||||
|
Condition66%2/3
$pwr >= 33 and $pwr <= 45
MC/DC50%1/2
$pwr >= 33 and $pwr <= 45
$pwr >= 33
$pwr <= 45
Truth table66%2/3
A and B
A= $pwr >= 33B= $pwr <= 45
| |||||||||||||||||||||||||||
| 60 | 1 | ▶ | elsif ( $pwr >= 45 && $pwr <= 50 ) { $color = 'yellow'; } | ||||||||||||||||||||||||
|
Condition66%2/3
$pwr >= 45 and $pwr <= 50
MC/DC50%1/2
$pwr >= 45 and $pwr <= 50
$pwr >= 45
$pwr <= 50
Truth table66%2/3
A and B
A= $pwr >= 45B= $pwr <= 50
| |||||||||||||||||||||||||||
| 61 | 3 | elsif ( $pwr > 50 ) { $color = 'red'; } | |||||||||||||||||||||||||
| 62 | 6 | return colored($pwr, $color); | |||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||
| 65 | 6 | ▶ | sub _ussnr { | ||||||||||||||||||||||||
|
Subroutine
_ussnr: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||
| 66 | 6 | my $snr = shift; | |||||||||||||||||||||||||
| 67 | 6 | my $color = 'red'; | |||||||||||||||||||||||||
| 68 | 6 | ▶ | if ( $snr < 20 ) { $color = 'red'; } | ||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||
| 69 | 1 | ▶ | elsif ( $snr >= 20 && $snr <= 25 ) { $color = 'yellow'; } | ||||||||||||||||||||||||
|
Condition66%2/3
$snr >= 20 and $snr <= 25
MC/DC50%1/2
$snr >= 20 and $snr <= 25
$snr >= 20
$snr <= 25
Truth table66%2/3
A and B
A= $snr >= 20B= $snr <= 25
| |||||||||||||||||||||||||||
| 70 | 4 | elsif ( $snr > 25 ) { $color = 'green'; } | |||||||||||||||||||||||||
| 71 | 6 | return colored($snr, $color); | |||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||
| 74 | 6 | ▶ | sub _dspwr { | ||||||||||||||||||||||||
|
Subroutine
_dspwr: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||
| 75 | 6 | my $input = shift; | |||||||||||||||||||||||||
| 76 | 6 | my $pwr = $input; | |||||||||||||||||||||||||
| 77 | 6 | $pwr =~ s/ //g; # remove all spaces, leaving possible negative sign and value | |||||||||||||||||||||||||
| 78 | 6 | my $color = 'red'; | |||||||||||||||||||||||||
| 79 | 6 | ▶ | if ( $pwr < -15 ) { $color = 'red'; } | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 80 | 1 | ▶ | elsif ( $pwr >= -15 && $pwr <= -9 ) { $color = 'yellow'; } | ||||||||||||||||||||||||
|
Condition66%2/3
$pwr >= -15 and $pwr <= -9
MC/DC50%1/2
$pwr >= -15 and $pwr <= -9
$pwr >= -15
$pwr <= -9
Truth table66%2/3
A and B
A= $pwr >= -15B= $pwr <= -9
| |||||||||||||||||||||||||||
| 81 | 1 | ▶ | elsif ( $pwr >= -9 && $pwr <= 9 ) { $color = 'green'; } | ||||||||||||||||||||||||
|
Condition66%2/3
$pwr >= -9 and $pwr <= 9
MC/DC50%1/2
$pwr >= -9 and $pwr <= 9
$pwr >= -9
$pwr <= 9
Truth table66%2/3
A and B
A= $pwr >= -9B= $pwr <= 9
| |||||||||||||||||||||||||||
| 82 | 1 | ▶ | elsif ( $pwr >= 9 && $pwr <= 15 ) { $color = 'yellow'; } | ||||||||||||||||||||||||
|
Condition66%2/3
$pwr >= 9 and $pwr <= 15
MC/DC50%1/2
$pwr >= 9 and $pwr <= 15
$pwr >= 9
$pwr <= 15
Truth table66%2/3
A and B
A= $pwr >= 9B= $pwr <= 15
| |||||||||||||||||||||||||||
| 83 | 2 | elsif ( $pwr > 15 ) { $color = 'red'; } | |||||||||||||||||||||||||
| 84 | 6 | return colored($input, $color); | |||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||
| 87 | 6 | ▶ | sub _dssnr { | ||||||||||||||||||||||||
|
Subroutine
_dssnr: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||
| 88 | 6 | my $input = shift; | |||||||||||||||||||||||||
| 89 | 6 | my $snr = $input; | |||||||||||||||||||||||||
| 90 | 6 | $snr =~ s/ //g; # remove all spaces, leaving possible negative sign and value | |||||||||||||||||||||||||
| 91 | 6 | my $color = 'red'; | |||||||||||||||||||||||||
| 92 | 6 | ▶ | if ( $snr eq '-----' ) { $color = 'yellow'; } | ||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||
| 93 | 1 | elsif ( $snr < 35 ) { $color = 'red'; } | |||||||||||||||||||||||||
| 94 | 1 | ▶ | elsif ( $snr >= 35 && $snr <= 35 ) { $color = 'yellow'; } | ||||||||||||||||||||||||
|
Condition66%2/3
$snr >= 35 and $snr <= 35
MC/DC50%1/2
$snr >= 35 and $snr <= 35
$snr >= 35
$snr <= 35
Truth table66%2/3
A and B
A= $snr >= 35B= $snr <= 35
| |||||||||||||||||||||||||||
| 95 | 3 | elsif ( $snr > 35 ) { $color = 'green'; } | |||||||||||||||||||||||||
| 96 | 6 | return colored($input, $color); | |||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||
| 99 | 6 | ▶ | sub _cpu { | ||||||||||||||||||||||||
|
Subroutine
_cpu: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||
| 100 | 6 | my $cpu = shift; | |||||||||||||||||||||||||
| 101 | 6 | my $color = 'green'; | |||||||||||||||||||||||||
| 102 | 6 | ▶ | if ($cpu > 0) { $color = 'yellow'; } | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 103 | 6 | ▶ | if ($cpu > 1) { $color = 'red'; } | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 104 | 6 | return colored($cpu, $color); | |||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||
| 107 | 2 | ▶ | sub _interface { | ||||||||||||||||||||||||
|
Subroutine
_interface: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||
| 108 | # without knowing syntax, this will automatically handle err-disable and | ||||||||||||||||||||||||||
| 109 | # any other weird corner cases by defaulting to red. | ||||||||||||||||||||||||||
| 110 | 2 | my $color = 'red'; | |||||||||||||||||||||||||
| 111 | 2 | ▶ | if ($_[0] eq 'up') { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 112 | 1 | $color = 'green'; | |||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||
| 114 | 2 | return colored($_[0], $color); | |||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||
| 117 | =head2 colorize | ||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||
| 119 | my $output = $self->colorize($input); | ||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||
| 121 | Given a line of text from a cisco router, this will try to colorize it. | ||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||
| 125 | 24 | ▶ | sub colorize { | ||||||||||||||||||||||||
|
Subroutine
colorize: called CC=6 SCAR=20.1
Pod
documented
| |||||||||||||||||||||||||||
| 126 | 24 | my ($self, $text) = @_; | |||||||||||||||||||||||||
| 127 | 24 | my $CISCO_MAC = q{(?:[a-f0-9]{4}\.){2}[a-f0-9]{4}}; | |||||||||||||||||||||||||
| 128 | 24 | my $CABLE_INT = q{C\d+/\d+/U\d+}; | |||||||||||||||||||||||||
| 129 | 24 | my $F = q{\d+\.\d+}; # 00.00 floating point number | |||||||||||||||||||||||||
| 130 | 24 | my $NEG = q{(?:\s+\-?)}; | |||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||
| 133 | 24 | $text =~s/(\S+) is (.*), line protocol is (\S+)/sprintf("%s is %s, line protocol is %s", colored($1, 'magenta'), | |||||||||||||||||||||||||
| 134 | _interface($2), _interface($3))/eg; | ||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||
| 136 | # sh cable modem phy | ||||||||||||||||||||||||||
| 137 | 24 | $text =~ s#($CISCO_MAC $CABLE_INT\s+\d+\s+)($F)(\s+)($F)(\s+\!?\d+)($NEG$F)($NEG$F|\s+\-{5})# | |||||||||||||||||||||||||
| 138 | 6 | sprintf("%s%s%s%s%s%s%s", $1, _uspwr($2), $3, _ussnr($4), $5, _dspwr($6), _dssnr($7))#eg; | |||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||
| 140 | # more show interface | ||||||||||||||||||||||||||
| 141 | 0 | $text =~ s/Full-duplex/colored('Full-duplex', 'green')/eg; | |||||||||||||||||||||||||
| 142 | 0 | $text =~ s/Half-duplex/colored('Half-duplex', 'yellow')/eg; | |||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||
| 144 | # sh proc cpu | ||||||||||||||||||||||||||
| 145 | 24 | $text =~ s#(\s+\d+\s+\d+\s+\d+\s+\d+\s+)($F)(%\s+)($F)(%\s+)($F)# | |||||||||||||||||||||||||
| 146 | 1 | sprintf("%s%s%s%s%s%s", $1, _cpu($2), $3, _cpu($4), $5, _cpu($6))#eg; | |||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||
| 148 | # 4500x sh proc cpu | ||||||||||||||||||||||||||
| 149 | 24 | $text =~ s#((?<!\.)\d+\s+\d+\s+\d+\s+\d+\s+)($F)(\s+)($F)(\s+)($F)(\s+\d+\s+\w+)# | |||||||||||||||||||||||||
| 150 | 1 | sprintf("%s%s%s%s%s%s%s", $1, _cpu($2), $3, _cpu($4), $5, _cpu($6), $7)#eg; | |||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||
| 152 | # parts of sh run | ||||||||||||||||||||||||||
| 153 | 24 | ▶ | if ($text =~ /^(ip|ipv6) route /) { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 154 | 0 | $text = colored($text, 'yellow'); | |||||||||||||||||||||||||
| 155 | } elsif ($text =~ /^aaa/) { | ||||||||||||||||||||||||||
| 156 | 0 | $text = colored($text, 'green'); | |||||||||||||||||||||||||
| 157 | } elsif ($text =~ /^(?:(?:no )?tacacs-server|radius-server|ntp)/) { | ||||||||||||||||||||||||||
| 158 | 0 | $text = colored($text, 'magenta'); | |||||||||||||||||||||||||
| 159 | } elsif ($text =~ /^(?:mac )?access-list/) { | ||||||||||||||||||||||||||
| 160 | 0 | $text = colored($text, 'cyan'); | |||||||||||||||||||||||||
| 161 | } elsif ($text =~ /^snmp-server/) { | ||||||||||||||||||||||||||
| 162 | 0 | $text = colored($text, 'bright_white'); | |||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||
| 165 | 24 | $text = _crazy($text, | |||||||||||||||||||||||||
| 166 | '(\d+) runts, (\d+) giants, (\d+) throttles', | ||||||||||||||||||||||||||
| 167 | '(\d+) input errors, (\d+) CRC, (\d+) frame, (\d+) overrun, (\d+) ignored', | ||||||||||||||||||||||||||
| 168 | '(\d+) input packets with dribble condition detected', | ||||||||||||||||||||||||||
| 169 | 'Total output drops: (\d+)', | ||||||||||||||||||||||||||
| 170 | '(\d+) output errors, (\d+) interface resets', | ||||||||||||||||||||||||||
| 171 | '(\d+) output errors, (\d+) collisions, (\d+) interface resets', | ||||||||||||||||||||||||||
| 172 | '(\d+) output buffer failures, (\d+) output buffers swapped out', | ||||||||||||||||||||||||||
| 173 | '(\d+) carrier transitions', | ||||||||||||||||||||||||||
| 174 | 'Output queue (\S+), (\d+) drops; input queue (\S+), (\d+) drops', | ||||||||||||||||||||||||||
| 175 | '(\d+)\/(\d+) \(size\/max\/drops\/flushes\)\;', | ||||||||||||||||||||||||||
| 176 | '(\d+) (pause input|watchdog|underruns|no buffer|pause output|abort)', | ||||||||||||||||||||||||||
| 177 | '(\d+) output errors, (\d+) collisions, (\d+) interface resets', | ||||||||||||||||||||||||||
| 178 | '(\d+) babbles, (\d+) late collision, (\d+) deferred', | ||||||||||||||||||||||||||
| 179 | '(\d+) lost carrier, (\d+) no carrier', | ||||||||||||||||||||||||||
| 180 | ); | ||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||
| 182 | 24 | return $text; | |||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||
| 185 | 2 | 1; | |||||||||||||||||||||||||