forked from sensu-plugins/sensu-plugins-graylog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add check-graylog-streams script (sensu-plugins#6)
* add check-graylog-streams script * remove ruby 1.9ism
- Loading branch information
1 parent
b337abe
commit ef087aa
Showing
4 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#!/usr/bin/env ruby | ||
# | ||
# Checks Graylog paused streams | ||
# === | ||
# | ||
# DESCRIPTION: | ||
# This plugin checks Graylog for any 'paused' streams. | ||
# | ||
# OUTPUT: | ||
# plain-text | ||
# | ||
# PLATFORMS: | ||
# all | ||
# | ||
# DEPENDENCIES: | ||
# A graylog user with streams:read permission (admin or role perm) | ||
# | ||
# LICENSE: | ||
# Seandy Wibowo <[email protected]> | ||
# nathan hruby <[email protected]> | ||
# Released under the same terms as Sensu (the MIT license); see LICENSE | ||
# for details. | ||
|
||
require 'sensu-plugin/check/cli' | ||
require 'rest-client' | ||
require 'json' | ||
|
||
class CheckGraylogStreams < Sensu::Plugin::Check::CLI | ||
option :username, | ||
short: '-u', | ||
long: '--username USERNAME', | ||
description: 'Graylog Username', | ||
required: true | ||
|
||
option :password, | ||
short: '-p', | ||
long: '--password PASSWORD', | ||
description: 'Graylog Password', | ||
required: true | ||
|
||
option :host, | ||
short: '-h', | ||
long: '--host GRAYLOG_HOST', | ||
description: 'Graylog host to query', | ||
default: 'localhost' | ||
|
||
option :port, | ||
short: '-P', | ||
long: '--port GRAYLOG_PORT', | ||
description: 'Graylog port to query', | ||
default: 12_900 | ||
|
||
option :apipath, | ||
short: '-a', | ||
long: '--apipath /api', | ||
description: 'Graylog API path prefix', | ||
default: '' | ||
|
||
def graylog_streams | ||
resource = RestClient::Resource.new( | ||
"http://#{config[:host]}:#{config[:port]}#{config[:apipath]}/streams", | ||
user: config[:username], | ||
password: config[:password], | ||
timeout: 10 | ||
) | ||
JSON.parse(resource.get, symbolize_names: true) | ||
|
||
rescue RestClient::RequestTimeout | ||
unknown 'Connection timeout' | ||
rescue SocketError | ||
unknown 'Network unavailable' | ||
rescue RestClient::Unauthorized | ||
unknown 'Missing or incorrect API credentials' | ||
rescue JSON::ParserError | ||
unknown 'API returned invalid JSON' | ||
end | ||
|
||
def run | ||
streams = graylog_streams | ||
disabled_streams = streams[:streams].select { |s| (s[:disabled] == true) } | ||
|
||
if disabled_streams.count > 0 | ||
streams_desc = [] | ||
disabled_streams.each { |v| streams_desc.push(v[:title]) } | ||
|
||
critical("Streams currently paused/disabled: #{streams_desc.join(', ')}") | ||
else | ||
ok('No streams are paused') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
require_relative './spec_helper.rb' | ||
require_relative '../bin/check-graylog-streams.rb' | ||
|
||
describe 'CheckGraylogStreams', '#run' do | ||
before(:all) do | ||
CheckGraylogStreams.class_variable_set(:@@autorun, nil) | ||
end | ||
|
||
it 'accepts config' do | ||
args = %w(--username foo --password bar) | ||
check = CheckGraylogStreams.new(args) | ||
expect(check.config[:password]).to eq 'bar' | ||
end | ||
|
||
it 'returns ok' do | ||
stub_request(:get, 'http://localhost:12900/streams') | ||
.with(basic_auth: %w(foo bar)) | ||
.to_return( | ||
status: 200, | ||
headers: { | ||
'Content-Type' => 'application/json' | ||
}, | ||
body: { | ||
'total' => 2, | ||
'streams' => [ | ||
{ | ||
'id' => '000000000000000000000001', | ||
'title' => 'All messages', | ||
'disabled' => false | ||
}, | ||
{ | ||
'id' => 'abc123def456deadbeef', | ||
'title' => 'test stream', | ||
'disabled' => false | ||
} | ||
] | ||
}.to_json | ||
) | ||
args = %w(--username foo --password bar --host localhost --port 12900) | ||
|
||
check = CheckGraylogStreams.new(args) | ||
expect(check).to receive(:ok).with('No streams are paused').and_raise(SystemExit) | ||
expect { check.run }.to raise_error(SystemExit) | ||
end | ||
|
||
it 'returns critical' do | ||
stub_request(:get, 'http://localhost:12900/streams') | ||
.with(basic_auth: %w(foo bar)) | ||
.to_return( | ||
status: 200, | ||
headers: { | ||
'Content-Type' => 'application/json' | ||
}, | ||
body: { | ||
'total' => 2, | ||
'streams' => [ | ||
{ | ||
'id' => '000000000000000000000001', | ||
'title' => 'All messages', | ||
'disabled' => false | ||
}, | ||
{ | ||
'id' => 'abc123def456deadbeef', | ||
'title' => 'test stream', | ||
'disabled' => true | ||
} | ||
] | ||
}.to_json | ||
) | ||
args = %w(--username foo --password bar --host localhost --port 12900) | ||
|
||
check = CheckGraylogStreams.new(args) | ||
expect(check).to receive(:critical).with('Streams currently paused/disabled: test stream').and_raise(SystemExit) | ||
expect { check.run }.to raise_error(SystemExit) | ||
end | ||
end |