Skip to content

Instantly share code, notes, and snippets.

@alice-xu
Created June 16, 2014 01:25
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 alice-xu/3c8ff8aeab77155c031d to your computer and use it in GitHub Desktop.
Save alice-xu/3c8ff8aeab77155c031d to your computer and use it in GitHub Desktop.
OpenVPN client stats monitoring script for Zabbix
#!/usr/bin/env bash
#
# openvpn_client_stats.sh:
# OpenVPN client stats 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)
# $5: OpenVPN client host parameter(<SERVER>_<IP>_<CN>)
#
# 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"
[ -z "$5" ] && echo 'ZBX_NOTSUPPORTED' && exit 1
VPN_CLIENT_PARAMS="$5"
VPN_CLIENT_CN=$( echo "$5" | cut -d'_' -f3- )
VPN_CLIENT_IP=$( echo "$5" | cut -d'_' -f2 )
POLLING_TIME=$( date '+%s' )
OVPN_CLIENT_STATS=$( echo -e "${OVPN_MGMT_PASSWORD}status\n" \
| nc -w ${OVPN_MGMT_TIMEOUT} ${OVPN_HOST} ${OVPN_MGMT_PORT} \
| tr -d '\r' \
| grep -E "^${VPN_CLIENT_CN}," )
if [ $? -ne 0 ]; then
echo "Down"
exit 1
fi
echo "Up"
BYTES_IN=$( echo "${OVPN_CLIENT_STATS}" | cut -d',' -f3 )
BYTES_OUT=$( echo "${OVPN_CLIENT_STATS}" | cut -d',' -f4 )
{
echo "\"${VPN_CLIENT_PARAMS}\" "script.openvpn_client_stats.incomming" ${POLLING_TIME} ${BYTES_IN}"
echo "\"${VPN_CLIENT_PARAMS}\" "script.openvpn_client_stats.outbound" ${POLLING_TIME} ${BYTES_OUT}"
} | zbx_bulk_sender > /dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment