senedsa 0.0.5 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -11,33 +11,40 @@
11
11
  Options are as follows:
12
12
 
13
13
  Senedsa options
14
- -C, --config CONFIG senedsa configuration file
14
+ -c, --config CONFIG senedsa configuration file
15
15
 
16
16
  NSCA options:
17
17
  -H, --nsca_hostname HOSTNAME NSCA server hostname [REQUIRED]
18
- -p, --nsca_port PORT NSCA server port
18
+ -P, --nsca_port PORT NSCA server port
19
19
 
20
20
  Send_Nsca options:
21
- -t, --send_nsca_timeout TIMEOUT send_nsca connection timeout
22
- -d, --send_nsca_delim DELIM send_nsca field delimited
23
- -c, --send_nsca_config CONFIG send_nsca configuration file
24
- -b, --send_nsca_binary BINARY send_nsca binary path
21
+ -T, --send_nsca_timeout TIMEOUT send_nsca connection timeout
22
+ -D, --send_nsca_delim DELIM send_nsca field delimited
23
+ -C, --send_nsca_config CONFIG send_nsca configuration file
24
+ -B, --send_nsca_binary BINARY send_nsca binary path
25
25
 
26
- Service (svc_) options:
26
+ Service options:
27
27
  -h, --svc_hostname HOSTNAME service hostname [REQUIRED]
28
28
  -S, --svc_descr DESCR service description [REQUIRED]
29
29
  -s, --svc_status STATUS service status: ok, warning, critical, unknown [REQUIRED]
30
30
 
31
31
  General options:
32
+ -d, --debug Enable debug mode
32
33
  -a, --about Display senedsa information
33
34
  -V, --version Display senedsa version
34
35
  --help Show this message
35
36
 
36
- 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`.
37
+ With no options or arguments, `senedsa` displays help (as shown above).
38
+
39
+ Options `--nsca_hostname`, `--svc_hostname`, `--svc_descr` and `--svc_status` are mandatory (unless specified in the configuration file).
40
+
41
+ Finally, `svc_output` need not be quoted: anything passed as an argument is considered part of `svc_output`.
37
42
 
38
43
  # CONFIGURATION
39
44
 
40
- 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:
45
+ The priority of options is: command line options `>` configuration file options `>` default options
46
+
47
+ 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 the 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:
41
48
 
42
49
  ---
43
50
  :send_nsca_binary: /usr/local/bin/send_nsca
@@ -48,7 +55,7 @@ Thus, we can now run `senedsa` like so:
48
55
 
49
56
  senedsa -h myhost.example.com -S mypassiveservice -s ok Everthing ok with myservice
50
57
 
51
- 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:
58
+ In cases where `senedsa` is being used by some external script for a specific host and service (assuming `send_nsca` is in the PATH and the configuration is its standard location), the configuration file `/etc/senedsa/script_service` could be:
52
59
 
53
60
  ---
54
61
  :nsca_hostname: nsca.example.com
@@ -57,22 +64,68 @@ In extreme cases, where `senedsa` is being used by some external script for a sp
57
64
 
58
65
  Then, the script would invoke `senedsa` as follows:
59
66
 
60
- senedsa -C /etc/senedsa.foo -s ok service is doing great
67
+ senedsa -c /etc/senedsa/script_service -s ok service is doing great
61
68
 
62
69
  # LIBRARY
63
70
 
64
- Using *Senedsa* as a library:
71
+ To use *Senedsa* as a library, simply:
65
72
 
66
- require 'senedsa/send_nsca'
73
+ require 'senedsa'
74
+
75
+ svc_hostname = "foo.example.com"
76
+ svc_descr = "sample service description"
67
77
 
68
78
  begin
69
- @send_nsca = SendNsca.new hostname, svc_descr, :nsca_hostname => nsca.example.com
70
- @send_nsca.send(:ok,"Everything ok with my service")
79
+ s = SendNsca.new svc_hostname, svc_descr, :nsca_hostname => "nsca.example.com"
80
+ s.send :ok, "Everything ok with my service"
71
81
  rescue => e
72
82
  # rescue logic
73
83
  end
74
84
 
75
- If you wish to use a configuration file to set defaults:
85
+ ## CONSTRUCTORS
86
+
87
+ *Senedsa* accepts four different constructors, aimed at fitting different situations.
88
+
89
+ * `(SendNsca) initialize(config_file)`
90
+ * `(SendNsca) initialize(config_hash)`
91
+ * `(SendNsca) initialize(svc_hostname,svc_descr)`
92
+ * `(SendNsca) initialize(svc_hostname,svc_descr,config_hash)`
93
+
94
+ Where:
95
+
96
+ * `config_file` is a path to a valid configuration file
97
+ * `config_hash` is a hash keyed by long option names with their corresponding values
98
+ * `svc_hostname` is the hostname of the service hostname
99
+ * `svc_descr` is the service description of the service
100
+
101
+ An instance does not need all options defined until the `send` method is invoked.
102
+
103
+ ## DEFAULTS
104
+
105
+ `Senedsa` has sensible defaults for the following options, mostly following `send_nsca`'s documented defaults:
106
+
107
+ * `nsca_port = 5667`
108
+ * `send_nsca_timeout = 10`
109
+ * `send_nsca_delim = '\t'`
110
+ * `send_nsca_binary = 'send_nsca'`
111
+
112
+ It is therefore not necessary to set these if your environment doesn't need them changed.
113
+
114
+ ## SETTERS, GETTERS and CONFIGURATION FILES
115
+
116
+ All options are settable (and gettable) through attribute methods. For instance:
117
+
118
+ sn = SendNsca.new "foo.example.com", "web_service"
119
+ sn.nsca_hostname = "nsca.example.com"
120
+ sn.send :ok, "Service ok"
121
+
122
+ Another example:
123
+
124
+ sn = SendNsca.new "foo.example.com", "web_service", :nsca_hostname => "nsca.example.com"
125
+ sn.nsca_port = 55667
126
+ sn.send :ok, Service ok"
127
+
128
+ If you wish to use a configuration file to set some defaults:
76
129
 
77
130
  ---
78
131
  :nsca_hostname: nsca.example.com
@@ -80,34 +133,15 @@ If you wish to use a configuration file to set defaults:
80
133
 
81
134
  Then:
82
135
 
83
- begin
84
- cfg_options = SendNsca.configure(cfg_file)
85
- @send_nsca = SendNsca.new hostname, svc_descr, cfg_options
86
- @send_nsca.send(:ok,"Everything ok with my service")
87
- rescue => e
88
- # rescue logic
89
- end
136
+ config_file = '/etc/senedsa.cfg'
137
+ s = SendNsca.new config_file
138
+ s.svc_descr = "web_service"
139
+ s = s.send :ok, "Everything ok with web_service"
90
140
 
91
- Alternatively, you can set defaults in the `SendNsca` class before creating any instances:
141
+ Alternatively you can set defaults in the `SendNsca` class before creating any instances:
92
142
 
93
143
  SendNsca.defaults[:nsca_hostname] = "nsca.example.com"
94
- @send_nsca = SendNsca.new svc_hostname, svc_descr
95
- @send_nsca.send :ok, "Everything ok with my service"
96
-
97
- After a SendNsca instance is created, changing the defaults has no effect on said instance. You must then make changes to the instance itself:
98
-
99
- @send_nsca = SendNsca.new hostname, svc_descr
100
- @send_nsca.hostname = "nsca.example.com"
101
-
102
- If you have multiple services in the same host:
144
+ s = SendNsca.new svc_hostname, svc_descr
145
+ s.send :ok, "Everything ok with my service"
103
146
 
104
- SendNsca.nsca.hostname = "nsca.example.com" # default NSCA server
105
- SendNsca.send_nsca.binary = "/usr/local/bin/send_nsca" # default binary location
106
- SendNsca.send_nsca.config = "/local/etc/nagios/send_nsca.cfg" # default config location
107
-
108
- svc1 = SendNsca.new hostname, "service 1"
109
- svc2 = SendNsca.new hostname, "service 2"
110
-
111
- svc1.send :warning, "Service is flaking out"
112
- svc2.send :critical, "Service is dead"
113
-
147
+ After a SendNsca instance is created, changing the defaults has no effect on said instance. You must then make changes to the instance itself.
data/bin/senedsa CHANGED
@@ -4,6 +4,7 @@ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
4
  $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
5
 
6
6
  require 'senedsa'
7
+ require 'senedsa/cli'
7
8
 
8
9
  module Senedsa
9
10
 
data/lib/senedsa/cli.rb CHANGED
@@ -6,13 +6,13 @@ module Senedsa
6
6
  class CLI
7
7
 
8
8
  ID = File.basename($PROGRAM_NAME).to_sym
9
- CFGFILE = File.join(ENV['HOME'],"/.#{ID}/config")
9
+ DEFAULT_CONFIG_FILE = File.join(ENV['HOME'],"/.#{ID}/config")
10
10
 
11
11
  attr_reader :options
12
12
 
13
13
  def initialize(arguments)
14
14
  @arguments = arguments
15
- @cli_options = { :senedsa_config => CFGFILE, :debug => false }
15
+ @cli_options = { :debug => false }
16
16
  @options = {}
17
17
  end
18
18
 
@@ -22,12 +22,12 @@ module Senedsa
22
22
  config_options?
23
23
  arguments_valid?
24
24
  options_valid?
25
- rescue ArgumentError, OptionParser::MissingArgument, StandardError => e
25
+ process_options
26
+ process_arguments
27
+ process_command
28
+ rescue ArgumentError, OptionParser::MissingArgument, SendNsca::ConfigurationError => e
26
29
  output_message e.message, 1
27
30
  end
28
- process_options
29
- process_arguments
30
- process_command
31
31
  end
32
32
 
33
33
  protected
@@ -39,19 +39,19 @@ module Senedsa
39
39
  opts.separator ""
40
40
 
41
41
  opts.separator "Senedsa options"
42
- opts.on('-C', '--config CONFIG', String, "senedsa configuration file") { |config| @cli_options[:senedsa_config] = config }
42
+ opts.on('-c', '--config CONFIG', String, "senedsa configuration file") { |config| @cli_options[:senedsa_config] = config }
43
43
  opts.separator ""
44
44
 
45
45
  opts.separator "NSCA options:"
46
46
  opts.on('-H', '--nsca_hostname HOSTNAME', String, "NSCA server hostname") { |hostname| @cli_options[:nsca_hostname] = hostname }
47
- opts.on('-p', '--nsca_port PORT', Integer, "NSCA server port") { |port| @cli_options[:nsca_port] = port }
47
+ opts.on('-P', '--nsca_port PORT', Integer, "NSCA server port") { |port| @cli_options[:nsca_port] = port }
48
48
  opts.separator ""
49
49
 
50
50
  opts.separator "Send_Nsca options:"
51
- opts.on('-t', '--send_nsca-timeout TIMEOUT', Integer, "send_nsca connection timeout") { |timeout| @cli_options[:send_nsca_timeout] = timeout }
52
- opts.on('-d', '--send_nsca-delim DELIM', String, "send_nsca field delimited") { |delim| @cli_options[:send_nsca_delim] = delim }
53
- opts.on('-c', '--send_nsca-config CONFIG', String, "send_nsca configuration file") { |config| @cli_options[:send_nsca_config] = config }
54
- opts.on('-b', '--send_nsca-binary BINARY', String, "send_nsca binary path") { |binary| @cli_options[:send_nsca_binary] = binary }
51
+ opts.on('-T', '--send_nsca-timeout TIMEOUT', Integer, "send_nsca connection timeout") { |timeout| @cli_options[:send_nsca_timeout] = timeout }
52
+ opts.on('-D', '--send_nsca-delim DELIM', String, "send_nsca field delimited") { |delim| @cli_options[:send_nsca_delim] = delim }
53
+ opts.on('-C', '--send_nsca-config CONFIG', String, "send_nsca configuration file") { |config| @cli_options[:send_nsca_config] = config }
54
+ opts.on('-B', '--send_nsca-binary BINARY', String, "send_nsca binary path") { |binary| @cli_options[:send_nsca_binary] = binary }
55
55
  opts.separator ""
56
56
 
57
57
  opts.separator "Service options:"
@@ -61,9 +61,10 @@ module Senedsa
61
61
  opts.separator ""
62
62
 
63
63
  opts.separator "General options:"
64
+ opts.on('-d', '--debug', "Enable debug mode") { @cli_options[:debug] = true}
64
65
  opts.on('-a', '--about', "Display #{ID} information") { output_message ABOUT, 0 }
65
66
  opts.on('-V', '--version', "Display #{ID} version") { output_message VERSION, 0 }
66
- opts.on_tail('--help', "Show this message") { output_message opts; exit 0 }
67
+ opts.on_tail('--help', "Show this message") { output_message opts 0 }
67
68
 
68
69
  output_message opts, 0 if @arguments.size == 0
69
70
 
@@ -74,10 +75,13 @@ module Senedsa
74
75
  end
75
76
 
76
77
  def config_options?
77
- cfg_options = SendNsca.configure(@cli_options[:senedsa_config])
78
- raise
79
- cfg_options.delete(:senedsa_config) unless cfg_options[:senedsa_config].nil?
80
- @options.merge!(cfg_options)
78
+ cfg_file = @cli_options[:senedsa_config] unless @cli_options[:senedsa_config].nil?
79
+ cfg_file = DEFAULT_CONFIG_FILE if @cli_options[:senedsa_config].nil? and File.readable? DEFAULT_CONFIG_FILE
80
+
81
+ unless cfg_file.nil?
82
+ @options.merge!(SendNsca.configure(cfg_file))
83
+ @options[:senedsa_config] = cfg_file
84
+ end
81
85
  end
82
86
 
83
87
  def options_valid?
@@ -98,7 +102,7 @@ module Senedsa
98
102
  end
99
103
 
100
104
  def process_arguments
101
- @arguments = @arguments.join(' ')
105
+ @cli_options[:svc_output] = @arguments.join(' ')
102
106
  end
103
107
 
104
108
  def output_message(message, exitstatus=nil)
@@ -109,10 +113,13 @@ module Senedsa
109
113
 
110
114
  def process_command
111
115
  begin
112
- @send_nsca = SendNsca.new @options[:svc_hostname], @options[:svc_descr], @options
113
- @send_nsca.send(@options[:svc_status],@arguments)
116
+ SendNsca.new(@options).send
114
117
  rescue => e
115
- output_message e.message, 1
118
+ if @options[:debug]
119
+ output_message "%s\n%s" % [e.message,e.backtrace.join("\n ")], 1
120
+ else
121
+ output_message e.message,1
122
+ end
116
123
  end
117
124
  exit 0
118
125
  end
@@ -28,13 +28,17 @@ module Senedsa
28
28
  attr_accessor :defaults end
29
29
 
30
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
31
+ cfg_options = {}
32
+ unless cfg_file.nil?
33
+ raise ConfigurationError, "unable to read configuration file #{cfg_file}" unless File.readable? cfg_file
34
+ begin
35
+ cfg_options = YAML.load File.open(cfg_file)
36
+ raise ConfigurationError, "senedsa_config not allowed in configuration file (#{cfg_file})" unless cfg_options[:senedsa_config].nil?
37
+ rescue Psych::SyntaxError => e
38
+ raise ConfigurationError, "syntax error in configuration file #{cfg_file}: #{e.message}"
39
+ rescue Errno::ENOENT, Errno::EACCES => e
40
+ raise ConfigurationError, e.message
41
+ end
38
42
  end
39
43
  cfg_options
40
44
  end
@@ -44,11 +48,41 @@ module Senedsa
44
48
  class Error < StandardError; end
45
49
  class SendNscaError < Error; end
46
50
  class ConfigurationError < SendNscaError; end
51
+ class InitializationError < SendNscaError; end
52
+
53
+ def initialize(*args)
54
+
55
+ case args.size
56
+
57
+ when 1
58
+ if args[0].is_a? String
59
+ cfg_options = SendNsca.configure(args[0])
60
+ hsh_options = {}
61
+ elsif args[0].is_a? Hash
62
+ cfg_options = SendNsca.configure(args[0][:senedsa_config])
63
+ hsh_options = args[0]
64
+ else
65
+ raise InitializationError, "invalid argument types"
66
+ end
47
67
 
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
68
+ when 2
69
+ raise InitializationError, "invalid argument types" unless args[0].is_a? String and args[1].is_a? String
70
+ cfg_options = SendNsca.configure(args[0][:senedsa_config])
71
+ hsh_options = { :svc_hostname => args[0], :svc_descr => args[1] }
72
+
73
+ when 3
74
+ raise InitializationError, "invalid argument types" unless args[0].is_a? String and args[1].is_a? String and args[2].is_a? Hash
75
+ cfg_options = SendNsca.configure(args[0][:senedsa_config])
76
+ hsh_options = args[2].merge({ :svc_hostname => args[0], :svc_descr => args[1] })
77
+
78
+ else
79
+ raise InitializationError, "invalid number of arguments"
80
+ end
81
+ @options = SendNsca.defaults.merge(cfg_options).merge(hsh_options)
82
+ end
83
+
84
+ def send(*args)
85
+ run(@options[:status],@options[:svc_output])
52
86
  end
53
87
 
54
88
  SendNsca.defaults.keys.each do |attr|
@@ -56,10 +90,6 @@ module Senedsa
56
90
  define_method(attr.to_s + '=') { |value| @options[attr.to_sym] = value }
57
91
  end
58
92
 
59
- def send(status,svc_output)
60
- run(status,svc_output)
61
- end
62
-
63
93
  private
64
94
 
65
95
  def command
@@ -1,3 +1,3 @@
1
1
  module Senedsa
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.7'
3
3
  end
data/lib/senedsa.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'senedsa/version'
2
2
  require 'senedsa/about'
3
3
  require 'senedsa/send_nsca'
4
- require 'senedsa/cli'
5
4
 
6
5
  module Senedsa
7
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: senedsa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-29 00:00:00.000000000 Z
12
+ date: 2012-05-30 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Senedsa is a small utility and library wrapper for the Nagios send_nsca.
15
15
  email: gerir@evernote.com