Class: Senedsa::SendNsca

Inherits:
Object
  • Object
show all
Defined in:
lib/senedsa/send_nsca.rb

Defined Under Namespace

Classes: ConfigurationError, Error, InitializationError, SendNscaError

Constant Summary collapse

STATUS =
{
    :ok       => 0,
    :warning  => 1,
    :critical => 2,
    :unknown  => 3
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SendNsca

Returns a new instance of SendNsca.



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
# File 'lib/senedsa/send_nsca.rb', line 55

def initialize(*args)

  @options = {}

  case args.size

    when 1
      if args[0].is_a? String
        cfg_file = args[0].nil? ? nil : args[0]
        cfg_options = cfg_file.nil? ? {} : SendNsca.configure(cfg_file)
        hsh_options = {}
      elsif args[0].is_a? Hash
        cfg_file = args[0][:senedsa_config].nil? ? nil : args[0][:senedsa_config]
        cfg_options = cfg_file.nil? ? {} : SendNsca.configure(cfg_file)
        hsh_options = args[0]
      else
        raise InitializationError, "invalid argument types"
      end

    when 2
      raise InitializationError, "invalid argument types" unless args[0].is_a? String and args[1].is_a? String
      cfg_options = SendNsca.configure(@options[:senedsa_config])
      hsh_options = { :svc_hostname => args[0], :svc_descr => args[1] }

    when 3
      raise InitializationError, "invalid argument types" unless args[0].is_a? String and args[1].is_a? String and args[2].is_a? Hash
      cfg_options = SendNsca.configure(args[0][:senedsa_config])
      hsh_options = args[2].merge({ :svc_hostname => args[0], :svc_descr => args[1] })

    else
      raise ArgumentError, "wrong number of arguments"
  end
  @options = SendNsca.defaults.merge(cfg_options).merge(hsh_options)
  @options[:svc_hostname] = Socket.gethostname if @options[:svc_hostname].nil?
end

Class Attribute Details

.defaultsObject

Returns the value of attribute defaults.



30
31
32
# File 'lib/senedsa/send_nsca.rb', line 30

def defaults
  @defaults
end

Instance Attribute Details

#nscaObject

Returns the value of attribute nsca.



48
49
50
# File 'lib/senedsa/send_nsca.rb', line 48

def nsca
  @nsca
end

#send_nscaObject

Returns the value of attribute send_nsca.



48
49
50
# File 'lib/senedsa/send_nsca.rb', line 48

def send_nsca
  @send_nsca
end

Class Method Details

.configure(cfg_file) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/senedsa/send_nsca.rb', line 32

def self.configure(cfg_file)
  cfg_options = {}
  unless cfg_file.nil?
    raise ConfigurationError, "unable to read configuration file #{cfg_file}" unless File.readable? cfg_file
    begin
      cfg_options = Psych.load File.open(cfg_file)
      raise ConfigurationError, "senedsa_config not allowed in configuration file (#{cfg_file})" unless cfg_options[:senedsa_config].nil?
    rescue Psych::SyntaxError => e
      raise ConfigurationError, "syntax error in configuration file #{cfg_file}: #{e.message}"
    rescue Errno::ENOENT, Errno::EACCES => e
     raise ConfigurationError, e.message
    end
  end
  cfg_options
end

Instance Method Details

#inspectObject



115
116
117
# File 'lib/senedsa/send_nsca.rb', line 115

def inspect
  @options
end

#send(*args) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/senedsa/send_nsca.rb', line 91

def send(*args)

  case args.size
    when 0
      # svc_status and svc_output should be set on @options
      raise ArgumentError, "svc_status or svc_output not set" if @options[:svc_status].nil? or @options[:svc_output].nil?
      svc_status = @options[:status]
      svc_output = @options[:svc_output]
    when 2
      raise ArgumentError, "invalid svc_status" unless args[0].is_a? Symbol and STATUS.keys.include?(args[0])
      raise ArgumentError, "invalid svc_output" unless args[1].is_a? String
      svc_status = args[0]
      svc_output = args[1]
    else
      raise ArgumentError, "wrong number of arguments"
  end
  run svc_status, svc_output
end