script/sla.pl untested

filter: Statementstmt 0.0% 0 / 129 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 198 CC 2 SCAR 10.6
CC
2
Cov
0%
dispInterval
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.9
1 #!/usr/local/bin/perl -w
2  
3 use strict;
4  
5 use XML::XPath;
6 use XML::XPath::XMLParser;
7  
8 use Getopt::Std;
9 use Time::Local;
10 use Date::Parse;
11  
12 my %opts;
13 getopts('rldf:sa:p:M:', \%opts);
14  
15 use NBU;
16 NBU->debug($opts{'d'});
17  
18  
19 my $config = "/usr/local/etc/sla.xml";
20 if (defined($opts{'f'})) {
21 $config = $opts{'f'};
22 die "No such configuration file: $config\n" if (! -f $config);
23 }
24  
25 my $xp;
26 if (-f $config) {
27 $xp = XML::XPath->new(filename => $config);
28 die "sla.pl: Could not parse XML configuration file $config\n" unless (defined($xp));
29 }
30  
31 my $master;
32 if ($opts{'M'}) {
33 $master = NBU::Host->new($opts{'M'});
34 }
35 else {
36 my @masters = NBU->masters; $master = $masters[0];
37 }
38  
39 sub dispInterval {
40 my $i = shift;
41  
42 return "--:--:--" if (!defined($i));
43  
44 my $seconds = $i % 60; $i = int($i / 60);
45 my $minutes = $i % 60;
46 my $hours = int($i / 60);
47  
48 my $fmt = sprintf("%02d", $seconds);
49 $fmt = sprintf("%02d:", $minutes).$fmt;
50 $fmt = sprintf("%02d:", $hours).$fmt;
51 return $fmt;
52 }
53 my $period = 1;
54 if ($opts{'p'}) {
55 $period = $opts{'p'};
56 }
57  
58 my $asOf = NBU::Job->loadJobs($master, $opts{'r'}, $opts{'l'});
59 my $mm; my $dd; my $yyyy;
60 my ($s, $m, $h, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($asOf);
61 $year += 1900;
62 $mm = $mon + 1;
63 $dd = $mday;
64 $yyyy = $year;
65  
66 my $since;
67 if ($opts{'a'}) {
68 $asOf = str2time($opts{'a'});
69 }
70 $since = $asOf - ($period * (24 * 60 * 60));
71  
72 my %businessNames;
73 my %businesses;
74 my $nodeset = $xp->find('//business');
75 if ($nodeset->size <= 0) {
76 print "No business definitions found in $config\n";
77 exit 1;
78 }
79 foreach my $node ($nodeset->get_nodelist) {
80 my $name = $node->getAttribute("id");
81 $businessNames{$name} += 1;
82 $businesses{$name} = [];
83 }
84  
85  
86 my $notClassified = 0;
87 for my $job (NBU::Job->list) {
88 next if ($job->active || $job->queued);
89  
90 next if ($job->stop < $since);
91 if ($job->start < $since) {
92 next unless ($job->stop <= $asOf);
93 }
94 next if ($job->start > $asOf);
95 next if ($job->stop > $asOf);
96  
97 next if (!defined($job->storageUnit));
98  
99 #
100 # See if any policy elements fit this job
101 my $nodeset = $xp->find(
102 '//policy[@name=\''.$job->class->name.'\'][@client=\''.$job->client->name.'\']'
103 .' | '.
104 '//policy[@name=\''.$job->class->name.'\'][not(@client)]');
105  
106 if ($nodeset->size > 0) {
107 #print "We appear to care about job ".$job->class->name;
108 #
109 # When this job does appear to have someones's interest, find out which
110 # business that is by looking among the policy element's ancestors for a
111 # business element
112 foreach my $node ($nodeset->get_nodelist) {
113 my $business = $node->find('ancestor::business')->pop;
114 $job->business(my $businessName = $business->getAttribute("id"));
115 my $list = $businesses{$businessName};
116 push @$list, $job;
117  
118 if (defined(my $system = $node->find('ancestor::system')->pop)) {
119 $job->system($system->getAttribute("name"));
120 }
121 }
122 }
123 else {
124 $notClassified += 1;
125 }
126 }
127  
128 print "<?xml version=\"1.0\"?>\n";
129 print "<business-list>\n";
130 foreach my $businessName (sort (keys %businessNames)) {
131 my $list = $businesses{$businessName};
132 my $total = @$list;
133 my %distribution;
134 my %successes; my %failures;
135  
136 $distribution{"hcart2"} = 0;
137  
138  
139 foreach my $job (@$list) {
140 $distribution{my $density = $job->storageUnit->density} += 1;
141 if ($job->success) {
142 $successes{$density} += 1;
143 }
144 else {
145 $failures{$density} += 1;
146 }
147 }
148  
149 my $converted = $total ? sprintf("%.2f", $distribution{"hcart2"} / $total * 100) : "-.--";
150 print " <business id=\"$businessName\" total=\"$total\" converted=\"$converted%\">\n";
151 if ($opts{'s'}) {
152 foreach my $job (@$list) {
153 my $jid = $job->id;
154 my $density = $job->storageUnit->density;
155 my $policyName = $job->class->name;
156 my $scheduleName = $job->schedule->name;
157 print " <stream policy=\"$policyName\" schedule=\"$scheduleName\"";
158 #print " id=\"$jid\";
159 if (($job->elapsedTime > 0) && defined($job->dataWritten) && ($job->dataWritten > 0)) {
160 print " elapsed=\"".dispInterval($job->elapsedTime)."\"";
161 print " kbytes=\"".$job->dataWritten."\"";
162 my $speed = sprintf("%.2f", ($job->dataWritten / $job->elapsedTime / 1024));
163 print " speed=\"$speed\"";
164 print " density=\"$density\"" if (defined($density));
165 }
166 print "/>\n";
167 }
168 }
169 print " </business>\n";
170 }
171 print "<!-- $notClassified streams were not associated with a specific business) -->\n";
172 print "</business-list>\n";