Class: ServiceStatus::Status
- Inherits:
-
Object
- Object
- ServiceStatus::Status
- Defined in:
- lib/service_status/status.rb
Instance Attribute Summary collapse
-
#checks ⇒ Object
readonly
Returns the value of attribute checks.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #add_check(name, successful, description = nil) ⇒ Object
- #add_http_get_check(name, url) ⇒ Object
- #add_stat(name, value, description = nil) ⇒ Object
- #disk_usage ⇒ Object
-
#initialize(name, version, boot_time) ⇒ Status
constructor
A new instance of Status.
- #online? ⇒ Boolean
- #status ⇒ Object
-
#to_json(*a) ⇒ Object
rubocop:disable MethodLength.
- #uptime ⇒ Object
Constructor Details
#initialize(name, version, boot_time) ⇒ Status
Returns a new instance of Status.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/service_status/status.rb', line 13 def initialize(name, version, boot_time) @boot_time = boot_time @name = name @version = version @hostname = Socket.gethostname @checks = [] @timestamp = Time.now.strftime('%Y-%m-%d %T') @stats = [] @errors = [] end |
Instance Attribute Details
#checks ⇒ Object (readonly)
Returns the value of attribute checks.
11 12 13 |
# File 'lib/service_status/status.rb', line 11 def checks @checks end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
11 12 13 |
# File 'lib/service_status/status.rb', line 11 def hostname @hostname end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/service_status/status.rb', line 11 def name @name end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
11 12 13 |
# File 'lib/service_status/status.rb', line 11 def stats @stats end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
11 12 13 |
# File 'lib/service_status/status.rb', line 11 def @timestamp end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
11 12 13 |
# File 'lib/service_status/status.rb', line 11 def version @version end |
Instance Method Details
#add_check(name, successful, description = nil) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/service_status/status.rb', line 24 def add_check(name, successful, description = nil) check = { name: name, successful: successful } check[:description] = description if description @checks << check @errors << name unless successful end |
#add_http_get_check(name, url) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/service_status/status.rb', line 31 def add_http_get_check(name, url) successful = true uri = URI(url) begin res = Net::HTTP.get_response(uri) successful = res.is_a?(Net::HTTPSuccess) rescue successful = false end add_check(name, successful) end |
#add_stat(name, value, description = nil) ⇒ Object
43 44 45 46 47 |
# File 'lib/service_status/status.rb', line 43 def add_stat(name, value, description = nil) stat = { name: name, value: value } stat[:description] = description if description @stats << stat end |
#disk_usage ⇒ Object
66 67 68 |
# File 'lib/service_status/status.rb', line 66 def disk_usage format('%01d%%', (disk_used / disk_size) * 100) end |
#online? ⇒ Boolean
53 54 55 |
# File 'lib/service_status/status.rb', line 53 def online? @errors.empty? end |
#status ⇒ Object
49 50 51 |
# File 'lib/service_status/status.rb', line 49 def status online? ? 'online' : 'offline' end |
#to_json(*a) ⇒ Object
rubocop:disable MethodLength
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/service_status/status.rb', line 71 def to_json(*a) { name: name, version: version, hostname: hostname, stats: stats, checks: checks, timestamp: , uptime: uptime, diskusage: disk_usage, status: status }.to_json(*a) end |
#uptime ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/service_status/status.rb', line 57 def uptime total_seconds = Time.now - @boot_time seconds = total_seconds % 60 minutes = (total_seconds / 60) % 60 hours = total_seconds / (60 * 60) days = total_seconds / (60 * 60 * 24) format('%01dd:%02d:%02d:%02d', days, hours, minutes, seconds) end |