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

Create a new instance of Nagios::Nrdp and set the URL and token

Parameters:

  • args (Hash) (defaults to: {})

    parameters for this instance

Options Hash (args):

  • :url (String)

    the URL of the NRDP endpoint

  • :token (String)

    the authentication token

Raises:

  • (ArgumentError)

    when the args fail validation



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nagios/nrdp.rb', line 28

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

#tokenString

The authentication token

Returns:

  • (String)

    the authentication token



19
20
21
# File 'lib/nagios/nrdp.rb', line 19

def token
  @token
end

#urlString

The URL of the NRDP endpoint

Returns:

  • (String)

    the URL of the NRDP endpoint



15
16
17
# File 'lib/nagios/nrdp.rb', line 15

def url
  @url
end

Instance Method Details

#submit_check(the_check) ⇒ Object #submit_check(the_checks) ⇒ Object Also known as: submit_checks

Overloads:

  • #submit_check(the_check) ⇒ Object

    Submit a single passive check result

    Parameters:

    • the_check (Hash)

      the passive check result data

    Options Hash (the_check):

    • :hostname (String)

      The hostname for this passive check

    • :servicename (String)

      The optional service name for this passive check

    • :state (Integer)

      The state of this passive check

    • :output (String)

      The output of this passive check

    Raises:

    • (RuntimeError)

      when the submission fails

  • #submit_check(the_checks) ⇒ Object

    Parameters:

    • the_checks (Array<Hash>)

      an array of passive check results

    Raises:

    • (RuntimeError)

      when the submission fails



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
88
89
90
91
92
93
# File 'lib/nagios/nrdp.rb', line 53

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

Submit a Nagios command

Parameters:

  • the_command (String) (defaults to: '')

    the command to be submitted

Raises:

  • (ArgumentError)

    when the args fail validation

  • (RuntimeError)

    when the submission fails



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/nagios/nrdp.rb', line 102

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