send_nsca 0.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ key_a[key_index]= send_nsca
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 kevinzen. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "send_nsca"
8
+ gem.summary = %Q{A pure ruby implementation of the send_nsca program for sending passive alerts to Nagios through NSCA.}
9
+ gem.description = %Q{A pure ruby implementation of the send_nsca program for sending passive alerts to Nagios through NSCA.}
10
+ gem.email = "kbedell@backchannelmedia.com"
11
+ gem.homepage = "http://github.com/kevinzen/send_nsca"
12
+ gem.authors = ["kevinzen"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.require_path = "lib"
15
+ gem.files = %w(History.txt install.rb MIT-LICENSE.txt README.rdoc Rakefile) + Dir["lib/**/*"] + Dir["spec/**/*"]
16
+ gem.test_files = Dir["spec/**/*"]
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+
25
+
26
+ require 'spec/rake/spectask'
27
+ Spec::Rake::SpecTask.new(:spec) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.spec_files = FileList['spec/**/*_spec.rb']
30
+ end
31
+
32
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
33
+ spec.libs << 'lib' << 'spec'
34
+ spec.pattern = 'spec/**/*_spec.rb'
35
+ spec.rcov = true
36
+ end
37
+
38
+ task :spec => :check_dependencies
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "send_nsca #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
@@ -0,0 +1,149 @@
1
+ module SendNsca
2
+
3
+ class NscaConnection
4
+
5
+ require 'socket' # Sockets are in standard library
6
+
7
+ # todo: replace timeout with a better method of handling communication timeouts.
8
+ require 'timeout'
9
+
10
+ # params for connecting to the nsca/nagios server
11
+ attr_accessor :nscahost
12
+ attr_accessor :port
13
+
14
+ # connection status and error if one found
15
+ attr_reader :connected
16
+ attr_reader :error
17
+
18
+ # read from the nsca/nagios server
19
+ attr_accessor :xor_key_and_timestamp
20
+
21
+ # converted from :xor_key_and_timestamp
22
+ attr_accessor :xor_key_and_timestamp_str
23
+ attr_accessor :xor_key_string
24
+ attr_accessor :xor_key
25
+ attr_accessor :timestring
26
+ attr_accessor :timestamp_hex
27
+ attr_accessor :timestamp
28
+
29
+ # status data to send to nagios
30
+ # Currently all 4 are required, meaning we only support 'service' passive checkins.
31
+ # todo: add host checkins
32
+ attr_accessor :hostname
33
+ attr_accessor :service
34
+ attr_accessor :return_code
35
+ attr_accessor :status
36
+
37
+ # for sending to nsca
38
+ attr_accessor :crc
39
+ PACKET_VERSION = 3
40
+ PACK_STRING = "nxx N a4 n a64 a128 a512xx"
41
+
42
+ CRC_TABLE = [ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d]
43
+
44
+ def initialize(args)
45
+
46
+ # connecting to nagios
47
+ @nscahost = args[:nscahost]
48
+ @port = args[:port]
49
+ @hostname = args[:hostname]
50
+ @service = args[:service]
51
+ @return_code = args[:return_code]
52
+ @status = args[:status]
53
+ @connected = false
54
+
55
+ end
56
+
57
+ def connect_and_get_keys
58
+ begin
59
+ timeout(1) do #the server has one second to answer
60
+ @tcp_client = TCPSocket.open(@nscahost, @port)
61
+ @connected = true
62
+ @xor_key_and_timestamp = @tcp_client.recv(132)
63
+ end
64
+ rescue
65
+ @connected = false
66
+ @error = "send_ncsa - error connecting to nsca/nagios: #{$!}"
67
+ puts @error
68
+ raise # re-raise same exception
69
+ end
70
+ timestamp_for_logging
71
+ end
72
+
73
+ def convert_timestamp
74
+ # convert timestamp for use in comm to nagios
75
+ @timestring = @xor_key_and_timestamp[@xor_key_and_timestamp.length-4,@xor_key_and_timestamp.length]
76
+ end
77
+
78
+ def timestamp_for_logging
79
+ # convert timestamp in a format we can log
80
+ @xor_key_and_timestamp_str = @xor_key_and_timestamp.unpack("H*")
81
+ @timestring_for_log = @xor_key_and_timestamp_str[0][256,8]
82
+ @timestamp_hex = @timestring_for_log.hex
83
+ @timestamp = Time.at(@timestamp_hex)
84
+ end
85
+
86
+ def convert_xor_key
87
+ # strip off the last 4 characters which are the timestamp
88
+ @xor_key = @xor_key_and_timestamp[0,@xor_key_and_timestamp.length-4]
89
+ end
90
+
91
+ def intialize_nsca_connection
92
+ connect_and_get_keys
93
+ convert_timestamp
94
+ convert_xor_key
95
+ end
96
+
97
+ def send_nsca
98
+ intialize_nsca_connection
99
+ @crc = 0
100
+ nsca_params = [PACKET_VERSION, @crc, @timestring, @return_code, @hostname, @service, @status ]
101
+ string_to_send_without_crc = nsca_params.pack(PACK_STRING)
102
+
103
+ @crc = SendNsca::NscaConnection.crc32(string_to_send_without_crc)
104
+ nsca_params = [PACKET_VERSION, @crc, @timestring, @return_code, @hostname, @service, @status ]
105
+ string_to_send_with_crc = nsca_params.pack(PACK_STRING)
106
+
107
+ # puts("DEBUG: PACKET_VERSION = #{PACKET_VERSION},\n @crc = #{@crc},\n @timestring = #{@timestring},\n @timestamp_hex = #{Time.at(timestamp_hex)},\n @return_code #{@return_code},\n @hostname = #{@hostname},\n @service = #{@service},\n @status = #{@status}\n")
108
+ # puts "string_to_send_with_crc = #{string_to_send_with_crc.length}"
109
+ # puts "string_to_send_with_crc = #{string_to_send_with_crc.unpack('H*')}"
110
+
111
+ encrypted_string_to_send = SendNsca::NscaConnection.xor(@xor_key, string_to_send_with_crc)
112
+ # puts "encrypted_string_to_send = #{encrypted_string_to_send.length}"
113
+ # puts "encrypted_string_to_send = #{encrypted_string_to_send.unpack('H*')}"
114
+
115
+ @tcp_client.send(encrypted_string_to_send, 0)
116
+ @tcp_client.close
117
+ end
118
+
119
+ def self.crc32(c)
120
+ r = 0xFFFFFFFF
121
+ c.each_byte do |b|
122
+ r ^= b
123
+ 8.times do
124
+ r = (r>>1) ^ (0xEDB88320 * (r & 1))
125
+ end
126
+ end
127
+ r ^ 0xFFFFFFFF
128
+ end
129
+
130
+ def self.xor(xor_key, str)
131
+
132
+ str_a = str.unpack("C*")
133
+ key_a = xor_key.unpack("C*")
134
+
135
+ result = []
136
+ key_index = 0
137
+ str_index = 0
138
+ str_a.each do |c|
139
+ result[str_index] = str_a[str_index] ^ key_a[key_index]
140
+ str_index += 1
141
+ key_index = key_index == xor_key.length-1 ? 0 : key_index += 1
142
+ end
143
+ ret_val = result.pack("C*")
144
+ end
145
+
146
+ end
147
+
148
+ end
149
+
data/lib/send_nsca.rb ADDED
@@ -0,0 +1,3 @@
1
+ module SendNsca
2
+ end
3
+ require File.join(File.dirname(__FILE__), 'send_nsca', 'send_nsca')
@@ -0,0 +1,64 @@
1
+ namespace :nagios do
2
+ desc <<-DESC
3
+ Sends summary emails to those users who have requested they get a summary email of their clicks periodically. By default, this is everyone.
4
+ DESC
5
+ task :send_ok => [:environment] do
6
+ # make sure the params were passed correctly
7
+ if ENV['nag_host'].nil? || ENV['monitored_host'].nil? || ENV['service'].nil? || ENV['msg'].nil?
8
+ puts "Must pass params like this: rake nagios:send_ok nag_host=hostname monitored_host=this_hostname service=\"service-name\" msg=\"here's the message for the alert\""
9
+ else
10
+ args = {
11
+ :nscahost => ENV['nag_host'],
12
+ :port => 5667,
13
+ :hostname => ENV['nag_host'],
14
+ :service => ENV['service'],
15
+ :return_code => 0,
16
+ :status => ENV['service']
17
+ }
18
+ nsca_connection = SendNsca::NscaConnection.new(args)
19
+ nsca_connection.send_nsca
20
+ end
21
+ end
22
+
23
+ desc <<-DESC
24
+ Resets all accounts that are set up to receie weekly or daily summaries so that the next time the summaries job is run they receive their summary email
25
+ DESC
26
+ task :send_warning => [:environment] do
27
+ # make sure the params were passed correctly
28
+ if ENV['nag_host'].nil? || ENV['monitored_host'].nil? || ENV['service'].nil? || ENV['msg'].nil?
29
+ puts "Must pass params like this: rake nagios:send_ok nag_host=hostname monitored_host=this_hostname service=\"service-name\" msg=\"here's the message for the alert\""
30
+ else
31
+ args = {
32
+ :nscahost => ENV['nag_host'],
33
+ :port => 5667,
34
+ :hostname => ENV['nag_host'],
35
+ :service => ENV['service'],
36
+ :return_code => 1,
37
+ :status => ENV['service']
38
+ }
39
+ nsca_connection = SendNsca::NscaConnection.new(args)
40
+ nsca_connection.send_nsca
41
+ end
42
+ end
43
+
44
+ desc <<-DESC
45
+ Resets a single account so that it will receive a summary email the next time the job is run. Pass account by email like this: rake email:reset_by_email email=account@email.com
46
+ DESC
47
+ task :send_critical => [:environment] do
48
+ # make sure the params were passed correctly
49
+ if ENV['nag_host'].nil? || ENV['monitored_host'].nil? || ENV['service'].nil? || ENV['msg'].nil?
50
+ puts "Must pass params like this: rake nagios:send_ok nag_host=hostname monitored_host=this_hostname service=\"service-name\" msg=\"here's the message for the alert\""
51
+ else
52
+ args = {
53
+ :nscahost => ENV['nag_host'],
54
+ :port => 5667,
55
+ :hostname => ENV['nag_host'],
56
+ :service => ENV['service'],
57
+ :return_code => 2,
58
+ :status => ENV['service']
59
+ }
60
+ nsca_connection = SendNsca::NscaConnection.new(args)
61
+ nsca_connection.send_nsca
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ include SendNsca
4
+
5
+ describe "SendNsca" do
6
+
7
+ before(:each) do
8
+ # do something here if needed
9
+ end
10
+
11
+ it "should create a valid crc" do
12
+
13
+ # Hello crc = 4157704578
14
+ hello = "Hello"
15
+ crc_hello = SendNsca::NscaConnection.crc32(hello)
16
+ crc_hello.should eql 4157704578
17
+
18
+ # world! crc = 2459426729
19
+ world = " world!"
20
+ hello_world_1 = SendNsca::NscaConnection.crc32(world)
21
+ hello_world_1.should eql 2459426729
22
+
23
+ # Hello World crc = 1243066710
24
+ hello_world = "Hello World"
25
+ hello_world_2 = SendNsca::NscaConnection.crc32(hello_world)
26
+ hello_world_2.should eql 1243066710
27
+ end
28
+
29
+ it "should perform valid xor encryption that can be deencrypted" do
30
+
31
+ xor_key = "\377\376\375"
32
+ str = "Hello!"
33
+ encrypted_str = SendNsca::NscaConnection.xor(xor_key,str)
34
+ deencrypted_str = SendNsca::NscaConnection.xor(xor_key,encrypted_str)
35
+ deencrypted_str.should eql str
36
+
37
+ xor_key = "adsfkahudsflihasdflkahdsfoaiudfh-3284rqiuy8rtq49087ty 2-\123\666\001\004\377"
38
+ str = "Hey There This is awesome!!!\000!"
39
+ encrypted_str = SendNsca::NscaConnection.xor(xor_key,str)
40
+ deencrypted_str = SendNsca::NscaConnection.xor(xor_key,encrypted_str)
41
+ deencrypted_str.should eql str
42
+
43
+ str = "\000\000\000\000\111\222\333\123\321"
44
+ encrypted_str = SendNsca::NscaConnection.xor(xor_key,str)
45
+ deencrypted_str = SendNsca::NscaConnection.xor(xor_key,encrypted_str)
46
+ deencrypted_str.should eql str
47
+ end
48
+
49
+ it "should correctly send a message to the server" do
50
+
51
+ args = {
52
+ :nscahost => "192.168.1.216",
53
+ :port => 5667,
54
+ :hostname => "kbedell",
55
+ :service => "passive-checkin-test01" ,
56
+ :return_code => 0,
57
+ :status => "TEST"
58
+ }
59
+ nsca_connection = SendNsca::NscaConnection.new(args)
60
+
61
+ nsca_connection.send_nsca
62
+
63
+ end
64
+
65
+ end
66
+
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'send_nsca'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: send_nsca
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 4
8
+ - 9
9
+ version: 0.4.9
10
+ platform: ruby
11
+ authors:
12
+ - kevinzen
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-19 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: A pure ruby implementation of the send_nsca program for sending passive alerts to Nagios through NSCA.
35
+ email: kbedell@backchannelmedia.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.rdoc
42
+ files:
43
+ - README.rdoc
44
+ - Rakefile
45
+ - lib/send_nsca.rb
46
+ - lib/send_nsca/send_nsca.rb
47
+ - lib/tasks/nagios.rake
48
+ - spec/send_nsca_spec.rb
49
+ - spec/spec.opts
50
+ - spec/spec_helper.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/kevinzen/send_nsca
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.6
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: A pure ruby implementation of the send_nsca program for sending passive alerts to Nagios through NSCA.
81
+ test_files:
82
+ - spec/send_nsca_spec.rb
83
+ - spec/spec.opts
84
+ - spec/spec_helper.rb