nrperb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2004-2011 Bulat Shakirzyanov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is furnished
8
+ to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,24 @@
1
+ # NRPErb
2
+
3
+ Ruby interface to Nagios Remote Plugin Executor daemon.
4
+ Let's you run remote nagios checks from ruby
5
+
6
+ ```ruby
7
+ require 'nrpe'
8
+
9
+ NRPE.session :host => '10.190.157.127', :port => 5666 do |session|
10
+ result = session.execute('check_load')
11
+ puts result.ok?
12
+ puts result.warning?
13
+ puts result.critical?
14
+ puts result.text
15
+ end
16
+ ```
17
+
18
+ ## Install
19
+
20
+ * clone repository
21
+ * run `bundle install`
22
+ * run `bundle exec check_nrpe -h`
23
+ * ...
24
+ * profit
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+
6
+ require 'optparse'
7
+ require 'nrpe'
8
+
9
+ options = {
10
+ :command => '_NRPE_CHECK'
11
+ }
12
+
13
+ OptionParser.new do |opts|
14
+ opts.banner = "Usage: check_nrpe -H <host> [-n] [-u] [-p <port>] [-t <timeout>] [-c <command>] [<arglist...>]"
15
+
16
+ opts.separator ""
17
+ opts.separator "Specific options:"
18
+
19
+ opts.on("-n", nil, "Do not use ssl") do |n|
20
+ options[:use_ssl] = false
21
+ end
22
+
23
+ opts.on("-u", nil, "Make socket timeouts return an UNKNOWN state instead of CRITICAL") do |n|
24
+ options[:unknown_on_timeout] = true
25
+ end
26
+
27
+ opts.on("-H", :REQUIRED, "=MANDATORY", "The address of the host running the NRPE daemon") do |host|
28
+ options[:host] = host
29
+ end
30
+
31
+ opts.on("-p", "=MANDATORY", Integer, "The port on which the daemon is running (default=5666)") do |port|
32
+ options[:port] = port
33
+ end
34
+
35
+ opts.on("-t", "=MANDATORY", Integer, "Number of seconds before connection times out (default=10)") do |timeout|
36
+ options[:timeout] = timeout
37
+ end
38
+
39
+ opts.on("-c", "=MANDATORY", String, "The name of the command that the remote daemon should run") do |command|
40
+ options[:command] = command
41
+ end
42
+
43
+ opts.separator ""
44
+ opts.separator "Optional arguments that should be passed to the command."
45
+ opts.separator "Multiple arguments should be separated by a space."
46
+ opts.separator "If provided, this must be the last option supplied on the command line."
47
+ opts.separator ""
48
+ end.parse!
49
+
50
+ NRPE.session(options) do |session|
51
+ begin
52
+ result = session.execute(options[:command])
53
+ $stdout.puts(result.text)
54
+ exit result.code
55
+ rescue Errno::ETIMEDOUT, Timeout::Error => e
56
+ $stderr.puts 'Socket timed out'
57
+ exit 2 if options[:unknown_on_timeout]
58
+ exit 3
59
+ rescue Errno::ECONNREFUSED => e
60
+ $stderr.puts 'Connection refused'
61
+ exit 3
62
+ end
63
+ end
@@ -0,0 +1,17 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+
4
+ module NRPE
5
+ autoload :Packet, 'nrpe/packet'
6
+ autoload :Result, 'nrpe/result'
7
+ autoload :Session, 'nrpe/session'
8
+
9
+ def NRPE.session(options)
10
+ session = Session.new(options)
11
+ begin
12
+ yield session if block_given?
13
+ ensure
14
+ session.close
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,59 @@
1
+ require 'zlib'
2
+
3
+ module NRPE
4
+ class Packet < Struct.new(:packet_version, :packet_type, :crc32, :result_code, :buffer, :random)
5
+ NRPE_PACKET_VERSION_3 = 3
6
+ NRPE_PACKET_VERSION_2 = 2
7
+ NRPE_PACKET_VERSION_1 = 1
8
+
9
+ QUERY_PACKET = 1
10
+ RESPONSE_PACKET = 2
11
+
12
+ MAX_PACKETBUFFER_LENGTH = 1024
13
+
14
+ MAX_PACKET_SIZE = 12 + 1024
15
+
16
+ def initialize(*args)
17
+ if args.length == 1
18
+ super(*args.first.unpack("nnNnA#{MAX_PACKETBUFFER_LENGTH}n"))
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ def packet_version
25
+ super || NRPE_PACKET_VERSION_2
26
+ end
27
+
28
+ def packet_type=(type)
29
+ raise "Invalid packet type" unless [QUERY_PACKET, RESPONSE_PACKET].include?(type)
30
+ super
31
+ end
32
+
33
+ def result_code
34
+ super || 0
35
+ end
36
+
37
+ def random
38
+ super || 1
39
+ end
40
+
41
+ def to_s
42
+ [packet_version, packet_type, crc32, result_code, buffer, random].pack("nnNna#{MAX_PACKETBUFFER_LENGTH}n")
43
+ end
44
+
45
+ def crc32
46
+ crc32 = calculate_crc32 unless crc32
47
+ end
48
+
49
+ def validate_crc32
50
+ raise 'Invalid CRC32' unless crc32 == calculate_crc32
51
+ end
52
+
53
+ private
54
+
55
+ def calculate_crc32
56
+ Zlib::crc32([packet_version, packet_type, 0, result_code, buffer, random].pack("nnNna#{MAX_PACKETBUFFER_LENGTH}n"))
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,25 @@
1
+ module NRPE
2
+ class Result
3
+ CODE_OK = 0
4
+ CODE_WARNING = 1
5
+ CODE_CRITICAL = 2
6
+
7
+ attr_reader :code, :text
8
+
9
+ def initialize(code, text)
10
+ @code, @text = code, text
11
+ end
12
+
13
+ def ok?
14
+ @code == CODE_OK
15
+ end
16
+
17
+ def warning?
18
+ @code == CODE_WARNING
19
+ end
20
+
21
+ def critical?
22
+ @code == CODE_CRITICAL
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,71 @@
1
+ require 'socket'
2
+ require 'openssl'
3
+ require 'timeout'
4
+
5
+ module NRPE
6
+ class Session
7
+ def initialize(options)
8
+ process_options!(options)
9
+ end
10
+
11
+ def close
12
+ socket.close if @socket
13
+ end
14
+
15
+ def execute(check)
16
+ send_query(check)
17
+ response = read_response
18
+
19
+ Result.new(response.result_code, response.buffer)
20
+ end
21
+
22
+ private
23
+
24
+ def send_query(check)
25
+ packet = Packet.new.tap do |p|
26
+ p.packet_type = Packet::QUERY_PACKET
27
+ p.buffer = check
28
+ end
29
+ socket.write(packet.to_s)
30
+ end
31
+
32
+ def read_response
33
+ response = Packet.new(socket.read(Packet::MAX_PACKET_SIZE))
34
+ response.validate_crc32
35
+ response
36
+ end
37
+
38
+ def process_options!(options)
39
+ raise ArgumentError, "host is a required option" if options[:host].nil?
40
+ options[:port] ||= 5666
41
+ options[:timeout] ||= 10
42
+ options[:use_ssl] ||= true
43
+ @options = options
44
+ end
45
+
46
+ def socket
47
+ @socket ||= begin
48
+ optval = [Integer(@options[:timeout]), 0].pack("l_2")
49
+
50
+ socket = timeout(@options[:timeout]) do
51
+ TCPSocket.open(@options[:host], @options[:port])
52
+ end
53
+
54
+ socket.setsockopt(:SOCKET, :RCVTIMEO, optval)
55
+ socket.setsockopt(:SOCKET, :SNDTIMEO, optval)
56
+
57
+ if @options[:use_ssl]
58
+ ssl_context = OpenSSL::SSL::SSLContext.new "SSLv23_client"
59
+ ssl_context.ciphers = 'ADH'
60
+ socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
61
+ socket.sync_close = true
62
+ socket.connect
63
+ end
64
+
65
+ ObjectSpace.define_finalizer(self, proc {|id| socket.close})
66
+
67
+ socket
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module NRPE
2
+ VERSION = "0.1.0" unless defined?(::NRPE::VERSION)
3
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nrperb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bulat Shakirzyanov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-21 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: NRPErb is a nagios remote plugin executor protocol implementation in
15
+ Ruby, that lets you ruby remove nagios checks from your ruby scripts
16
+ email:
17
+ - mallluhuct@gmail.com
18
+ executables:
19
+ - check_nrpe
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - bin/check_nrpe
24
+ - lib/nrpe/packet.rb
25
+ - lib/nrpe/result.rb
26
+ - lib/nrpe/session.rb
27
+ - lib/nrpe/version.rb
28
+ - lib/nrpe.rb
29
+ - LICENSE
30
+ - README.md
31
+ homepage: http://github.com/avalanche123/nrperb
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: 1.3.6
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.10
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Execute nagios remote plugins from ruby
55
+ test_files: []