Skip to content

Instantly share code, notes, and snippets.

@alice-xu
Last active Jun 10, 2017
Embed
What would you like to do?
OpenVPN monitoring script for Zabbix
#!/usr/bin/env bash
#
# openvpn_stats.sh:
# OpenVPN monitoring script for Zabbix
#
# $1: OpenVPN server host IP/hostname. (default: 127.0.0.1)
# $2: OpenVPN management CLI port. (default: 7505)
# $3: Password file for OpenVPN management CLI. (default: <none>)
# $4: Timeout(sec) (default: 3)
#
# script settings.
cd `dirname $0`
source ./zbx_functions
# Default parameters
[ -z "$1" ] && OVPN_HOST="127.0.0.1" || OVPN_HOST="$1"
[ -z "$2" ] && OVPN_MGMT_PORT="7505" || OVPN_MGMT_PORT="$2"
[ -z "$3" ] && OVPN_MGMT_PASSWORD="" || OVPN_MGMT_PASSWORD="$( cat $3 )\n"
[ -z "$4" ] && OVPN_MGMT_TIMEOUT="3" || OVPN_MGMT_TIMEOUT="$4"
POLLING_TIME=$( date '+%s' )
OVPN_STATS=$( echo -e "${OVPN_MGMT_PASSWORD}load-stats\n" \
| nc -w ${OVPN_MGMT_TIMEOUT} ${OVPN_HOST} ${OVPN_MGMT_PORT} \
| tr -d '\r' \
| grep -E '^SUCCESS:' )
if [ $? -ne 0 ]; then
echo "Error: Could not connect to OpenVPN management interface(Host:${OVPN_HOST}, Port:${OVPN_MGMT_PORT}, Timeout:${OVPN_MGMT_TIMEOUT})."
exit 1
fi
ACTIVE_CLIENTS=$( echo "${OVPN_STATS}" | cut -d',' -f1 | cut -d'=' -f2 )
BYTES_IN=$( echo "${OVPN_STATS}" | cut -d',' -f2 | cut -d'=' -f2 )
BYTES_OUT=$( echo "${OVPN_STATS}" | cut -d',' -f3 | cut -d'=' -f2 )
echo "${OVPN_STATS}"
{
echo "\"${MY_HOSTNAME}\" script.openvpn_stats.nclients ${POLLING_TIME} ${ACTIVE_CLIENTS}"
echo "\"${MY_HOSTNAME}\" script.openvpn_stats.incomming ${POLLING_TIME} ${BYTES_IN}"
echo "\"${MY_HOSTNAME}\" script.openvpn_stats.outbound ${POLLING_TIME} ${BYTES_OUT}"
} | zbx_bulk_sender 2>&1
@acceptor
Copy link

acceptor commented Oct 29, 2014

hi! I like your script! and I using it in my zabbix checks, but i correct it. I use "-q" instead of "-w" for nc, because the script runs faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment