| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use File::Path; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Spec::Functions; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use List::MoreUtils qw< uniq >; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use RackMan; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use RackMan::Config; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use RackMan::Tasks; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use RackMan::Utils; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Socket; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | $::PROGRAM = "RackTables consistency check"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | $::VERSION = "1.02"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | $::COMMAND = basename($0); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | CONFIG_SECTION => "racktables-check", | |||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | OP_EQUAL => "=", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | OP_REGEXP => "~", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | OP_FUNCTION => "%", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | PROP_ATTRIBUTE => "attribute", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | PROP_GATEWAY => "default_ipv4_gateway", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | PROP_INTERFACES => "interfaces", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | PROP_IPV4ADDRS => "regular_ipv4addrs", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | PROP_MACADDRS => "regular_mac_addrs", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | RE_ALL_OPS => join("|", OP_EQUAL, OP_REGEXP, OP_FUNCTION), | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | my %alias = ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | addresses => PROP_IPV4ADDRS, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | addrs => PROP_IPV4ADDRS, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | attr => PROP_ATTRIBUTE, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | gateway => PROP_GATEWAY, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | interfaces => PROP_INTERFACES, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ipv4_addrs => PROP_IPV4ADDRS, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | mac_addrs => PROP_MACADDRS, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | tag => "explicit_tags", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | type => "object_type", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | my %check_func = ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | check_dns => \&check_dns, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # main | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | MAIN: { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | if (not caller()) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | run(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | exit RackMan->status; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # run() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # --- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # detect if stdout is connected to a terminal | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | (-t STDOUT ? $Term::ANSIColor::AUTORESET : $ENV{ANSI_COLORS_DISABLED}) = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $|++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # default options | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my %options = ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | config => "/usr/local/etc/rack.conf", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | format => "yaml", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # parse options | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | Getopt::Long::Configure(qw< no_auto_abbrev no_ignore_case >); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | GetOptions(\%options, qw{ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | help|usage|h! man! version|V! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | verbose|v! config|c=s filter|F=s output|o=s format|t=s | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | }) or pod2usage(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # handle --version, --help and --man | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $options{man} and pod2usage(2); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $options{help} and pod2usage(1); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $options{version} and print "$::PROGRAM v$::VERSION\n" and exit; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$options{"version"} and print "$main::PROGRAM v$main::VERSION\n"
MC/DC0%0/2
$options{"version"} and print "$main::PROGRAM v$main::VERSION\n"
$options{"version"}
print "$main::PROGRAM v$main::VERSION\n"
Truth table0%0/3
A and B
A= $options{"version"}B= print "$main::PROGRAM v$main::VERSION\n"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # read configuration file | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $config = RackMan::Config->new(-file => $options{config}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # instanciate the backend object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $rackman = RackMan->new({ options => \%options, config => $config }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # do the actual work | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | process($rackman, @ARGV); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # pod2usage() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # --------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub pod2usage { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pod2usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my ($n) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | require Pod::Usage; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | Pod::Usage::pod2usage({ -exitval => 0, -verbose => $n, -noperldoc => 1 }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # process() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # ------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub process { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process: not called CC=75 SCAR=86.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my ($rackman, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my @results; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | my $n; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | # parse the rules | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | my @rule_defs = $rackman->config->val(CONFIG_SECTION, "rule"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | my @rules; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | for my $rule_def (@rule_defs) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | next if not defined $rule_def or $rule_def =~ /^\s+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $rule_def or $rule_def =~ /^\s+$/
MC/DC0%0/2
not defined $rule_def or $rule_def =~ /^\s+$/
not defined $rule_def
$rule_def =~ /^\s+$/
Truth table0%0/3
not(A) or B
A= not defined $rule_defB= $rule_def =~ /^\s+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | my $rule = $rule_def; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my (%enforce, %selector); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # extract an optional selector | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ($rule =~ s/\s*\[\s*([^\]]+)\s*\]\s*//) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my ($prop, $value) = split / *= */, $1, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my @values = split /\|/, $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | $prop = ":$prop" unless index($prop, ":") >= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | my ($type, $name) = split /:/, $prop; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | %selector = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | proptype => $alias{$type} || $type || "", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | propname => $alias{$name} || $name, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | propval => { map { $_ => 1 } @values }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/8
$alias{$type} || $type || ''
$alias{$type} or $type
$alias{$name} || $name
MC/DC0%0/2
$alias{$type} || $type || ''
$alias{$type}
$type
MC/DC0%0/2
$alias{$name} || $name
$alias{$name}
$name
Truth table0%0/3
A or B
A= $alias{$type}B= $type
Truth table0%0/3
A || B
A= $alias{$name}B= $name
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | # extract an optional operator and second operand | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if ($rule =~ s/\s*(@{[RE_ALL_OPS]})\s*(.+)$//) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ($enforce{operator}, $enforce{operand}) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | # compile the regexp to detect errors early | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ($enforce{operator} eq OP_REGEXP) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ($enforce{operand}, my $err) = parse_regexp($enforce{operand}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | RackMan->error("invalid regexp: $err") if $err; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # verify that the check function is actually known | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | if ($enforce{operator} eq OP_FUNCTION) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | RackMan->error("unknown check function '$enforce{operand}'") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | unless exists $check_func{$enforce{operand}}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # what is left should be the property to check | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | s/^\s+//g, s/\s+$//g for $rule; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | $rule = ":$rule" unless index($rule, ":") >= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | my ($type, $name) = split /:/, $rule; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | $enforce{proptype} = $alias{$type} || $type || ""; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$alias{$type} || $type || ''
$alias{$type} or $type
MC/DC0%0/2
$alias{$type} || $type || ''
$alias{$type}
$type
Truth table0%0/3
A or B
A= $alias{$type}B= $type
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | $enforce{propname} = $alias{$name} || $name; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$alias{$name} || $name
MC/DC0%0/2
$alias{$name} || $name
$alias{$name}
$name
Truth table0%0/3
A || B
A= $alias{$name}B= $name
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | push @rules, { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | string => $rule_def, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | enforce => \%enforce, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | (selector => \%selector)x!! keys %selector, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # construct the filter | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | my %filter; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | my $filter = $rackman->options->{filter} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | || $rackman->config->val(CONFIG_SECTION, "filter", ""); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$rackman->options->{'filter'} || $rackman->config->val('racktables-check', 'filter', '')
MC/DC0%0/2
$rackman->options->{'filter'} || $rackman->config->val('racktables-check', 'filter', '')
$rackman->options->{'filter'}
$rackman->config->val('racktables-check', 'filter', '')
Truth table0%0/3
A || B
A= $rackman->options->{'filter'}B= $rackman->config->val('racktables-check', 'filter', '')
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if ($filter) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | for my $token (split / *, */, $filter) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | if (index($token, "tag:") == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | $filter{tag}{ lc substr($token, 4) } = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | next | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if (index($token, "=") > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my ($name, $value) = split /=/, $token; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | push @{ $filter{attr}{$name} }, $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | next | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my $enforce_interface_match | |||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | = $rackman->config->val(CONFIG_SECTION, "enforce_interface_match"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # fetch the list of devices | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | my @devices = @args | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | ? map +{ name => $_ }, @args | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | : RackMan::Tasks->task_list({ rackman => $rackman, type => "all" }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | DEVICE: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | for my $device (@devices) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | next if $device->{name} eq "(null)"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | my $rackdev = exists $device->{id} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | ? $rackman->device_by_id($device->{id}) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | : $rackman->device_by_name($device->{name}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | my $type = $rackdev->object_type; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | my @problems; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # fetch the tags for this RackObject, filter them if needed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | my @tags = map { s/\W+/_/g; $_ } map lc, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | @{ $rackdev->explicit_tags }, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | @{ $rackdev->implicit_tags }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | next unless @tags; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # fetch the attributes of this RackObject | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | my %attr = %{ $rackdev->attributes }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # filter out devices without the requested tags or attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | if ($filter) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # filter on tags | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | if (exists $filter{tag}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | next unless grep $filter{tag}{$_}, @tags; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # filter on attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | if (exists $filter{attr}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | my $keep = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | ATTR: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | for my $name (keys %{ $filter{attr} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | for my $value (@{ $filter{attr}{$name} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | next unless exists $attr{$name} and defined $attr{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $attr{$name} and defined $attr{$name}
MC/DC0%0/2
exists $attr{$name} and defined $attr{$name}
exists $attr{$name}
defined $attr{$name}
Truth table0%0/3
A and B
A= exists $attr{$name}B= defined $attr{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | $keep ||= $value eq $attr{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$keep ||= $value eq $attr{$name}
MC/DC0%0/2
$keep ||= $value eq $attr{$name}
$keep
$value eq $attr{$name}
Truth table0%0/3
A ||= B
A= $keepB= $value eq $attr{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | last ATTR if $keep; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | next unless $keep; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | RULE: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | for my $rule (@rules) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | # apply selector filter, if any | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | if (exists $rule->{selector}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | my $propval = property_value($rackdev, $rule->{selector}) || ""; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
property_value($rackdev, $rule->{'selector'}) || ''
MC/DC0%0/1
property_value($rackdev, $rule->{'selector'}) || ''
property_value($rackdev, $rule->{'selector'})
Truth table0%0/2
A
A= property_value($rackdev, $rule->{'selector'})
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | next RULE unless $rule->{selector}{propval}{$propval}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | # fetch the property values | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | my $enforce = $rule->{enforce}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | my $propname = $enforce->{propname}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my @propvals = property_value($rackdev, $enforce); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | VALUE: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | for my $propval (@propvals) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | # check if the property is defined | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | push @problems, "property '$propname' not defined" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
defined $propval || push(@problems, "property '${propname}' not defined") && (next RULE)
push @problems, "property '${propname}' not defined" and next RULE
MC/DC0%0/2
defined $propval || push(@problems, "property '${propname}' not defined") && (next RULE)
defined $propval
push @problems, "property '${propname}' not defined"
Truth table0%0/3
A || B
A= defined $propvalB= push @problems, "property '${propname}' not defined"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | and next RULE unless defined $propval; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | # if there is an operation to apply, do it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | if (exists $enforce->{operator}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | my $op = $enforce->{operator}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | my $val = $enforce->{operand}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if ($op eq OP_EQUAL) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | push @problems, "property '$propname' value '$propval'" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$propval eq $val || push(@problems, "property '${propname}' value '${propval}'" . " != '${val}'") && (next)
push @problems, "property '${propname}' value '${propval}'" . " != '${val}'" and next
MC/DC0%0/2
$propval eq $val || push(@problems, "property '${propname}' value '${propval}'" . " != '${val}'") && (next)
$propval eq $val
push @problems, "property '${propname}' value '${propval}'" . " != '${val}'"
Truth table0%0/3
A || B
A= $propval eq $valB= push @problems, "property '${propname}' value '${propval}'" . " != '${val}'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | . " != '$val'" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | and next unless $propval eq $val; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | elsif ($op eq OP_REGEXP) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | push @problems, "property '$propname' value '$propval'" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$propval =~ /$val/ || push(@problems, "property '${propname}' value '${propval}'" . " does not match $val") && (next)
push @problems, "property '${propname}' value '${propval}'" . " does not match $val" and next
MC/DC0%0/2
$propval =~ /$val/ || push(@problems, "property '${propname}' value '${propval}'" . " does not match $val") && (next)
$propval =~ /$val/
push @problems, "property '${propname}' value '${propval}'" . " does not match $val"
Truth table0%0/3
A || B
A= $propval =~ /$val/B= push @problems, "property '${propname}' value '${propval}'" . " does not match $val"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | . " does not match $val" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | and next unless $propval =~ /$val/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | elsif ($op eq OP_FUNCTION) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | # $val contains the function name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | my @errs = $check_func{$val}->($propval); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | push @problems, map "property '$propname' $_", @errs | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
@errs && (push(@problems, map(("property '${propname}' $_"), @errs)) && (next))
push @problems, map(("property '${propname}' $_"), @errs) and next
MC/DC0%0/2
@errs && (push(@problems, map(("property '${propname}' $_"), @errs)) && (next))
@errs
push @problems, map(("property '${propname}' $_"), @errs)
Truth table0%0/3
A && B
A= @errsB= push @problems, map(("property '${propname}' $_"), @errs)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | and next if @errs; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | # specific rule: check if each network interface is associated | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | # (at least, logically) with a port of the same name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | if ($enforce_interface_match) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | my %port = map { $_->{name} => $_ } @{ $rackdev->ports }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | for my $interface ($rackdev->physical_interfaces) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | my $name = $interface->{iface}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | push @problems, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | "interface '$name' has no associated port" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | and next unless exists $port{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
exists $port{$name} || push(@problems, "interface '${name}' has no associated port") && (next)
push @problems, "interface '${name}' has no associated port" and next
MC/DC0%0/2
exists $port{$name} || push(@problems, "interface '${name}' has no associated port") && (next)
exists $port{$name}
push @problems, "interface '${name}' has no associated port"
Truth table0%0/3
A || B
A= exists $port{$name}B= push @problems, "interface '${name}' has no associated port"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | push @problems, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | "interface '$name' associated port has no MAC address" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | and next unless defined $port{$name}{l2address} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | and $port{$name}{l2address} =~ /^\w+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/8
defined $port{$name}{'l2address'} && $port{$name}{'l2address'} =~ /^\w+$/ || push(@problems, "interface '${name}' associated port has no MAC address") && (next)
defined $port{$name}{"l2address"} and $port{$name}{"l2address"} =~ /^\w+$/
push @problems, "interface '${name}' associated port has no MAC address" and next
MC/DC0%0/3
defined $port{$name}{'l2address'} && $port{$name}{'l2address'} =~ /^\w+$/ || push(@problems, "interface '${name}' associated port has no MAC address") && (next)
defined $port{$name}{"l2address"}
$port{$name}{"l2address"} =~ /^\w+$/
push @problems, "interface '${name}' associated port has no MAC address"
Truth table0%0/5
A and B || C
A= defined $port{$name}{"l2address"}B= $port{$name}{"l2address"} =~ /^\w+$/C= push @problems, "interface '${name}' associated port has no MAC address"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | push @problems, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | "interface '$name' associated port is not linked" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | and next unless defined $port{$name}{peer_port_id} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | and $port{$name}{peer_port_id} > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/8
defined $port{$name}{'peer_port_id'} && $port{$name}{'peer_port_id'} > 0 || push(@problems, "interface '${name}' associated port is not linked") && (next)
defined $port{$name}{"peer_port_id"} and $port{$name}{"peer_port_id"} > 0
push @problems, "interface '${name}' associated port is not linked" and next
MC/DC0%0/3
defined $port{$name}{'peer_port_id'} && $port{$name}{'peer_port_id'} > 0 || push(@problems, "interface '${name}' associated port is not linked") && (next)
defined $port{$name}{"peer_port_id"}
$port{$name}{"peer_port_id"} > 0
push @problems, "interface '${name}' associated port is not linked"
Truth table0%0/5
A and B || C
A= defined $port{$name}{"peer_port_id"}B= $port{$name}{"peer_port_id"} > 0C= push @problems, "interface '${name}' associated port is not linked"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | $n += @problems; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | push @results, { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | name => $device->{name}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | type => $device->{type}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | problems => \@problems, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | } if @problems; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | # print the results | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | if ($n) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | my $out; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | my $format = $rackman->options->{format}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | my $path = $rackman->options->{output}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | if ($format eq "yaml") { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | ▶ | eval "use YAML::XS; 1" or RackMan->error("can't load YAML::XS"); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | $out = YAML::XS::Dump(\@results); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | elsif ($format eq "json") { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | eval "use JSON::XS; 1" or RackMan->error("can't load JSON::XS"); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | $out = encode_json(\@results); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | if ($path) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | print "$n problems detected.\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | open my $fh, ">", $path | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | or RackMan->error("can't write to $path: $!"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | print {$fh} $out; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | print $out; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | print "You have been well trained, my young apprentice.\n", | |||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | "They will be no match for you.\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | # property_value() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | # -------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | sub property_value { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
property_value: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | my ($rackdev, $prop) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | my $proptype = $prop->{proptype}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | my $propname = $prop->{propname}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | ▶ | if ($proptype eq PROP_ATTRIBUTE) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | return $rackdev->attributes->{$propname} | |||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | elsif (defined $propname) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | ▶ | if ($propname eq PROP_GATEWAY) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | return $rackdev->$propname->{addr} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | elsif ($propname eq PROP_INTERFACES) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | return map $_->{iface}, $rackdev->physical_interfaces | |||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | elsif ($propname eq PROP_IPV4ADDRS) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | return map $_->{addr}, $rackdev->$propname | |||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | elsif ($propname eq PROP_MACADDRS) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | return map $_->{l2address_text}, $rackdev->$propname | |||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | elsif ($rackdev->can($propname)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | return $rackdev->$propname | |||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | RackMan->error("unknown property '$propname'"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | RackMan->error("empty property name"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # check_dns() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | # --------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | ▶ | sub check_dns { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_dns: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | # try to resolve the given name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | my (undef, undef, $addrtype, $length, @addrs) = gethostbyname($name); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | return "- '$name' can't be resolved" unless @addrs; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | # try to reverse resolve the address | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | my $fqdn = gethostbyaddr($addrs[0], $addrtype); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | my $addr_str = inet_ntoa($addrs[0]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | ▶ | return "- $addr_str has no PTR" unless $fqdn; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | # check that the resulting FQDN matches the original name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | return "- '$name' -> $addr_str -> '$fqdn'" unless $fqdn eq $name; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 1 | 1 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||