1
0
mirror of https://github.com/opinkerfi/nagios-plugins.git synced 2024-12-22 17:13:52 +01:00
nagios-plugins/check_package_updates/check_package_updates

107 lines
3.4 KiB
Plaintext
Raw Normal View History

2013-07-15 00:42:47 +02:00
#!/usr/bin/python
#
# Copyright 2013, Tomas Edwardsson <tommi@tommi.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from subprocess import Popen, PIPE
from pynag.Plugins import PluginHelper, unknown, ok
from collections import defaultdict
known_types = ['Enhancement', 'Normal', 'Bug fix', 'Security']
2013-07-16 12:27:04 +02:00
def main():
p = PluginHelper()
p.parse_arguments()
p.status(ok)
total_updates = 0
pkg_updates = {}
try:
total_updates, pkg_updates = pkcon_get_updates()
except Exception, e:
p.add_summary("Error: " + e.message)
p.status(unknown)
p.exit()
p.add_summary("Total: %i, " % total_updates +
2013-07-16 12:27:04 +02:00
", ".join(["%s: %i" % (x, len(pkg_updates[x])) for x in pkg_updates.keys()]))
p.add_metric("total", total_updates)
for update_type in pkg_updates:
p.add_metric(update_type.lower(), len(pkg_updates[update_type]))
if len(pkg_updates[update_type]):
p.add_long_output(update_type)
for pkg in pkg_updates[update_type]:
p.add_long_output(" %s" % pkg)
for m in known_types:
m = m.lower()
if not p.get_metric(m):
p.add_metric(m, 0)
p.check_all_metrics()
p.exit()
2013-07-15 00:42:47 +02:00
2013-07-16 12:27:04 +02:00
2013-07-15 00:42:47 +02:00
def pkcon_get_updates():
"""
Fetches all package updates and returns a tuple containing:
total_updates,
dictionary where the type of update is the key and the value is a array of package names.
:return: { "Bug fix": [ "pkg-1.0.1", "anthr-pkg-3.1.4" ], "Security": [ "pkg2-2.1.1" ],
"""
update_types = defaultdict(list)
for t in known_types:
update_types[t] = []
stdout = ""
stderr = ""
rc = 255
try:
p = Popen(["pkcon", "get-updates"], stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
rc = p.wait() >> 8
except OSError, e:
if e.errno == 2:
2013-07-16 12:27:04 +02:00
raise Exception("%s - PackageKit not installed?" % e.strerror)
total_updates = 0
2013-07-15 00:42:47 +02:00
results_section = False
for line in stdout.splitlines():
if not line:
continue
if line.startswith("There are no updates"):
2013-07-17 00:09:23 +02:00
continue
if results_section is False and line == "Results:":
2013-07-15 00:42:47 +02:00
results_section = True
elif results_section:
update_type = line[:13].strip()
update_package = line[13:].strip()
2013-07-15 00:42:47 +02:00
update_types[update_type].append(update_package)
total_updates += 1
2013-07-15 00:42:47 +02:00
if rc > 0:
2013-07-16 12:27:04 +02:00
raise Exception(
"pkcon returned non zero return code %i, output:\n%s\nstderr output:\n%s" % (rc, stdout, stderr))
if results_section is False:
raise Exception("pkcon returned no 'Results:' section. Output of pkcon command:\n" + stdout)
2013-07-16 12:27:04 +02:00
return total_updates, update_types
2013-07-15 00:42:47 +02:00
if __name__ == "__main__":
main()