Skip to content

Commit 1c6c2a4

Browse files
Yuri Zubovmajormoses
authored andcommitted
Added ipcs
1 parent 4bfa53a commit 1c6c2a4

7 files changed

Lines changed: 131 additions & 4 deletions

File tree

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ RegexpLiteral:
2929
Style/Documentation:
3030
Enabled: false
3131

32+
Style/FlipFlop:
33+
Enabled: false
34+
3235
# testing
3336
Metrics/BlockLength:
3437
Enabled: true

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins
55

66

77
## [Unreleased]
8+
### Added
9+
- metrics-ipcs.rb: Add ipcs metrics (@yuri-zubov sponsored by Actility, https://www.actility.com)
10+
811

912
## [3.0.2] - 2018-03-27
1013
### Security

Rakefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ require 'yard'
77
require 'yard/rake/yardoc_task'
88
require 'English'
99

10-
args = %i[spec make_bin_executable yard rubocop check_binstubs]
11-
1210
YARD::Rake::YardocTask.new do |t|
1311
OTHER_PATHS = %w[].freeze
1412
t.files = ['lib/**/*.rb', 'bin/**/*.rb', OTHER_PATHS]
@@ -38,4 +36,4 @@ task :check_binstubs do
3836
end
3937
end
4038

41-
task default: args
39+
task default: %i[spec make_bin_executable yard rubocop check_binstubs]

bin/metrics-ipcs.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#! /usr/bin/env ruby
2+
#
3+
# metrics-ipcs
4+
#
5+
# DESCRIPTION:
6+
# metrics-ipcs get metrics from ipcs
7+
#
8+
# OUTPUT:
9+
# metric-data
10+
#
11+
# PLATFORMS:
12+
# Linux
13+
#
14+
# DEPENDENCIES:
15+
#
16+
# USAGE:
17+
#
18+
#
19+
# NOTES:
20+
#
21+
# LICENSE:
22+
# Zubov Yuri <yury.zubau@gmail.com> sponsored by Actility, https://www.actility.com
23+
# Released under the same terms as Sensu (the MIT license); see LICENSE
24+
# for details.
25+
#
26+
27+
require 'sensu-plugin/metric/cli'
28+
require 'open3'
29+
require 'socket'
30+
31+
class MetricsIPCS < Sensu::Plugin::Metric::CLI::Graphite
32+
option :scheme,
33+
description: 'Metric naming scheme, text to prepend to metric',
34+
short: '-s SCHEME',
35+
long: '--scheme SCHEME',
36+
default: "#{Socket.gethostname}.ipcs"
37+
38+
def run_ipcs
39+
stdout, result = Open3.capture2('ipcs -u')
40+
unknown 'Unable to get ipcs' unless result.success?
41+
stdout
42+
end
43+
44+
def run
45+
ipcs_status = run_ipcs
46+
47+
found = false
48+
index = -1
49+
key = nil
50+
ipcs_status.each_line do |line|
51+
next unless line.match(/[[:space:]]*------/) ... (line == "\n")
52+
if line.strip == ''
53+
index = -1
54+
else
55+
index += 1
56+
end
57+
if index.zero? || index < 0
58+
key = line.tr("-\n", '').strip.tr(' ', '-').downcase
59+
else
60+
result = line.match(/[[:space:]]*(?<name>[a-zA-Z ]*).*(?<value>\d+)/)
61+
output "#{config[:scheme]}.#{key}.#{result[:name].strip.tr(' ', '-')}", result[:value].strip
62+
end
63+
end
64+
if found
65+
ok
66+
else
67+
critical('Not found')
68+
end
69+
end
70+
end

sensu-plugins-process-checks.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ Gem::Specification.new do |s| # rubocop: disable Metrics/BlockLength
4141
s.add_development_dependency 'pry', '~> 0.10'
4242
s.add_development_dependency 'rake', '~> 10.0'
4343
s.add_development_dependency 'redcarpet', '~> 3.2'
44-
s.add_development_dependency 'rspec', '~> 3.1'
44+
s.add_development_dependency 'rspec', '~> 3.7'
45+
s.add_development_dependency 'rspec-mocks', '~> 3.7'
4546
s.add_development_dependency 'rubocop', '~> 0.51.0'
4647
s.add_development_dependency 'yard', '~> 0.9.11'
4748
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def ipcs_answer
2+
<<-TEXT
3+
4+
------ Messages Status --------
5+
allocated queues = 0
6+
used headers = 0
7+
used space = 0 bytes
8+
9+
------ Shared Memory Status --------
10+
segments allocated 31
11+
pages allocated 52233
12+
pages resident 16915
13+
pages swapped 0
14+
Swap performance: 0 attempts 0 successes
15+
16+
------ Semaphore Status --------
17+
used arrays = 0
18+
allocated semaphores = 0
19+
20+
TEXT
21+
end

test/metrics-ipcs_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require_relative './spec_helper'
2+
require_relative '../bin/metrics-ipcs'
3+
require_relative './fixtures/metrics-ips_fixture'
4+
5+
describe 'MetricsIPCS' do
6+
before do
7+
MetricsIPCS.class_variable_set(:@@autorun, false)
8+
end
9+
10+
describe 'with positive answer' do
11+
before do
12+
@default_parameters = '--scheme=test'
13+
@metrics = MetricsIPCS.new(@default_parameters.split(' '))
14+
allow(@metrics).to receive(:run_ipcs).and_return(ipcs_answer)
15+
allow(@metrics).to receive(:ok)
16+
allow(@metrics).to receive(:critical)
17+
end
18+
19+
describe '#run' do
20+
it 'tests that a check are ok' do
21+
@output_result = {}
22+
allow(@metrics).to receive(:output).and_wrap_original do |_m, *args|
23+
@output_result[args[0]] = args[1]
24+
end
25+
@metrics.run
26+
expect(@output_result['test.shared-memory-status.pages-resident']).to eq '5'
27+
expect(@output_result['test.shared-memory-status.pages-allocated']).to eq '3'
28+
end
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)