check_from_file 0.2.0 → 0.3.0

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: 901a66f425cf19a6cef69a5c840af0aa9807dee4
4
- data.tar.gz: f6bfd14981015a516b098d126808a9d7ebe7b4cd
3
+ metadata.gz: 09dbdfbe57149fc6367bf4a9527c3414c948ffb7
4
+ data.tar.gz: 178e811e63710c613cd6f51515f1ab167f98e69b
5
5
  SHA512:
6
- metadata.gz: ab967cd5745ccaee048213d96f55d9a76198dc70ce7eb4e7a7f214823c05eeb034a553726798b829ec52dfdc977aedad84376af00326ca4cd5e962c946b3c970
7
- data.tar.gz: 4c3a84eb78718991b9d7414da073b24f130aa6bff3de2ecb74ff43424e2908a876d80bfac681f27e05b55d526fd4938e31b7b770efe9135df94d908c75a7f74d
6
+ metadata.gz: 904cf4904dcc3f90df440ae70691b0c2d8eecf4a700f078e4bf7b4ef5bc935121e4390e9d1f29a2aa64b2265b00271768ad29672016bd26e8808c55e98f9695c
7
+ data.tar.gz: 421c7cf788e96bb5524da1f2d5863476097da35e9ebb344eff7f839da9bdea38ba6ce9f88e0d3168356c707cdb14bd7b07fc3c3b7f9962aa88a1a31587e71a35
@@ -1,5 +1,6 @@
1
1
  require 'check_from_file'
2
2
  require 'nagiosplugin'
3
+ require 'timeout'
3
4
 
4
5
  module CheckFromFile
5
6
  class Check < Nagios::Plugin
@@ -23,6 +24,41 @@ module CheckFromFile
23
24
  end
24
25
 
25
26
  def check
27
+ # Aquire lock before reading
28
+ if @options[:lock]
29
+ begin
30
+ File.open(@options[:lock], File::RDWR) do |lock|
31
+ Timeout::timeout(@options[:lock_timeout]) do
32
+ lock.flock(File::LOCK_EX) # Exclusive lock
33
+ end
34
+ read_files
35
+ lock.flock(File::LOCK_UN) # Unlock
36
+ end
37
+ rescue Timeout::Error
38
+ @lock_timeout = true
39
+ end
40
+ # Don't aquire lock before reading
41
+ else
42
+ read_files
43
+ end
44
+ end
45
+
46
+ def message
47
+ return "One or more output files are #{@age} seconds old!" if @stale
48
+ return "Timed out after #{options[:lock_timeout]} seconds while trying to "\
49
+ 'get lock!' if @lock_timeout
50
+
51
+ ret = "Command: #{@options[:command]} returned "
52
+ if @critical
53
+ ret << "#{@return}, STDOUT: #{@stdout}, STDERR: #{@stderr}"
54
+ else
55
+ ret << "successfully"
56
+ end
57
+ return ret
58
+ end
59
+
60
+ private
61
+ def read_files
26
62
  [:return, :stdout, :stderr].each do |file|
27
63
  @age = (Time.now - File.stat(@options[file]).mtime).to_i
28
64
  case
@@ -39,17 +75,5 @@ module CheckFromFile
39
75
  @stdout = File.read(@options[:stdout])
40
76
  @stderr = File.read(@options[:stderr])
41
77
  end
42
-
43
- def message
44
- return "One or more output files are #{@age} seconds old!" if @stale
45
-
46
- ret = "Command: #{@options[:command]} returned "
47
- if @critical
48
- ret << "#{@return}, STDOUT: #{@stdout}, STDERR: #{@stderr}"
49
- else
50
- ret << "successfully"
51
- end
52
- return ret
53
- end
54
78
  end
55
79
  end
@@ -8,9 +8,10 @@ module CheckFromFile
8
8
  options = OpenStruct.new
9
9
  options[:file_age_warning] = 150
10
10
  options[:file_age_critical] = 300
11
+ options[:lock_timeout] = 30
11
12
 
12
13
  opt_parser = OptionParser.new do |opts|
13
- opts.banner = "Usage: #{$0} -c <command> -o <stdout> -e <stderr> -r <ret>"
14
+ opts.banner = "Usage: #{$0} -c <command> -o <stdout> -e <stderr> -r <ret> [-l <lock-file>]"
14
15
 
15
16
  opts.on('-c', '--command COMMAND', :REQUIRED, 'Command that was run to generate this output') do |command|
16
17
  options[:command] = command
@@ -36,6 +37,14 @@ module CheckFromFile
36
37
  options[:file_age_critical] = ret.to_i
37
38
  end
38
39
 
40
+ opts.on('-l', '--lock-file FILE', 'If passed, use file for exclusive lock before trying to read files') do |lock|
41
+ options[:lock] = lock
42
+ end
43
+
44
+ opts.on('-t', '--lock-timeout', 'Number of seconds to wait for lock acquisition') do |timeout|
45
+ options[:timeout] = timeout
46
+ end
47
+
39
48
  opts.on_tail("-h", "--help", "Show this message") do
40
49
  puts opts
41
50
  exit
@@ -1,3 +1,3 @@
1
1
  module CheckFromFile
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: check_from_file
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Thompson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-12 00:00:00.000000000 Z
11
+ date: 2016-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler