-
Notifications
You must be signed in to change notification settings - Fork 4
/
FlowTrack.pl
executable file
·298 lines (244 loc) · 7.34 KB
/
FlowTrack.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
#!/usr/bin/env perl
#
# Copyright 2012 Andrew Williams <[email protected]>
#
# Start services
# Main Collection Routines start in FT/FlowCollector.pm
#
# For Documentation & License see the README
#
# Basic Roadmap
# -------------
# TODO: Docs
# TODO: Cleanup Comments etc.
# TODO: ** Release ** 0.01
# TODO: Error Checking config file
# TODO: Deeper server interaction on datatables
# TODO: Sparkline page
# TODO: Long term RRD graphs
# TODO: Add index support to the schema definitions
# TODO: ** Release ** 0.02
use strict;
use warnings;
use Log::Log4perl qw(get_logger);
use English;
use Carp;
use Getopt::Long;
use Data::Dumper;
use Time::HiRes qw ( gettimeofday tv_interval);
use FT::Configuration;
use FT::FlowCollector;
use FT::FlowTrackWeb;
use FT::Reporting;
use POSIX ":sys_wait_h";
use Mojo::Server;
use Mojo::Server::Daemon;
# get things started. Init configs/loops etc. Starts all the processes.
main();
sub main
{
my $command_hash;
my $children;
my $command_line;
my $config_file;
my $printHelp;
my $logger;
# Set to 0 on sigterm
my $keepAlive = 1;
# handle the command line
$command_line = GetOptions( "config=s" => \$config_file );
# Mojolicious consumes --help and -h off of @ARGV making it hard
# to use getopt properly. HOWEVER, it does set MOJO_HELP. We'll use
# that. Still kinda dumb though.
if ( $ENV{MOJO_HELP} )
{
print "\t--help\t\t\t This message\n";
print "\t--config=flowTrack.conf\t Location of config file (defaults to ./flowTrack.conf)\n";
exit;
}
# init the config file
FT::Configuration::setConf($config_file);
# Init our log4perl configuration.
my $config = FT::Configuration::getConf();
if ( exists $config->{logging_conf} && -r $config->{logging_conf} )
{
Log::Log4perl->init_and_watch( $config->{logging_conf} );
$logger = get_logger();
$logger->debug("Loaded l4p configuration");
}
#
# Start launching processes
#
# Daemonize ourself
if (fork)
{
# Original process
close(STDOUT);
close(STDIN);
close(STDERR);
exit;
}
#
# Signal handlers
# These could probably be improved, but they seem to work for now.
#
local $SIG{CHLD} = sub {
my $kid;
do
{
$kid = waitpid( -1, WNOHANG );
} while $kid > 0;
$logger->info("Child died");
};
local $SIG{TERM} = sub { $keepAlive = 0 };
# Here is where we define which routines to fork and run.
# perhaps a bit of over kill, but seems easier to add and change
# stuff this way.
$command_hash->{runReports} = \&runReports;
$command_hash->{Collector} = \&startCollector;
$command_hash->{WebServer} = \&startWebserver;
while ($keepAlive)
{
foreach my $process ( keys %$command_hash )
{
# We don't want to fork or try starting the process
# if it's already running
if ( !isRunning( $process, $children ) )
{
my $pid = fork;
if ($pid)
{
# Parent
# save our pid file
savePIDFile( "main", $$ );
# Add the child to the children hash
# this is used to keep track of who's running or not
# (as well as who to kill in cleanup)
$children->{$pid} = $process;
next;
}
# Child
$logger->info("Starting: $process ($PID)");
# Save the pidfile for the process
savePIDFile( $process, $$ );
# Run the command
&{ $command_hash->{$process} };
# Likely that the below will never get called
$logger->info("Exiting: $process ($$)");
exit;
}
}
# Pause a bit before we go back through the loop
# the signals will wake us up though.
sleep 15;
}
#
# Cleanup
#
$logger->info( "Exiting, killing off children " . join( " ", keys %$children ) );
kill( 2, keys %$children );
# Remove the pid files
removePIDFile('main');
foreach my $child ( keys %$children )
{
removePIDFile( $children->{$child} );
}
exit;
}
# This is the bit that actually does the flow collection
# is just a net::server listener
sub startCollector
{
FT::FlowCollector::CollectorStart();
return;
}
# Setups the mojo server. The application code lives in FlowTrackWeb.pm
sub startWebserver
{
my $config = FT::Configuration::getConf();
my $daemon = Mojo::Server::Daemon->new( listen => [ 'http://*:' . $config->{web_port} ] );
my $app = FT::FlowTrackWeb->new();
$app->secrets( ['3305CA4A-DE4D-4F34-9A38-F17E0A656A25'] );
$daemon->app( FT::FlowTrackWeb->new() );
$daemon->run();
return;
}
# This sub handles running the reports.
# The report loop runs every 5 minutes, on the 5 minute boundary, so at startup we
# figure out how many seconds it is to the next 5 minute boundary and sleep for that long.
#
# Reports don't do anything right now. Will update RRDs consolidate stats etc at a later point
# in time.
sub runReports
{
my $logger = get_logger();
my $config = FT::Configuration::getConf();
my $reports = FT::Reporting->new($config);
while (1)
{
# sleep to the next 5 minute boundary
sleep( ( $config->{reporting_interval} * 60 ) - ( time % ( $config->{reporting_interval} * 60 ) ) );
$logger->info('Running report');
my $start_time = [gettimeofday];
$reports->runReports();
$logger->info( 'Reporting took: ' . tv_interval($start_time) );
sleep( $config->{reporting_interval} * 60 );
}
return;
}
# Writes out pid files
sub savePIDFile
{
my ( $process, $pid ) = @_;
my $config = FT::Configuration::getConf();
my $logger = get_logger();
if ( -w $config->{pid_files} )
{
open( my $fh, ">", $config->{pid_files} . "/$process.pid" )
|| $logger->logconfess( "Couldn't open " . $config->{pid_files} . "/$process.pid: $!" );
print $fh "$pid\n";
close($fh);
}
else
{
$logger->logconfess( $config->{pid_files} . " is either missing or not writable" );
}
return;
}
# Removes the pid files
sub removePIDFile
{
my ($process) = @_;
my $config = FT::Configuration::getConf();
my $logger = get_logger();
my $filename = $config->{pid_files} . "/$process.pid";
unlink $filename or $logger->warn("Couldn't remove $filename");
return;
}
# Figure out whether a given pid is running
# This isn't perfect ()
sub isRunning
{
my ( $process, $children ) = @_;
my $logger = get_logger();
foreach my $pid ( keys %$children )
{
if ( $children->{$pid} eq $process )
{
# Kill 0 should tell us if the process is still running
if ( !kill( 0, $pid ) )
{
$logger->warn("Looks like $process died unexpectedly.");
delete $children->{$pid};
return 0;
}
else
{
return 1;
}
}
}
# If we get here the process wasn't in the children hash, so we
# should assume it's not running
return 0;
}