File: grep_log

package info (click to toggle)
pandorafms-agent 6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 588 kB
  • sloc: perl: 3,800; sh: 924; makefile: 5
file content (284 lines) | stat: -rwxr-xr-x 7,142 bytes parent folder | download
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
#!/usr/bin/perl
###############################################################################
#
# Copyright (c) 2008  Ramon Novoa  <rnovoa@artica.es>
# Copyright (c) 2008  Artica Soluciones Tecnologicas S.L.
#
# grep_log	Perl script to search log files for a matching pattern. The last
#           searched position is saved in an index file so that consecutive
#           runs do not return the same results. The log file inode number is 
#           also saved to detect log rotation.
#
# 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; version 2 of the License.
# 
# 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.	
#
###############################################################################

use strict;
use File::Basename;

# Output format (module or log_module).
my $Output = 'module';

# Be verbose
my $Verbose = 0;

# Index file storage directory, with a trailing '/'
my $Idx_dir='/tmp/';

# Log file
my $Log_file = '';

# Module name
my $Module_name = "default_log";

# Index file
my $Idx_file = '';

# Log file position index
my $Idx_pos = 0;

# Log file inode number
my $Idx_ino = '';

# Log file size
my $Idx_size = 0;

# Regular expression to be matched
my $Reg_exp = '';

###############################################################################
# SUB error_msg
# Print an error message and exit.
###############################################################################
sub error_msg ($) {
	my $err_msg = $_[0];

	if (! -z $err_msg) {
		print(stderr "[error] $err_msg.\n");
	}

	exit 1;
}

###############################################################################
# SUB print_help
# Print a help message.
###############################################################################
sub print_help () {
	print "Usage: $0 <log_file> <module_name> <pattern>\n";
}

###############################################################################
# SUB log_msg
# Print a log message.
###############################################################################
sub log_msg ($) {
	my $log_msg = $_[0];

	if (! -z $log_msg && $Verbose == 1) {
		print(stdout "[log] $log_msg.\n");
	}
}

###############################################################################
# SUB load_idx
# Load index file.
###############################################################################
sub load_idx () {
	my $line;
	my $current_ino;
	my $current_size;

	log_msg("Loading index file $Idx_file");

	open(IDXFILE, $Idx_file) || error_msg("Error opening file $Idx_file: " .
	                                         $!);

	# Read position and date
	$line = <IDXFILE>;
	($Idx_pos, $Idx_ino, $Idx_size) = split(' ', $line);

	close(IDXFILE);

	# Reset the file index if the file has changed
	$current_ino = (stat($Log_file))[1];
	$current_size = -s "$Log_file";
	if ($current_ino != $Idx_ino || $current_size < $Idx_size) {
		log_msg("File changed, resetting index");

		$Idx_pos = 0;
		$Idx_ino = $current_ino;
	}
	$Idx_size = $current_size;

	return;
}

###############################################################################
# SUB save_idx
# Save index file.
###############################################################################
sub save_idx () {

	log_msg("Saving index file $Idx_file");

	open(IDXFILE, "> $Idx_file") || error_msg("Error opening file $Idx_file: "
	                                          . $!);
	print (IDXFILE $Idx_pos . " " . $Idx_ino . " " . $Idx_size);
	close(IDXFILE);

	return;
}

###############################################################################
# SUB create_idx
# Create index file.
###############################################################################
sub create_idx () {
	my $first_line;

	log_msg("Creating index file $Idx_file");

	open(LOGFILE, $Log_file) || error_msg("Error opening file $Log_file: " .
	                                     $!);

	# Go to EOF and save the position
	seek(LOGFILE, 0, 2);
	$Idx_pos = tell(LOGFILE);

	close(LOGFILE);

	# Save the file inode number
	$Idx_ino = (stat($Log_file))[1];

	# Save the index file
	save_idx();

	return;
}

###############################################################################
# SUB parse_log
# Parse log file starting from position $Idx_pos.
###############################################################################
sub parse_log () {
	my $line;

	log_msg("Parsing log file $Log_file");

	# Open log file for reading
	open(LOGFILE, $Log_file) || error_msg("Error opening file $Log_file: " .
	                                     $!);

	# Go to starting position. 
	seek(LOGFILE, $Idx_pos, 0);

	# Parse log file
	my @data;
	while ($line = <LOGFILE>) {
		if ($line =~ m/$Reg_exp/i) {
			push (@data, $line);
		}
	}

	$Idx_pos = tell(LOGFILE);
	close(LOGFILE);

	# Save the index file
	save_idx();

	return @data;
}

###############################################################################
# SUB parse_log
# Print log data to stdout.
###############################################################################
sub print_log (@) {
	my @data = @_;

	# No data
	if ($#data < 0) {
		return;
	}
	
	# Log module
	if ($Output eq 'log_module') {
		my $output = "<log_module>\n";
		$output .= "<source><![CDATA[" . $Module_name . "]]></source>\n";
		$output .= "<data><![CDATA[";
		foreach my $line (@data) {
			$output .= $line;
		}
		$output .= "]]></data>";
		$output .= "</log_module>\n";

		print stdout $output;
	}
	# Regular module
	else {
		my $output = "<module>\n";
		$output .= "<name><![CDATA[" . $Module_name . "]]></name>\n";
		$output .= "<type><![CDATA[async_string]]></type>\n";
		$output .= "<datalist>\n";
		foreach my $line (@data) {
			$output .= "<data><value><![CDATA[$line]]></value></data>\n";
		}
		$output .= "</datalist>\n";
		$output .= "</module>\n";

		print stdout $output;
	}
}

###############################################################################
###############################################################################
## Main
###############################################################################
###############################################################################

# Check command line parameters
if ($#ARGV != 2) {
	print_help();
	exit 1;
}

$Log_file = $ARGV[0];
$Module_name = $ARGV[1];
$Reg_exp = $ARGV[2];

# Create index file storage directory
if ( ! -d $Idx_dir) {
	mkdir($Idx_dir) || error_msg("Error creating directory $Idx_dir: "
	                             . $!);
}

# Check that log file exists
if (! -e $Log_file) {
	error_msg("File $Log_file does not exist");
}

# Create index file if it does not exist
$Idx_file=$Idx_dir . $Module_name . "_" . basename($Log_file) . ".idx";
if (! -e $Idx_file) {
	create_idx();
	exit 0;
}

# Load index file
load_idx();

# Parse log file
my @data = parse_log();

# Print output to stdout
print_log (@data);

exit 0;