Ticket #66: Log.pm

File Log.pm, 5.5 KB (added by Joachim Langenbach, 14 years ago)

A Perl class to handle the log analyze, output and mailing

Line 
1package PB::Log;
2
3# this class can be used to store and analyze the complete log from pb
4# this includes more than one vm
5
6use strict;
7use Mail::Sendmail;
8use PB::Log::Item;
9
10sub new {
11 # contains the object name (here PBLog)
12 my $object = shift;
13 my $self = {};
14 # $self should point to an object of type $object
15 bless($self, $object);
16
17 # this array stores our childs
18 $self->{'logitems'} = [];
19
20 return($self);
21}
22
23# returns number of handled PB::Log::Item's
24sub countItems {
25 my $self = shift;
26 return scalar(@{$self->{'logitems'}});
27}
28
29# returns an array of all names of handled PB::Log::Item's
30# the name is the vm name (e.g. ubuntu-10.04-i386 (by default))
31sub itemNames {
32 my $self = shift;
33 my @result = ();
34 foreach my $item (@{$self->{'logitems'}}){
35 push(@result, $item->name());
36 }
37 return @result;
38}
39
40# set's the log for PB::Log::item $vmname
41# if such an item is not present, one is added
42# $log should only contain the log of one machine
43sub setLog {
44 my $self = shift;
45 my $vmname = shift;
46 my $log = shift;
47
48 my $logitem = $self->findItem($vmname);
49 if(!$logitem){
50 $logitem = new PB::Log::Item($vmname);
51 push(@{$self->{'logitems'}}, $logitem);
52 }
53 $logitem->setLog($log);
54}
55
56# used to analyze the complete log of pb
57sub setCompleteLog {
58 my $self = shift;
59 my $log = shift;
60
61 my $tmplog;
62 my $item;
63 foreach my $line (split("\n", $log)){
64 if($line =~ m/^Waiting [0-9]+ s for VM/){
65 # here starts a new machine, so append the tmplog to the last one
66 if(defined($item)){
67 $item->setLog($tmplog);
68 }
69 if($line =~ m/VM ([^\s]+)/){
70 $item = new PB::Log::Item($1);
71 push(@{$self->{'logitems'}}, $item);
72 $tmplog = 0;
73 }
74 }
75 else{
76 $tmplog .= $line ."\n";
77 }
78 }
79 if(defined($item) && ($tmplog)){
80 $item->setLog($tmplog);
81 }
82}
83
84# nums the issues (Warnings and Errors from lintian and rpmlint
85# if no name is given, the total of all PB::Log::Item's is returned
86sub numQaIssues {
87 my $self = shift;
88 my $itemname = shift || "";
89 my $result = 0;
90 if($itemname eq ""){
91 # no machine selected, so return combine from all items
92 foreach my $item (@{$self->{'logitems'}}){
93 $result += scalar($item->qaIssues());
94 }
95 }
96 else{
97 my $item = $self->findItem($itemname);
98 if($item){
99 $result = $item->numQaIssues();
100 }
101 }
102 return $result;
103}
104
105# returns the issues itself
106# behaves like numQaIssues
107sub qaIssues {
108 my $self = shift;
109 my $itemname = shift || "";
110 my @result = ();
111 if($itemname eq ""){
112 # no machine selected, so return combine from all items
113 foreach my $item (@{$self->{'logitems'}}){
114 push(@result, $item->qaIssues());
115 }
116 }
117 else{
118 my $item = $self->findItem($itemname);
119 if($item){
120 push(@result, $item->qaIssues());
121 }
122 }
123 return @result;
124}
125
126# same as num qaIssues but for compile errors
127sub numErrors {
128 my $self = shift;
129 my $itemname = shift || "";
130
131 my $result = 0;
132 if($itemname eq ""){
133 # no machine selected, so return combine from all items
134 foreach my $item (@{$self->{'logitems'}}){
135 $result += $item->numErrors();
136 }
137 }
138 else{
139 my $item = $self->findItem($itemname);
140 if($item){
141 $result = $item->numErrors();
142 }
143 }
144 return $result;
145}
146
147# returns the compile errors itself
148# behaves like numQaIssues
149sub errors {
150 my $self = shift;
151 my $itemname = shift || "";
152
153 my @result = ();
154 if($itemname eq ""){
155 # no machine selected, so return combine from all items
156 foreach my $item (@{$self->{'logitems'}}){
157 push(@result, $item->errors());
158 }
159 }
160 else{
161 my $item = $self->findItem($itemname);
162 if($item){
163 push(@result, $item->errors());
164 }
165 }
166 return @result;
167}
168
169# same as num qaIssues but for compile warnings
170sub numWarnings {
171 my $self = shift;
172 my $itemname = shift || "";
173
174 my $result = 0;
175 if($itemname eq ""){
176 # no machine selected, so return combine from all items
177 foreach my $item (@{$self->{'logitems'}}){
178 $result += $item->numWarnings();
179 }
180 }
181 else{
182 my $item = $self->findItem($itemname);
183 if($item){
184 $result = $item->numWarnings();
185 }
186 }
187 return $result;
188}
189
190# returns the compile warnings itself
191# behaves like numQaIssues
192sub warnings {
193 my $self = shift;
194 my $itemname = shift || "";
195
196 my @result = ();
197 if($itemname eq ""){
198 # no machine selected, so return combine from all items
199 foreach my $item (@{$self->{'logitems'}}){
200 push(@result, $item->warnings());
201 }
202 }
203 else{
204 my $item = $self->findItem($itemname);
205 if($item){
206 push(@result, $item->warnings());
207 }
208 }
209 return @result;
210}
211
212# prints out a summary of the log
213sub summary {
214 my $self = shift;
215 my $summary = "";
216 $summary = "Items: ". $self->countItems();
217 $summary .= " (QA Issues: ". $self->numQaIssues();
218 $summary .= ", Warnings: ". $self->numWarnings();
219 $summary .= ", Errors: ". $self->numErrors() .")\n";
220 foreach my $name ($self->itemNames()){
221 $summary .= $name ." (QA Issues: ". $self->numQaIssues($name);
222 $summary .= ", Warnings: ". $self->numWarnings($name);
223 $summary .= ", Errors: ". $self->numErrors($name) .")\n";
224 }
225 return $summary;
226}
227
228# mails the summary to $to
229sub mailSummary {
230 my $self = shift;
231 my $to = shift || "";
232 if($to eq ""){
233 print "Please give a to address\n";
234 return;
235 }
236 my %mail = ( To => $to,
237 From => "pb\@localhost",
238 Message => $self->summary()
239 );
240 sendmail(%mail) or return $Mail::Sendmail::error;
241 print "Mail send to ". $to ."\n";
242}
243
244# private part (perl does not no about private, but it is meant so)
245
246# find's item with name $vmname in handled OB::Log::Item's
247sub findItem {
248 my $self = shift;
249 my $vmname = shift;
250 # find existing item or add item if needed
251 foreach my $logitem (@{$self->{'logitems'}}){
252 if($logitem->name eq $vmname){
253 return $logitem;
254 }
255 }
256 return 0;
257}
258
2591;