-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_dnf.rb
executable file
·71 lines (59 loc) · 1.6 KB
/
check_dnf.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env ruby
#
# Check DNF update plugin for nagios / icinga
#
# Author : Nicolas Gailly v1.0
# Works with DNF v1.0.0
require 'open3'
TITLE = "check_dnf"
VERSION = "1.0"
## Nagios / icinga output
CODES = { OK: 0, WARNING: 1, CRITICAL: 2, UNKNOWN: 3 }
def run cmd
success = false
output = nil
Open3.popen3(cmd) do |inp,out,err,t|
inp.close
output = out.read.chomp
out.close
leave :UNKNOWN,"Command #{cmd} error : #{err}" unless t.value.success?
end
output
end
def leave status, msg
code = CODES[status]
puts status.to_s + " - " + msg
exit code
end
def check_updates
cmd = "dnf updateinfo"
output = run cmd
if output =~ /^Updates Information Summary: (\w+)$/
unless $1 =~ /available/
leave :UNKNOWN,"DNF do not have update information summary available. Check with DNF"
end
else
leave :OK,"DNF shows no updates to do !"
end
updates = Hash.new { |h,k| h[k] = 0 }
code = :OK
re = /(\d{1,4})\s+(\w+)\s+notice\(s\)$/
output.split("\n").each do |line|
next unless line.match re
updates[$2] += $1.to_i
code = :CRITICAL if $2 =~ /security/i
end
code = :WARNING if updates.size > 0 && code != :CRITICAL
if updates.size == 0
msg = "No updates to do on this host."
else
msg = updates.reduce([]) { |col,(k,v)| col << "#{v} #{k.downcase}"}.join(", ") + " update(s) to do."
end
return code,msg
end
begin
code,msg = check_updates
leave code,msg
rescue StandardError => e
leave :UNKNOWN,"Unknown error : #{e}"
end