1
0
mirror of https://github.com/opinkerfi/nagios-plugins.git synced 2024-12-23 09:33:56 +01:00
nagios-plugins/check_updates/check_updates

103 lines
3.3 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
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:
if len(pkg_updates[update_type]):
p.add_long_output(update_type)
p.add_metric(update_type.lower(), len(pkg_updates[update_type]))
for pkg in pkg_updates[update_type]:
p.add_long_output(" %s" % pkg)
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" ],
"""
2013-07-16 12:27:04 +02:00
update_types = {}
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.split("\n"):
if not line:
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
if update_type not in update_types:
update_types[update_type] = []
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)
for t in known_types:
if t not in update_types:
update_types[t] = []
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()