-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.pl
executable file
·177 lines (150 loc) · 4.49 KB
/
logger.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
#!/usr/bin/env perl
#
# This is a simple PERL program for opening a UDP ocket & listening for
# UDP logging datagrams.
#
# Each log message is in ASCII and is formatted as:
#
# <file>:<log test>
#
# A hash table (hashing filenames into file handles) is maintained to keep
# track of open files to which data is logged.
#
use strict;
use warnings 'all';
use Socket;
use POSIX;
use Getopt::Std;
# globals
my %handles = ();
my $maxRotateBytes = 1000000;
my $maxFiles = 12;
our($opt_p, $opt_d, $opt_f, $opt_m);
sub check_rotate {
my $file = $_[0];
my $done = 0;
my $filesz;
my $i;
my $j;
my $tmp;
if ( $handles{$file} ) {
$tmp = sysseek( $handles{$file}, 0, SEEK_CUR );
$filesz = $tmp;
# print "$file: $filesz of $maxRotateBytes\n";
if ( $filesz > $maxRotateBytes ) {
# print "$file > $maxRotateBytes\n";
for ( $i = 1 ; $i < $maxFiles && $done == 0 ; ) {
if ( !-e "$file.$i" ) {
$done = 1;
}
else {
$i++;
}
}
# print "largest suffix is $i\n";
if ( $i == $maxFiles ) {
# print "rmmax $file.$i\n";
unlink("$file.$i");
}
# blank out the hashmap entry; a new file will
# be opened when the next log message arrives
# print "close $file\n";
close( $handles{$file} );
undef $handles{$file};
# Now do the actual 'rotate'
for ( $j = $i - 1 ; $j >= 0 ; $j--, $i-- ) {
if ( $j == 0 ) {
# move filename to filename.1
# print "mv $file, $file.$i\n";
rename( $file, "$file.$i" );
}
else {
# move filename.i-1 to filename.i
# print "mv $file.$j, $file.$i\n";
rename( "$file.$j", "$file.$i" );
}
}
}
}
}
# parse command line options
my $port = 2500;
my $logdir = ".";
getopt('pdfm');
if ($opt_p) {
$port = $opt_p;
}
if ($opt_d) {
$logdir = $opt_d;
printf( "logdir=%s\n", $logdir );
}
if ($opt_f) {
$maxFiles = $opt_f;
printf( "maxFiles=%s\n", $maxFiles );
}
if ($opt_m) {
$maxFiles = $opt_m;
printf( "maxRotateBytes=%s\n", $maxRotateBytes );
}
if ( ( length($logdir) > 1 ) || ( $logdir ne "." ) ) {
if ( !-d $logdir ) {
printf("Invalid target directory specified\n");
exit;
}
chdir($logdir) || die "Cannot change to $logdir";
}
printf(
"logger.pl: Listening on port %d; Logging to '%s'; Rotate=%d@%d\n",
$port, $logdir, $maxFiles, $maxRotateBytes
);
my $proto = getprotobyname('udp');
if ( socket( LISTEN, PF_INET, SOCK_DGRAM, $proto ) ) {
setsockopt( LISTEN, SOL_SOCKET, SO_REUSEADDR, pack( "l", 1 ) );
bind( LISTEN, sockaddr_in( $port, INADDR_ANY ) );
listen( LISTEN, SOMAXCONN );
}
else {
print STDERR "Failed to open listening socket : $!\n";
}
#
# need to keep a dynamically instantiated table of logfile names to
# file descriptors.
#
while (1) {
my $from = recv LISTEN, my $rcvbuf, 1024, 0;
my $fromip = inet_ntoa( ( unpack_sockaddr_in($from) )[1] );
# $toip = inet_ntoa((unpack_sockaddr_in(getsockname(LISTEN)))[1]);
# get the target file
# skip packet if undesirable most likely empty packet
next if length($rcvbuf) <= 1;
my @fields = split( /:/, $rcvbuf );
# skip packet if no filename
next if length($fields[0]) < 1;
# skip packet if no data inside
next if !defined $fields[1] or (length($fields[1]) <= 1);
my $leaffile = $fields[0];
my $dir = $fromip;
# create directory and set filename
mkdir $dir, 0777 if !-d $dir;
my $file = "$dir/$leaffile";
# remove the file prefix from the buffer...
my $outbuf = substr $rcvbuf, length($leaffile) + 1;
# check to see if the file should be rotated...
check_rotate($file) if -e $file;
#
# Check to see if the extracted filename already exists in our
# table of open file decriptors.
#
if ( !$handles{$file} ) {
if ( open( $handles{$file}, ">> $file" ) ) {
print "opened $file\n" if !-e "$file.1";
}
else {
print "Failed to open $file !!!\n";
}
}
# write to the file
syswrite $handles{$file}, "$outbuf\n";
# listen for more messages...
listen LISTEN, SOMAXCONN;
}