-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhlsCurrentListners.pl
133 lines (92 loc) · 3.2 KB
/
hlsCurrentListners.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
#!/usr/bin/perl
## Author: [email protected] 2024-05-09
##
## This program will look at the log and output a time stamp
## and the number of listeners. The first listener column
## includes session info where available, the second is the number
## of uniqie IPs.
##
## Designed to be called from Cron once a minute.
## ToDo: demonize, output to configurable logfiles
## This is for all program URLs.
## ToDo: Version that writes out the listener numbers for all
## the possible programs.
use strict;
use warnings;
use File::Tail;
use Time::Piece;
#Extra verbosity. TODO: add to command line.
my $debug=0;
my $filename = '/var/log/nginx/access.log';
#How many lines should we tail. Increase based on the traffic your server gets.
my $maxlines=10000;
#TODO: if the first line read is equal to the timestamp, increase this number and try again.
my $current_time = localtime;
# Subtract one minute from the current time
my $previous_minute = $current_time - 60;
# Get the timestamp for the previous minute
my $previous_period_time = $previous_minute->strftime("%d/%b/%Y:%H:%M");
my ($formatted_time )= $previous_minute->strftime("%a %b %e %H:%M:%S %Y");
my $display_previous_period_time = $previous_minute->strftime("%a %b %e %H:%M:%S %Y");
my $listenerIPCount=0;
my $listenerCount=0;
my(%uniqueIPs);
my(%uniqueIPandUID);
open TAIL, "tail -n $maxlines $filename|" || die "Cannot open file '$filename' for reading: $!";
while(<TAIL>) {
chomp;
my $line=$_;
#my($theip,$request) = Process($line)
my ($theip, $request) = Process($line);
if ($theip) {
print "$theip -> $request\n" if $debug;
my($iponly)=$theip;
$iponly=~s/-.*//;
unless (exists $uniqueIPs{$iponly}) {
$uniqueIPs{$iponly}++ ;
$listenerIPCount++ ;
print "uniqueIPs $iponly $theip\n" if $debug;
}
unless (exists $uniqueIPandUID{"$theip"}) {
$uniqueIPandUID{"$theip"}++;
$listenerCount++;
print "uniqueIPandUID $theip " . $uniqueIPandUID{"$theip"} . "\n" if $debug;
}
}
}
#Experimenting with different formats
#printf("%5s %s\n", $listenerCount, $previous_period_time);
print "$formatted_time\t$listenerCount\t$listenerIPCount\n";
sub Process {
my($line)=@_;
my($uuid);
print "\n==== LINE: $line ======\n" if $debug;
if ($line =~ /^(\S+) (\S+) (\S+) \[([^\]]+)\] "([^"]+)" (\d+) (\d+) "([^"]+)" "([^"]+)" "([^"]+)"$/) {
my ($ip, $dash1, $dash2, $timestamp, $request, $status, $size, $referer, $user_agent, $dash3) = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
#Skip unless previous minute
return('') unless ($timestamp=~/$previous_period_time/);
return ('') unless ($request=~/GET /);
#
$request=~s/^GET //g;
$request=~s/\/\//\//g;
$request=~s/ HTTP\/.*//;
my ($uuid)='';
#This is possibly broken for items that have multiple query strings!
if ($request =~ /id=([A-Za-z0-9\-]+)/) {
$uuid=$1;
$uuid=~s/\?.*//; #Hope that fixes it?
$uuid=~s/\&.*//;
print "Got $uuid\n" if $debug;
}
$request=~s/\?.*//g;
if ($uuid) {
$ip.='-' . $uuid;
}
print "RETURNING $ip $timestamp $request\n" if $debug;
return ('') unless ($request=~/\/program.m3u8/);
return($ip,$request);
} else {
return('');
print "Line does not match expected format.\n" if $debug;
}
}