Class: Nagios::Nrdp
- Inherits:
-
Object
- Object
- Nagios::Nrdp
- Defined in:
- lib/nagios/nrdp.rb
Overview
Implements an interface to Nagios NRDP to facilitate submitting check results and commands
Instance Attribute Summary collapse
-
#token ⇒ String
The authentication token.
-
#url ⇒ String
The URL of the NRDP endpoint.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Nrdp
constructor
Create a new instance of Nagios::Nrdp and set the URL and token.
- #submit_check(*args) ⇒ Object (also: #submit_checks)
-
#submit_command(the_command = '') ⇒ Object
Submit a Nagios command.
Constructor Details
#initialize(args = {}) ⇒ Nrdp
Create a new instance of Nagios::Nrdp and set the URL and token
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
#token ⇒ String
The authentication token
19 20 21 |
# File 'lib/nagios/nrdp.rb', line 19 def token @token end |
#url ⇒ 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
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 = doc.xpath('//message').first.content fail "Status isn't 0 (" + + ')' 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
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 = doc.xpath('//message').first.content fail "Status isn't 0 (" + + ')' unless status == 0 true end |