forked from Leen15/emq-logs-nodes-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs-nodes.rb
executable file
·70 lines (65 loc) · 1.86 KB
/
logs-nodes.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
#!/usr/bin/env ruby
require 'json'
require 'open-uri'
MQTT_USERNAME = ENV['MQTT_USERNAME'] || ''
MQTT_PASSWORD = ENV['MQTT_PASSWORD'] || ''
MQTT_SERVER = ENV['MQTT_SERVER'] || ''
MQTT_API_VERSION = ENV['MQTT_API_VERSION'] || 'v2'
if MQTT_API_VERSION == 'v2'
# get nodes
json = open(
"#{MQTT_SERVER}/api/v2/management/nodes",
http_basic_authentication: [MQTT_USERNAME, MQTT_PASSWORD]
).read
JSON.parse(json)['result'].map do |node|
# get clients for this node
json = open(
"#{MQTT_SERVER}/api/v2/nodes/#{node['name']}/clients?page_size=100000",
http_basic_authentication: [MQTT_USERNAME, MQTT_PASSWORD]
).read
clients_json = JSON.parse(json)['result']['objects']
printf(
"name=\"%s\" uptime=\"%s\" version=\"%s\" node_status=\"%s\" clients_count=\"%s\"\n",
node['name'],
node['uptime'],
node['version'],
node['node_status'],
clients_json.count
)
end
elsif MQTT_API_VERSION == 'v3'
# get nodes
json = open(
"#{MQTT_SERVER}/api/v3/nodes",
http_basic_authentication: [MQTT_USERNAME, MQTT_PASSWORD]
).read
JSON.parse(json)['data'].map do |node|
printf(
"name=\"%s\" uptime=\"%s\" version=\"%s\" node_status=\"%s\" clients_count=\"%s\"\n",
node['name'],
node['uptime'],
node['version'],
node['node_status'],
node['connections']
)
end
elsif MQTT_API_VERSION == 'v4'
# get nodes
json = open(
"#{MQTT_SERVER}/api/v4/nodes",
http_basic_authentication: [MQTT_USERNAME, MQTT_PASSWORD]
).read
# puts json
JSON.parse(json)['data'].map do |node|
printf(
"name=\"%s\" uptime=\"%s\" version=\"%s\" node_status=\"%s\" clients_count=\"%s\"\n",
node['node'],
node['uptime'],
node['version'],
node['node_status'],
node['connections'],
)
end
else
puts "MQTT_API_VERSION unsupported."
end