hot_catch 0.1.6.8 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bbfee917472dc467699e577ce68ab9fb7b0cc7cdae38fe822f0681eb722ee176
4
- data.tar.gz: f6c6778062d2d8712c9b5c5305774363aa3f7665b480bb21d02fe2838e880290
3
+ metadata.gz: 75f293769b8daa376ff88365d306a256a995d4b26e003df34737fbd48890ce14
4
+ data.tar.gz: e58b5bf8cb390925934a821a0aa67952a744d7f5461388f2de82a4c2bb535abf
5
5
  SHA512:
6
- metadata.gz: 4db091215d06224ddda9e3ecd09eeaa1c6602676909d8ab45b0b050a8c2a4e9be78bc4f28c733ba538119342b0b7fff17086df8904907ead977f61d380dbc6b6
7
- data.tar.gz: 767ab0a8ccbdc90c129b174498ecce4c32f22697ff9ba407e82d83ffd5e2abeb3d2483c5c2c5285d1ebcc0f5d04cd6f970bc522073ef9ad9cb9124e27273c27d
6
+ metadata.gz: 3fc4889873c1df0cd56f9e13124aac720b500b4e3e32db8e43ba286b586a94c2cb883dda615b80d7ee09be9e34ecffa412d53dca93238e230969050a61252f73
7
+ data.tar.gz: d541d582e492febf4dc35267994fc99b8374e23bc099fd8f447caa671fb9795fe5a80bccb1bf8ca4607abe37f8a03847a4208c2f9ddb1ab3ce3b27ae60fbcbc5
@@ -41,7 +41,9 @@ module HotCatch
41
41
  "require 'sidekiq-cron'\n" +
42
42
  "Rails.application.config.sender_logs = HotCatch::MakeHttpsRequest.new\n" +
43
43
  "Sidekiq::Cron::Job.create(name: 'NginxSystemWorker - every 1min', cron: " +
44
- "'*/1 * * * *', class: 'NginxSystemWorker')\n"
44
+ "if Sidekiq.server?\n" +
45
+ " '*/1 * * * *', class: 'NginxSystemWorker')\n" +
46
+ "end\n"
45
47
  end
46
48
  end
47
49
 
@@ -2,7 +2,7 @@ module HotCatch
2
2
  require 'hot_catch/hard_worker'
3
3
  require 'hot_catch/nginx_system_worker'
4
4
  require 'hot_catch/custom_log_subscribers.rb'
5
- require 'hot_catch/get_nginx_logs.rb'
5
+ require 'hot_catch/receive_nginx_logs.rb'
6
6
  require 'hot_catch/get_system_metrics'
7
7
 
8
8
  require 'uri'
@@ -8,7 +8,8 @@ class NginxSystemWorker
8
8
  sender = HotCatch::MakeHttpsRequest.new
9
9
  sender.system_g_log(logs)
10
10
 
11
- logs = get_nginx_logs()
11
+ nginx_logs = ReceiveNginxLogs.new
12
+ logs = nginx_logs.get_last_logs
12
13
  sender = HotCatch::MakeHttpsRequest.new
13
14
  sender.nginx_g_log(logs)
14
15
  end
@@ -0,0 +1,94 @@
1
+ require_relative './time'
2
+ =begin
3
+ Принцип получения логов (сделано так, чтобы не потерять логи за секунду между интервалами выборок):
4
+ 1. Время последнего получения логов хранится в файле tmp/hot_catch_from_time.txt
5
+ 2. Сами логи хранятся в файле log/nginx.access.log
6
+ 3. Логи берутся от времени последнего получения логов минус одна секунда и до лога
7
+ время которого меньше текущего минус одна секунда
8
+ (интервал смещён для точной передачи всех данных за время t - 1 секунда)
9
+ 4. Если нет файла tmp/hot_catch_from_time.txt или он пустой, берутся все логи
10
+ от начала до последнего лога, время которого меньше текущего минус одна секунда.
11
+ =end
12
+ class ReceiveNginxLogs
13
+ attr_accessor :last_get_logs_time_filename, :input_filename, :format_datetime_logs,
14
+ :regexp_datetime_logs
15
+
16
+ SinceDatetimeGetLogsFormat = "%Y-%m-%d %H:%M:%S %z"
17
+
18
+ def initialize
19
+ @last_get_logs_time_filename = 'tmp/hot_catch_from_time.txt'
20
+ @input_filename = 'log/nginx.access.log'
21
+ @format_datetime_logs = "%d/%b/%Y:%H:%M:%S %z"
22
+ @regexp_datetime_logs = /\d{2}\/\w+\/\d{4}:\d{2}:\d{2}:\d{2}\s\+\d{4}/
23
+ @delta_get_logs = 1.seconds
24
+ @logs = []
25
+ @since_datetime_get_logs = nil
26
+ end
27
+
28
+ def get_last_logs
29
+ read_logs
30
+ cut_logs
31
+ @logs.join("")
32
+ end
33
+
34
+ private
35
+
36
+ def read_logs
37
+ if File.exist?(@input_filename)
38
+ @logs = File.open(@input_filename, 'r'){|file| file.readlines}
39
+ else
40
+ []
41
+ end
42
+ end
43
+
44
+ def cut_logs
45
+ current_time = Time.now
46
+ if @logs.present?
47
+ if get_time.present?
48
+ search_index = search_index_from_logs_since_get_logs
49
+ @logs = search_index ? @logs[search_index..-1] : []
50
+ end
51
+ search_index = search_index_for_delta(current_time)
52
+ @logs = search_index ? @logs[0..search_index] : []
53
+ File.open(@last_get_logs_time_filename, 'w'){|file| file.write current_time.strftime(SinceDatetimeGetLogsFormat)}
54
+ end
55
+ end
56
+
57
+ def get_time
58
+ if File.exist?(@last_get_logs_time_filename)
59
+ @since_datetime_get_logs = File.open(@last_get_logs_time_filename, 'r'){|file| file.read }
60
+ # safe_strptime in lib/hot_catch/time.rb
61
+ @since_datetime_get_logs = Time.safe_strptime(@since_datetime_get_logs, SinceDatetimeGetLogsFormat)
62
+ end
63
+ @since_datetime_get_logs
64
+ end
65
+
66
+ def search_index_from_logs_since_get_logs
67
+ search_index = nil
68
+ (@logs.size - 1).downto(0) do |index|
69
+ current_log_datetime = Time.strptime(@logs[index].match(@regexp_datetime_logs)[0], @format_datetime_logs)
70
+ if @since_datetime_get_logs <= (current_log_datetime - @delta_get_logs)
71
+ search_index = index
72
+ else
73
+ break
74
+ end
75
+ end
76
+ search_index
77
+ end
78
+
79
+ # need for getting logs without cut last node
80
+ # example:
81
+ # IP - - [26/Jan/2019:14:43:25 +0000] "GET / HTTP/1.0"
82
+ # Write part log: "IP - - [26/Jan/2019:14:43:" in 26/Jan/2019:14:43:25.5
83
+ # Write part log: "25 +0000] "GET / HTTP/1.0"" in 26/Jan/2019:14:43:25.7
84
+ # collect data start in 26/Jan/2019:14:43:25.6 between (.5 and .7) and last node cutting into 2 logs
85
+ def search_index_for_delta(current_time)
86
+ @logs.reverse.each_with_index do |log, index|
87
+ t = Time.strptime(log.match(@regexp_datetime_logs)[0], @format_datetime_logs)
88
+ if t < (current_time - @delta_get_logs)
89
+ return (@logs.size - index - 1)
90
+ end
91
+ end
92
+ @logs.size
93
+ end
94
+ end
@@ -0,0 +1,7 @@
1
+ class Time
2
+ def self.safe_strptime(value, format, default = nil)
3
+ Time.strptime(value.to_s, format)
4
+ rescue ArgumentError
5
+ default
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module HotCatch
2
- VERSION = '0.1.6.8'
2
+ VERSION = '0.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hot_catch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6.8
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Davhot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-21 00:00:00.000000000 Z
11
+ date: 2019-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -66,11 +66,12 @@ files:
66
66
  - lib/generators/hot_catch/uninstall_generator.rb
67
67
  - lib/hot_catch.rb
68
68
  - lib/hot_catch/custom_log_subscribers.rb
69
- - lib/hot_catch/get_nginx_logs.rb
70
69
  - lib/hot_catch/get_system_metrics.rb
71
70
  - lib/hot_catch/hard_worker.rb
72
71
  - lib/hot_catch/hot_catch_logger.rb
73
72
  - lib/hot_catch/nginx_system_worker.rb
73
+ - lib/hot_catch/receive_nginx_logs.rb
74
+ - lib/hot_catch/time.rb
74
75
  - lib/hot_catch/version.rb
75
76
  - lib/tasks/hot_catch_tasks.rake
76
77
  homepage: https://myhomepage.com
@@ -1,62 +0,0 @@
1
- require 'time'
2
- =begin
3
- Принцип получения логов (сделано так, чтобы не потерять логи за секунду между интервалами выборок):
4
- 1. Время последнего получения логов хранится в файле tmp/hot_catch_from_time.txt
5
- 2. Сами логи хранятся в файле log/nginx.access.log
6
- 3. Логи берутся от времени последнего получения логов минус одна секунда и до лога
7
- время которого меньше текущего минус одна секунда
8
- (интервал смещён для точной передачи всех данных за время t - 1 секунда)
9
- 4. Если нет файла tmp/hot_catch_from_time.txt или он пустой, берутся все логи
10
- от начала до последнего лога, время которого меньше текущего минус одна секунда.
11
- =end
12
-
13
- def get_ind_log_from_now_minus_one_second(logs, now)
14
- logs.reverse.each_with_index do |elem, index|
15
- t = Time.strptime(logs[0 - index - 1].match(/\d{2}\/\w+\/\d{4}:\d{2}:\d{2}:\d{2}/)[0], "%d/%b/%Y:%H:%M:%S")
16
- if t < (now - 1)
17
- return (logs.size - 1 - index)
18
- end
19
- end
20
- nil # все логи удовлетворяют выборке
21
- end
22
-
23
- def get_nginx_logs(from_time_file = 'tmp/hot_catch_from_time.txt', input_file = 'log/nginx.access.log')
24
- from_time = ""
25
- if File.exist?(from_time_file) && File.open(from_time_file, 'r'){|file| from_time = file.read } && !from_time.empty?
26
- from_time = Time.parse(from_time) - 1
27
- logs = ""
28
- if File.exist?(input_file) && File.open(input_file, 'r'){|file| logs = file.readlines} && !logs.empty?
29
- now = Time.now
30
- from_ind = nil
31
- for i in 0 ... logs.size
32
- t = Time.strptime(logs[0 - i - 1].match(/\d{2}\/\w+\/\d{4}:\d{2}:\d{2}:\d{2}/)[0], "%d/%b/%Y:%H:%M:%S")
33
- if from_time <= t
34
- from_ind = 0 - i - 1
35
- else
36
- break
37
- end
38
- end
39
- logs = from_ind ? logs[from_ind..-1] : ""
40
- if !logs.empty?
41
- to_ind = get_ind_log_from_now_minus_one_second(logs, now)
42
- logs = to_ind ? logs[0..to_ind] : ""
43
- end
44
-
45
- File.open(from_time_file, 'w'){|file| file.write now}
46
- logs.empty? ? logs : logs.join("")
47
- end
48
- else
49
- logs = ""
50
- if File.exist?(input_file) && File.open(input_file, 'r'){|file| logs = file.readlines}
51
- end
52
- now = Time.now
53
- if !logs.empty?
54
- ind = get_ind_log_from_now_minus_one_second(logs, now)
55
- puts ind
56
- logs = ind ? logs[0..ind] : ""
57
- File.open(from_time_file, 'w'){|file| file.write now}
58
- logs = logs.join("")
59
- end
60
- logs
61
- end
62
- end