-
Notifications
You must be signed in to change notification settings - Fork 2
/
acl.pl
executable file
·341 lines (273 loc) · 9.99 KB
/
acl.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/perl
# acl.pl - Advanced Color Logs
$version = "0.7.0";
# written by Patrick Mullen ([email protected])
#
# VISIT THE LINUX RESOURCE CENTER -- http://www.LinuxRC.org
#
# The LATEST VERSION of this program can be found at
# http://www.LinuxRC.org/projects/acl
#
# USAGE
# acl.pl [-f <configfile>] < file.to.colorize
#
# Default config file is acl.conf, and is searched for in
# the path ".:$HOME/.acl:/etc/acl". An alternate config file
# may be specified using the -f command-line parameter.
use Getopt::Long;
# Create the colors Assoc. Array
%colors = (
'default' => "\033[0m",
'black' => "\033[30m",
'red' => "\033[31m",
'green' => "\033[32m",
'yellow' => "\033[33m",
'blue' => "\033[34m",
'magenta' => "\033[35m",
'cyan' => "\033[36m",
'white' => "\033[37m"
);
%attributes = (
'normal' => "\033[00m",
'bright' => "\033[01m",
'underlined' => "\033[04m",
'blinking' => "\033[05m",
'background' => "\033[07m",
'beep' => "\007",
'wake' => "\033[13]"
);
$normal = $colors{default};
$timestampexp = '[A-S][a-u][b-y] [0-9 ][0-9] [0-2][0-9]:[0-6][0-9]:[0-6][0-9]';
$timestampcolor = $attributes{'bright'}.$colors{'blue'};
$defaulthostnamecolor = $attributes{'bright'}.$colors{'green'};
$servicecolor = $attributes{'bright'}.$colors{'yellow'};
GetOptions("f=s" => \$configfile, "file=s" => \$configfile, "info" => \$infoarg,
"version" => \$versionarg, "help" => \$helparg,
"syslog" => \$syslog, "wake" => \$wake, "nosyslog" => \$nosyslog,
"nowake" => \$nowake) or die &print_help;
if($infoarg) {
&program_info; exit;
} elsif($helparg) {
&print_help; exit;
} elsif($versionarg) {
print "$version\n"; exit;
}
$configdir = "/usr/local/etc";
$hconfigdir = "$ENV{'HOME'}/.acl";
$configfile = ($configfile || "acl.conf");
$legacyconfigfile = "/etc/acl.conf";
$configfile = &find_configfile($configfile);
open CFG, $configfile or die "Error opening configuration file $configfile. Exiting.\n";
$maxindex = 0;
while(<CFG>) {
chomp;
# Strip comments
s/:.*$//;
# Next line if line is empty now:
next if (/^\s*$/);
# Chomp out the whitespace
s/^\s*//;
s/\s*$//;
# Search for special characters
s/\~/\\\~/g;
# s/\!/\\\!/g; # We use ! to denote a line is NOT to have a string
# May want to make this smarter, ie don't substitute
# if we find ",!" or ",[whitespace]!"
s/\@/\\\@/g;
s/\#/\\\#/g;
s/\$/\\\$/g;
s/\%/\\\%/g;
s/\^/\\\^/g;
s/\&/\\\&/g;
s/\*/\\\*/g;
s/\-/\\\-/g;
s/\_/\\\_/g;
s/\=/\\\=/g;
s/\+/\\\+/g;
s/\[/\\\[/g;
s/\]/\\\]/g;
s/\{/\\\{/g;
s/\}/\\\}/g;
s/\|/\\\|/g;
s/\"/\\\"/g;
s/\;/\\\;/g;
s/\</\\\</g;
s/\>/\\\>/g;
s/\?/\\\?/g;
s/\(/\\\(/g;
s/\)/\\\)/g;
s/\`/\\\`/g;
s/\'/\\\'/g;
if(/^option/i) {
($tag, $option) = split(/\s*,(.*)/);
for($option) {
/syslog/i && do { $syslog = 1; last; };
/wake/i && do { $wake = 1; last; };
die("Unknown option in config file: $option");
}
next;
}
if(/^host/i) {
s/ //g; # We don't care about spaces; they can't be in hostnames.
# By stripping them it makes our associative array easier.
($tag, $host, $colorconfig) = split(/\,/, $_, 3);
$hosts{$host} = &parse_color($colorconfig);
next;
}
if(/^timestamp/i) {
($tag, $colorconfig) = split(/\,/, $_, 2);
$timestampcolor = &parse_color($colorconfig);
next;
}
if(/^service/i) {
s/ //g; # We don't care about spaces; they can't be in servicenames.
# By stripping them it makes our associative array easier.
# If the above is incorrect, this part will break, as will
# the parsing of lines further down.
($tag, $service, $colorconfig) = split(/\,/, $_, 3);
$services{$service} = &parse_color($colorconfig);
next;
}
#ZDNOTE: Can this be made into one split?
($colorconfig, $rest_of_line) = split(/\s*,(.*)/);
$_ = $rest_of_line;
($primary_match, $secondary_matches) = split(/\s*,(.*)/);
$search_strings{$maxindex, 'color'} = &parse_color($colorconfig);
$search_strings{$maxindex, 'primary_match'} = $primary_match;
$search_strings{$maxindex, 'secondary_matches'} = $secondary_matches;
$search_strings{$maxindex, 'hide'} = ($colorconfig =~ /hide/i);
$maxindex++;
}
# If command line was used to override config file options, we
# need to account for them here.
if($nowake) { $wake = 0; }
if($nosyslog) { $syslog = 0; }
# The main loop. Process each input line and print them in color.
while($line = <>) {
$found = 0;
chomp($line);
for($index = 0; $index < $maxindex; $index++) {
$color = $search_strings{$index, 'color'};
$primary_match = $search_strings{$index, 'primary_match'};
$secondary_matches = $search_strings{$index, 'secondary_matches'};
$hide = $search_strings{$index, 'hide'};
if($primary_match && $line =~ /$primary_match/) {
$found = 1;
$rest_of_line=$secondary_matches;
while($rest_of_line) {
$_ = $rest_of_line;
($string, $rest_of_line) = split(/\s*,(.*)/);
if(grep(/^!/, $string)) {
if($line =~ substr($string, 1)) {
$found = 0;
}
} else {
if($line !~ $string) {
$found = 0;
}
}
}
if($found) { last; }
}
}
if(!$found) { $color = $default; $hide = 0; }
# This is just until I get it set up with independent colors
# $servicecolor = $color;
if(!$hide) {
if($wake) { print $attributes{'wake'}; }
if($syslog) {
if($line =~ /($timestampexp)\s+(\S+)\s+(\S+:)\s+(.*)/) {
$hostnamecolor = $defaulthostnamecolor;
foreach(keys %hosts) {
if($2 =~ /$_/i) { $hostnamecolor = $hosts{$_}; last; }
}
$servicecolor = $color; # If we don't have one specificly for this service,
# color it with rest of line.
foreach(keys %services) {
if($3 =~ /$_/i) { $servicecolor = $services{$_}; last; }
}
$line = "$normal$timestampcolor$1$normal $hostnamecolor$2$normal $servicecolor$3$normal$color $4";
} elsif($line =~ /($timestampexp)\s+(\S+)\s+(.*)/) {
$hostnamecolor = $defaulthostnamecolor;
foreach(keys %hosts) {
if($2 =~ /$_/i) { $hostnamecolor = $hosts{$_}; last; }
}
# This format doesn't have a servicename
$line = "$normal$timestampcolor$1$normal $hostnamecolor$2$normal$color $3";
} else {
$line = $color.$line;
}
} else {
$line = $color.$line;
}
print "$line$normal\n";
}
}
sub parse_color {
local($colorconfig) = @_;
local($color);
# This is a switch/case statement(!), see man perlsyn
for ($colorconfig) {
/black/i && do { $color = $colors{'black'}; last; };
/red/i && do { $color = $colors{'red'}; last; };
/green/i && do { $color = $colors{'green'}; last; };
/yellow/i && do { $color = $colors{'yellow'}; last; };
/blue/i && do { $color = $colors{'blue'}; last; };
/magenta/i && do { $color = $colors{'magenta'}; last; };
/cyan/i && do { $color = $colors{'cyan'}; last; };
/white/i && do { $color = $colors{'white'}; last; };
$color = $colors{'default'};
}
for ($colorconfig) {
$color=$color.$attributes{'normal'} if(/normal/i);
$color=$color.$attributes{'bright'} if(/bright/i);
$color=$color.$attributes{'underlined'} if(/underlined/i);
$color=$color.$attributes{'blinking'} if(/blinking/i);
$color=$color.$attributes{'background'} if(/background/i);
$color=$color.$attributes{'beep'} if(/beep/i);
$color=$color.$attributes{'wake'} if(/wake/i);
}
$color;
}
sub find_configfile() {
# Try to get config in several places:
my @try=("$_[0]", "$_[0].conf",
"$hconfigdir/$_[0]", "$hconfigdir/$_[0].conf",
"$configdir/$_[0]", "$configdir/$_[0].conf",
"$legacyconfigfile");
foreach (@try) { return $_ if (-r $_); }
# More files than this are tried, but for cosmetic reasons we don't
# want to display that we tried files like "acl.conf.conf".
$errormsg = "Could not find any of these files:\n";
$errormsg = $errormsg."\t./$_[0]\n";
$errormsg = $errormsg."\t$hconfigdir/$_[0]\n";
$errormsg = $errormsg."\t$configdir/$_[0]\n";
$errormsg = $errormsg."\t$legacyconfigfile\n";
die($errormsg);
}
sub program_info() {
print "Advanced Color Logs $version by ";
print "Patrick Mullen \<p_mullen\@linuxmail.org\>\n\n";
&print_help;
print<<EndOfInformation;
The default configuration file is named acl.conf and will be searched for in
the following path: '.:\$HOME/.acl:/etc/acl'.
For backwards compatibility, /etc/acl.conf may also be used.
The configuration file consists of comma separated fields:
'Color/Attributes, primary search string, additional strings'
The primary search string must be found in the line to match, but additional
strings can be either found in the line, or they can be specified as not being
in a line by preceding the string with '!'.
Valid colors are magenta, blue, yellow, green, red, cyan, white, and black.
Valid attributes are normal, underlined, blinking, bright, and background.
In addition, beep is a valid attribute as well as hide.
EndOfInformation
}
sub print_help() {
print<<EndOfHelp;
-help => Help (this)
-info => Program information
-version => Program version
-file => Specify configuration file
EndOfHelp
}