Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add attribute magic for boolean and time values

  • Loading branch information...
commit 5e218752eaad5eb6952fcd86bbb51325ec314fa1 1 parent 5260392
@hollow hollow authored
View
77 lib/livestatus/model/base.rb
@@ -1,21 +1,84 @@
module Livestatus
class Base
+ attr_reader :data
+
def initialize(data)
@data = data.symbolize_keys!
end
- def self.find(options = {})
- Livestatus.get(table_name, options).map do |data|
- self.new(data)
+ def method_missing(name, *args)
+ data[name.to_sym]
+ end
+
+ class << self
+ def find(options = {})
+ Livestatus.get(table_name, options).map do |data|
+ new(data)
+ end
+ end
+
+ def table_name
+ to_s.demodulize.tableize.downcase.pluralize
+ end
+
+ def boolean_attributes(*accessors)
+ accessors.each do |m|
+ define_method(m) do
+ data[m] == 1
+ end
+ end
+ end
+
+ def time_attributes(*accessors)
+ accessors.each do |m|
+ define_method(m) do
+ Time.at(data[m].to_i)
+ end
+ end
end
end
+ end
- def self.table_name
- self.to_s.demodulize.tableize.downcase.pluralize
+ module ID
+ def id
+ Digest::SHA1.hexdigest(_id)[0..7]
end
+ end
- def method_missing(name, *args)
- @data[name.to_sym]
+ module State
+ def state
+ {
+ 0 => :ok,
+ 1 => :warning,
+ 2 => :critical,
+ 3 => :unknown,
+ }[data[:state]]
+ end
+
+ def state_class
+ {
+ :ok => :green,
+ :warning => :yellow,
+ :critical => :red,
+ :unknown => :orange,
+ :pending => :gray,
+ }[state]
+ end
+
+ def state_type
+ {
+ 0 => :soft,
+ 1 => :hard,
+ }[data[:state_type]]
+ end
+ end
+
+ module CheckType
+ def check_type
+ {
+ 0 => :active,
+ 1 => :passive,
+ }[data[:check_type]]
end
end
end
View
31 lib/livestatus/model/host.rb
@@ -1,19 +1,20 @@
class Livestatus::Host < Livestatus::Base
- def state
- {
- 0 => :ok,
- 1 => :warning,
- 2 => :critical,
- 3 => :unknown,
- }[@data[:state]]
- end
+ include Livestatus::ID
+ include Livestatus::CheckType
+ include Livestatus::State
+
+ boolean_attributes :accept_passive_checks, :acknowledged,
+ :active_checks_enabled, :checks_enabled, :event_handler_enabled,
+ :flap_detection_enabled, :has_been_checked, :in_check_period,
+ :in_notification_period, :is_executing, :is_flapping,
+ :notifications_enabled, :obsess_over_host, :pending_flex_downtime,
+ :process_performance_data
+
+ time_attributes :last_check, :last_hard_state, :last_hard_state_change,
+ :last_notification, :last_state_change, :last_time_down,
+ :last_time_unreachable, :last_time_up, :next_check, :next_notification
- def state_class
- {
- :ok => :green,
- :warning => :orange,
- :critical => :red,
- :unknown => :gray,
- }[state]
+ def _id
+ display_name
end
end
View
40 lib/livestatus/model/service.rb
@@ -1,19 +1,29 @@
class Livestatus::Service < Livestatus::Base
- def state
- {
- 0 => :ok,
- 1 => :warning,
- 2 => :critical,
- 3 => :unknown,
- }[@data[:state]]
- end
+ include Livestatus::ID
+ include Livestatus::CheckType
+ include Livestatus::State
+
+ boolean_attributes :accept_passive_checks, :acknowledged,
+ :active_checks_enabled, :checks_enabled, :event_handler_enabled,
+ :flap_detection_enabled, :has_been_checked, :host_accept_passive_checks,
+ :host_acknowledged, :host_active_checks_enabled, :host_checks_enabled,
+ :host_event_handler_enabled, :host_flap_detection_enabled,
+ :host_has_been_checked, :host_in_check_period,
+ :host_in_notification_period, :host_is_executing, :host_is_flapping,
+ :host_notifications_enabled, :host_obsess_over_host,
+ :host_pending_flex_downtime, :host_process_performance_data,
+ :in_check_period, :in_notification_period, :is_executing, :is_flapping,
+ :notifications_enabled, :obsess_over_service, :process_performance_data
+
+ time_attributes :host_last_check, :host_last_hard_state,
+ :host_last_hard_state_change, :host_last_notification,
+ :host_last_state_change, :host_last_time_down, :host_last_time_unreachable,
+ :host_last_time_up, :host_next_check, :last_check, :last_hard_state,
+ :last_hard_state_change, :last_notification, :last_state_change,
+ :last_time_critical, :last_time_ok, :last_time_unknown, :last_time_warning,
+ :next_check, :next_notification
- def state_class
- {
- :ok => :green,
- :warning => :orange,
- :critical => :red,
- :unknown => :gray,
- }[state]
+ def _id
+ host_display_name + display_name
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.