From c2336a1cd2d0dc8529badbfc1d953e1913984460 Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Tue, 29 Dec 2020 22:05:47 +0100 Subject: [PATCH] 2to3/manual: check_netconnections.py --- check_netconnections.py | 46 +++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/check_netconnections.py b/check_netconnections.py index 438a67f..3536322 100755 --- a/check_netconnections.py +++ b/check_netconnections.py @@ -1,11 +1,11 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- encoding: utf-8 -*- ##################################################################### # (c) 2010-2011 by Sven Velt and team(ix) GmbH, Nuernberg, Germany # # sv@teamix.net # -# (c) 2016 by Sven Velt, Germany # -# sven-mymonplugins@velt.biz # +# (c) 2016- by Sven Velt, Germany # +# sven-mymonplugins@velt.biz # # # # This file is part of "velt.biz - My Monitoring Plugins" # # a fork of "team(ix) Monitoring Plugins" in 2015 # @@ -30,19 +30,18 @@ import re import sys try: - from monitoringplugin import MonitoringPlugin + from MyMonPlugin 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' + print('==========================') + print('AIKS! Python import error!') + print('==========================\n') + print('Could not find class "MonitoringPlugin"!\n') + print('Did you download "%s"' % os.path.basename(sys.argv[0])) + print('without "MyMonPlugin/"?\n') + print('Please go back to') + print('https://gogs.velt.biz/velt.biz/MyMonPlugins and:') + 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) @@ -103,16 +102,13 @@ for version in versions: matcher = v4match for proto in protos: - filename = '/proc/net/%s%s' % (proto, version) - f = file(filename) - lines = f.readlines() - - for line in lines: - m = matcher.match(line) - if m: - port = int(m.group(2), 16) - if port == plugin.options.port and m.group(3) not in ['00000000','00000000000000000000000000000000']: - count += 1 + with open('/proc/net/%s%s' % (proto, version)) as fh: + for line in fh: + m = matcher.match(line) + if m: + port = int(m.group(2), 16) + if port == plugin.options.port and m.group(3) not in ['00000000','00000000000000000000000000000000']: + count += 1 returncode = plugin.value_wc_to_returncode(count, plugin.options.warn, plugin.options.crit)