-
Notifications
You must be signed in to change notification settings - Fork 27
/
octo_dispatcher
executable file
·306 lines (245 loc) · 7.83 KB
/
octo_dispatcher
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
#!/usr/bin/perl
=head1 NAME
octo_dispatcher - Octopussy Dispatcher program
=head1 DESCRIPTION
octo_dispatcher is the program used by the Octopussy Project
to receive syslog lines and dispatche into device directories
(syslog --> <device>/Incoming/YYYY/MM/DD/msg_HHhMM_SS.log)
=cut
use strict;
use warnings;
use File::Copy;
use AAT::Syslog;
use AAT::Utils qw( NOT_NULL );
use Octopussy;
use Octopussy::App;
use Octopussy::Cache;
use Octopussy::Device;
use Octopussy::FS;
use Octopussy::RRDTool;
use Octopussy::Storage;
my $PROG_NAME = 'octo_dispatcher';
my $QR_ISO8601_SYSLOG =
qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?(?:Z|(?:.\d{2}:\d{2})) (\S+)/i;
exit if (!Octopussy::App::Valid_User($PROG_NAME));
my $exit_request = 0;
my $cache = Octopussy::Cache::Init('octo_dispatcher');
my %dir_device;
my %device_type;
my %logs;
my %dtype_stats = ();
my %nb_events = ();
=head1 FUNCTIONS
=head2 Init()
Inits Dispatcher
=cut
sub Init
{
%dir_device = ();
%device_type = ();
AAT::Syslog::Message($PROG_NAME, 'LOAD_DEVICES_CONFIG');
my @devices = Octopussy::Device::Configurations('name');
foreach my $d (@devices)
{
my $type = $d->{type};
$type =~ s/ /_/g;
my $status = (defined $d->{status} ? $d->{status} : 'Paused');
$device_type{$d->{name}} = ($status ne 'Stopped' ? $type : '');
$dir_device{$d->{name}} =
Octopussy::Storage::Directory_Incoming($d->{name});
}
Octopussy::RRDTool::Syslog_By_DeviceType_Init();
return (scalar @devices);
}
=head2 Stop()
Stops octo_dispatcher
=cut
sub Stop
{
$exit_request = 1;
return ($exit_request);
}
=head2 Handle_Dir($device, $year, $month, $day, $hour, $min)
Handles directory
=cut
sub Handle_Dir
{
my ($device, $year, $month, $day, $hour, $min) = @_;
if (!defined $dir_device{$device})
{
if (!Octopussy::Device::Valid_Name($device))
{
AAT::Syslog::Message($PROG_NAME,
'LOGS_DROPPED_BECAUSE_INVALID_DEVICE_NAME', $device);
return (undef);
}
my $param_auto_create =
Octopussy::Parameter('automatic_device_creation');
my $param_device_regexp =
Octopussy::Parameter('device_filtering_regexp');
if ( (defined $param_auto_create)
&& ($param_auto_create =~ /^Disabled$/i))
{
AAT::Syslog::Message($PROG_NAME,
'LOGS_DROPPED_BECAUSE_AUTO_DEVICE_CREATION_DISABLED', $device);
return (undef);
}
elsif ((defined $param_device_regexp)
&& ($device !~ /$param_device_regexp/))
{
AAT::Syslog::Message($PROG_NAME,
'LOGS_DROPPED_BECAUSE_DEVICE_DIDNT_MATCH_REGEXP', $device);
return (undef);
}
else
{
if (!-f Octopussy::Device::Filename($device))
{
Octopussy::Device::New(
{
name => $device,
address => $device,
description =>
"New Device ($year/$month/$day $hour:$min) !"
}
);
$device_type{$device} = Octopussy::Parameter('devicetype');
$dir_device{$device} =
Octopussy::Storage::Directory_Incoming($device);
}
}
}
return (undef) if (!defined $dir_device{$device});
my $dir_incoming =
"$dir_device{$device}/$device/Incoming/$year/$month/$day";
Octopussy::FS::Create_Directory($dir_incoming);
return ($dir_incoming);
}
=head2 Write_Logs_10secs($y, $m, $d, $hour, $min, $mod)
Writes Logs by 10 seconds block
=cut
sub Write_Logs_10secs
{
my ($y, $m, $d, $hour, $min, $mod) = @_;
foreach my $device (keys %logs)
{
my $dir = Handle_Dir($device, $y, $m, $d, $hour, $min);
if (defined $dir)
{
my $file = "$dir/msg_${hour}h${min}_$mod.log";
my $i = 0;
if (defined open my $FILE, '>>', $file)
{
foreach my $l (@{$logs{$device}})
{
print {$FILE} "$l\n";
$i++;
}
close $FILE;
$nb_events{$device} += $i;
$dtype_stats{$device_type{$device}} += $i;
Octopussy::FS::Chown($file) if (-f $file);
}
else
{
AAT::Syslog::Message($PROG_NAME, 'UNABLE_OPEN_FILE', $file);
}
delete $logs{$device};
}
}
return (1);
}
=head2 Write_Stats($y, $m, $d, $hour, $min)
Writes Statistics
=cut
sub Write_Stats
{
my ($y, $m, $d, $hour, $min) = @_;
my @syslogs = ();
my $total = 0;
foreach my $k (sort keys %nb_events)
{
push @syslogs, "Device: $k - Events: $nb_events{$k}";
$total += $nb_events{$k};
}
AAT::Syslog::Messages($PROG_NAME, \@syslogs);
$cache->set('dispatcher_stats_datetime', "$y$m$d$hour$min");
$cache->set('dispatcher_stats_devices', \%nb_events);
$cache->set('dispatcher_stats_devicetypes', \%dtype_stats);
my $nb = $cache->get("dispatcher_stats_hourly_$y$m$d$hour");
$nb = (NOT_NULL($nb) ? $nb + $total : $total);
$cache->set("dispatcher_stats_hourly_$y$m$d$hour", $nb);
%dtype_stats = ();
%nb_events = ();
return ($total);
}
#
# MAIN
#
my ($device, $line) = (undef, undef);
my ($year, $month, $mday, $hour, $min, $sec) = AAT::Utils::Now();
my ($n_year, $n_month, $n_mday, $n_hour, $n_min, $n_sec) = AAT::Utils::Now();
$SIG{HUP} = \&Init;
$SIG{USR1} = \&Stop;
Octopussy::PID_File($PROG_NAME);
AAT::Syslog::Message($PROG_NAME, 'PROGRAM_START');
Init();
while (!$exit_request)
{
my $fifo = Octopussy::FS::File('fifo');
Octopussy::Create_Fifo($fifo);
open my $FIFO, '<', $fifo
or Octopussy::Die($PROG_NAME,
"[CRITICAL] Can't open named pipe $fifo: $!");
while (my $line = <$FIFO>)
{
chomp $line;
$line =~ s/^<\d+>//;
if ($line =~ $QR_ISO8601_SYSLOG)
{
$device = $1;
# Line dropped if devicename is 'not valid'
if (!Octopussy::Device::Valid_Name($device))
{
AAT::Syslog::Message($PROG_NAME,
'LOGS_DROPPED_BECAUSE_INVALID_DEVICE_NAME', $device);
next;
}
($n_year, $n_month, $n_mday, $n_hour, $n_min, $n_sec) =
AAT::Utils::Now();
if ( (int($n_sec / 10) != int($sec / 10))
|| ($n_min != $min)
|| ($n_hour != $hour)
|| ($n_mday != $mday)
|| ($n_month != $month))
{
my $mod = (int($sec / 10) * 10);
# every 10 secs, we write log files like msg_HHhMM_XX.log
Write_Logs_10secs($year, $month, $mday, $hour, $min, $mod);
if ( ($n_min != $min)
|| ($n_hour != $hour)
|| ($n_mday != $mday)
|| ($n_month != $month)
) # not the same minute than last minute
{
Write_Stats($year, $month, $mday, $hour, $min);
($year, $month, $mday, $hour, $min) =
($n_year, $n_month, $n_mday, $n_hour, $n_min);
}
$sec = $n_sec;
}
push @{$logs{$device}}, $line
if ((!defined $device_type{$device})
|| ($device_type{$device} ne ''));
}
last if ($exit_request);
}
close $FIFO;
Write_Logs_10secs($year, $month, $mday, $hour, $min, int($sec / 10) * 10);
}
=head1 AUTHOR
Sebastien Thebert <octopussy@onetool.pm>
=head1 SEE ALSO
octo_extractor, octo_parser, octo_uparser, octo_reporter, octo_rrd,
octo_scheduler, octo_sender
=cut