sensu-plugins-barman-checks 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: daecdff7dc4c470c8666cb281c1c6d1b0b46bb09
4
- data.tar.gz: 70b3c3b5262d559cbcfff0f10a0086b20c756a44
3
+ metadata.gz: 03bbb0aaed34536c05481b43bd9c268e30d9b615
4
+ data.tar.gz: db700583a86523ade199db6b5409c4fb4a15b7b0
5
5
  SHA512:
6
- metadata.gz: 0eaba82aed8a591915df6db942354855e6a19053537f518b7d683710049bf8ccbb45ba45490364d4fdb03f78c2d64c3d4b1e8c6b0800a3d5773ad437fc8d8677
7
- data.tar.gz: 817decc9332870a7b3c5b9935577f945ad25c622d4057eabff29b720b749ff243b98f05b4ad5191b9ab1e3320f8483381f3127f2f3e88009a33dd87b1c914a35
6
+ metadata.gz: 59f41557878938ea94caf7c6cc05ebf3fb503d3326ca5c7df9e8ad1c3f483353d81548cd77f3fdb05c523d997c7320c203c96004d9e164761cd8cf9030cb8d84
7
+ data.tar.gz: a6a370736737dd56b320f43207d375e83f0da66274c5f1dfa1ba9defd2d1ff8f68fa27a146fcb9b4892a90f6aea8077bb4335c6531b7bc78aaf918c4b464623b
data/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4
4
  This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)
5
5
 
6
6
  ## [Unreleased]
7
+ ## 0.0.3 - 2016-11-30
8
+ ### Fixed
9
+ - The plugin now actually works
10
+
11
+ ### Removed
12
+ - Dropped rbarman + sudo gems and rewrote plugin using regular sudo calls
7
13
 
8
14
  ## 0.0.2 - 2016-11-30
9
15
  ### Fixed
data/README.md CHANGED
@@ -16,7 +16,13 @@ checked to ensure it has backups, that backup status is successful, and that the
16
16
 
17
17
  ## Usage
18
18
 
19
- /path_to_sensu/embedded/bin/ruby /path_to_sensu/embedded/bin/check-barman-backup-status.rb [-m MIN_NUMBER_BACKUPS] [--max-age MAX_AGE_IN_HOURS_OF_LATEST_BACKUP] [--warn WARN_AGE_IN_HOURS_OF_LATEST_BACKUP]
19
+ This plugin requires the use of sudo, as barman requires either the barman user or sudo user and we can only use the sensu user. To configure sudo access, setup the following in your sudoers file:
20
+
21
+ `sensu ALL=(ALL) NOPASSWD:/usr/bin/barman, /opt/sensu/embedded/bin/ruby, /opt/sensu/embedded/bin/check-barman-backup-status.rb`
22
+
23
+ `Defaults:sensu !requiretty`
24
+
25
+ `/path_to_sensu/embedded/bin/ruby /path_to_sensu/embedded/bin/check-barman-backup-status.rb [-m MIN_NUMBER_BACKUPS] [--max-age MAX_AGE_IN_HOURS_OF_LATEST_BACKUP] [--warn WARN_AGE_IN_HOURS_OF_LATEST_BACKUP]`
20
26
 
21
27
  ## Installation
22
28
 
@@ -1,12 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: UTF-8
3
3
  #
4
- # Sudo wrapper for running barman backup status check
4
+ # Check Barman Backup Status
5
5
  # ===
6
6
  #
7
7
  # DESCRIPTION:
8
8
  # This plugin checks the status and age of Barman backups for each configured server
9
- # The sensu user must be configured as NOPASSWD in sudoers for this script in order to work.
9
+ # Do not run this script directly. It needs to be called via check-barman-backup-status.rb
10
+ # because the barman gem requires sudo access to work properly and we have to run as
11
+ # the sensu user, so we use a sudo wrapper in check-barman-backup-status.rb
10
12
  #
11
13
  # PLATFORMS:
12
14
  # Linux, BSD, Solaris
@@ -23,13 +25,124 @@
23
25
  # Released under the same terms as Sensu (the MIT license); see LICENSE
24
26
  # for details.
25
27
 
26
- require_relative './check-barman-backup-status-class'
27
- require 'sudo'
28
+ require 'sensu-plugin/check/cli'
29
+ require 'time'
28
30
 
29
- # Wrapper around the actual check class since barman requires root or the barman user
30
- # and we have to run as the sensu user to run the plugin
31
- CheckBarmanBackupStatus.new
32
- sudo = Sudo::Wrapper.new
33
- sudo.start!
34
- sudo[CheckBarmanBackupStatus].run
35
- sudo.stop!
31
+ # main plugin class
32
+ class CheckBarmanBackupStatus < Sensu::Plugin::Check::CLI
33
+ option :min_backups,
34
+ description: 'Minimum number of backups that should exist before alerting',
35
+ short: '-m',
36
+ long: '--min-backups',
37
+ required: true,
38
+ default: 1
39
+
40
+ option :warn_age,
41
+ description: 'The maximum age of backup before warning (in hours)',
42
+ short: '-w',
43
+ long: '--warn-age',
44
+ required: true,
45
+ default: 24
46
+
47
+ option :max_age,
48
+ description: 'The maximum age of backup before critical alert (in hours)',
49
+ long: '--max-age',
50
+ required: true,
51
+ default: 25
52
+
53
+ option :homedir,
54
+ description: 'The home directory path for barman',
55
+ long: '--homedir PATH',
56
+ required: true,
57
+ default: '/var/lib/barman'
58
+
59
+ def time_diff(start_time, end_time)
60
+ diff = end_time.to_i - start_time.to_i
61
+ hours = (diff / 60 / 60)
62
+ hours
63
+ end
64
+
65
+ def run
66
+ start_time = Time.now
67
+ @crit = []
68
+ @warn = []
69
+
70
+ servers = `sudo barman list-server | awk '{print $1}'`.split(/\n/).map(&:strip)
71
+
72
+ servers.each do |server|
73
+ @server_hash = {
74
+ server.to_s => {
75
+ 'attrs' => {},
76
+ 'status' => {}
77
+ }
78
+ }
79
+
80
+ @backup_hash = {
81
+ server.to_s => {}
82
+ }
83
+
84
+ # No YAML or other supported output to parse so we'll have to do it manually
85
+ server_attrs = `sudo barman show-server #{server}`.split(/\n/).map(&:strip)
86
+ server_attrs.each do |attr|
87
+ key, value = attr.split(':').map(&:strip)
88
+ @server_hash[server]['attrs'][key] = value
89
+ end
90
+
91
+ server_status = `sudo barman status #{server}`.split(/\n/).map(&:strip)
92
+ server_status.each do |status|
93
+ key, value = status.split(':').map(&:strip)
94
+ @server_hash[server]['status'][key] = value
95
+ end
96
+
97
+ if @server_hash[server]['status']['Active'] == 'False'
98
+ @crit << "Server #{server} is not in active status. Status is: #{@server_hash[server]['status']['Active']}"
99
+ end
100
+
101
+ check_status = `sudo barman check #{server} | grep -v 'Server #{server}'`.split(/\n/).map(&:strip)
102
+ check_status.each do |check|
103
+ key, value = check.split(':').map(&:strip)
104
+ unless value =~ /^OK/
105
+ @crit << "Status check failed on configured server #{server}. #{key}: #{value}"
106
+ end
107
+ end
108
+
109
+ backups = `sudo barman list-backup #{server} | awk '{print $2}'`.split(/\n/).map(&:strip)
110
+
111
+ if backups.count < config[:min_backups].to_i
112
+ @crit << "Minimum backups not met for server #{server}. Count is #{backups.count}"
113
+ else
114
+ puts "Number of backups for server #{server}: #{backups.count}"
115
+ end
116
+
117
+ backups.each do |backup|
118
+ status = `sudo barman show-backup #{server} #{backup} | grep Status | awk '{print $NF}'`.strip
119
+ unless status.casecmp('done') || status.casecmp('started')
120
+ @crit << "Backup for server #{server} with ID #{backup} is in error state: #{status.downcase}"
121
+ end
122
+ end
123
+
124
+ latest = @server_hash[server]['status']['Last available backup']
125
+ latest_time = Time.parse(`sudo barman show-backup #{server} #{latest} | grep 'Begin time' | awk '{print $4,$5}'`.strip)
126
+ backup_age = time_diff(latest_time, start_time).to_i
127
+
128
+ if backup_age > config[:warn_age].to_i
129
+ @warn << "Warning threshold for backup age exceeded for latest backup on server #{server}. Age is #{backup_age} hour(s)"
130
+ end
131
+
132
+ if backup_age > config[:max_age].to_i
133
+ @crit << "Maximum age exceeded for latest backup on server #{server}. Age is: #{backup_age} hour(s)"
134
+ else
135
+ puts "Age of latest backup for server #{server}: #{backup_age} hour(s)"
136
+ end
137
+ end
138
+
139
+ # Assuming we got this far, exit cleanly
140
+ if @crit.empty? && @warn.empty?
141
+ ok 'Recent backups exist for each configured server. Each configured server is also active and in a good state.'
142
+ elsif !@crit.empty?
143
+ critical "critical: #{@crit}"
144
+ elsif !@warn.empty?
145
+ warning "warning: #{@warn}"
146
+ end
147
+ end
148
+ end
@@ -2,7 +2,7 @@ module SensuPluginsBarmanChecks
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- PATCH = 2
5
+ PATCH = 3
6
6
 
7
7
  VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-plugins-barman-checks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Brimhall
@@ -25,34 +25,6 @@ dependencies:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: '1.2'
28
- - !ruby/object:Gem::Dependency
29
- name: rbarman
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - "~>"
33
- - !ruby/object:Gem::Version
34
- version: 0.0.15
35
- type: :runtime
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - "~>"
40
- - !ruby/object:Gem::Version
41
- version: 0.0.15
42
- - !ruby/object:Gem::Dependency
43
- name: sudo
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - "~>"
47
- - !ruby/object:Gem::Version
48
- version: '0.1'
49
- type: :runtime
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - "~>"
54
- - !ruby/object:Gem::Version
55
- version: '0.1'
56
28
  - !ruby/object:Gem::Dependency
57
29
  name: bundler
58
30
  requirement: !ruby/object:Gem::Requirement
@@ -185,7 +157,6 @@ description: |-
185
157
  properly generated.
186
158
  email: "<scottbrimhall@gmail.com>"
187
159
  executables:
188
- - check-barman-backup-status-class.rb
189
160
  - check-barman-backup-status.rb
190
161
  extensions: []
191
162
  extra_rdoc_files: []
@@ -193,7 +164,6 @@ files:
193
164
  - CHANGELOG.md
194
165
  - LICENSE
195
166
  - README.md
196
- - bin/check-barman-backup-status-class.rb
197
167
  - bin/check-barman-backup-status.rb
198
168
  - lib/sensu-plugins-barman-checks.rb
199
169
  - lib/sensu-plugins-barman-checks/version.rb
@@ -1,121 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
- #
4
- # Check Barman Backup Status
5
- # ===
6
- #
7
- # DESCRIPTION:
8
- # This plugin checks the status and age of Barman backups for each configured server
9
- # Do not run this script directly. It needs to be called via check-barman-backup-status.rb
10
- # because the barman gem requires sudo access to work properly and we have to run as
11
- # the sensu user, so we use a sudo wrapper in check-barman-backup-status.rb
12
- #
13
- # PLATFORMS:
14
- # Linux, BSD, Solaris
15
- #
16
- # DEPENDENCIES:
17
- # Sensu Plugins run as the sensu user and barman needs to run with sudo or barman user
18
- # gem: sensu-plugin
19
- # gem: sudo
20
- # gem: rbarman
21
- #
22
- # LICENSE:
23
- # Copyright 2016 Scott Brimhall <scottbrimhall@gmail.com>
24
- #
25
- # Released under the same terms as Sensu (the MIT license); see LICENSE
26
- # for details.
27
-
28
- require 'sensu-plugin/check/cli'
29
- require 'rbarman'
30
- require 'time'
31
-
32
- # main plugin class
33
- class CheckBarmanBackupStatus < Sensu::Plugin::Check::CLI
34
- option :min_backups,
35
- description: 'Minimum number of backups that should exist before alerting',
36
- short: '-m',
37
- long: '--min-backups',
38
- required: true,
39
- default: 1
40
-
41
- option :warn_age,
42
- description: 'The maximum age of backup before warning (in hours)',
43
- short: '-w',
44
- long: '--warn-age',
45
- required: true,
46
- default: 24
47
-
48
- option :max_age,
49
- description: 'The maximum age of backup before critical alert (in hours)',
50
- long: '--max-age',
51
- required: true,
52
- default: 25
53
-
54
- option :homedir,
55
- description: 'The home directory path for barman',
56
- long: '--homedir PATH',
57
- required: true,
58
- default: '/var/lib/barman'
59
-
60
- def time_diff(start_time, end_time)
61
- diff = end_time.to_i - start_time.to_i
62
- hours = (diff / 60 / 60)
63
- hours
64
- end
65
-
66
- def run
67
- # The rbarman gem defaults to ENV['HOME'] for things. Need to set this to barman's homedir
68
- ENV['HOME'] = config[:homedir]
69
- start_time = Time.now
70
- @crit = []
71
- @warn = []
72
-
73
- servers = Rbarman::Servers.all(opts = {})
74
-
75
- servers.each do |server|
76
- puts "Name: #{server.name}"
77
- backups = RBarman::Backups.all(server.name, opts = {})
78
-
79
- if backups.count < config[:min_backups].to_i
80
- @crit << "Minimum backups not met for server #{server.name}. Count is #{backups.count}"
81
- else
82
- puts "Number of backups for server #{server.name}: #{backups.count}"
83
- end
84
-
85
- latest_backup = backups.latest.backup_end
86
- backup_time = Time.parse(latest_backup)
87
- backup_age = time_diff(backup_time, start_time)
88
-
89
- if backup_age < config[:warn_age].to_i
90
- @warn << "Warning threshold for backup age exceeded for latest backup on server #{server.name}. Age is #{backup_age} hour(s)"
91
- end
92
-
93
- if backup_age < config[:max_age].to_i
94
- @crit << "Maximum age exceeded for latest backup on server #{server.name}. Age is: #{backup_age} hour(s)"
95
- else
96
- puts "Age of latest backup for server #{server.name}: #{backup_age} hour(s)"
97
- end
98
-
99
- backups.each do |b|
100
- puts "Backup ID: #{b.id}"
101
- if b.status != 'done'
102
- @crit << "Backup ID #{b.id} is in error state (#{b.status}) for server #{server.name}"
103
- else
104
- puts "Backup Status: #{b.status}"
105
- end
106
-
107
- puts "Backup Size: #{b.size} bytes"
108
- puts "Backup Wal file size: #{b.wal_file_size} bytes"
109
- end
110
- end
111
-
112
- # Assuming we got this far, exit cleanly
113
- if @crit.empty? && @warn.empty?
114
- ok
115
- elsif !@crit.empty?
116
- critical "critical: #{@crit}"
117
- elsif !@warn.empty?
118
- warning "warning: #{@warn}"
119
- end
120
- end
121
- end