-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactive_users.rb
More file actions
54 lines (44 loc) · 892 Bytes
/
active_users.rb
File metadata and controls
54 lines (44 loc) · 892 Bytes
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
require 'set'
class ActiveUsers
def initialize
@ips = Hash.new
@mutex = Mutex.new
@running = true
end
def run
@expire_thread = Thread.new { expire @ips, @mutex }
while (log_line = gets) && @running
ip = log_line.match(/(?:[0-9]{1,3}\.){3}[0-9]{1,3}/).tap do |result|
result[0] if result
end
if ip
@mutex.synchronize do
@ips[ip] = Time.now
end
print "Active Users: " + @ips.size.to_s + "\r"
end
end
end
def stop
@running = false
@expire_thread.join
end
private
def expire ips, mutex
while @running
mutex.synchronize do
ips.each do |ip, last_request|
if last_request < Time.now - 60
ips.delete ip
end
end
end
sleep 5
end
end
end
a = ActiveUsers.new
Signal.trap("INT") do
a.stop
end
a.run