sensu-package-metrics 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTcyZmI4YzA0M2JmMmZmMmZlZTYwY2Y0OGFmNmJjYjQyMTlhN2ViMQ==
5
+ data.tar.gz: !binary |-
6
+ YWUwYTVkMzhiNjIxZWYzYzNhYTBkYWEyYjI4ZTM2NWQ2MWZiM2I0Yw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OTI1YjJiODFjODg0OWM0NmJjOGMxMzUyZGZmMmIwMjliMTM1NjQyMzI4ZGJj
10
+ MjhmNzFhZWJmYTUyYmU3MGFkMTQxN2NlMDYzNTkzNDllYjA5YmQ1NWU1NzRm
11
+ MzdjNTIyZTllMzkzMmFiZDYxNjg1ZTkyOWY5YWNkOWNlZGUzODU=
12
+ data.tar.gz: !binary |-
13
+ MTI4NWM4NzVhY2JjODIyMjM2MDA0MmE0ZTcyNDg2YzkwMGI2YjNjNDFiMjMz
14
+ NDBjOThlMmEwZTI2OTdjNGI0MWM2ZjI0NWEyNjhmNTM4YjQ5ODBmNWZkMjQw
15
+ Njc5MGFhMWFjMThlZWU2OTRlNTRlZjdjNjVkMjFkMThhZGZlZDM=
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ deploy:
5
+ provider: rubygems
6
+ api_key:
7
+ secure: NDnz0PbD9cZcwM667A/20I4cV0weVzI91u3u7f5XzufNyylftXm7OMC5kHXWx2uaGdwciOJU5TFJzvOFfQXKl7DUv/7TboAgEqUR8Xc1oR1yUGf40vYFnYApgVfsxv6Jesyt/kYQUiT7DDqcM/Y2TY7jV4Nx+jU1g+4ZX49JStUaQ8EpwuYfcve9u7d22kqtclsg5lQeaMAifWNYO0XHRSRXqbTYg8gSHsZgNX8pgsfxPCL7NBfREV954MPWdQi0sMJ79NCLGAamjDSFWtnlfKasSHH0SeoHy4mCKHL1HmeRL6ROOujofLDwpMBRQwmDBfuDxiZE+oe2VbvPyxFEaUhCsze1KinyStUt5ZlnnWhLMWLxYk9AW/a1Tf+s8gIVviGDn/ezE8iNMD5QFsHSA1DhdIzD+Jcj0e+FIwKFtgy3dGgtkI66YElIJLCJNNpLDLCxEoY6AMz2XOajSQs8+enZAs56zPYnUHozx6iL+M5z+k0GPtGc/dWCeA4IMe8mimyqe5wh0h5LM6tRFuFhaRXaP1cna1QzVL7QMUa/TzOe0Fp2Q5jOdB5ORRfnyA3NHNKgJyPaucMLXjQnqbIg+aTtMJcziF4pm+FJgzqoVQ5IcHd+FxGA2bEIsKnnMv54GyUKREmMfeaaU6LhYfZ24L97NSpJXrMsSL8W46PS1SI=
8
+ gem: sensu-package-metrics
9
+ on:
10
+ tags: true
11
+ repo: alphagov/sensu-package-metrics
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sensu-package-metrics.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Crown Copyright (Government Digital Service)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # Sensu Package Metrics [![Build Status](https://travis-ci.org/alphagov/sensu-package-metrics.svg?branch=master)](https://travis-ci.org/alphagov/sensu-package-metrics)
2
+
3
+ This gem provides a sensu check that can be run to return metric details regarding installed versions of a package. The expected output is a JSON object like so:
4
+ ```
5
+ {
6
+ 'package': <expected-package>
7
+ 'status': 'installed'
8
+ 'version': <some-version-number>
9
+ 'installed_at': <some-unix-timestamp-when-package-was-installed'
10
+ 'timestamp': <some-unix-timestamp-when-check-was-run>
11
+ }
12
+ ```
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'sensu-package-metrics'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install sensu-package-metrics
28
+
29
+ ## Usage
30
+
31
+ Once installed, the metric can be run with `report-package-version`. Please use `-h` to see usage:
32
+ ```
33
+ Usage: report-package-version (options)
34
+ -l LOGFILE
35
+ -p PACKAGE
36
+ ```
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( https://github.com/[my-github-username]/sensu-package-metrics/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create a new Pull Request
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+
8
+ require 'cucumber'
9
+ require 'cucumber/rake/task'
10
+
11
+ Cucumber::Rake::Task.new(:features) do |t|
12
+ t.cucumber_opts = "features --format pretty --strict"
13
+ end
14
+
15
+ task :default => [:spec, :features]
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ require 'sensu-plugin/metric/cli'
3
+ require 'sensu/package/metrics/package_log_reader'
4
+ require 'date'
5
+ class ReportPackageVersionMetric < Sensu::Plugin::Metric::CLI::JSON
6
+ option :package, short: "-p PACKAGE", required: true
7
+ option :log, short: "-l LOGFILE"
8
+
9
+ def run
10
+ logfile = config.fetch(:log, "/var/log/dpkg.log")
11
+ package = config.fetch(:package)
12
+ ok Sensu::Package::Metrics::PackageLogReader.new(logfile).find(package).to_h
13
+ end
14
+
15
+ end
@@ -0,0 +1,36 @@
1
+ Feature: Reports package version in json
2
+
3
+ Scenario: ask to retrieve version of foobar
4
+ Given a file named "dpkg.log" with:
5
+ """
6
+ 2015-11-04 10:31:26 status installed foobar 1485
7
+ """
8
+ When I run `report-package-version -p foobar -l dpkg.log`
9
+ Then the stdout should be JSON with:
10
+ | package | foobar |
11
+ | status | installed |
12
+ | version | 1485 |
13
+ | installed_at | 1446633086 |
14
+
15
+ Scenario: ask for package not in log
16
+ Given a file named "dpkg.log" with:
17
+ """
18
+ 2015-11-04 10:31:26 status installed foobaz 1485
19
+ """
20
+ When I run `report-package-version -p foobar -l dpkg.log`
21
+ Then the stdout should be JSON with:
22
+ | package | foobar |
23
+ | status | not_found |
24
+
25
+ Scenario: ask for package appearing twice in log
26
+ Given a file named "dpkg.log" with:
27
+ """
28
+ 2015-11-04 10:31:26 status installed foobar 1483
29
+ 2015-11-04 10:31:26 status installed foobar 1485
30
+ """
31
+ When I run `report-package-version -p foobar -l dpkg.log`
32
+ Then the stdout should be JSON with:
33
+ | package | foobar |
34
+ | status | installed |
35
+ | version | 1485 |
36
+ | installed_at | 1446633086 |
File without changes
@@ -0,0 +1,8 @@
1
+ Then(/^the stdout should be JSON with:$/) do |table|
2
+ output = all_commands.map { |c| c.output }.join("\n")
3
+ json = JSON.parse(output)
4
+ table.raw.each do |row|
5
+ expected = Integer(row[1]) rescue row[1]
6
+ expect(json.fetch(row[0])).to eql expected
7
+ end
8
+ end
@@ -0,0 +1 @@
1
+ require 'aruba/cucumber'
@@ -0,0 +1,9 @@
1
+ require "sensu/package/metrics/version"
2
+
3
+ module Sensu
4
+ module Package
5
+ module Metrics
6
+ # Your code goes here...
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,67 @@
1
+ module Sensu
2
+ module Package
3
+ module Metrics
4
+ class PackageLogReader
5
+
6
+ def initialize(logfile)
7
+ @logfile = logfile
8
+ end
9
+
10
+ def find(package)
11
+ line = find_line(package)
12
+ if line.nil?
13
+ LineNotFound.new(package)
14
+ else
15
+ LineFound.new(package, line)
16
+ end
17
+ end
18
+
19
+ def find_line(package)
20
+ pattern = "status installed #{package}"
21
+ File.readlines(@logfile).select {|line| line.include?(pattern) }.last
22
+ end
23
+
24
+
25
+ class LineNotFound
26
+ def initialize(package)
27
+ @package = package
28
+ end
29
+
30
+ def to_h
31
+ {
32
+ package: @package,
33
+ status: 'not_found'
34
+ }
35
+ end
36
+ end
37
+
38
+ class LineFound
39
+ def initialize(package, line)
40
+ @package = package
41
+ @line = line
42
+ end
43
+
44
+ def to_h
45
+ analyse
46
+ end
47
+
48
+ def analyse
49
+ @line.scan(/(.+) status installed #{@package} (\d+)/) do |timestamp, version|
50
+ unix_timestamp = DateTime.strptime(timestamp, "%Y-%m-%d %H:%M:%S").to_time.to_i
51
+ return {
52
+ package: @package,
53
+ status: 'installed',
54
+ version: version.to_i,
55
+ installed_at: unix_timestamp
56
+ }
57
+ end
58
+ return {
59
+ package: @package,
60
+ status: 'unknown'
61
+ }
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ module Sensu
2
+ module Package
3
+ module Metrics
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sensu/package/metrics/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sensu-package-metrics"
8
+ spec.version = Sensu::Package::Metrics::VERSION
9
+ spec.authors = ["Christopher Holmes"]
10
+ spec.email = ["christopher.holmes@digital.cabinet-office.gov.uk"]
11
+ spec.summary = %q{A sensu plugin for reporting the versions of packages}
12
+ spec.description = spec.summary
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sensu-plugin", "~> 1.2"
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.2"
25
+ spec.add_development_dependency "cucumber", "~> 2.1"
26
+ spec.add_development_dependency "aruba", "~> 0.10.0"
27
+ end
@@ -0,0 +1,96 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Allows RSpec to persist some state between runs in order to support
54
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
55
+ # you configure your source control system to ignore this file.
56
+ config.example_status_persistence_file_path = "spec/examples.txt"
57
+
58
+ # Limits the available syntax to the non-monkey patched syntax that is
59
+ # recommended. For more details, see:
60
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
61
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
62
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
63
+ config.disable_monkey_patching!
64
+
65
+ # This setting enables warnings. It's recommended, but in some cases may
66
+ # be too noisy due to issues in dependencies.
67
+ config.warnings = true
68
+
69
+ # Many RSpec users commonly either run the entire suite or an individual
70
+ # file, and it's useful to allow more verbose output when running an
71
+ # individual spec file.
72
+ if config.files_to_run.one?
73
+ # Use the documentation formatter for detailed output,
74
+ # unless a formatter has already been configured
75
+ # (e.g. via a command-line flag).
76
+ config.default_formatter = 'doc'
77
+ end
78
+
79
+ # Print the 10 slowest examples and example groups at the
80
+ # end of the spec run, to help surface which specs are running
81
+ # particularly slow.
82
+ config.profile_examples = 10
83
+
84
+ # Run specs in random order to surface order dependencies. If you find an
85
+ # order dependency and want to debug it, you can fix the order by providing
86
+ # the seed, which is printed after each run.
87
+ # --seed 1234
88
+ config.order = :random
89
+
90
+ # Seed global randomization in this process using the `--seed` CLI option.
91
+ # Setting this allows you to use `--seed` to deterministically reproduce
92
+ # test failures related to randomization by passing the same `--seed` value
93
+ # as the one that triggered the failure.
94
+ Kernel.srand config.seed
95
+ =end
96
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-package-metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Holmes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sensu-plugin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: cucumber
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '2.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '2.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: aruba
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.0
97
+ description: A sensu plugin for reporting the versions of packages
98
+ email:
99
+ - christopher.holmes@digital.cabinet-office.gov.uk
100
+ executables:
101
+ - report-package-version
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .rspec
107
+ - .travis.yml
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/report-package-version
113
+ - features/reports_package_version_in_json.feature
114
+ - features/step_definitions/log_steps.rb
115
+ - features/step_definitions/output_steps.rb
116
+ - features/support/env.rb
117
+ - lib/sensu/package/metrics.rb
118
+ - lib/sensu/package/metrics/package_log_reader.rb
119
+ - lib/sensu/package/metrics/version.rb
120
+ - sensu-package-metrics.gemspec
121
+ - spec/spec_helper.rb
122
+ homepage: ''
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.4.5
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: A sensu plugin for reporting the versions of packages
146
+ test_files:
147
+ - features/reports_package_version_in_json.feature
148
+ - features/step_definitions/log_steps.rb
149
+ - features/step_definitions/output_steps.rb
150
+ - features/support/env.rb
151
+ - spec/spec_helper.rb