nsca-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/README.md +8 -0
- data/Rakefile +2 -0
- data/lib/nsca/client.rb +46 -0
- data/lib/nsca/client/host.rb +17 -0
- data/lib/nsca/client/message.rb +27 -0
- data/lib/nsca/client/notifier.rb +17 -0
- data/lib/nsca/client/remote_server.rb +12 -0
- data/lib/nsca/client/service.rb +12 -0
- data/lib/nsca/client/version.rb +5 -0
- data/nsca-client.gemspec +22 -0
- data/spec/nsca/client/host_spec.rb +12 -0
- data/spec/nsca/client/message_spec.rb +31 -0
- data/spec/nsca/client/notifier_spec.rb +20 -0
- data/spec/nsca/client/remote_server_spec.rb +12 -0
- data/spec/nsca/client/service_spec.rb +16 -0
- data/spec/nsca/client_spec.rb +31 -0
- data/spec/spec_helper.rb +9 -0
- metadata +104 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
NSCA Client
|
2
|
+
===========
|
3
|
+
|
4
|
+
NSCA is a Linux/Unix daemon allows you to integrate passive alerts and checks
|
5
|
+
from remote machines and applications with Nagios. Useful for processing
|
6
|
+
security alerts, as well as redundant and distributed Nagios setups.
|
7
|
+
|
8
|
+
**This client allows you to send easily passive alerts and checks.**
|
data/Rakefile
ADDED
data/lib/nsca/client.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'nsca/client/host'
|
2
|
+
require 'nsca/client/message'
|
3
|
+
require 'nsca/client/notifier'
|
4
|
+
require 'nsca/client/remote_server'
|
5
|
+
require 'nsca/client/service'
|
6
|
+
require 'nsca/client/version'
|
7
|
+
|
8
|
+
module NSCA
|
9
|
+
module Client
|
10
|
+
class << self
|
11
|
+
def servers
|
12
|
+
@servers ||= []
|
13
|
+
end
|
14
|
+
|
15
|
+
def ok(service, message = 'OK')
|
16
|
+
status = false
|
17
|
+
servers.each do |server|
|
18
|
+
message = Message.new(:ok, message, service, server)
|
19
|
+
notifier = Notifier.new(message)
|
20
|
+
status = true if notifier.send_nsca
|
21
|
+
end
|
22
|
+
status
|
23
|
+
end
|
24
|
+
|
25
|
+
def warning(service, message = 'WARNING')
|
26
|
+
status = false
|
27
|
+
servers.each do |server|
|
28
|
+
message = Message.new(:warn, message, service, server)
|
29
|
+
notifier = Notifier.new(message)
|
30
|
+
status = true if notifier.send_nsca
|
31
|
+
end
|
32
|
+
status
|
33
|
+
end
|
34
|
+
|
35
|
+
def critical(service, message = 'CRITICAL')
|
36
|
+
status = false
|
37
|
+
servers.each do |server|
|
38
|
+
message = Message.new(:critical, message, service, server)
|
39
|
+
notifier = Notifier.new(message)
|
40
|
+
status = true if notifier.send_nsca
|
41
|
+
end
|
42
|
+
status
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'socket'
|
2
|
+
|
3
|
+
module NSCA
|
4
|
+
module Client
|
5
|
+
class Host
|
6
|
+
attr_reader :hostname
|
7
|
+
|
8
|
+
def initialize(options)
|
9
|
+
@hostname = options[:hostname]
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.current
|
13
|
+
Host.new(:hostname => Socket.gethostname)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module NSCA
|
2
|
+
module Client
|
3
|
+
class Message
|
4
|
+
CODE = { :ok => 0, :warn => 1, :critical => 2 }
|
5
|
+
|
6
|
+
attr_reader :return_code
|
7
|
+
|
8
|
+
def initialize(alert, status, service, remote_server)
|
9
|
+
@return_code = CODE[alert]
|
10
|
+
@status = status
|
11
|
+
@service = service
|
12
|
+
@remote_server = remote_server
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_h
|
16
|
+
{
|
17
|
+
:nscahost => @remote_server.host,
|
18
|
+
:port => @remote_server.port,
|
19
|
+
:hostname => @service.host.hostname,
|
20
|
+
:service => @service.name,
|
21
|
+
:return_code => @return_code,
|
22
|
+
:status => @status
|
23
|
+
}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'send_nsca'
|
2
|
+
|
3
|
+
module NSCA
|
4
|
+
module Client
|
5
|
+
class Notifier
|
6
|
+
def initialize(message)
|
7
|
+
@message = message
|
8
|
+
end
|
9
|
+
|
10
|
+
def send_nsca
|
11
|
+
connection = SendNsca::NscaConnection.new(@message.to_h)
|
12
|
+
connection.send_nsca
|
13
|
+
true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/nsca-client.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "nsca/client/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.authors = `git log --raw | grep Author: | awk -F ': | <|>' '{ print $2 }' | sort | uniq`.split("\n")
|
7
|
+
gem.email = `git log --raw | grep Author: | awk -F ': | <|>' '{ print $3 }' | sort | uniq`.split("\n")
|
8
|
+
gem.description = %q{Send passive notifications to Nagios Service Check Acceptor (NSCA).}
|
9
|
+
gem.summary = %q{NSCA Client}
|
10
|
+
gem.homepage = "https://github.com/felipecvo/nsca-client"
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^spec/})
|
15
|
+
gem.name = "nsca-client"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = NSCA::Client::VERSION
|
18
|
+
|
19
|
+
gem.add_dependency "send_nsca"
|
20
|
+
|
21
|
+
gem.add_development_dependency "rspec"
|
22
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NSCA::Client::Host do
|
4
|
+
subject { described_class.new(:hostname => 'dummy') }
|
5
|
+
its(:hostname) { should eq 'dummy' }
|
6
|
+
|
7
|
+
context "current machine" do
|
8
|
+
before { Socket.stub(:gethostname).and_return('dummy-client-machine') }
|
9
|
+
subject { described_class.current }
|
10
|
+
its(:hostname) { should eq 'dummy-client-machine' }
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NSCA::Client::Message do
|
4
|
+
let(:server) { NSCA::Client::RemoteServer.new(:host => '172.0.0.1') }
|
5
|
+
let(:host) { NSCA::Client::Host.new(:hostname => 'dummy') }
|
6
|
+
let(:service) { NSCA::Client::Service.new(:name => 'TestMessage', :host => host) }
|
7
|
+
|
8
|
+
subject { described_class.new(:ok, 'OK', service, server).to_h }
|
9
|
+
|
10
|
+
its([:nscahost]) { should eq '172.0.0.1' }
|
11
|
+
its([:port]) { should eq 5667 }
|
12
|
+
its([:hostname]) { should eq 'dummy' }
|
13
|
+
its([:service]) { should eq 'TestMessage' }
|
14
|
+
its([:return_code]) { should eq 0 }
|
15
|
+
its([:status]) { should eq 'OK' }
|
16
|
+
|
17
|
+
context "ok message" do
|
18
|
+
subject { described_class.new(:ok, 'OK', service, server) }
|
19
|
+
its(:return_code) { should eq 0 }
|
20
|
+
end
|
21
|
+
|
22
|
+
context "warn message" do
|
23
|
+
subject { described_class.new(:warn, 'WARN', service, server) }
|
24
|
+
its(:return_code) { should eq 1 }
|
25
|
+
end
|
26
|
+
|
27
|
+
context "critical message" do
|
28
|
+
subject { described_class.new(:critical, 'CRITICAL', service, server) }
|
29
|
+
its(:return_code) { should eq 2 }
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NSCA::Client::Notifier do
|
4
|
+
let(:server) { NSCA::Client::RemoteServer.new(:host => '172.0.0.1') }
|
5
|
+
let(:host) { NSCA::Client::Host.new(:hostname => 'dummy') }
|
6
|
+
let(:service) { NSCA::Client::Service.new(:name => 'TestMessage', :host => host) }
|
7
|
+
let(:message) { NSCA::Client::Message.new(:ok, 'OK', service, server) }
|
8
|
+
|
9
|
+
context "send message successfully" do
|
10
|
+
before do
|
11
|
+
SendNsca::NscaConnection.any_instance.should_receive(:send_nsca)
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:notifier) { NSCA::Client::Notifier.new(message) }
|
15
|
+
|
16
|
+
subject { notifier.send_nsca }
|
17
|
+
|
18
|
+
it { should be_true }
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NSCA::Client::RemoteServer do
|
4
|
+
subject { described_class.new(:host => '172.0.0.1') }
|
5
|
+
its(:host) { should eq '172.0.0.1' }
|
6
|
+
its(:port) { should eq 5667 }
|
7
|
+
|
8
|
+
context "custom port" do
|
9
|
+
subject { described_class.new(:port => 12345) }
|
10
|
+
its(:port) { should eq 12345 }
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NSCA::Client::Service do
|
4
|
+
let(:host) { NSCA::Client::Host.new(:hostname => 'asdf') }
|
5
|
+
subject { described_class.new(:name => 'TestMessage', :host => host) }
|
6
|
+
|
7
|
+
its(:name) { should eq 'TestMessage' }
|
8
|
+
its(:host) { should eq host }
|
9
|
+
|
10
|
+
context "default host to current machine" do
|
11
|
+
subject { described_class.new(:name => 'SimpleMessage') }
|
12
|
+
|
13
|
+
its(:name) { should eq 'SimpleMessage' }
|
14
|
+
its(:host) { should be_a NSCA::Client::Host }
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NSCA::Client do
|
4
|
+
let(:service) { NSCA::Client::Service.new(:name => 'TestMessage') }
|
5
|
+
|
6
|
+
before(:all) do
|
7
|
+
NSCA::Client.servers << NSCA::Client::RemoteServer.new(:host => '172.0.0.1')
|
8
|
+
end
|
9
|
+
|
10
|
+
before(:each) do
|
11
|
+
SendNsca::NscaConnection.any_instance.should_receive(:send_nsca)
|
12
|
+
end
|
13
|
+
|
14
|
+
context "normal service operation" do
|
15
|
+
it "should notify" do
|
16
|
+
NSCA::Client.ok(service).should be_true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "unstable service operation" do
|
21
|
+
it "should notify" do
|
22
|
+
NSCA::Client.warning(service).should be_true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "critical service state" do
|
27
|
+
it "should notify" do
|
28
|
+
NSCA::Client.critical(service).should be_true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nsca-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Felipe Oliveira
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: send_nsca
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Send passive notifications to Nagios Service Check Acceptor (NSCA).
|
47
|
+
email:
|
48
|
+
- felipe.oliveira@abril.com.br
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .rspec
|
55
|
+
- Gemfile
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/nsca/client.rb
|
59
|
+
- lib/nsca/client/host.rb
|
60
|
+
- lib/nsca/client/message.rb
|
61
|
+
- lib/nsca/client/notifier.rb
|
62
|
+
- lib/nsca/client/remote_server.rb
|
63
|
+
- lib/nsca/client/service.rb
|
64
|
+
- lib/nsca/client/version.rb
|
65
|
+
- nsca-client.gemspec
|
66
|
+
- spec/nsca/client/host_spec.rb
|
67
|
+
- spec/nsca/client/message_spec.rb
|
68
|
+
- spec/nsca/client/notifier_spec.rb
|
69
|
+
- spec/nsca/client/remote_server_spec.rb
|
70
|
+
- spec/nsca/client/service_spec.rb
|
71
|
+
- spec/nsca/client_spec.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
homepage: https://github.com/felipecvo/nsca-client
|
74
|
+
licenses: []
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.8.23
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: NSCA Client
|
97
|
+
test_files:
|
98
|
+
- spec/nsca/client/host_spec.rb
|
99
|
+
- spec/nsca/client/message_spec.rb
|
100
|
+
- spec/nsca/client/notifier_spec.rb
|
101
|
+
- spec/nsca/client/remote_server_spec.rb
|
102
|
+
- spec/nsca/client/service_spec.rb
|
103
|
+
- spec/nsca/client_spec.rb
|
104
|
+
- spec/spec_helper.rb
|