-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththunderstorm-collector.pl
executable file
·187 lines (165 loc) · 5.89 KB
/
thunderstorm-collector.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
#!/usr/bin/perl -s
#
# THOR Thunderstorm Collector
# Florian Roth
# v0.1
# April 2021
#
# Requires LWP::UserAgent
# - on Linux: apt-get install libwww-perl
# - other: perl -MCPAN -e 'install Bundle::LWP'
#
# Usage examples:
# $> perl thunderstorm-collector.pl -- -s thunderstorm.internal.net
# $> perl thunderstorm-collector.pl -- --dir / --server thunderstorm.internal.net
use warnings;
use strict;
use Getopt::Long;
use LWP::UserAgent;
use File::Spec::Functions qw( catfile );
use Cwd; # module for finding the current working directory
# Configuration
our $debug = 0;
my $targetdir = "/";
my $server = "";
my $port = 8080;
my $scheme = "http";
our $max_age = 3; # in days
our $max_size = 10; # in megabytes
our @skipElements = map { qr{$_} } ('^\/proc', '^\/mnt', '\.dat$', '\.npm');
our @hardSkips = ('/proc', '/dev', '/sys');
# Command Line Parameters
GetOptions("dir=s" => \$targetdir, # same for --dir or -d
"server=s" => \$server, # same for --server or -s
"port=i" => \$port, # same for --port or -p
"debug" => \$debug # --debug
);
# Composed Values
our $api_endpoint = "$scheme://$server:$port/api/checkAsync";
our $current_date = time;
# Stats
our $num_submitted = 0;
our $num_processed = 0;
# Objects
our $ua;
# Process Folders
sub processDir {
my ($workdir) = shift;
my ($startdir) = &cwd;
# keep track of where we began
chdir($workdir) or do { print "[ERROR] Unable to enter dir $workdir:$!\n"; return; };
opendir(DIR, ".") or do { print "[ERROR] Unable to open $workdir:$!\n"; return; };
my @names = readdir(DIR) or do { print "[ERROR] Unable to read $workdir:$!\n"; return; };
closedir(DIR);
foreach my $name (@names){
next if ($name eq ".");
next if ($name eq "..");
#print("Workdir: $workdir Name: $name\n");
my $filepath = catfile($workdir, $name);
# Hard directory skips
my $skipHard = 0;
foreach ( @hardSkips ) {
$skipHard = 1 if ( $filepath eq $_ );
}
next if $skipHard;
# Is a Directory
if (-d $filepath){
#print "IS DIR!\n";
# Skip symbolic links
if (-l $filepath) { next; }
# Process Dir
&processDir($filepath);
next;
} else {
if ( $debug ) { print "[DEBUG] Checking $filepath ...\n"; }
}
# Characteristics
my $size = (stat($filepath))[7];
my $mdate = (stat($filepath))[9];
#print("SIZE: $size MDATE: $mdate\n");
# Count
$num_processed++;
# Skip some files ----------------------------------------
# Skip Folders / elements
my $skipRegex = 0;
# Regex Checks
foreach ( @skipElements ) {
if ( $filepath =~ $_ ) {
if ( $debug ) { print "[DEBUG] Skipping file due to configured exclusion $filepath\n"; }
$skipRegex = 1;
}
}
next if $skipRegex;
# Size
if ( ( $size / 1024 / 1024 ) gt $max_size ) {
if ( $debug ) { print "[DEBUG] Skipping file due to file size $filepath\n"; }
next;
}
# Age
#print("MDATE: $mdate CURR_DATE: $current_date\n");
if ( $mdate lt ( $current_date - ($max_age * 86400) ) ) {
if ( $debug ) { print "[DEBUG] Skipping file due to age $filepath\n"; }
next;
}
# Submit
&submitSample($filepath);
chdir($startdir) or die "Unable to change back to dir $startdir:$!\n";
}
}
sub submitSample {
my ($filepath) = shift;
print "[SUBMIT] Submitting $filepath ...\n";
my $retry = 0;
for ($retry = 0; $retry < 4; $retry++) {
if ($retry > 0) {
my $sleep_time = 2 << $retry;
print "[SUBMIT] Waiting $sleep_time seconds to retry submitting $filepath ...\n";
sleep($sleep_time)
}
my $successful = 0;
eval {
my $req = $ua->post($api_endpoint,
Content_Type => 'form-data',
Content => [
"file" => [ $filepath ],
],
);
$successful = $req->is_success;
$num_submitted++;
print "\nError: ", $req->status_line unless $successful;
} or do {
my $error = $@ || 'Unknown failure';
warn "Could not submit '$filepath' - $error";
};
if ($successful) {
last;
}
}
}
# MAIN ----------------------------------------------------------------
# Default Values
print "==============================================================\n";
print " ________ __ __ \n";
print " /_ __/ / __ _____ ___/ /__ _______ / /____ ______ _ \n";
print " / / / _ \\/ // / _ \\/ _ / -_) __(_--/ __/ _ \\/ __/ ' \\ \n";
print " /_/ /_//_/\\_,_/_//_/\\_,_/\\__/_/ /___/\\__/\\___/_/ /_/_/_/ \n";
print " \n";
print " Florian Roth, Nextron Systems GmbH, 2021 \n";
print " \n";
print "==============================================================\n";
print "Target Directory: '$targetdir'\n";
print "Thunderstorm Server: '$server'\n";
print "Thunderstorm Port: '$port'\n";
print "Using API Endpoint: $api_endpoint\n";
print "Maximum Age of Files: $max_age\n";
print "Maximum File Size: $max_size\n";
print "\n";
# Instanciate an object
$ua = LWP::UserAgent->new;
print "Starting the walk at: $targetdir ...\n";
# Start the walk
&processDir($targetdir);
# End message
my $end_date = time;
my $minutes = int(( $end_date - $current_date ) / 60);
print "Thunderstorm Collector Run finished (Checked: $num_processed Submitted: $num_submitted Minutes: $minutes)\n";