{"id":6709,"date":"2020-08-16T23:37:14","date_gmt":"2020-08-16T20:37:14","guid":{"rendered":"https:\/\/kifarunix.com\/?p=6709"},"modified":"2024-03-14T22:31:38","modified_gmt":"2024-03-14T19:31:38","slug":"how-to-monitor-openvpn-connections-using-openvpn-monitor-tool","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/how-to-monitor-openvpn-connections-using-openvpn-monitor-tool\/","title":{"rendered":"How to Monitor OpenVPN Connections using openvpn-monitor tool"},"content":{"rendered":"\n<p>In this tutorial, we are going to learn how to monitor OpenVPN connections using openvpn-monitor tool. This tutorial focuses on monitoring OpenVPN connections established using the <a href=\"https:\/\/community.openvpn.net\/openvpn\/wiki\/OverviewOfOpenvpn\" target=\"_blank\" rel=\"noreferrer noopener\">OpenVPN Community Edition<\/a>. OpenVPN CE provides a de-facto standard for creating a secure VPN connections over the internet using a custom security protocol that utilizes SSL\/TLS.<\/p>\n\n\n\n<p>While scouring the Internet for the tools that can be used to monitor active OpenVPN connections, I came across a python based tool called <a href=\"https:\/\/github.com\/furlongm\/openvpn-monitor\" target=\"_blank\" rel=\"noreferrer noopener\">openvpn-monitor<\/a> which utilizes OpenVPN management interface to generate an html report that displays the status of an OpenVPN server, including all current connections.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Monitoring OpenVPN Connections using openvpn-monitor tool<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<p>Before you can proceed, there are a few things that needs to have setup.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Install and configure OpenVPN Server<\/h4>\n\n\n\n<p>Of course you can be wanting to monitor OpenVPN server connections without having an OpenVPN server running. Follow the links below to install and setup OpenVPN server on CentOS\/Ubuntu systems;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/setup-openvpn-server-on-centos-8\/\">Install and<\/a> <a href=\"https:\/\/kifarunix.com\/setup-openvpn-server-on-centos-8\/\" target=\"_blank\" rel=\"noreferrer noopener\">Setup OpenVPN Server on CentOS 8<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-and-setup-openvpn-server-on-ubuntu-20-04\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install and Setup OpenVPN Server on Ubuntu 20.04<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Enable Management Interface<\/h4>\n\n\n\n<p><code><strong>openvpn-monitor<\/strong><\/code> tools requires that OpenVPN management interface is enabled. The OpenVPN Management interface allows OpenVPN to be administratively controlled from an external program via a TCP or unix domain socket. OpenVPN management server can be enabled on a Unix socket or on a designated TCP port. While using the Unix socket is the recommended method, the openvpn-monitor tool uses OpenVPN management interface TCP connection. It is therefore strongly recommended that you set the OpenVPN management Interface IP to 127.0.0.1 (localhost) to restrict accessibility of the management server to local clients.<\/p>\n\n\n\n<p>OpenVPN management interface TCP connection can be enabled by editing the OpenVPN server configuration file and adding the line, <em><code><strong>management IP port<\/strong><\/code><\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/openvpn\/server\/server.conf<\/code><\/pre>\n\n\n\n<pre class=\"scrollbox\"><code>...\ncomp-lzo\npersist-key\npersist-tun\nstatus \/var\/log\/openvpn\/openvpn-status.log\nlog-append  \/var\/log\/openvpn\/openvpn.log\nverb 3\nexplicit-exit-notify 1\nauth SHA512\n# Enable OpenVPN Management Interface on localhost using TCP port 17562\n<strong>management 127.0.0.1 17562<\/strong>\n<\/code><\/pre>\n\n\n\n<p>The line;<\/p>\n\n\n\n<pre id=\"block-39ba78dc-5263-495b-8009-6422e1489606\" class=\"wp-block-preformatted\"><strong>management 127.0.0.1 17562<\/strong><\/pre>\n\n\n\n<p>Sets the OpenVPN management interface IP address to 127.0.0.1 (localhost) and TCP port 17562. The openvpn-monitor expects the OpenVPN Management Interface to be listening on TCP port 5555. We will change that later.<\/p>\n\n\n\n<p>Check that the assigned port is not being used by any other program on your system;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>lsof -i :17562<\/code><\/pre>\n\n\n\n<p>If no application is using the port, restart OpenVPN server service;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart openvpn-server@server.service<\/code><\/pre>\n\n\n\n<p>Check that port is now opened;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>lsof -i :17562<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>COMMAND   PID USER   FD   TYPE DEVICE SIZE\/OFF NODE NAME\nopenvpn 44178 root    3u  IPv4 189028      0t0  TCP localhost:17562 (LISTEN)<\/code><\/pre>\n\n\n\n<p>Ensure that OpenVPN server service is running;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl status openvpn-server@server.service<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\u25cf openvpn-server@server.service - OpenVPN service for server\n     Loaded: loaded (\/lib\/systemd\/system\/openvpn-server@.service; enabled; vendor preset: enabled)\n     Active: active (running) since Sun 2020-08-16 16:15:46 UTC; 1h 15min ago\n       Docs: man:openvpn(8)\n             https:\/\/community.openvpn.net\/openvpn\/wiki\/Openvpn24ManPage\n             https:\/\/community.openvpn.net\/openvpn\/wiki\/HOWTO\n   Main PID: 44178 (openvpn)\n<strong>     Status: \"Initialization Sequence Completed\"\n<\/strong>      Tasks: 1 (limit: 2282)\n     Memory: 1.9M\n     CGroup: \/system.slice\/system-openvpn\\x2dserver.slice\/openvpn-server@server.service\n             \u2514\u250044178 \/usr\/sbin\/openvpn --status \/run\/openvpn-server\/status-server.log --status-version 2 --suppress-timestamps --config server.conf\n\nAug 16 16:15:46 ubuntu20.kifarunix-demo.com systemd[1]: Starting OpenVPN service for server...\nAug 16 16:15:46 ubuntu20.kifarunix-demo.com systemd[1]: Started OpenVPN service for server.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Install and Setup openvpn-monitor tool<\/h3>\n\n\n\n<p>There are different methods of installing openvpn-monitor tool as outlined on the program&#8217;s <a href=\"https:\/\/github.com\/furlongm\/openvpn-monitor#install-options\" target=\"_blank\" rel=\"noreferrer noopener\">Github repository<\/a>. In this tutorial, we will use Apache to server the openvpn-monitor OpenVPN connections.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Install Apache for openvpn-monitor<\/h4>\n\n\n\n<p>Install Apache and the required package dependencies;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install git apache2 libapache2-mod-wsgi python3-geoip2 python3-ipaddr python3-humanize python3-bottle python3-semantic-version geoip-database geoipupdate<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Download the openvpn-monitor program<\/h4>\n\n\n\n<p>Next, clone the openvpn-monitor Github repository to your default web server root directory. In this setup, we use, <code><strong>\/var\/www\/html\/openvpn-monitor<\/strong><\/code> directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>git clone https:\/\/github.com\/furlongm\/openvpn-monitor.git \/var\/www\/html\/openvpn-monitor<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls \/var\/www\/html\/openvpn-monitor\/<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>AUTHORS COPYING images MANIFEST.in openvpn-monitor.conf.example openvpn-monitor.py README.md requirements.txt setup.py tests VERSION.txt<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure openvpn-monitor<\/h4>\n\n\n\n<p>Rename the sample configuration file, <code><strong>openvpn-monitor.conf.example<\/strong><\/code>&nbsp;to <strong><code>openvpn-monitor.conf<\/code><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp \/var\/www\/html\/openvpn-monitor\/openvpn-monitor.conf{.example,}<\/code><\/pre>\n\n\n\n<p>Replace the OpenVPN management interface if you changed it to a port other than 5555 defined on the <code><strong>openvpn-monitor.py<\/strong><\/code> Python program.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd \/var\/www\/html\/openvpn-monitor<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>grep -irl 5555 . | xargs -I {} sed -i 's\/5555\/17562\/' {}<\/code><\/pre>\n\n\n\n<p>Next, you can now open the configuration file and set site name, add a logo, set the default map location (latitude and longitude, defaults to New York, USA).<\/p>\n\n\n\n<p>This is how our modified configuration file looks like;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cat \/var\/www\/html\/openvpn-monitor\/openvpn-monitor.conf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;openvpn-monitor]\nsite=Kifarunix-demo-VPN\n#logo=logo.jpg\nlatitude=11.016844\nlongitude=76.955833\nmaps=True\ngeoip_data=\/var\/lib\/GeoIP\/GeoLite2-City.mmdb\ndatetime_format=%d\/%m\/%Y %H:%M:%S\n\n&#91;VPN1]\nhost=localhost\nport=17562\nname=Kifarunix-demo VPN\nshow_disconnect=False<\/code><\/pre>\n\n\n\n<p>Note the GeoIP2 City Database location. If it is not available on your system, register and download a free version from <a href=\"https:\/\/dev.maxmind.com\/geoip\/geoip2\/geolite2\/\" target=\"_blank\" rel=\"noreferrer noopener\">MaxMind<\/a>.<\/p>\n\n\n\n<p>One more thing is that, this setup is tested on Ubuntu 20.04, with python 3 being the default. Since openvpn-monitor uses python, simply create a symbolic link from python 3 to python;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ln -s \/usr\/bin\/python3 \/usr\/bin\/python<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure Apache to Execute openvpn-monitor script<\/h4>\n\n\n\n<p>Next, execute the command below to configure Apache to execute the openvpn-monitor Python script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/apache2\/sites-available\/openvpn-monitor.conf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ScriptAlias \/ \/var\/www\/html\/openvpn-monitor\/openvpn-monitor.py\n&lt;Directory \/var\/www\/html\/openvpn-monitor&gt;\n\tOptions +ExecCGI\n\tAddHandler cgi-script .py\n\tDirectoryIndex openvpn-monitor.py\n\n\tAllowOverride None\n\tRequire ip 192.168.0.0\/16\n&lt;\/Directory&gt;<\/code><\/pre>\n\n\n\n<p>Save and exit the configuration file.<\/p>\n\n\n\n<p>Set the ownership of the openvpn-monitor web root directory to Apache user, <code><strong>www-data<\/strong><\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>chown -R www-data: \/var\/www\/html\/openvpn-monitor\/<\/code><\/pre>\n\n\n\n<p>Check Apache configuration syntax;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apachectl -t<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Syntax OK<\/code><\/pre>\n\n\n\n<p>Enable openvpn-monitor Apache site configuration and disable the default site.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>a2ensite openvpn-monitor.conf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>a2dissite 000-default.conf<\/code><\/pre>\n\n\n\n<p>Start Apache and enable it to run on system boot;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl enable --now apache2<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Debugging openvpn-monitor<\/h4>\n\n\n\n<p>You can run openvpn-monitor from the command line to check if it actually generates the html report correctly:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd \/var\/www\/html\/openvpn-monitor\npython openvpn-monitor.py<\/code><\/pre>\n\n\n\n<p>You can as well add option <code><strong>-d<\/strong><\/code> for debugging;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>python openvpn-monitor.py -d<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"scrollbox\"><code>...\noms.addListener(\"click\", function(marker) {\n   popup.setContent(marker.alt);\n   popup.setLatLng(marker.getLatLng());\n   map.openPopup(popup);\n});\noms.addListener(\"spiderfy\", function(markers) {\n   map.closePopup();\n});\nbounds.extend(centre);\nmap.fitBounds(bounds);\n&lt;\/script&gt;\n&lt;\/div&gt;&lt;\/div&gt;\n&lt;div class=\"well well-sm\"&gt;\nPage automatically reloads every 5 minutes.\nLast update: &lt;b&gt;16\/08\/2020 19:10:26&lt;\/b&gt;&lt;\/div&gt;\n&lt;\/div&gt;&lt;\/body&gt;&lt;\/html&gt;\nDEBUG:\n === begin vpns\n{'VPN1': {'host': 'localhost',\n          'name': 'Staff VPN',\n          'port': '17562',\n          'release': 'OpenVPN 2.4.7 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] '\n                     '[LZ4] [EPOLL] [PKCS11] [MH\/PKTINFO] [AEAD] built on Sep  '\n                     '5 2019',\n          'sessions': {},\n          'show_disconnect': False,\n          'socket_connected': True,\n          'state': {'connected': 'CONNECTED',\n                    'local_ip': IPv4Address('10.8.0.1'),\n                    'mode': 'Server',\n                    'remote_ip': '',\n                    'success': 'SUCCESS',\n                    'up_since': datetime.datetime(2020, 8, 16, 16, 15, 46)},\n          'stats': {'bytesin': 0, 'bytesout': 0, 'nclients': 0},\n          'version': Version('2.4.7')}}\n=== end vpns\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Accessing openvpn-monitor on Browser<\/h3>\n\n\n\n<p>If all is well, you can now access your OpenVPN statistics via the address <code><strong>http:\/\/&lt;OpenVPN-server-Address\/openvpn-monitor<\/strong><\/code> or <strong><code>http:\/\/&lt;OpenVPN-server-Address\/<\/code><\/strong> as per our redirection.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1907\" height=\"920\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/08\/openvpn-monitor.png\" alt=\"monitoring OpenVPN connections using openvpn-monitor tool\" class=\"wp-image-6712\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/08\/openvpn-monitor.png?v=1597609866 1907w, https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/08\/openvpn-monitor-768x371.png?v=1597609866 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/08\/openvpn-monitor-1536x741.png?v=1597609866 1536w\" sizes=\"(max-width: 1907px) 100vw, 1907px\" \/><\/figure>\n\n\n\n<p>And there you go. Beautiful, isn&#8217;t? All credit goes to <a href=\"https:\/\/github.com\/furlongm\/openvpn-monitor\/commits?author=furlongm\" target=\"_blank\" rel=\"noreferrer noopener\">furlongm<\/a>. As you can see, we have connection status for my locally connected OpenVPN clients.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Reference<\/h4>\n\n\n\n<p><a href=\"https:\/\/github.com\/furlongm\/openvpn-monitor#apache\" target=\"_blank\" rel=\"noreferrer noopener\">openvpn-monitor<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Related Tutorials<\/h4>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/install-and-setup-openvpn-server-on-ubuntu-20-04\/\" target=\"_blank\">Install and Setup OpenVPN Server on Ubuntu 20.04<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/configure-openvpn-ldap-based-authentication\/\" target=\"_blank\">Configure OpenVPN LDAP Based Authentication<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/assign-static-ip-addresses-for-openvpn-clients\/\" target=\"_blank\">Assign Static IP Addresses for OpenVPN Clients<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/configure-strongswan-vpn-client-on-ubuntu-18-04-centos-8\/\" target=\"_blank\" rel=\"noreferrer noopener\">Configure strongSwan VPN Client on Ubuntu 18.04\/CentOS 8<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we are going to learn how to monitor OpenVPN connections using openvpn-monitor tool. This tutorial focuses on monitoring OpenVPN connections established using<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,72,282,34,321],"tags":[1915,283,1916,1200],"class_list":["post-6709","post","type-post","status-publish","format-standard","hentry","category-howtos","category-monitoring","category-openvpn","category-security","category-vpn","tag-monitor-openvpn-connections","tag-openvpn","tag-openvpn-monitor","tag-ubuntu-20-04","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6709"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=6709"}],"version-history":[{"count":7,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6709\/revisions"}],"predecessor-version":[{"id":21477,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6709\/revisions\/21477"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=6709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=6709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=6709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}