forked from wedaa/LongTail-Log-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongTail_import_yesterday_to_mysql.pl
executable file
·364 lines (338 loc) · 9.99 KB
/
LongTail_import_yesterday_to_mysql.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/perl
# You must run the following commands so that geo-location works
# and you must download the latest databases after the second tuesday
# of each month
#
# yum install cpan
#cpan Geo::IP
#cpan Socket6
#mkdir /usr/local/share/GeoIP
#mkdir /usr/local/share/GeoIP/backups # Will be used to store older copies of database files
#cd /usr/local/share/GeoIP
#wget geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
#wget geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
#wget http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz
#gunzip GeoIP.dat.gz
#gunzip GeoLiteCity.dat.gz
#gunzip GeoIPv6.dat.gz
#
use strict;
use warnings;
use DBI;
use Geo::IP;
use File::Tail;
my $old_files;
my $valid_action;
my $DEBUG=0;
my $INPUT;
my $new_record;
my $record;
my $print_record;
my $account;
my $host;
my $what;
my $password;
my $files;
my $ip;
my $trash ;
my $who;
my $action;
my $date;
my $munged_date;
my $munged_time;
my $dbh;
my @date_array;
my $country;
my $city;
my $line_counter;
my $today_day;
my $today_month;
my $today_year;
my $today;
my $yesterday_day;
my $yesterday_month;
my $yesterday_year;
my $yesterday;
my $last_month;
my $last_year; # This is really used to figure out what year last_month is in
my $timeout=60; # Wait time in seconds before re-trying a write to myql
my $mail_errors_to="eric.wedaa\@marist.edu";
#my $mail_errors_to="eric.wedaa\@marist.edu";
my $retry_again="no";
my $dev;
my $ino;
my $mode;
my $nlink;
my $uid;
my $gid;
my $rdev;
my $size;
my $atime;
my $mtime;
my $ctime;
my $blksize;
my $blocks;
my $filesize;
my $old_size;
my $current_size;
######################################################################
#
# Initialize a bunch of stuff
#
sub init {
$|=1;
$today_day=`date +"%d" --date="1 day ago"`;
chomp $today_day;
$today_month=`date +"%m" --date="1 day ago"`;
chomp $today_month;
$today_year=`date +"%Y" --date="1 day ago"`;
chomp $today_year;
$today="$today_year-$today_month-$today_day";
$yesterday_day=`date +"%d"`;
chomp $yesterday_day;
$yesterday_month=`date +"%m"`;
chomp $yesterday_month;
$yesterday_year=`date +"%Y"`;
chomp $yesterday_year;
$yesterday="$yesterday_year-$yesterday_month-$yesterday_day";
$last_month=`date +"%m" --date="last month"`;
chomp $last_month;
$last_year=`date +"%Y" --date="last month"`;
chomp $last_year;
$new_record=0;
$record="";
$print_record=0;
$account="LongTail";
$host="longtail.it.marist.edu";
$password="lo96hjmsow";
$country="";
$city="";
$line_counter=0;
if ( -d "/var/www/html/honey/historical/$yesterday_year/$yesterday_month/$yesterday_day" ){
chdir "/var/www/html/honey/historical/$yesterday_year/$yesterday_month/$yesterday_day";
}
else {
print "Can not cd to /var/www/html/honey/historical/$yesterday_year/$yesterday_month/$yesterday_day, exiting now!\n";
exit;
}
}
sub geolocate_ip{
my $ip=shift;
my $local_ip_1;
my $local_ip_2;
$local_ip_1="10\\.";
$local_ip_2="148.100";
#print (STDERR "In geolocate for $ip\n");
my $tmp;
my $tmp2;
if ($ip =~ /^$local_ip_1/){return ("US-Marist:Poughkeepsie");}
if ($ip =~ /^$local_ip_2/){return ("US-Marist:Poughkeepsie");}
#print (STDERR "Foo\n");
my $gi = Geo::IP->open("/usr/local/share/GeoIP/GeoLiteCity.dat", GEOIP_STANDARD);
my $record = $gi->record_by_addr($ip);
#print $record->country_code,
# $record->country_code3,
# $record->country_name,
# $record->region,
# $record->region_name,
# $record->city,
# $record->postal_code,
# $record->latitude,
# $record->longitude,
# $record->time_zone,
# $record->area_code,
# $record->continent_code,
# $record->metro_code;
# ericw note: I have no idea what happens if there IS a record
# but there is no country code
undef ($tmp);
if (defined $record){
$tmp=$record->country_name;
$tmp2=$record->city;
return ("$tmp:$tmp2");
}
else {
return ("undefined:undefined");
}
}
sub geo_locate_country{
$ip=shift;
my $tmp;
my $gi = Geo::IP->open("/usr/local/share/GeoIP/GeoLiteCity.dat", GEOIP_STANDARD);
my $record = $gi->record_by_addr($ip);
# ericw note: I have no idea what happens if there IS a record
# but there is no country code
undef ($tmp);
if (defined $record){
$tmp=$record->country_code;
return ($tmp);
}
return ("undefined");
}
sub read_file {
my $file=shift;
my $tmp;
my $date_ok;
my $munged_month;
my $date;
my $name;
my $time;
my $line;
my $tmp_date;
my $file_handle;
my %months=("Jan","01",
"Feb","02",
"Mar","03",
"Apr","04",
"May","05",
"Jun","06",
"Jul","07",
"Aug","08",
"Sep","09",
"Oct","10",
"Nov","11",
"Dec","12");
my $dbh = DBI->connect("DBI:mysql:database=shibboleth_logins;host=$host",
"$account", "$password",
{'RaiseError' => 1});
if ($@) {
print "Can't connect to mysql database on $host, exiting now\n";
`logger "$0 can't connect to mysql database on $host, failure, exiting now"`;
`echo "This is bad, NOT adding Shibboleth IDP logins to database" |mailx -s "import_idp-process.log-to-mysql-tail.pl can not connect to mysql database on $host, failure, exiting now" $mail_errors_to`;
}
# Create a new table 'foo' if it does not exist already.
# This must not fail, thus we don't catch errors.
#$dbh->do("CREATE TABLE foo (id INTEGER, name VARCHAR(20))");
#CREATE TABLE `login_attempts` (
# `ticket` varchar(80) NOT NULL,
# `hostname` varchar(30) DEFAULT NULL,
# `date` date DEFAULT NULL,
# `time` time DEFAULT NULL,
# `username` varchar(100) DEFAULT NULL,
# `src_ip` varchar(15) DEFAULT NULL,
# `src_as` varchar(20) DEFAULT NULL,
# `src_country` varchar(40) DEFAULT NULL,
# `src_city` varchar(40) DEFAULT NULL,
# `action` varchar(40) DEFAULT NULL,
# PRIMARY KEY (`ticket`)
#) ;
chomp $file;
print (STDERR "Looking at $file now\n");
if ($file =~ /.gz/){
#open (FILE, "zcat $file|") || die "Can not open $file for reading\n";
if ($action eq "-tail"){
print "Something is wrong, tail should NEVER be on a compressed file\n";
exit;
}
}
else {
if ($action eq "-tail"){
$file_handle=File::Tail->new("$file");
}
else {
print "You should never get here, exiting now\n";
}
}
$date_ok=0;
while (defined($line=$file_handle->read)) {
# It's ok if the file rotates as I am not using line-numbers
# as part of the index field since we have millisecond accuracy.
$_ = $line;
chomp;
$line_counter++;
if (/Successfully authenticated/){
($date,$time,$trash,$trash,$trash,$trash,$ip,$trash,$trash,$trash,$trash,$name)=split (/ /,$_);
$tmp=&geolocate_ip($ip);
($country,$city)=split(/:/,$tmp);
$what="$date $time $name";
$dbh->do("INSERT ignore INTO login_attempts VALUES ('$what','idp.it.marist.edu','$date','$time'," . $dbh->quote("$name") . ",'$ip',''," . $dbh->quote("$country") . ",'$city','ValidLogin') ");
if ($@){
print (STDERR "THIS is bad, could not insert into database\n");
print (STDERR "Will retry.\n");
# I'd better get the file size so I can see if the log
# was rotated while I was stuck here.
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$current_size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($files);
`logger "$0 Could not write to mysql database, trying again..."`;
$retry_again="yes";
while ( $retry_again eq "yes"){
sleep ($timeout);
#
# Let's try and open the database again
#
eval {$dbh = DBI->connect("DBI:mysql:database=shibboleth_logins;host=$host",
"$account", "$password",
{'RaiseError' => 1}) };
if ($@) {
print (STDERR "Can't connect to mysql database on $host\n");
`logger "$0 can't connect to mysql database on $host, failure, sleeping now"`;
}
eval { $dbh->do("INSERT ignore INTO login_attempts VALUES ('$what','login.marist.edu','$munged_date','$munged_time'," . $dbh->quote("$who") . ",'$ip',''," . $dbh->quote("$country") . ",'$city','$action') ")};
if ($@){
print (STDERR "THIS is bad, insert into database failed again, failure\n");
print (STDERR "Entry was lost, failure.\n");
print (STDERR "Exiting now DANGER.\n");
`logger "$0 Could not write to mysql database again, will keep trying "`;
open (FILE, ">/opt/tomcat/logs/write_to_mysql_failed");
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($files);
print (FILE "$size\n");
close (FILE);
}
else {
unlink ("/opt/tomcat/logs/write_to_mysql_failed");
$retry_again="no";
# Check here to see if the file shrank while we were
# stuck waiting.... Toss a warning and an email if it did
}
}
}
}
if (/User authentication for.* failed/){
($date,$time,$trash,$trash,$trash,$trash,$ip,$trash,$trash,$trash,$trash,$name)=split (/ /,$_);
$tmp=&geolocate_ip($ip);
($country,$city)=split(/:/,$tmp);
$what="$date $time $name";
$dbh->do("INSERT ignore INTO login_attempts VALUES ('$what','idp.it.marist.edu','$date','$time'," . $dbh->quote("$name") . ",'$ip',''," . $dbh->quote("$country") . ",'$city','FailedLogin') ");
}
}
$tmp_date=`date`;
chomp $tmp_date;
print "Exited from while loop, this should never happen! This was at $tmp_date\n";
# Disconnect from the database.
$dbh->disconnect();
}
&init;
$action=$ARGV[0];
$valid_action=0;
if (! defined ($action)){
$action="";
}
if ($action eq "-tail"){
print "Tailing the file now\n";
$files="idp-process.log";
if ( $files ne "" ){
print "this month's file found: $files\n";
&read_file ("$files");
print "Back from read_file, this should never happen!\n";
exit;
}
else {
print "Could not find this month's file $files\n";
}
$valid_action=1;
}
if ($valid_action < 1 ){
print "No valid action found, printing help screen now\n";
$action ="-help";
}
if (($action eq "-help") || ($valid_action < 1 )){
print "\n";
print "/usr/local/sbin/import_idp-process.log-to-mysql-tail.pl -help || -tail \n";
print " -tail will tail the idp-process.log file add add entries as appropriate forever.\n";
print " -help will show this screen\n";
print "Please also see /usr/local/sbin/import_idp-process.log-to-mysql.pl\n";
print "\n";
}