tiny_info_service 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/tiny_info_service.rb +124 -0
  3. metadata +57 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ba7ae22876f2c605f0c0bc24eeba58c3fa36600fa6cb4f38314b404ae5a1c441
4
+ data.tar.gz: d2ef61e2848951688464485b3ef4918f9b612fc6829e5e7cfc9c1b02198d9f6a
5
+ SHA512:
6
+ metadata.gz: c616b8321f4550335c4e3036d3f9da8b6ffc67e2e053c763a5db14d99fdae49c57a17c544f2713b8c0efbad1717c2b44e1cb1e739934dd386c41a284bf999d13
7
+ data.tar.gz: d3bbd7d4ceff922a66a3622506fc36fc994927ad48a6dd54d9e30a425e236b8ca053de2da932ed9bf09852f59cbbec8343b433404f4074d500510ea6c7159764
@@ -0,0 +1,124 @@
1
+ require 'time'
2
+ require 'tiny_tcp_service'
3
+
4
+ # usage:
5
+ # s = TinyInfoService.new(
6
+ # 1234,
7
+ # 'TinyInfoService',
8
+ # 2,
9
+ # {
10
+ # 'uptime': {
11
+ # 'kind': 'shell_cmd',
12
+ # 'shell_cmd': 'uptime'
13
+ # }
14
+ # }
15
+ # )
16
+ # s.stop!
17
+ class TinyInfoService
18
+ TIME_MULTIPLIERS = {
19
+ 's' => 1,
20
+ 'm' => 60,
21
+ 'h' => 60 * 60,
22
+ 'd' => 60 * 60 * 24
23
+ }
24
+
25
+ def initialize(port, label, refresh_interval_in_seconds, config)
26
+ @service = TinyTCPService.new(port)
27
+ @service.msg_handler = self
28
+ @label = label
29
+ @msg_counter = 0
30
+ @err_counter = 0
31
+ @label = label
32
+ @label_and_port = "#{@label}:#{port}"
33
+ @config = config
34
+ @config.each do |key, details|
35
+ @config[key]['cache'] = _parse_cache(details['cache']) if details['cache']
36
+ end
37
+
38
+ @cache = {}
39
+ @cache_hits = 0
40
+
41
+ # status printing thread
42
+ Thread.new do
43
+ start_time = Time.now
44
+ print "\e[?25l" # hide cursor
45
+ loop do
46
+ break unless @service.running?
47
+ sec_runtime = Time.now - start_time
48
+ human_runtime = "%02d:%02d:%02d" % [sec_runtime / 3600, sec_runtime / 60 % 60, sec_runtime % 60]
49
+
50
+ print "\e[1;1H"
51
+ puts "label/port: #{@label_and_port.rjust(28)}\e[K"
52
+ puts "time : #{DateTime.now.iso8601.rjust(28)}\e[K"
53
+ puts "runtime : #{human_runtime.rjust(28)}\e[K"
54
+ puts "clients : #{@service.num_clients.to_s.rjust(28)}\e[K"
55
+ puts "msgs : #{@msg_counter.to_s.rjust(28)}\e[K"
56
+ puts "errs : #{@err_counter.to_s.rjust(28)}\e[K"
57
+
58
+ sleep refresh_interval_in_seconds
59
+ end
60
+ print "\e[?25h" # show cursor
61
+ end
62
+ end
63
+
64
+ # interface for TinyTCPService
65
+ def call(m)
66
+ raise TinyTCPService::BadClient.new("nil message") if m.nil?
67
+
68
+ case
69
+ when m[0] == '?'
70
+ # return list of keys
71
+ req = m[1..]
72
+
73
+ if @config.keys.include?(req)
74
+ @msg_counter += 1
75
+ case @config[req]['kind']
76
+ when 'shell_cmd'
77
+ cached_value = @cache[req]['value'] if @config[req]['cache'] &&
78
+ @cache.dig(req, 'value') &&
79
+ Time.now - @cache[req]['time'] < @config[req]['cache']
80
+
81
+ if cached_value
82
+ @cache_hits += 1
83
+ return cached_value
84
+ end
85
+
86
+ result = `#{@config[req]['shell_cmd']}`
87
+
88
+ if $?.exitstatus == 0
89
+ t = @config[req]['transform']
90
+ final = t ? eval("result#{t}") : result
91
+
92
+ @cache[req] = {
93
+ 'value' => final,
94
+ 'time' => Time.now
95
+ }
96
+
97
+ final
98
+ else
99
+ _error!
100
+ end
101
+ else
102
+ _error!
103
+ end
104
+ else
105
+ _error!
106
+ end
107
+ else
108
+ _error!
109
+ end
110
+ end
111
+
112
+ def join
113
+ @service.join
114
+ end
115
+
116
+ def _parse_cache(cache_value)
117
+ cache_value[..-2].to_f * TIME_MULTIPLIERS[cache_value[-1]]
118
+ end
119
+
120
+ def _error!
121
+ @err_counter += 1
122
+ 'err'
123
+ end
124
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_info_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Lunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tiny_tcp_service
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: uses the tiny_tcp_service gem to implement a system information service
28
+ email: jefflunt@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/tiny_info_service.rb
34
+ homepage: https://github.com/jefflunt/tiny_info_serivce
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.4.1
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: uses the tiny_tcp_service gem to implement a system information service
57
+ test_files: []