2016-09-20 20:50:12 +00:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
|
|
|
#####################################################################
|
2017-11-09 11:29:07 +00:00
|
|
|
# (c) 2016-2017 by Sven Velt, Germany #
|
2016-09-20 20:50:12 +00:00
|
|
|
# sven-mymonplugins@velt.biz #
|
|
|
|
# #
|
|
|
|
# This file is part of "velt.biz - My Monitoring Plugins" #
|
|
|
|
# a fork of "team(ix) Monitoring Plugins" in 2015 #
|
|
|
|
# URL: https://gogs.velt.biz/velt.biz/MyMonPlugins/ #
|
|
|
|
# #
|
|
|
|
# This file 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 2 of the License, #
|
|
|
|
# or (at your option) any later version. #
|
|
|
|
# #
|
|
|
|
# This file 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 file. If not, see <http://www.gnu.org/licenses/>. #
|
|
|
|
#####################################################################
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
try:
|
|
|
|
from monitoringplugin import MonitoringPlugin
|
|
|
|
except ImportError:
|
|
|
|
print '=========================='
|
|
|
|
print 'AIKS! Python import error!'
|
|
|
|
print '==========================\n'
|
|
|
|
print 'Could not find "monitoringplugin.py"!\n'
|
|
|
|
print 'Did you download "%s"' % os.path.basename(sys.argv[0])
|
|
|
|
print 'without "monitoringplugin.py"?\n'
|
|
|
|
print 'Please go back to'
|
|
|
|
print 'https://gogs.velt.biz/velt.biz/MyMonPlugins and download it,'
|
|
|
|
print 'or even better:'
|
|
|
|
print 'get a full archive at http://gogs.velt.biz/velt.biz/MyMonPlugins/releases'
|
|
|
|
print 'or a master snapshot at http://gogs.velt.biz/velt.biz/MyMonPlugins/archive/master.tar.gz\n'
|
|
|
|
sys.exit(127)
|
|
|
|
|
|
|
|
|
|
|
|
plugin = MonitoringPlugin(
|
|
|
|
pluginname='check_xbps',
|
|
|
|
tagforstatusline='XBPS',
|
|
|
|
description='Check XBPS package manager for updates',
|
|
|
|
version='0.1',
|
|
|
|
)
|
|
|
|
|
|
|
|
plugin.add_cmdlineoption('-P', '--path-xbps-install', 'xbps_install', 'full path to xbps-install', default='/usr/bin/xbps-install')
|
|
|
|
plugin.add_cmdlineoption('-S', '--sync-repo-index', 'sync_repo_index', 'sync repository index files at startup', default=False, action='store_true')
|
|
|
|
plugin.add_cmdlineoption('', '--ignore-sync-failure', 'fail_on_sync_failure', 'ignore repo index sync failures', default=True, action='store_false')
|
2016-12-09 12:31:01 +00:00
|
|
|
plugin.add_cmdlineoption('', '--sudo', 'sudo', 'call "xbps-install" with sudo', default=False, action='store_true')
|
2016-09-20 20:50:12 +00:00
|
|
|
|
2016-11-24 17:49:10 +00:00
|
|
|
plugin.add_cmdlineoption('', '--mymonplugins-testmode', 'mymonplugins_testmode', None, default=False, action='store_true')
|
2016-09-20 20:50:12 +00:00
|
|
|
|
|
|
|
plugin.parse_cmdlineoptions()
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
##### Testmode
|
|
|
|
|
|
|
|
if plugin.options.mymonplugins_testmode:
|
|
|
|
mymonplugins_testmode = {}
|
|
|
|
|
|
|
|
mymonplugins_testmode['xbps-install -S'] = '''[*] Updating `https://repo.voidlinux.eu/current/x86_64-repodata' ...
|
|
|
|
'''.split('\n')
|
|
|
|
|
|
|
|
mymonplugins_testmode['xbps-install -un'] = '''elementary-1.17.1_1 remove x86_64 https://repo.voidlinux.eu/current 19821495 12302924
|
|
|
|
bash-4.3.046_2 update x86_64 https://repo.voidlinux.eu/current 5019665 998440
|
|
|
|
libllvm3.9-3.9.0_2 install x86_64 https://repo.voidlinux.eu/current 51052672 12480860
|
|
|
|
linux4.7-4.7.4_1 install x86_64 https://repo.voidlinux.eu/current 61839987 55203908
|
|
|
|
linux-4.7_1 update x86_64 https://repo.voidlinux.eu/current 624
|
|
|
|
linux4.6-4.6.7_1 update x86_64 https://repo.voidlinux.eu/current 60995961 54434936
|
|
|
|
lxc-2.0.4_1 update x86_64 https://repo.voidlinux.eu/current 1837102 468024'''.split('\n')
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
2016-12-26 21:58:47 +00:00
|
|
|
def run_command(cmdline, needs_sudo=False):
|
2016-09-20 20:50:12 +00:00
|
|
|
tstart = time.time()
|
2016-12-26 21:58:47 +00:00
|
|
|
if needs_sudo and plugin.options.sudo:
|
2016-12-09 12:31:01 +00:00
|
|
|
new = ['sudo', '-n', '--']
|
|
|
|
new.extend(cmdline)
|
|
|
|
cmdline = new
|
2016-09-20 20:50:12 +00:00
|
|
|
plugin.verbose(1, 'Running command line: %s' % subprocess.list2cmdline(cmdline))
|
|
|
|
try:
|
|
|
|
cmd = subprocess.Popen(
|
|
|
|
cmdline,
|
|
|
|
stdout = subprocess.PIPE,
|
|
|
|
stderr = subprocess.PIPE
|
|
|
|
)
|
|
|
|
except OSError:
|
2016-11-24 18:36:26 +00:00
|
|
|
plugin.back2nagios(plugin.RETURNCODE['UNKNOWN'], 'Could not execute command line: %s' % subprocess.list2cmdline(cmdline))
|
2016-09-20 20:50:12 +00:00
|
|
|
|
|
|
|
(sout,serr) = cmd.communicate()
|
|
|
|
plugin.verbose(2, 'Runtime %.3fs' % (time.time() - tstart, ) )
|
|
|
|
|
|
|
|
sout = sout.rstrip().split('\n')
|
|
|
|
if sout == ['']:
|
|
|
|
sout = []
|
|
|
|
serr = serr.rstrip().split('\n')
|
|
|
|
if serr == ['']:
|
|
|
|
serr = []
|
|
|
|
|
|
|
|
return( (sout, serr, cmd.returncode,) )
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
if plugin.options.sync_repo_index:
|
2016-11-24 17:50:30 +00:00
|
|
|
plugin.verbose(1, '-S/--sync_repo_index given')
|
2016-09-20 20:50:12 +00:00
|
|
|
cmdline = [plugin.options.xbps_install, '-S',]
|
2016-12-26 21:58:47 +00:00
|
|
|
(sout, serr, rc) = run_command(cmdline, needs_sudo=True)
|
2016-09-20 20:50:12 +00:00
|
|
|
sout and plugin.verbose(3, sout, prefix='stdout: ')
|
|
|
|
serr and plugin.verbose(3, serr, prefix='stderr: ')
|
|
|
|
plugin.verbose(2, 'Return code: %d' % rc)
|
2016-12-09 12:31:01 +00:00
|
|
|
if plugin.options.fail_on_sync_failure and (rc != 0 or 'ERROR:' in ' '.join(serr)):
|
2017-11-09 11:29:07 +00:00
|
|
|
if 'Permission denied' in ' '.join(serr):
|
|
|
|
plugin.back2nagios(plugin.RETURNCODE['CRITICAL'], 'Syncing of repository index files failed, permission denied. Do you need "--sudo"?', multiline=serr)
|
2016-12-09 12:31:01 +00:00
|
|
|
plugin.back2nagios(plugin.RETURNCODE['CRITICAL'], 'Syncing of repository index files failed', multiline=serr)
|
2016-09-20 20:50:12 +00:00
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
cmdline = [plugin.options.xbps_install, '-un',]
|
|
|
|
(sout, serr, rc) = run_command(cmdline)
|
|
|
|
sout and plugin.verbose(3, sout, prefix='stdout: ')
|
|
|
|
serr and plugin.verbose(3, serr, prefix='stderr: ')
|
|
|
|
plugin.verbose(2, 'Return code: %d' % rc)
|
|
|
|
|
2016-12-09 12:31:01 +00:00
|
|
|
if plugin.options.sudo and 'sudo: ' in ' '.join(serr):
|
|
|
|
# Running with sudo
|
|
|
|
if rc == 1:
|
|
|
|
# sudo RC 1: a password is required
|
|
|
|
plugin.back2nagios(plugin.RETURNCODE['CRITICAL'], ' '.join(serr))
|
2016-11-24 18:36:26 +00:00
|
|
|
if rc == 8:
|
|
|
|
# RC 8: Transaction aborted due to unresolved shlibs.
|
|
|
|
plugin.back2nagios(plugin.RETURNCODE['WARNING'], serr[-1], serr[:-1])
|
|
|
|
elif rc not in [0, 6]:
|
|
|
|
# RC 0: Packages to update
|
|
|
|
# RC 6: Package(s) already installed
|
|
|
|
plugin.back2nagios(plugin.RETURNCODE['WARNING'], 'Unknown returncode "%s", please contact the author of plugin or open an issue!' % rc)
|
|
|
|
|
2016-12-26 21:58:22 +00:00
|
|
|
action = {'remove':[], 'update':[], 'install':[], 'configure':[]}
|
2016-09-20 20:50:12 +00:00
|
|
|
arch = {}
|
|
|
|
repo = {}
|
|
|
|
downby = 0L
|
|
|
|
|
|
|
|
for line in sout:
|
|
|
|
cols = line.split(' ')
|
|
|
|
# Append package name to action type list
|
|
|
|
try:
|
|
|
|
action[cols[1]].append(cols[0])
|
|
|
|
except KeyError:
|
|
|
|
action[cols[1]] = [ cols[0], ]
|
|
|
|
|
|
|
|
# Count per arch
|
|
|
|
try:
|
|
|
|
arch[cols[2]] += 1
|
|
|
|
except KeyError:
|
|
|
|
arch[cols[2]] = 1
|
|
|
|
|
|
|
|
# Count per repo
|
|
|
|
try:
|
|
|
|
repo[cols[3]] += 1
|
|
|
|
except KeyError:
|
|
|
|
repo[cols[3]] = 1
|
|
|
|
|
|
|
|
# Count bytes to download
|
|
|
|
if len(cols) == 5:
|
|
|
|
downby += long(cols[4])
|
|
|
|
else:
|
|
|
|
downby += long(cols[5])
|
|
|
|
|
|
|
|
if len(sout) == 0:
|
|
|
|
plugin.remember_check('Updates', plugin.RETURNCODE['OK'], 'Everything uptodate')
|
|
|
|
else:
|
|
|
|
out = []
|
|
|
|
multiline = []
|
2016-12-26 21:58:22 +00:00
|
|
|
for act in ['update', 'install', 'remove', 'configure']:
|
2016-09-20 20:50:12 +00:00
|
|
|
pkgs = action.pop(act)
|
|
|
|
pkgs.sort()
|
|
|
|
l = len(pkgs)
|
|
|
|
out.append('%s package%s to %s' % (l, l != 1 and 's' or '', act) )
|
|
|
|
l and multiline.append('%s(%s): %s' % (act, l, ', '.join(pkgs)) )
|
2016-12-26 21:58:22 +00:00
|
|
|
for act in action.keys():
|
2016-09-20 20:50:12 +00:00
|
|
|
pkgs = action.pop(act)
|
|
|
|
pkgs.sort()
|
|
|
|
l = len(pkgs)
|
|
|
|
out.append('%s package%s to %s' % (l, l != 1 and 's' or '', act) )
|
|
|
|
l and multiline.append('%s(%s): %s' % (act, l, ', '.join(pkgs)) )
|
|
|
|
|
|
|
|
out = ', '.join(out)
|
|
|
|
if downby:
|
|
|
|
out += ' - %s to download' % plugin.value_to_human_binary(downby, 'B')
|
|
|
|
|
|
|
|
stats = 'Statistics: '
|
|
|
|
for (k, v) in arch.iteritems():
|
|
|
|
stats += '%sx %s, ' % (v, k)
|
|
|
|
for (k, v) in repo.iteritems():
|
|
|
|
stats += '%s from %s, ' % (v, k)
|
|
|
|
multiline.append(stats[:-2])
|
|
|
|
|
2016-11-24 18:36:26 +00:00
|
|
|
plugin.remember_check('Updates', plugin.RETURNCODE['CRITICAL'], out, multilineoutput=multiline)
|
2016-09-20 20:50:12 +00:00
|
|
|
|
|
|
|
# Exit
|
|
|
|
plugin.brain2output()
|
|
|
|
plugin.exit()
|
|
|
|
|