Compare commits

...

8 commits

Author SHA1 Message Date
Sven Velt baa916df81 Merge pull request 'Detect correct Python version' (#2) from devel into stable
Reviewed-on: #2
2024-03-25 15:44:29 +00:00
Sven Velt 092b3a1103 Added /usr/libexec/platform-python for python discovery 2023-12-01 15:34:47 +01:00
Sven Velt 76065290c5 Fixed small python detection bug 2023-12-01 09:02:59 +01:00
Sven Velt 9862a7bade Detect python version 2022-01-31 15:21:30 +01:00
Sven Velt ba1cff4d61 Fix format string error for exceptions 2022-01-25 15:30:27 +01:00
Sven Velt f0a969d40f Added syslog logging and some command line arguments
-s:   Do NOT log to syslog
:     Log errors only
-v:   + executed command
-vv:  + matching regexp
-vvv: + command output & return code
2020-10-23 14:30:37 +02:00
Sven Velt be2ff6a2cd Fix for awk/os-release with different OS 2020-02-22 13:31:28 +01:00
Sven Velt f8934c59f9 Converted to python3
Squashed commit of the following:

commit 621e91d31ac04c7a692fb87a5cdf9235489e75b9
Author: Sven Velt <sven@velt.de>
Date:   Wed Nov 27 22:31:44 2019 +0100

    Decode byte strings to UTF-8

commit 92c878cc8f14d66f54b1ed9d4587f41902e4c537
Author: Sven Velt <sven@velt.de>
Date:   Wed Nov 27 16:02:54 2019 +0100

    2to3, interpreter changed

    Es werden aber binäre Strings ausgegeben :-/
2020-02-22 13:30:29 +01:00

View file

@ -1,16 +1,40 @@
#!/usr/bin/env python #!/bin/sh
'''':
for pyint in /usr/libexec/platform-python python3 python python2; do
command -v $pyint >/dev/null 2>&1 && exec $pyint "$0" "$@"
done
echo "$0: No python could be found" >&2
exit 1
# '''
import argparse
import os import os
import re import re
import shlex import shlex
import subprocess import subprocess
import sys import sys
import syslog
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument('--silent', action='store_true', default=False)
args = parser.parse_args()
if args.silent:
args.verbose = -1
if args.verbose >= 0:
syslog.openlog(
ident=sys.argv[0],
logoption=syslog.LOG_PID,
facility=syslog.LOG_LOCAL3 | syslog.LOG_ERR
)
allowed = [ allowed = [
##### System informations ##### System informations
r'^/usr/bin/lsb_release\s+-d$', # Linux r'^/usr/bin/lsb_release\s+-d$', # Linux
r'^/(usr/)?bin/uname\s+-mrs$', # Linux, BSD & others r'^/(usr/)?bin/uname\s+-mrs$', # Linux, BSD & others
r'''^/(usr/)?s?bin/awk -F'"' -e '/PRETTY_NAME/{ print \$2; }' /etc/os-release''', # Linux: /etc/os-release via awk for get_os.py r'''^/(usr/)?s?bin/awk -F'"' (-e\s*)?'/PRETTY_NAME/{ print \$2; }' /etc/os-release''', # Linux: /etc/os-release via awk for get_os.py
##### Complete command lines (Monitoring-Plugins on Debian) ##### Complete command lines (Monitoring-Plugins on Debian)
r'^/usr/lib/nagios/plugins/check_disk -w \d+% -c \d+% -p /[/a-z]*$', r'^/usr/lib/nagios/plugins/check_disk -w \d+% -c \d+% -p /[/a-z]*$',
@ -33,22 +57,37 @@ allowed = [
cmdline = os.getenv('SSH_ORIGINAL_COMMAND') cmdline = os.getenv('SSH_ORIGINAL_COMMAND')
if not cmdline: if not cmdline:
print 'This is just a wrapper, no command specified!' print('This is just a wrapper, no command specified!')
if args.verbose >= 0:
syslog.syslog('Called without SSH_ORIGINAL_COMMAND')
sys.exit(3) sys.exit(3)
for maybe in allowed: for maybe in allowed:
if re.match(maybe, cmdline): if re.match(maybe, cmdline):
cmdline = shlex.split(cmdline) if args.verbose >= 2:
syslog.syslog(syslog.LOG_INFO, 'Found command line >%s< with regexp >%s<' % ( cmdline, maybe ) )
cmdlinelist = shlex.split(cmdline)
try: try:
cmd = subprocess.Popen(cmdline, stdout=subprocess.PIPE) cmd = subprocess.Popen(cmdlinelist, stdout=subprocess.PIPE)
except Exception, exc: except Exception as exc:
print 'Could not execute plugin ("%s"): %s' % (' '.join(cmdline), exc) print('Could not execute plugin: %s' % exc)
if args.verbose >= 0:
syslog.syslog('Could not execute plugin >%s<' % cmdline)
sys.exit(3) sys.exit(3)
else: else:
print cmd.communicate()[0].rstrip() (out, outerr) = cmd.communicate()
out = out.rstrip().decode('utf-8')
outerr = (outerr or b'').rstrip().decode('utf-8')
print(out)
if args.verbose >= 1:
syslog.syslog('Executed command line >%s<' % cmdline)
if args.verbose >= 3:
syslog.syslog('Output >%s<, Error >%s<' % (out, outerr))
sys.exit(cmd.returncode) sys.exit(cmd.returncode)
print '%s: No allowed command found!' % sys.argv[0] print('%s: No allowed command found!' % sys.argv[0])
if args.verbose >= 0:
syslog.syslog('No allowed command found for >%s<' % cmdline)
sys.exit(3) sys.exit(3)