-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonibento.rb
122 lines (95 loc) · 2.57 KB
/
monibento.rb
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
require 'json'
require 'getoptlong'
require_relative 'lib/log'
require_relative 'lib/convenient_hash_access'
require_relative 'lib/server'
require_relative 'lib/broadcast_server'
hostname = Socket.gethostname
verbose = false
origin = '*'
port = 3333
opts = GetoptLong.new(
[ '--hostname', '-h', GetoptLong::REQUIRED_ARGUMENT ],
[ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
[ '--origin', '-o', GetoptLong::REQUIRED_ARGUMENT ],
[ '--port', '-p', GetoptLong::REQUIRED_ARGUMENT ]
)
opts.each do |opt, arg|
case opt
when '--hostname'
hostname = arg
when '--verbose'
verbose = true
when '--origin'
origin = arg
when '--port'
port = arg
end
end
log = Log.new(verbose)
log.info("monibento starting on host #{hostname}")
server = BroadcastServer.new(port, origin: origin)
server.start Proc.new {
@data_old = @data_new.dup unless @data_new.nil?
@data_new = []
data = { host: hostname, cpu: [], memory: {} }
stat = File.read '/proc/stat'
memstat = File.read '/proc/meminfo'
mems = memstat.lines.select do |line|
line[/^\s*(?:MemTotal|MemFree|Buffers|Cached|SwapTotal|SwapFree)/]
end
mems.each_with_index do |mem, index|
mem[/^\s*(\w+):\s*(\d+)\s*(\w*)/]
amount = $2.to_i
case $3
when 'kB'
amount *= 1024
when 'MB'
amount *= 1024 * 1024
else
end
case $1
when 'MemTotal'
data.memory.total = amount
when 'MemFree'
data.memory.free = amount
when 'Buffers'
data.memory.buffers = amount
when 'Cached'
data.memory.cached = amount
when 'SwapTotal'
data.memory.swap_total = amount
when 'SwapFree'
data.memory.swap_free = amount
else
end
end
cpus = stat.lines.select do |line|
line[/^cpu\d+/]
end
cpus.each_with_index do |cpu, index|
cpu.gsub! /\n/, ''
cpu[/((?:\d+\s+){9}\d+)$/]
values = $1.split /\s+/
values.map! do |value|
value.to_i
end
values << values.inject do |value, sum|
sum += value
end
@data_new.push values.dup
next if @data_old.nil?
values = (0...10).map do |value_index|
if (@data_new[index][10] - @data_old[index][10] == 0.0)
(@data_new[index][value_index] - @data_old[index][value_index]).to_f
else
(@data_new[index][value_index] - @data_old[index][value_index]).to_f / (@data_new[index][10] - @data_old[index][10]).to_f
end
end
keys = %i[ user nice system idle iowait irq softirq steal guest guest_nice]
data.cpu.push Hash[keys.zip(values)]
end
puts 'event: resources'
puts 'data: ' + data.to_json
puts
}