senedsa 0.0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,31 @@
1
+ /*
2
+ * Copyright (c) 2012 by Evernote Corporation, All rights reserved.
3
+ *
4
+ * Use of the source code and binary libraries included in this package
5
+ * is permitted under the following terms:
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions
9
+ * are met:
10
+ *
11
+ * 1. Redistributions of source code must retain the above copyright
12
+ * notice, this list of conditions and the following disclaimer.
13
+ * 2. Redistributions in binary form must reproduce the above copyright
14
+ * notice, this list of conditions and the following disclaimer in the
15
+ * documentation and/or other materials provided with the distribution.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ *
28
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
29
+ * not use this file except in compliance with the License. You may obtain a
30
+ * copy of the License at http://www.apache.org/licenses/LICENSE-2.0
31
+ */
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # OVERVIEW
2
+
3
+ *Senedsa* is a small utility and library that wraps around the Nagios `send_nsca` utility, which must be available in your system. *Senedsa* assumes by default that `send_nsca` is available in your PATH and that `send_nsca`'s configuration file is in its default location. Both of these items can be overriden via options in the configuration file or in the command line.
4
+
5
+ # SYNOPSIS
6
+
7
+ senedsa [options] svc_output
8
+
9
+ Options are as follows:
10
+
11
+ Senedsa options
12
+ -C, --config CONFIG senedsa configuration file
13
+
14
+ NSCA options:
15
+ -H, --nsca_hostname HOSTNAME NSCA server hostname [REQUIRED]
16
+ -p, --nsca_port PORT NSCA server port
17
+
18
+ Send_Nsca options:
19
+ -t, --send_nsca_timeout TIMEOUT send_nsca connection timeout
20
+ -d, --send_nsca_delim DELIM send_nsca field delimited
21
+ -c, --send_nsca_config CONFIG send_nsca configuration file
22
+ -b, --send_nsca_binary BINARY send_nsca binary path
23
+
24
+ Service (svc_) options:
25
+ -h, --svc_hostname HOSTNAME service hostname [REQUIRED]
26
+ -S, --svc_descr DESCR service description [REQUIRED]
27
+ -s, --svc_status STATUS service status: ok, warning, critical, unknown [REQUIRED]
28
+
29
+ General options:
30
+ -a, --about Display senedsa information
31
+ -V, --version Display senedsa version
32
+ --help Show this message
33
+
34
+ With no options or arguments, `senedsa` displays help (as shown above). Options `-H`, `-h`, `-S` and `-s` are mandatory (unless specified in the configuration file); `svc_output` need not be quoted: anything passed as an argument is considered part of `svc_output`.
35
+
36
+ # CONFIGURATION
37
+
38
+ A YAML-based configuration (default location is `~/.senedsa/config`) can be used to set defaults for any option (except `senedsa_config`), which can then be overriden in the command line. This is useful, for instance, if the `send_nsca` binary is not in your PATH, its configuration file is not in the default location, or so that the NSCA server hostname need not be specified on the command line in every invocation. Use long option names to set the corresponding values:
39
+
40
+ ---
41
+ :send_nsca_binary: /usr/local/bin/send_nsca
42
+ :send_nsca_config: /local/etc/nagios/send_nsca.cfg
43
+ :nsca_hostname: nsca.example.com
44
+
45
+ Thus, we can now run `senedsa` like so:
46
+
47
+ senedsa -h myhost.example.com -S mypassiveservice -s ok Everthing ok with myservice
48
+
49
+ In extreme cases, where `senedsa` is being used by some external script for a specific host and service (assume `send_nsca` is in the PATH and the configuration is its standard location), the configuration file `/etc/senedsa.foo` could be:
50
+
51
+ ---
52
+ :nsca_hostname: nsca.example.com
53
+ :send_nsca_hostname: my.hostname.example.com
54
+ :send_nsca_descr: script_service
55
+
56
+ Then, the script would invoke `senedsa` as follows:
57
+
58
+ senedsa -C /etc/senedsa.foo -s ok service is doing great
59
+
60
+ # LIBRARY
61
+
62
+ Using *Senedsa* as a library:
63
+
64
+ require 'senedsa/send_nsca'
65
+
66
+ begin
67
+ @send_nsca = SendNsca.new hostname, svc_descr, :nsca_hostname => nsca.example.com
68
+ @send_nsca.send(:ok,"Everything ok with my service")
69
+ rescue => e
70
+ # rescue logic
71
+ end
72
+
73
+ If you wish to use a configuration file to set defaults:
74
+
75
+ ---
76
+ :nsca_hostname: nsca.example.com
77
+ :send_nsca_hostname: my.hostname.example.com
78
+
79
+ Then:
80
+
81
+ begin
82
+ cfg_options = SendNsca.configure(cfg_file)
83
+ @send_nsca = SendNsca.new hostname, svc_descr, cfg_options
84
+ @send_nsca.send(:ok,"Everything ok with my service")
85
+ rescue => e
86
+ # rescue logic
87
+ end
88
+
89
+ Alternatively, you can set defaults in the `SendNsca` class before creating any instances:
90
+
91
+ SendNsca.defaults[:nsca_hostname] = "nsca.example.com"
92
+ @send_nsca = SendNsca.new svc_hostname, svc_descr
93
+ @send_nsca.send :ok, "Everything ok with my service"
94
+
95
+ After a SendNsca instance is created, changing the defaults has no effect on said instance. You must then make changes to the instance itself:
96
+
97
+ @send_nsca = SendNsca.new hostname, svc_descr
98
+ @send_nsca.hostname = "nsca.example.com"
99
+
100
+ If you have multiple services in the same host:
101
+
102
+ SendNsca.nsca.hostname = "nsca.example.com" # default NSCA server
103
+ SendNsca.send_nsca.binary = "/usr/local/bin/send_nsca" # default binary location
104
+ SendNsca.send_nsca.config = "/local/etc/nagios/send_nsca.cfg" # default config location
105
+
106
+ svc1 = SendNsca.new hostname, "service 1"
107
+ svc2 = SendNsca.new hostname, "service 2"
108
+
109
+ svc1.send :warning, "Service is flaking out"
110
+ svc2.send :critical, "Service is dead"
111
+
data/bin/senedsa ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
+
6
+ require 'senedsa'
7
+
8
+ module Senedsa
9
+
10
+ app = CLI.new(ARGV)
11
+ app.run
12
+
13
+ end
data/lib/senedsa.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'senedsa/version'
2
+ require 'senedsa/about'
3
+ require 'senedsa/send_nsca'
4
+ require 'senedsa/cli'
5
+
6
+ module Senedsa
7
+ end
@@ -0,0 +1,4 @@
1
+ module Senedsa
2
+ ME = :senedsa
3
+ ABOUT = "#{ME} v#{VERSION}\nhttps://github.com/evernote/ops-#{ME}\nCopyright (c) 2012 by Evernote Corporation\nLicensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)"
4
+ end
@@ -0,0 +1,122 @@
1
+ require 'optparse'
2
+ require 'yaml'
3
+ require 'awesome_print'
4
+
5
+ module Senedsa
6
+
7
+ class CLI
8
+
9
+ ID = File.basename($PROGRAM_NAME).to_sym
10
+ CFGFILE = File.join(ENV['HOME'],"/.#{ID}/config")
11
+
12
+ attr_reader :options
13
+
14
+ def initialize(arguments)
15
+ @arguments = arguments
16
+ @cli_options = { :senedsa_config => CFGFILE, :debug => false }
17
+ @options = {}
18
+ end
19
+
20
+ def run
21
+ begin
22
+ parsed_options?
23
+ config_options?
24
+ arguments_valid?
25
+ options_valid?
26
+ rescue ArgumentError, OptionParser::MissingArgument, StandardError => e
27
+ output_message e.message, 1
28
+ end
29
+ process_options
30
+ process_arguments
31
+ process_command
32
+ end
33
+
34
+ protected
35
+ def parsed_options?
36
+ begin
37
+ opts = OptionParser.new
38
+
39
+ opts.banner = "Usage: #{ID} [options] svc_output"
40
+ opts.separator ""
41
+
42
+ opts.separator "Senedsa options"
43
+ opts.on('-C', '--config CONFIG', String, "senedsa configuration file") { |config| @cli_options[:senedsa_config] = config }
44
+ opts.separator ""
45
+
46
+ opts.separator "NSCA options:"
47
+ opts.on('-H', '--nsca_hostname HOSTNAME', String, "NSCA server hostname") { |hostname| @cli_options[:nsca_hostname] = hostname }
48
+ opts.on('-p', '--nsca_port PORT', Integer, "NSCA server port") { |port| @cli_options[:nsca_port] = port }
49
+ opts.separator ""
50
+
51
+ opts.separator "Send_Nsca options:"
52
+ opts.on('-t', '--send_nsca-timeout TIMEOUT', Integer, "send_nsca connection timeout") { |timeout| @cli_options[:send_nsca_timeout] = timeout }
53
+ opts.on('-d', '--send_nsca-delim DELIM', String, "send_nsca field delimited") { |delim| @cli_options[:send_nsca_delim] = delim }
54
+ opts.on('-c', '--send_nsca-config CONFIG', String, "send_nsca configuration file") { |config| @cli_options[:send_nsca_config] = config }
55
+ opts.on('-b', '--send_nsca-binary BINARY', String, "send_nsca binary path") { |binary| @cli_options[:send_nsca_binary] = binary }
56
+ opts.separator ""
57
+
58
+ opts.separator "Service options:"
59
+ opts.on('-h', '--hostname HOSTNAME', String, "service hostname") { |hostname| @cli_options[:svc_hostname] = hostname }
60
+ opts.on('-S', '--service SVC_DESCR', String, "service description") { |svc_descr| @cli_options[:svc_descr] = svc_descr }
61
+ opts.on('-s', '--status STATUS', SendNsca::STATUS.keys, "service status: #{SendNsca::STATUS.keys.join ', '}") { |status| @cli_options[:svc_status] = status }
62
+ opts.separator ""
63
+
64
+ opts.separator "General options:"
65
+ opts.on('-a', '--about', "Display #{ID} information") { output_message ABOUT, 0 }
66
+ opts.on('-V', '--version', "Display #{ID} version") { output_message VERSION, 0 }
67
+ opts.on_tail('--help', "Show this message") { output_message opts; exit 0 }
68
+
69
+ output_message opts, 0 if @arguments.size == 0
70
+
71
+ opts.parse!(@arguments)
72
+ rescue => e
73
+ output_message e.message, 1
74
+ end
75
+ end
76
+
77
+ def config_options?
78
+ cfg_options = SendNsca.configure(@cli_options[:senedsa_config])
79
+ raise
80
+ cfg_options.delete(:senedsa_config) unless cfg_options[:senedsa_config].nil?
81
+ @options.merge!(cfg_options)
82
+ end
83
+
84
+ def options_valid?
85
+ true
86
+ end
87
+
88
+ def arguments_valid?
89
+ raise ArgumentError, "must specify svc_output" unless @arguments.size > 0
90
+ true
91
+ end
92
+
93
+ def process_options
94
+ @options.merge!(@cli_options)
95
+ raise OptionParser::MissingArgument, "NSCA hostname (-H) must be specified" if @options[:nsca_hostname].nil?
96
+ raise OptionParser::MissingArgument, "service description (-S) must be specified" if @options[:svc_descr].nil?
97
+ raise OptionParser::MissingArgument, "service hostname (-h) must be specified" if @options[:svc_hostname].nil?
98
+ raise OptionParser::MissingArgument, "service status (-s) must be specified" if @options[:svc_status].nil?
99
+ end
100
+
101
+ def process_arguments
102
+ @arguments = @arguments.join(' ')
103
+ end
104
+
105
+ def output_message(message, exitstatus=nil)
106
+ m = (! exitstatus.nil? and exitstatus > 0) ? "%s: error: %s" % [ID, message] : message
107
+ $stderr.write "#{m}\n"
108
+ exit exitstatus unless exitstatus.nil?
109
+ end
110
+
111
+ def process_command
112
+ begin
113
+ @send_nsca = SendNsca.new @options[:svc_hostname], @options[:svc_descr], @options
114
+ @send_nsca.send(@options[:svc_status],@arguments)
115
+ rescue => e
116
+ output_message e.message, 1
117
+ end
118
+ exit 0
119
+ end
120
+ end
121
+
122
+ end
@@ -0,0 +1,84 @@
1
+ require 'open3'
2
+
3
+ module Senedsa
4
+
5
+ class SendNsca
6
+
7
+ STATUS = {
8
+ :ok => 0,
9
+ :warning => 1,
10
+ :critical => 2,
11
+ :unknown => 3
12
+ }
13
+
14
+ @defaults = {
15
+ :send_nsca_binary => 'send_nsca',
16
+ :send_nsca_config => nil,
17
+ :send_nsca_delim => '\t',
18
+ :send_nsca_timeout => 10,
19
+ :nsca_hostname => nil,
20
+ :nsca_port => 5667,
21
+ :svc_hostname => nil,
22
+ :svc_descr => nil,
23
+ :svc_status => nil
24
+ }
25
+
26
+ class << self
27
+
28
+ attr_accessor :defaults end
29
+
30
+ def self.configure(cfg_file)
31
+ begin
32
+ cfg_options = YAML.load File.open(cfg_file)
33
+ raise ConfigurationError, "senedsa_config not allowed in configuration file (#{cfg_file})" unless cfg_options[:senedsa_config].nil?
34
+ rescue Psych::SyntaxError => e
35
+ raise StandardError, "syntax error in configuration file #{cfg_file}: #{e.message}"
36
+ rescue Errno::ENOENT, Errno::EACCES => e
37
+ raise StandardError, e.message
38
+ end
39
+ cfg_options
40
+ end
41
+
42
+ attr_accessor :send_nsca, :nsca
43
+
44
+ class Error < StandardError; end
45
+ class SendNscaError < Error; end
46
+ class ConfigurationError < SendNscaError; end
47
+
48
+ def initialize(svc_hostname,svc_descr,options)
49
+ @options = options.nil? ? SendNsca.defaults : SendNsca.defaults.merge(options)
50
+ @options[:svc_hostname] = svc_hostname
51
+ @options[:svc_descr] = svc_descr
52
+ end
53
+
54
+ SendNsca.defaults.keys.each do |attr|
55
+ define_method(attr.to_s) { @options[attr.to_sym] }
56
+ define_method(attr.to_s + '=') { |value| @options[attr.to_sym] = value }
57
+ end
58
+
59
+ def send(status,svc_output)
60
+ run(status,svc_output)
61
+ end
62
+
63
+ private
64
+
65
+ def command
66
+ c = "#{send_nsca_binary} -H #{nsca_hostname} -p #{nsca_port} -t #{send_nsca_timeout} -d '#{send_nsca_delim}'"
67
+ c << " -c #{send_nsca_config}" unless send_nsca_config.nil?
68
+ c
69
+ end
70
+
71
+ def run(status,svc_output)
72
+ begin
73
+ Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
74
+ stdin.write("%s\n" % [svc_hostname,svc_descr,STATUS[status],svc_output].join(send_nsca_delim))
75
+ $stdout.write stdout.gets
76
+ raise SendNscaError, stderr.gets.chomp unless wait_thr.value.exitstatus == 0
77
+ end
78
+ rescue Errno::ENOENT, Errno::EACCES => e
79
+ raise SendNscaError, e.message
80
+ end
81
+ end
82
+ end
83
+ end
84
+
@@ -0,0 +1,3 @@
1
+ module Senedsa
2
+ VERSION = '0.0.4.0'
3
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: senedsa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gerardo López-Fernádez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Senedsa is a small utility and library wrapper for the Nagios send_nsca.
15
+ email: gerir@evernote.com
16
+ executables:
17
+ - senedsa
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/senedsa/about.rb
22
+ - lib/senedsa/cli.rb
23
+ - lib/senedsa/send_nsca.rb
24
+ - lib/senedsa/version.rb
25
+ - lib/senedsa.rb
26
+ - bin/senedsa
27
+ - LICENSE
28
+ - README.md
29
+ homepage: https://github.com/evernote/ops-senedsa
30
+ licenses:
31
+ - Apache License, Version 2.0
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.3.5
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.23
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Utility and library wrapper for Nagios send_nsca utility
54
+ test_files: []
55
+ has_rdoc: