| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Getopt::Std; | ||
| 6 | use Time::Local; | ||
| 7 | |||
| 8 | my %opts; | ||
| 9 | getopts('d?exasfaimvnp:t:', \%opts); | ||
| 10 | |||
| 11 | if ($opts{'?'}) { | ||
| 12 | print STDERR <<EOT; | ||
| 13 | Usage: policy-list.pl [-sfm] [-ai] [-n] [-p <policy>] [-t <type>] [-ex] | ||
| 14 | Options: | ||
| 15 | -s List schedules for each policy | ||
| 16 | -f List files for each policy | ||
| 17 | -m List client members of each policy | ||
| 18 | |||
| 19 | -a Only list active policies | ||
| 20 | -i Only list in-active policies | ||
| 21 | |||
| 22 | -n Restrict schedules to NetBackup schedules | ||
| 23 | |||
| 24 | -p Match policies against argument (reg-exp) | ||
| 25 | -t Restrict listing to policy type argument | ||
| 26 | |||
| 27 | -e Explode policy/schedule/file/member attributes on each line | ||
| 28 | -x XML output | ||
| 29 | EOT | ||
| 30 | |||
| 31 | exit; | ||
| 32 | } | ||
| 33 | |||
| 34 | use NBU; | ||
| 35 | NBU->debug($opts{'d'}); | ||
| 36 | |||
| 37 | NBU::Class->populate; | ||
| 38 | |||
| 39 | sub printDetail { | ||
| 40 | my $h = shift; | ||
| 41 | my $d = shift; | ||
| 42 | |||
| 43 | if ($opts{'x'}) { | ||
| 44 | print $h; | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | my @elements = split(/\|/, $d); | ||
| 48 | my $sep = ""; | ||
| 49 | foreach my $e (@elements) { | ||
| 50 | print $sep."\"$e\""; | ||
| 51 | $sep = ","; | ||
| 52 | } | ||
| 53 | print "\n"; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | sub listSchedules { | ||
| 59 | my $c = shift; | ||
| 60 | my $lastHeader = shift; | ||
| 61 | my $prefix = shift; | ||
| 62 | my @detail = @_; | ||
| 63 | |||
| 64 | my $nextDetail = shift(@detail); | ||
| 65 | |||
| 66 | my @sl = ($c->schedules, $c->policies); | ||
| 67 | |||
| 68 | if ($opts{'n'}) { | ||
| 69 | my @internal; | ||
| 70 | for my $s (@sl) { | ||
| 71 | push @internal, $s if ($s->type ne "UBAK"); | ||
| 72 | } | ||
| 73 | @sl = @internal; | ||
| 74 | } | ||
| 75 | |||
| 76 | my $eCounter = 0; | ||
| 77 | if (@sl) { | ||
| 78 | for my $s (@sl) { | ||
| 79 | my $scheduleName = $s->name; | ||
| 80 | my $maximumMPX = $s->maximumMPX; | ||
| 81 | my $residence = $s->residence; | ||
| 82 | my ($header, $footer); | ||
| 83 | if ($opts{'x'}) { | ||
| 84 | my $f = $s->frequency; | ||
| 85 | my ($minutes, $hours, $days, $weeks); | ||
| 86 | $minutes = $f % (60 * 60); | ||
| 87 | $hours = int($f / (60 * 60)); | ||
| 88 | $days = int($hours / 24); $hours = $hours % 24; | ||
| 89 | $weeks = int($days / 7); $days = $days % 7; | ||
| 90 | |||
| 91 | my $frequency = ""; my $sep = ""; | ||
| 92 | if ($weeks) { | ||
| 93 | $frequency .= $sep."$weeks W"; | ||
| 94 | $sep = ";"; | ||
| 95 | } | ||
| 96 | if ($days) { | ||
| 97 | $frequency .= $sep."$days D"; | ||
| 98 | $sep = ";"; | ||
| 99 | } | ||
| 100 | if ($hours) { | ||
| 101 | $frequency .= $sep."$hours H"; | ||
| 102 | $sep = ";"; | ||
| 103 | } | ||
| 104 | if ($minutes) { | ||
| 105 | $frequency .= $sep."$minutes M"; | ||
| 106 | $sep = ";"; | ||
| 107 | } | ||
| 108 | |||
| 109 | $header = "<schedule name=\"$scheduleName\" type=\"".$s->type."\" frequency=\"$frequency\""; | ||
| 110 | $header .= " storage-unit=\"".$residence->label."\"" if (defined($residence)); | ||
| 111 | $header .= " mpx=\"$maximumMPX\"" if (defined($maximumMPX)); | ||
| 112 | $header .= ">\n"; | ||
| 113 | $footer = "</schedule>\n"; | ||
| 114 | } | ||
| 115 | else { | ||
| 116 | $header = $scheduleName."\n"; | ||
| 117 | $footer = ""; | ||
| 118 | } | ||
| 119 | if ($opts{'e'}) { | ||
| 120 | my $level = $prefix.'|'.$scheduleName; | ||
| 121 | if (defined($nextDetail)) { | ||
| 122 | $eCounter += &$nextDetail($c, $lastHeader.$header, $level, @detail) | ||
| 123 | } | ||
| 124 | else { | ||
| 125 | printDetail($lastHeader.$header, $level); | ||
| 126 | $eCounter += 1; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | print $lastHeader.$prefix.$header; | ||
| 131 | $eCounter += 1; | ||
| 132 | } | ||
| 133 | print $prefix.$footer if ($eCounter); | ||
| 134 | $lastHeader = ""; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | if (!$opts{'e'}) { | ||
| 138 | $eCounter += &$nextDetail($c, $lastHeader, $prefix." ", @detail) if (defined($nextDetail)); | ||
| 139 | } | ||
| 140 | return $eCounter; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub listMembers { | ||
| 144 | my $c = shift; | ||
| 145 | my $lastHeader = shift; | ||
| 146 | my $prefix = shift; | ||
| 147 | my @detail = @_; | ||
| 148 | |||
| 149 | my $nextDetail = shift(@detail); | ||
| 150 | |||
| 151 | # | ||
| 152 | # All members of the policy | ||
| 153 | my $eCounter = 0; | ||
| 154 | my @cl = (sort {$a->name cmp $b->name} $c->clients); | ||
| 155 | if (@cl) { | ||
| 156 | for my $client (@cl) { | ||
| 157 | my $clientName = $client->name; | ||
| 158 | my $header = $opts{'x'} ? "<client name=\"$clientName\">\n" : $clientName."\n"; | ||
| 159 | my $footer = $opts{'x'} ? "</client>\n" : ""; | ||
| 160 | if ($opts{'e'}) { | ||
| 161 | my $level = $prefix.'|'.$clientName; | ||
| 162 | if (defined($nextDetail)) { | ||
| 163 | $eCounter += &$nextDetail($c, $lastHeader.$header, $level, @detail) | ||
| 164 | } | ||
| 165 | else { | ||
| 166 | printDetail($lastHeader.$header, $level); | ||
| 167 | $eCounter += 1; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | else { | ||
| 171 | print $lastHeader.$prefix.$header; | ||
| 172 | $eCounter += 1; | ||
| 173 | } | ||
| 174 | print $footer if ($eCounter); | ||
| 175 | $lastHeader = ""; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | if (!$opts{'e'}) { | ||
| 179 | $eCounter += &$nextDetail($c, $lastHeader, $prefix." ", @detail) if (defined($nextDetail)); | ||
| 180 | } | ||
| 181 | return $eCounter; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub listFiles { | ||
| 185 | my $c = shift; | ||
| 186 | my $lastHeader = shift; | ||
| 187 | my $prefix = shift; | ||
| 188 | my @detail = @_; | ||
| 189 | |||
| 190 | my $nextDetail = shift(@detail); | ||
| 191 | |||
| 192 | # | ||
| 193 | # All included and excluded files of the policy | ||
| 194 | my $eCounter = 0; | ||
| 195 | my @ifl = $c->include; | ||
| 196 | if (@ifl) { | ||
| 197 | for my $if (@ifl) { | ||
| 198 | next if ($if eq "NEW_STREAM"); | ||
| 199 | my $header = $opts{'x'} ? "<file path=\"$if\">\n" : $if."\n"; | ||
| 200 | my $footer = $opts{'x'} ? "</file>\n" : ""; | ||
| 201 | if ($opts{'e'}) { | ||
| 202 | my $level = $prefix.'|'.$if; | ||
| 203 | if (defined($nextDetail)) { | ||
| 204 | $eCounter += &$nextDetail($c, $lastHeader.$header, $level, @detail) | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | printDetail($lastHeader.$header, $level); | ||
| 208 | $eCounter += 1; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | else { | ||
| 212 | print $lastHeader.$prefix.$header; | ||
| 213 | $eCounter += 1; | ||
| 214 | } | ||
| 215 | print $footer if ($eCounter); | ||
| 216 | $lastHeader = ""; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | if (!$opts{'e'}) { | ||
| 220 | $eCounter += &$nextDetail($c, $lastHeader, $prefix." ", @detail) if (defined($nextDetail)); | ||
| 221 | } | ||
| 222 | return $eCounter; | ||
| 223 | } | ||
| 224 | |||
| 225 | my @detail; | ||
| 226 | push @detail, \&listSchedules if ($opts{'s'}); | ||
| 227 | push @detail, \&listMembers if ($opts{'m'}); | ||
| 228 | push @detail, \&listFiles if ($opts{'f'}); | ||
| 229 | |||
| 230 | my @list; | ||
| 231 | if ($#ARGV > -1 ) { | ||
| 232 | for my $className (@ARGV) { | ||
| 233 | my $class = NBU::Class->byName($className); | ||
| 234 | push @list, $class if (defined($class)); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | else { | ||
| 238 | |||
| 239 | @list = (sort { | ||
| 240 | my $r = $a->type cmp $b->type; | ||
| 241 | $r = $a->name cmp $b->name if ($r == 0); | ||
| 242 | return $r; | ||
| 243 | } (NBU::Class->list)); | ||
| 244 | } | ||
| 245 | |||
| 246 | if ($opts{'x'}) { | ||
| 247 | print "<?xml version=\"1.0\"?>\n"; | ||
| 248 | print "<policy-list>\n"; | ||
| 249 | } | ||
| 250 | |||
| 251 | my $nextDetail = shift(@detail); | ||
| 252 | for my $c (@list) { | ||
| 253 | |||
| 254 | next if (!$c->active && !(defined($opts{'a'}) || defined($opts{'i'}))); | ||
| 255 | next if ($c->active && defined($opts{'i'})); | ||
| 256 | next unless (!defined($opts{'p'}) || ($c->name =~ /$opts{'p'}/)); | ||
| 257 | next unless (!defined($opts{'t'}) || ($c->type =~ /$opts{'t'}/)); | ||
| 258 | |||
| 259 | my $policyDescription = ""; | ||
| 260 | $policyDescription .= $c->name; | ||
| 261 | |||
| 262 | my $eCounter = 0; | ||
| 263 | my ($header, $footer); | ||
| 264 | if ($opts{'x'}) { | ||
| 265 | $header = "<policy name=\"$policyDescription\""; | ||
| 266 | $header .= " type=\"".$c->type."\""; | ||
| 267 | $header .= " storage-unit=\"".$c->residence->label."\"" if (defined($c->residence)); | ||
| 268 | $header .= " maxjobs=\"".$c->maxJobs."\"" if ($c->maxJobs != 2147483647); | ||
| 269 | $header .= ">\n"; | ||
| 270 | $footer = "</policy>\n"; | ||
| 271 | } | ||
| 272 | else { | ||
| 273 | $header = $policyDescription."\n"; | ||
| 274 | $footer = ""; | ||
| 275 | } | ||
| 276 | |||
| 277 | if ($opts{'e'}) { | ||
| 278 | if (defined($nextDetail)) { | ||
| 279 | $eCounter += &$nextDetail($c, $header, $policyDescription, @detail) | ||
| 280 | } | ||
| 281 | else { | ||
| 282 | printDetail($header, $policyDescription); | ||
| 283 | $eCounter += 1; | ||
| 284 | } | ||
| 285 | print $footer if ($eCounter); | ||
| 286 | } | ||
| 287 | else { | ||
| 288 | if (defined($nextDetail)) { | ||
| 289 | $eCounter += &$nextDetail($c, $header, " ", @detail) | ||
| 290 | } | ||
| 291 | else { | ||
| 292 | print $header; | ||
| 293 | $eCounter += 1; | ||
| 294 | } | ||
| 295 | print $footer if ($eCounter); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | if ($opts{'x'}) { | ||
| 299 | print "</policy-list>\n"; | ||
| 300 | } |