-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplestats.rb
79 lines (56 loc) · 1.15 KB
/
simplestats.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
## Implementation of the Simple Stats API REST server endpoints
require "json"
require "sinatra"
API_VERSION = "1"
def parse_info(file)
# parse the /proc/cpuinfo to JSON
begin
f = open(file,'r')
h = {}
f.readlines.each do |l|
l.chomp!
# the regex below breaks the {mem,cpu}info into k/v
e = l.split(/\t*:\s+/)
# if our split succeeded, store the k/v
h.store e[0], e[1] if e.length == 2
end
ensure
f.close
end
return h
end
get "/ping" do
"PONG"
end
get "/version" do
API_VERSION
end
get "/cpuinfo" do
content_type "application/json"
# parse the /proc/cpuinfo to JSON
# return result
return JSON.generate parse_info('/proc/cpuinfo')
end
get "/meminfo" do
content_type "application/json"
# parse the /proc/meminfo to JSON
# return result
return JSON.generate parse_info('/proc/meminfo')
end
get "/uptime" do
content_type "application/json"
h = {}
# parse the /proc/uptime to JSON
begin
f = open('/proc/uptime','r')
t = f.readline.chomp.split(/\s+/)
if t.length == 2
h['Uptime (seconds)'] = t[0]
h['Idle time (seconds)'] = t[1]
end
ensure
f.close
end
# return result
return JSON.generate h
end