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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SendNsca

Returns a new instance of SendNsca.



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

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

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



122
123
124
# File 'lib/senedsa/send_nsca.rb', line 122

def inspect
  @options
end

#send(*args) ⇒ Object

Raises:

  • (ArgumentError)


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

def send(*args)

  svc_status = nil
  svc_output = nil

  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
  SendNsca.defaults.each_key do |option|
    next if [:send_nsca_config, :svc_status, :svc_output].include? option
    raise ArgumentError, "missing send_nsca option #{option}" if @options[option].nil?
  end
  raise ArgumentError, "missing send_nsca svc_status" if @options[:svc_status].nil?
  raise ArgumentError, "missing send_nsca svc_output" if @options[:svc_output].nil?
  run @options[:svc_status], @options[:svc_output]
end