-
Notifications
You must be signed in to change notification settings - Fork 0
/
bboss-report.pl
executable file
·240 lines (198 loc) · 7.43 KB
/
bboss-report.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/perl
#
# BBoss Report - Bacula Boss's Report
# Generate a spreadsheet containing a report of what Bacula save: jobname, fileset, pre-post scripts ecc. All written in a human readable's format
#
# LICENSE:
#
# Copyright (c) 2016 Diennea s.r.l. by Davide Giunchi. https://github.com/davidegiunchidiennea
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#TODO:
# fix the "todo" around the code
# acquire the configuration
require '/etc/bacula/bboss-report.conf';
#http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel-2.40/lib/Spreadsheet/WriteExcel/Examples.pm
use Spreadsheet::WriteExcel;
$filename=$ARGV[0];
if (!defined($filename)) {
print "BBoss Report: Generate a report of what Bacula save\n";
print "Error, usage: bboss-report.pl /tmp/my-report.xls\n\n";
die;
}
my $workbook = Spreadsheet::WriteExcel->new($filename);
my $worksheet = $workbook->add_worksheet('Backup');
# Create a format for the headings
my $format = $workbook->add_format();
# Add a handler to store the width of the longest string written to a column.
# We use the stored width to simulate an autofit of the column widths.
# You should do this for every worksheet you want to autofit.
$worksheet->add_write_handler(qr[\w], \&store_string_widths);
$format->set_bold();
# textwrap, respect the newlines
# doc: http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel-2.40/lib/Spreadsheet/WriteExcel/Examples.pm#Example:_textwrap.pl
my $format_a_capo = $workbook->add_format();
$format_a_capo->set_text_wrap();
%onechar_to_human = ( 'F' => 'File' , 'D' => 'Directory' );
$line_counter=0;
open (JOBS,"echo 'show jobs'\|$bconsole |");
while (<JOBS>) {
# exclude unnecessary informations
unless ( ($_ =~ /^Connecting to Director/) or ($_ =~ /^1000 OK:/) or ($_ =~ /^Enter a period/) or ($_ =~ /^show jobs/) or ($_ =~ /^You have messages/)) {
#Job: name=name-of-job JobType=66 level= Priority=10 Enabled=1
if ( $_ =~ /^Job: name=(.+) JobType=(\d+) .+ Enabled=(\d)$/) {
### it's a new job, so write the previous report ###
if ($jobname) {
$worksheet->write($line_counter, 0, "$jobname");
$worksheet->write($line_counter, 1, "$jobtype");
# remove the last, useless, newline "\n"
chomp($fileset_decoded);
$worksheet->write($line_counter, 2, "$fileset_decoded",$format_a_capo);
chomp($script_pre);
$worksheet->write($line_counter, 3, "$script_pre",$format_a_capo);
chomp($script_post);
$worksheet->write($line_counter, 4, "$script_post",$format_a_capo);
$worksheet->write($line_counter, 5, "$enabled");
} else {
# it's the first line, generate column's name
#JOBNAME;JOBTYPE;FILESET;PRESCRIPT;POSTSCRIPT;DISABLE
$worksheet->write(0, 0, 'JobName', $format);
$worksheet->write(0, 1, 'JobType', $format);
$worksheet->write(0, 2, 'FileSet', $format);
$worksheet->write(0, 3, 'Script Pre Backup', $format);
$worksheet->write(0, 4, 'Script Post Backup', $format);
$worksheet->write(0, 5, 'Disable', $format);
}
$line_counter++;
$jobname = $1;
$jobtype = $2;
$enabled = $3;
if ($enabled == 1) {
# don't write anything if it's enabled: it makes only noise
$enabled = '';
} else {
$enabled = 'Disabled';
}
if ($jobtype == 66) {
$jobtype = 'Backup';
} elsif ($jobtype == 82) {
$jobtype = 'Restore';
} elsif ($jobtype == 99) {
$jobtype = 'Copy';
} elsif ($jobtype == 86) {
$jobtype = 'Verify';
}
# zeroing the fileset, to get the various O N I only after the true fileset
($fileset_name,$fileset_decoded,$options,$include,$exclude,$script_pre,$script_post,$script)='';
} elsif ( $_ =~ /^ --> FileSet: name=(.+)$/) {
$fileset_name = $1;
} elsif ( $_ =~ /^ N$/) {
#Separator
$fileset_decoded .= "\n";
# zeroing, even the include-exclude are finished
($include,$exclude)='';
} elsif ( $_ =~ /^ O (.+)$/) {
#Options
$options = $1;
$fileset_decoded .= "Options: ";
if ( $options =~ /e/) {
$fileset_decoded .= "Exclude ";
} elsif ( $options =~ /o/) {
$fileset_decoded .= "OneFS ";
}
} elsif ( $_ =~ /^ I (.+)$/) {
#DIR/FILE to include
$include_tmp=$1;
#TODO: use something better than a regexp
# se e' la prima volta
if ( $include =~ /^$/) {
$include = $include_tmp;
$fileset_decoded .= "- Include: $include_tmp , ";
} else {
$include = $include_tmp;
$fileset_decoded .= "$include_tmp , ";
}
#} elsif ( $_ =~ /^ R (.+)$/) {
} elsif ( $_ =~ /^ R(D|F)* (.+)$/) {
$fileset_decoded .= "RegExp" . $onechar_to_human{$1} . ": $2 ";
#$fileset_decoded .= "RegExp $1: $2 ";
# regexp
} elsif ( $_ =~ /^ W(D|F)* (.+)$/) {
#$fileset_decoded .= "Wild $1: $2 ";
$fileset_decoded .= "Wild" . $onechar_to_human{$1} . ": $2 ";
# regexp
} elsif ( $_ =~ /^ E (.+)$/) {
# exclude
$exclude_tmp=$1;
#TODO: use something better than a regexp
# if it's the first time
if ( $exclude =~ /^$/) {
$exclude = $exclude_tmp;
$fileset_decoded .= "Exclude $exclude , ";
} else {
$exclude = $exclude_tmp;
$fileset_decoded .= "$exclude , ";
}
### PRE/POST SCRIPT ###
} elsif ( $_ =~ /^ --> Command=(.+)$/) {
$script=$1;
} elsif ( $_ =~ /^ --> RunWhen=(\d)$/) {
#2 is pre, 1 is post
if ($1 == 2) {
$script_pre.=$script . "\n";
} else {
$script_post.=$script . "\n";
}
}
}
}
# http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel-2.40/lib/Spreadsheet/WriteExcel/Examples.pm#Example:_autofit.pl
# Run the autofit after you have finished writing strings to the workbook.
autofit_columns($worksheet);
###############################################################################
#
# Adjust the column widths to fit the longest string in the column.
#
sub autofit_columns {
my $worksheet = shift;
my $col = 0;
for my $width (@{$worksheet->{__col_widths}}) {
$worksheet->set_column($col, $col, $width) if $width;
$col++;
}
}
sub store_string_widths {
my $worksheet = shift;
my $col = $_[1];
my $token = $_[2];
return if not defined $token; # Ignore undefs.
return if $token eq ''; # Ignore blank cells.
return if ref $token eq 'ARRAY'; # Ignore array refs.
return if $token =~ /^=/; # Ignore formula
return if $token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
return if $token =~ m{^[fh]tt?ps?://};
return if $token =~ m{^mailto:};
return if $token =~ m{^(?:in|ex)ternal:};
my $old_width = $worksheet->{__col_widths}->[$col];
my $string_width = string_width($token);
if (not defined $old_width or $string_width > $old_width) {
$worksheet->{__col_widths}->[$col] = $string_width;
}
return undef;
}
sub string_width {
return 0.9 * length $_[0];
}