2020-12-29 21:05:47 +00:00
|
|
|
#!/usr/bin/env python3
|
2011-03-17 09:10:05 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
2011-02-09 13:26:18 +00:00
|
|
|
|
2011-03-17 09:10:05 +00:00
|
|
|
#####################################################################
|
|
|
|
# (c) 2010-2011 by Sven Velt and team(ix) GmbH, Nuernberg, Germany #
|
|
|
|
# sv@teamix.net #
|
2020-12-29 21:05:47 +00:00
|
|
|
# (c) 2016- by Sven Velt, Germany #
|
|
|
|
# sven-mymonplugins@velt.biz #
|
2011-03-17 09:10:05 +00:00
|
|
|
# #
|
2016-11-24 19:49:33 +00:00
|
|
|
# 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/ #
|
2011-03-17 09:10:05 +00:00
|
|
|
# #
|
|
|
|
# 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/>. #
|
|
|
|
#####################################################################
|
2011-02-09 13:26:18 +00:00
|
|
|
|
2011-03-17 09:10:05 +00:00
|
|
|
import os
|
2011-02-09 13:26:18 +00:00
|
|
|
import re
|
2011-03-17 09:10:05 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
try:
|
2020-12-29 21:05:47 +00:00
|
|
|
from MyMonPlugin import MonitoringPlugin
|
2011-03-17 09:10:05 +00:00
|
|
|
except ImportError:
|
2020-12-29 21:05:47 +00:00
|
|
|
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')
|
2011-03-17 09:10:05 +00:00
|
|
|
sys.exit(127)
|
|
|
|
|
2011-02-09 13:26:18 +00:00
|
|
|
|
|
|
|
plugin = MonitoringPlugin(pluginname='check_netconnections', tagforstatusline='NETCONNS', description='Count network connections', version='0.1')
|
|
|
|
|
|
|
|
plugin.add_cmdlineoption('-p', '', 'port', 'port number', default=None)
|
|
|
|
plugin.add_cmdlineoption('-t', '--tcp', 'tcp', 'count TCP connections (default)', action='store_true')
|
|
|
|
plugin.add_cmdlineoption('-u', '--udp', 'udp', 'count TCP connections', action='store_true')
|
|
|
|
plugin.add_cmdlineoption('-4', '', 'v4', 'count IPv4 connections (default)', action='store_true')
|
2011-07-15 10:22:23 +00:00
|
|
|
plugin.add_cmdlineoption('-6', '', 'v6', 'count IPv6 connections (default)', action='store_true')
|
2011-02-09 13:26:18 +00:00
|
|
|
plugin.add_cmdlineoption('-w', '', 'warn', 'warning thresold', default='20')
|
|
|
|
plugin.add_cmdlineoption('-c', '', 'crit', 'warning thresold', default='50')
|
|
|
|
|
|
|
|
plugin.parse_cmdlineoptions()
|
|
|
|
|
|
|
|
# Need a port number
|
|
|
|
if not plugin.options.port:
|
|
|
|
plugin.back2nagios(3, 'No port number specified!')
|
|
|
|
else:
|
|
|
|
plugin.options.port = int(plugin.options.port)
|
|
|
|
|
|
|
|
|
|
|
|
# Settings defaults
|
|
|
|
if not plugin.options.udp and not plugin.options.tcp:
|
|
|
|
plugin.options.tcp = True
|
|
|
|
|
|
|
|
if not plugin.options.v4 and not plugin.options.v6:
|
|
|
|
plugin.options.v4 = True
|
2011-07-15 10:22:23 +00:00
|
|
|
plugin.options.v6 = True
|
2011-02-09 13:26:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
# RegExp
|
|
|
|
v4match = re.compile('^\s*\d*:\s*([0-9A-Fa-f]{8}):([0-9A-Fa-f]{4})\s+([0-9A-Fa-f]{8}):([0-9A-Fa-f]{4})')
|
|
|
|
v6match = re.compile('^\s*\d*:\s*([0-9A-Fa-f]{32}):([0-9A-Fa-f]{4})\s+([0-9A-Fa-f]{32}):([0-9A-Fa-f]{4})')
|
|
|
|
|
|
|
|
|
|
|
|
# Prepare
|
|
|
|
count = 0
|
|
|
|
protos = []
|
|
|
|
versions = []
|
|
|
|
|
|
|
|
if plugin.options.tcp:
|
|
|
|
protos.append('tcp')
|
|
|
|
if plugin.options.udp:
|
|
|
|
protos.append('udp')
|
|
|
|
if plugin.options.v4:
|
|
|
|
versions.append('')
|
|
|
|
if plugin.options.v6:
|
|
|
|
versions.append('6')
|
|
|
|
|
|
|
|
|
|
|
|
# Go!
|
|
|
|
for version in versions:
|
|
|
|
|
|
|
|
if version == '6':
|
|
|
|
matcher = v6match
|
|
|
|
else:
|
|
|
|
matcher = v4match
|
|
|
|
|
|
|
|
for proto in protos:
|
2020-12-29 21:05:47 +00:00
|
|
|
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
|
2011-02-09 13:26:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
returncode = plugin.value_wc_to_returncode(count, plugin.options.warn, plugin.options.crit)
|
|
|
|
plugin.add_returncode(returncode)
|
|
|
|
|
|
|
|
plugin.add_output('%s network connections on port %s' % (count, plugin.options.port))
|
2011-03-16 15:06:56 +00:00
|
|
|
plugin.format_add_performancedata('netconns_%s' % plugin.options.port, count, '', warn=plugin.options.warn, crit=plugin.options.crit)
|
2011-02-09 13:26:18 +00:00
|
|
|
|
|
|
|
plugin.exit()
|
|
|
|
|