This repository was archived by the owner on Dec 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoh.rb
executable file
·158 lines (129 loc) · 4.34 KB
/
woh.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
153
154
155
156
157
158
#!/usr/bin/env ruby
# Purpose: Compile everything to static status page
# Author : Anh K. Huynh
# Date : 2015 Jun 19th
# License: MIT
require 'yaml'
require 'time'
# time > time > time > time > ...
def look_up_least_date(timestamp)
cloned = ($events2.keys | [timestamp]).sort.reverse
pos = cloned.index(timestamp)
if (pos and (pos < cloned.size))
return cloned[pos + 1]
end
return nil
end
def get_status(timestamp)
return {"status" => "bug", "message" => "Unknown status"} if timestamp.nil?
event = ($events2[timestamp] || {})
if not event.empty?
status = (event["status"] || "up")
message = (event["message"] || "(empty message)")
event = {"status" => status, "message" => message}
return event
end
if last_recored_date = look_up_least_date(timestamp)
event = Hash.new
event["message"] = $events2[last_recored_date]["message"]
event["status"] = $events2[last_recored_date]["status"]
event["message"] = "#{event["message"]}"
event["link"] = Time.at(last_recored_date).to_s
else
event = {"status" => "bug", "message" => "Unknown status"}
end
return event
end
########################################################################
# Main program
########################################################################
data = YAML.load_documents(STDIN.read)
$settings = {}
events = {}
$events2 = {}
data.each do |input|
$settings.merge!(input["settings"] || {})
events.merge!(input["events"] || {})
end
events.each do |datestring,details|
begin
timestamp = Time.parse(datestring).to_i
rescue => e
STDERR.puts ":: #{e}"
next
end
if details.is_a?(String)
details = {"status" => "up", "message" => details}
end
$events2[timestamp] ||= {}
$events2[timestamp].merge!(details)
end
########################################################################
# Option 1: Print a row of statuses of a service
########################################################################
if ARGV.index("--option1")
start_of_today = Time.now
start_of_today = start_of_today - (start_of_today.hour * 3600 + start_of_today.min * 60 + start_of_today.sec)
end_of_today = start_of_today + 24 * 3600
link = "#"
if service = ENV["SERVICE"]
link = "#{service.downcase}.html"
end
display_name = ($settings["name"] || ENV["SERVICE"] || "Unknown")
external = ""
if $settings["url"]
external = " <a href='#{$settings["url"]}'><img class='icon' src='./images/external.png' /></a>"
end
puts " <td class=\"service\"><a href=\"#{link}\">#{display_name}</a>#{external}</td>"
# Return the last known even (a week ago)
%w{0 1 2 3 4 5 6}.map(&:to_i).each do |offset|
timestamp = end_of_today.to_i - offset * 24 * 3600 - 1
event = get_status(timestamp)
puts " <td class='status'><img class='icon' src='./images/#{event["status"]}.png' /></td>"
end
exit(0)
end
########################################################################
# Option 2: Print all statuses of a service
########################################################################
if ARGV.index("--option2")
events_keys = $events2.keys.sort.reverse{|timestamp| timestamp > Time.now.to_i}
if events_keys.size == 0
puts "<table>"
puts "<tr>"
puts " <td class='message'><strong>No event found</strong></td>"
puts "</tr>"
puts "</table>"
exit(0)
end
prev_date = ""
events_keys.each do |timestamp|
event = get_status(timestamp)
curr_date = Time.at(timestamp).strftime("%Y-%b-%d")
if not curr_date == prev_date
if not prev_date.empty?
puts "</table>"
end
puts "<h3 id=\"#{curr_date}\">#{curr_date}</h3>"
puts "<table>"
end
prev_date = curr_date
puts "<tr>"
puts " <td class='timestamp'>#{Time.at(timestamp).strftime("%Y-%b-%d %H:%M:%S")}</td>"
puts " <td class='status_thin'><img class='icon' src='./images/#{event["status"]}.png' /></td>"
puts " <td class='message'>#{event['message']}</td>"
puts "</tr>"
end
puts "</table>"
exit(0)
end
########################################################################
# Option 2: Print the last status of a service
########################################################################
if ARGV.index("--option3")
event = get_status(Time.now.to_i)
puts "#{event['status']} #{event['message']}"
exit(0)
end
STDERR.puts ":: Syntax: $0 [--option1] [--option2]"
exit(1)