-
Notifications
You must be signed in to change notification settings - Fork 0
/
omPolicyParse.pl
executable file
·246 lines (210 loc) · 9.33 KB
/
omPolicyParse.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
241
242
243
244
#!/usr/bin/perl
## omPolicyParse.pl
## Tim Pilson
## 8/27/2013
## Parse OM policy files
##
## Input: Data diretory with OM policy files, *_data
## Output: .log: policy data formated to CSV
## .error: policies with missing TROUBLETICKET flag
use strict;
use Spreadsheet::WriteExcel;
use Getopt::Long;
my $outputDir = "."; ## Location of data files
my $outputFile = "omPolicyParse.csv"; ## Parsed data output
my $outputExcel = "omPolicyParse.xls"; ## Parsed data output
my $outputError = "omPolicyParse.error"; ## Policies w/out troubletickets
my $inputDir = "."; ## default current path for inputDir (override with command line option)
my $verbose = 0;
my $usage = "Usage: $0 [options]\n";
$usage .= "Options: --verbose, -[vV] Print each policy result to STDOUT.\n";
$usage .= " --input, -[iI] Set input data directory for policy files.\n";
$usage .= " --output, -[oO] Set output data directory for results.\n";
$usage .= " --help, -[hH] Print this help message.\n";
## Set Command Line Options
GetOptions ('input=s' => \$inputDir,
'i=s' => \$inputDir,
'output=s' => \$outputDir,
'o=s' => \$outputDir,
'verbose' => \$verbose,
'v' => \$verbose,
'help' => sub { print $usage; exit; },
'h' => sub { print $usage; exit; }
) or die($usage);
## Establish output files
open(OUTPUT,">$outputDir/$outputFile") || die("Can't open file: $outputFile\n");
open(OUTPUTERROR,">$outputDir/$outputError") || die("Can't open file: $outputError\n");
## Create Excel output file
my $workbook = Spreadsheet::WriteExcel->new("$outputDir/$outputExcel") || die "Can't open Excel output file\n";
my $worksheet = $workbook->add_worksheet();
print "Loading directory list of policy files... ";
## Open the directory where policy files are located
opendir(FILES,$inputDir) || die("Can't open $inputDir\n");
my @fileList = readdir(FILES);
print "done\n\n";
## Set counters for totals
my $totalPolicyCount = 0;
my $advmonitorCount = 0;
my $logfileCount = 0;
my $opcmsgCount = 0;
my $scheduleCount = 0;
my $conditionCount = 0;
my $conditionProcessed = 0;
my $formatError = 0;
my $noTroubleTicket = 0;
## Write the output file header
print OUTPUT "OpenView Policy ID,Category,Application,Item,Summary,Priority\n";
## Write the Excel output file header
$worksheet->write(0, 0, 'OpenView Policy ID');
$worksheet->write(0, 1, 'Policy Name');
$worksheet->write(0, 2, 'Category');
$worksheet->write(0, 3, 'Application');
$worksheet->write(0, 4, 'Item');
$worksheet->write(0, 5, 'Summary');
$worksheet->write(0, 6, 'Priority');
## Open each file from the directory
foreach my $file (@fileList) {
next if ( $file !~ /_data/ ); ## skip non-data files
(my $id = $file) =~ s/_data//; ## strip off _data from file name
$totalPolicyCount++; ## keep track of how many policies we read
open(POLICYFILE,"$inputDir/$file") || die("Can't open file: $file\n");
my $loopstart = 0; ## Used to determine the start of a condition
my $innerLoopstart = 0; ## Used to determine the inner loop structure of a condition
my $policyType = ""; ## Reset the policy type for new file
my $policyName = ""; ## Reset the policy name
my ($application,$msggrp,$object,$text,$severity);
## Read each line from the file
foreach my $polfile (<POLICYFILE>) {
chomp $polfile;
$polfile =~ s/^\s+//; ## Remove initial whitespace
## Determine the policy type
if ( $polfile =~ /^ADVMONITOR/ ) {
$policyType = "advmonitor";
$policyName = $polfile;
$policyName =~ s/ADVMONITOR\s+//; ## Remove keyword ADVMONITOR and trailing whitespace
$policyName =~ s/\"//g; ## Remove parans
$advmonitorCount++;
} elsif ( $polfile =~ /^LOGFILE/ ) {
$policyType = "logfile";
$policyName = $polfile;
$policyName =~ s/LOGFILE\s+//; ## Remove keyword LOGFILE and trailing whitespace
$policyName =~ s/\"//g; ## Remove parans
$logfileCount++;
} elsif ( $polfile =~ /^OPCMSG/ ) {
$policyType = "opcmsg";
$policyName = $polfile;
$policyName =~ s/OPCMSG\s+//; ## Remove keyword OPCMSG and trailing whitespace
$policyName =~ s/\"//g; ## Remove parans
$opcmsgCount++;
} elsif ( $polfile =~ /^SCHEDULE/ ) {
$policyType = "schedule";
$policyName = $polfile;
$policyName =~ s/SCHEDULE\s+//; ## Remove keyword SCHEDULE and trailing whitespace
$policyName =~ s/\"//g; ## Remove parans
$scheduleCount++;
}
## Only handle AdvMonitor, Logfile, and OpcMsg types
## since these are the only policy types that ticket
if ( $policyType =~ /advmonitor|logfile|opcmsg/ ) {
if (( $polfile =~ /CONDITION_ID/ ) && ( $loopstart == 1 )) { ## Missing TROUBLETICKET flag
if ( $verbose == 1 ) { print "$id: Contains a condition without a TROUBLETICKET flag\n\n"; }
print OUTPUTERROR "$id:$policyName: Contains a condition without a TROUBLETICKET flag\n";
$noTroubleTicket++;
} elsif ( $polfile =~ /CONDITION_ID/ ) {
$loopstart = 1; ## Start the condition id loop
## Reset variables for new condition
$severity = "";
$application = "";
$msggrp = "";
$object = "";
$text = "";
}
## Read the severity code
if (( $loopstart == 1 ) && ( $polfile =~ /SEVERITY/ )) {
if ( $polfile !~ /Normal/i ) { ## Ignore condition if categorized as "normal"
$innerLoopstart = 1;
$conditionCount++;
$severity = $polfile;
$severity =~ s/SEVERITY\s+//;
} else {
$innerLoopstart = 0; ## kill the loop initializer if a "Normal" condition
}
}
## Read the application name
if (( $loopstart == 1 ) && ( $innerLoopstart == 1) && ( $polfile =~ /APPLICATION/ )) {
$application = $polfile;
$application =~ s/APPLICATION\s+//; ## Remove keyword APPLICATION and trailing whitespace
$application =~ s/\"//g; ## Remove parans
}
## Read the message group
if (( $loopstart == 1 ) && ( $innerLoopstart == 1) && ( $polfile =~ /MSGGRP/ )) {
$msggrp = $polfile;
$msggrp =~ s/.*(MSGGRP)//; ## Remove everything before MSGGRP
$msggrp =~ s/MSGGRP\s+//; ## Remove whitespace after MSGGRP
$msggrp =~ s/^\s+//; ## Remove any remaining initial whitespace
$msggrp =~ s/\"//g; ## Remove parans
}
## Read the object
if (( $loopstart == 1 ) && ( $innerLoopstart == 1) && ( $polfile =~ /OBJECT/ )) {
$object = $polfile;
$object =~ s/OBJECT\s+//; ## Remove keyword OBJECT and trailing whitespace
$object =~ s/\"//g; ## Remove parans
}
## Read the message text
if (( $loopstart == 1 ) && ( $innerLoopstart == 1) && ( $polfile =~ /TEXT/ )) {
$text = $polfile;
$text =~ s/TEXT\s+//; ## Remove keyword TEXT and trailing whitespace
$text =~ s/^\"//; ## Remove opening paranthesis
$text =~ s/\"$//; ## Remove closing paranthesis
}
## Read the troubleticket line
if (( $loopstart == 1 ) && ( $innerLoopstart == 1) && ( $polfile =~ /TROUBLETICKET/ )) {
$loopstart = 0; ## TROUBLETICKET is the last string so reset loop condition
$innerLoopstart = 0; ## TROUBLETICKET is the last string so reset loop condition
$conditionProcessed++;
## Look for missing fields
if (( $application eq "" ) || ( $msggrp eq "" ) || ( $object eq "" ) || ( $text eq "" )) {
$formatError++;
}
if ( $verbose == 1 ) {
print " Policy ID: $id\n";
print " Policy Name: $policyName\n";
print " Priority: $severity\n";
print " Type: $application\n";
print " Category: $msggrp\n";
print " Object: $object\n";
print " Summary: $text\n\n";
}
print OUTPUT "$id,$policyName,$msggrp,$application,$object,$text,$severity\n";
## Create data row in Excel output file
$worksheet->write($conditionProcessed, 0, "$id");
$worksheet->write($conditionProcessed, 1, "$policyName");
$worksheet->write($conditionProcessed, 2, "$msggrp");
$worksheet->write($conditionProcessed, 3, "$application");
$worksheet->write($conditionProcessed, 4, "$object");
$worksheet->write($conditionProcessed, 5, "$text");
$worksheet->write($conditionProcessed, 6, "$severity");
}
}
}
}
close(FILES);
close(OUTPUT);
close(OUTPUTERROR);
if ( $totalPolicyCount == 0 ) { ## Check if any policies processed
print "No policy files found\n";
} else {
## Total all the policies that were handled
my $totalHandled = ($advmonitorCount + $logfileCount + $opcmsgCount + $scheduleCount);
## Print results/summary
print " Total Policies: $totalPolicyCount\n";
print " Policies Handled: $totalHandled\n";
print " Conditions Parsed: $conditionCount (found severity codes)\n";
print " Conditions (ticketed): $conditionProcessed\n";
print "Conditions (non-ticketed): $noTroubleTicket\n";
print " Format Errors: $formatError (missing fields)\n\n";
print " AdvMonitor Policies: $advmonitorCount\n";
print " Logfile Policies: $logfileCount\n";
print " OpcMsg Policies: $opcmsgCount\n";
print " Schedule Policies: $scheduleCount\n\n";
}