hls-log-monitor 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ef4d1c8df240fc7e2524596073561869a9ec76c7
4
+ data.tar.gz: ddaa6859f651d0501fa369b0c4db9d1d2936e221
5
+ SHA512:
6
+ metadata.gz: 08af5acc09c3fdfdd52c209cf338cf50c79a151c8a64602d1a7ffd665d656602e9bdc3f92e246bcaf7390028ad5ecc6767ed2ed5592bdd287955a02ac6c56947
7
+ data.tar.gz: ef74bfe42a8044c62607449862e604defee28afd7a514a9b0994c6a79d27fa62daa9d612bdf46653861e0233b6be6cb2479030a32bf14cd5d67dfbeb2b995c68
@@ -0,0 +1,14 @@
1
+ require 'file-tail'
2
+
3
+ def HLSLogMonitor(args)
4
+ File.open(args[:access_log]) do |log|
5
+ log.extend(File::Tail)
6
+ log.interval = 10
7
+ log.backward(10)
8
+ log.tail do |ln|
9
+ if ln.include?(args[:stream_path])
10
+ yield ln
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gem 'file-tail'
3
+ gem 'sinatra'
4
+ gem 'redis'
@@ -0,0 +1,23 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ file-tail (1.1.0)
5
+ tins (~> 1.0)
6
+ rack (1.5.2)
7
+ rack-protection (1.5.3)
8
+ rack
9
+ redis (3.1.0)
10
+ sinatra (1.4.5)
11
+ rack (~> 1.4)
12
+ rack-protection (~> 1.4)
13
+ tilt (~> 1.3, >= 1.3.4)
14
+ tilt (1.4.1)
15
+ tins (1.3.3)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ file-tail
22
+ redis
23
+ sinatra
@@ -0,0 +1,5 @@
1
+ require './redis-tracking'
2
+ fork do
3
+ RedisTracking::start_monitoring()
4
+ end
5
+ run Web
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/hls-log-monitor'
3
+
4
+ require 'sinatra/base'
5
+ require 'digest/md5'
6
+ require 'redis'
7
+ require 'json'
8
+
9
+ ACCESS_LOG, STREAM_PATH = '/var/log/nginx/access_log', '/hls/livestream'
10
+ REDIS_PREFIX = 'hls.'
11
+ REDIS_EXPIRE = 5
12
+
13
+ $redis = Redis.new()
14
+
15
+ module RedisTracking
16
+ def self.start_monitoring()
17
+ HLSLogMonitor( :access_log => ACCESS_LOG,
18
+ :stream_path => STREAM_PATH ) do |entry|
19
+ whitespace_splits, quote_splits = entry.split(' '), entry.chomp.split('"')
20
+ ip_addr, user_agent = whitespace_splits.first, quote_splits.last
21
+ client_id = Digest::MD5.hexdigest( [ip_addr, user_agent].join() )[0,10]
22
+ p [client_id, ip_addr, user_agent]
23
+ self.set_key( client_id, { 'ip_addr' => ip_addr, 'user_agent' => user_agent } )
24
+ end
25
+ end
26
+
27
+ def self.set_key(k, v)
28
+ key = REDIS_PREFIX+k
29
+ $redis.set(key, v.to_json)
30
+ $redis.expire(key, REDIS_EXPIRE)
31
+ end
32
+
33
+ def fetch_clients()
34
+ clients = {}
35
+ $redis.keys(REDIS_PREFIX+"*").each do |k|
36
+ raw_json = $redis.get(k)
37
+ k.gsub!(REDIS_PREFIX, '')
38
+ clients.store(k, JSON.load(raw_json))
39
+ end
40
+ clients
41
+ end
42
+
43
+ class Web < Sinatra::Base
44
+ get '/' do
45
+ erb :index
46
+ end
47
+
48
+ get '/fetch_clients' do
49
+ RedisTracking::fetch_clients().to_json
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ include RedisTracking
@@ -0,0 +1,41 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
6
+ <style>
7
+ body { font-family: sans-serif; }
8
+ span { padding: 4px; }
9
+ .client_id { background-color: #f0f0f0; }
10
+ .client_ip { color: #0000f0; }
11
+ </style>
12
+ <title>redis-tracking</title>
13
+ </head>
14
+ <body>
15
+ <div>
16
+ <h3><%= STREAM_PATH %></h3>
17
+ <ul id="clients">
18
+ </ul>
19
+ </div>
20
+
21
+ <script>
22
+ (function worker() {
23
+ $.ajax({
24
+ url: '/fetch_clients',
25
+ success: function(rawData) {
26
+ $('#clients li').remove();
27
+ data = JSON.parse(rawData);
28
+ for( client_id in data ) {
29
+ client = data[client_id];
30
+ li = '<li><span class="client_id">' + client_id + '</span> <span class="client_ip">' + client['ip_addr'] + '</span> <span class="client_ua">' + client['user_agent'] + '</span></li>' ;
31
+ $('#clients').append( li );
32
+ };
33
+ },
34
+ complete: function() {
35
+ setTimeout(worker, <%= REDIS_EXPIRE*1000/2 %>); // half time
36
+ }
37
+ });
38
+ })();
39
+ </script>
40
+ </body>
41
+ </html>
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative 'lib/hls-log-monitor'
3
+ require 'digest/md5'
4
+
5
+ HLSLogMonitor( :access_log => '/var/log/nginx/access_log',
6
+ :stream_path => '/hls/livestream' ) do |entry|
7
+
8
+ whitespace_splits, quote_splits = entry.split(' '), entry.chomp.split('"')
9
+ ip_addr, user_agent = whitespace_splits.first, quote_splits.last
10
+ client_id = Digest::MD5.hexdigest( [ip_addr, user_agent].join() )
11
+ p [client_id, ip_addr, user_agent]
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hls-log-monitor
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Matias Insaurralde
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: file-tail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ description: Monitoring nginx hls streams (nginx-rtmp-module)
28
+ email: matias@insaurral.de
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/hls-log-monitor.rb
34
+ - redis-tracking/Gemfile
35
+ - redis-tracking/Gemfile.lock
36
+ - redis-tracking/config.ru
37
+ - redis-tracking/redis-tracking.rb
38
+ - redis-tracking/views/index.erb
39
+ - sample.rb
40
+ homepage: https://github.com/heimusica/hls-log-monitor
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.2.2
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Gem for monitoring nginx hls streams
64
+ test_files: []