-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsano.rb
152 lines (121 loc) · 2.77 KB
/
sano.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require "json"
require "socket"
require "time"
module Sano
OK = 'OK'
WARNING = 'WARNING'
CRITICAL = 'CRITICAL'
LEVELS = [ OK, WARNING, CRITICAL]
class Page
attr_accessor :application, :status, :system
def initialize
@details = []
@datasources = []
@application = Application.new
@system = SystemInfo.new
@properties = {}
end
def add_property(key, value)
@properties[key] = value
end
def add_datasource(ds)
@datasources << ds
end
def add_detail(detail)
@details << detail
calculate_status
true
end
def to_json
json_hash = {
'application' => @application.to_h.merge({'statusDetails' => {}}),
'system' => @system.to_h,
'datasources' => [],
'properties' => @properties
}
@details.each do |d|
json_hash['application']['statusDetails'][d.name] = d.to_h
end
@datasources.each do |d|
json_hash['datasources'] << d.to_h
end
JSON.dump(json_hash)
end
protected
def calculate_status
tmp_status = OK
@details.each do |d|
if LEVELS.index(d.status) > LEVELS.index(tmp_status)
tmp_status = d.status
end
end
@application.status = tmp_status
@status = tmp_status
end
end
class Application < Struct.new(:name, :status, :version)
def to_h
{
'name' => name,
'status' => status,
'version' => (version || "UNSET")
}
end
end
class SystemInfo
attr_accessor :hostname, :system_time
def initialize
@hostname = Socket.gethostbyname(Socket.gethostname).first
@system_time = Time.now
end
def to_h
{
'hostname' => @hostname,
'systemTime' => @system_time.iso8601
}
end
end
class Detail < Struct.new(:name, :status, :message, :link)
attr_accessor :properties
def initialize
status = OK
@properties = {}
end
def self.create(&block)
d = Detail.new
yield(d) if block_given?
d
end
def to_h
{
'name' => name,
'status' => status,
'properties' => properties,
'message' => message,
'uri' => link
}
end
end
class Datasource
attr_accessor :name, :type, :properties, :hosts
def initialize(name, type, hosts = [], properties = {})
@name = name
@type = type
@hosts = hosts
@properties = properties
end
def self.create(&block)
datasource = Datasource.new("unnamed", "unknown")
yield(datasource) if block_given?
datasource
end
def to_h
{
'name' => @name,
'type' => @type,
'hosts' => @hosts,
'properties' => @properties
}
end
end
end