Class: Nagios::Nrdp

Inherits:
Object
  • Object
show all
Defined in:
lib/nagios/nrdp.rb

Overview

Implements an interface to Nagios NRDP to facilitate submitting check results and commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Nrdp

Returns a new instance of Nrdp.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nagios/nrdp.rb', line 12

def initialize(args = {})
  @url = args[:url] || nil
  @token = args[:token] || nil

  fail ArgumentError, 'The URL supplied is invalid!' unless @url && !@url.empty?
  begin
    the_uri = URI.parse(url)
    fail ArgumentError, 'The URL supplied is invalid!' unless the_uri.is_a? URI::HTTP
  rescue URI::InvalidURIError
    raise ArgumentError, 'The URL supplied is invalid!'
  end
  fail ArgumentError, 'The token supplied is invalid!' unless @token && !@token.empty?
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



10
11
12
# File 'lib/nagios/nrdp.rb', line 10

def token
  @token
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/nagios/nrdp.rb', line 9

def url
  @url
end

Instance Method Details

#submit_check(*args) ⇒ Object Also known as: submit_checks



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
# File 'lib/nagios/nrdp.rb', line 26

def submit_check(*args)
  if args[0].is_a? Hash
    the_checks = [args[0]]
  else
    the_checks = args[0]
  end

  payload = build_xml(the_checks)

  the_uri = URI.parse(@url)
  http = Net::HTTP.new(the_uri.host, the_uri.port)
  if the_uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end
  request = Net::HTTP::Post.new(the_uri.request_uri + '/')
  request.set_form_data(token: token, cmd: 'submitcheck', XMLDATA: payload)
  request['Accept'] = 'text/*'
  request['User-Agent'] = 'NrdpClient/1.0'
  response = http.request(request)

  if response.code != '200'
    fail "Didn't get a 200 (" + response.code.to_s + ')'
  end

  doc = Nokogiri::XML(response.body)

  status = doc.xpath('//status').first.content.to_i
  message = doc.xpath('//message').first.content

  fail "Status isn't 0 (" + message + ')' unless status == 0

  count_agrees = false
  doc.xpath('//output').each do |output|
    if output.content == "#{the_checks.count} checks processed."
      count_agrees = true
    end
  end
  fail 'Not all notifications were processed!' unless count_agrees
  true
end

#submit_command(the_command = '') ⇒ Object



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
# File 'lib/nagios/nrdp.rb', line 69

def submit_command(the_command = '')
  if !the_command || !the_command.is_a?(String) || the_command.empty?
    fail ArgumentError, 'Invalid command supplied!'
  end

  the_uri = URI.parse(@url)
  http = Net::HTTP.new(the_uri.host, the_uri.port)
  if the_uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end
  query = "?token=#{token}&cmd=submitcmd&command=#{the_command}"
  request = Net::HTTP::Get.new(the_uri.request_uri + '/' + query)
  request['Accept'] = 'text/*'
  request['User-Agent'] = 'NrdpClient/1.0'
  response = http.request(request)

  if response.code != '200'
    fail "Didn't get a 200 (" + response.code.to_s + ')'
  end

  doc = Nokogiri::XML(response.body)

  status = doc.xpath('//status').first.content.to_i
  message = doc.xpath('//message').first.content

  fail "Status isn't 0 (" + message + ')' unless status == 0
  true
end