Skip to content

Instantly share code, notes, and snippets.

@PhrozenByte
Created February 11, 2019 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhrozenByte/2ae839c8c825809c9ecefad8867bbd51 to your computer and use it in GitHub Desktop.
Save PhrozenByte/2ae839c8c825809c9ecefad8867bbd51 to your computer and use it in GitHub Desktop.
Munin plugin to probe TCP and UDP ports
#!/bin/bash
: << =cut
=head1 NAME
nc_port - Plugin to monitor TCP/UDP port connectivity
=head1 CONFIGURATION
The following environment variables are used by this plugin
nc - Netcat program to use
nc_args - Arguments to nc (default "-z")
host - Hosts and ports to connect to
=head2 CONFIGURATION EXAMPLES
The following configuration can be used to probe www.google.com
on TCP port 80 (http) and Google's 8.8.8.8 on UDP port 53 (dns):
[nc_port]
env.host www.google.com:80
[nc_port_udp]
env.host 8.8.8.8:53
env.critical 1:1
=head1 AUTHOR
Copyright (C) 2019 Daniel Rudolf
=head1 LICENSE
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; version 2 dated June, 1991.
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
=head1 MAGIC MARKERS
#%# family=manual
=cut
. "$MUNIN_LIBDIR/plugins/plugin.sh"
BASENAME="$(basename "$0")"
NC="${nc:-nc}"
NC_ARGS="${nc_args:--z}"
if [[ "$BASENAME" == nc_port_udp* ]]; then
BASENAME="${BASENAME:11}"
NC_ARGS+=" -u"
elif [[ "$BASENAME" == nc_port* ]]; then
BASENAME="${BASENAME:7}"
fi
FILE_HOST=""
if [[ "$BASENAME" == _* ]]; then
FILE_HOST="${BASENAME:1}"
fi
HOST="${host:-${FILE_HOST:-example.com}}"
if [ "$1" == "autoconf" ]; then
echo "yes"
exit 0
fi
PROTOCOL="TCP"
if [[ "$NC_ARGS" =~ (^| )\-[a-zA-Z0-9]*u[a-zA-Z0-9]*( |$) ]]; then
PROTOCOL="UDP"
fi
IFS=' '
for ARG in $HOST; do
if ! [[ "$HOST" =~ :[0-9]+$ ]]; then
if [ "$PROTOCOL" == "UDP" ]; then
HOST="$HOST:53"
else
HOST="$HOST:80"
fi
fi
done
if [ "$1" == "config" ]; then
echo "graph_title $PROTOCOL port connectivity"
echo "graph_args --lower-limit -0.2 --upper-limit 1.2"
echo "graph_vlabel connectivity"
echo "graph_category network"
echo "graph_info This graph shows whether a daemon is listening on the given address or not."
for ARG in $HOST; do
LABEL="$(clean_fieldname "$ARG")"
echo "${LABEL}.label $ARG"
echo "${LABEL}.info Daemon is (value 1) or is not (value 0) listening on $ARG"
print_warning "$LABEL"
print_critical "$LABEL"
done
exit 0
fi
for ARG in $HOST; do
LABEL="$(clean_fieldname "$ARG")"
if "$NC" $NC_ARGS "$(echo "$ARG" | cut -d ':' -f 1)" "$(echo "$ARG" | cut -d ':' -f 2)" 2>&1 > /dev/null; then
echo "${LABEL}.value 1"
else
echo "${LABEL}.value 0"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment