Change in osmo-sysmon[master]: Add OpenVPN probe

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Thu Jan 31 14:59:55 UTC 2019


Max has uploaded this change for review. ( https://gerrit.osmocom.org/12763


Change subject: Add OpenVPN probe
......................................................................

Add OpenVPN probe

This adds support for OpenVPN status probe which uses OpenVPN's
management interface (configured via 'management 127.0.0.1 1234' in
OpenVPN's config).

The output looks as follows:
...
  OpenVPN
    127.0.0.1:1234
      status: CONNECTED
      tunnel: 10.8.0.15
      remote: 144.76.43.77:1194
    localhost:4242
      status: management interface incompatible
    127.0.0.1:4444
      status: management interface unavailable
...

We show tunnel's IP (if available) as well as remote (OpenVPN server
itself) address/port in addition to general connection status. If
management interface is unavailable it's reported as such. If we've
managed to establish connection with a given management interface but
are unable to obtain expected information than we report this
incompatibility as well.

Related: SYS#2655
Change-Id: I4493e19b9a09dcebd289457eacd1719f7f8cc31c
---
M src/Makefile.am
M src/osysmon.h
M src/osysmon_main.c
A src/osysmon_openvpn.c
4 files changed, 307 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-sysmon refs/changes/63/12763/1

diff --git a/src/Makefile.am b/src/Makefile.am
index e38b70e..f9b79f2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -29,6 +29,7 @@
 
 osmo_sysmon_LDADD = $(LDADD) \
 	$(LIBOSMOVTY_LIBS) \
+	$(LIBOSMONETIF_LIBS) \
 	$(LIBMNL_LIBS) \
 	$(LIBOPING_LIBS) \
 	$(NULL)
@@ -40,6 +41,7 @@
 	osysmon_rtnl.c \
 	osysmon_file.c \
 	osysmon_ping.c \
+	osysmon_openvpn.c \
 	osysmon_main.c \
 	$(NULL)
 
diff --git a/src/osysmon.h b/src/osysmon.h
index df8bf8d..2f82c47 100644
--- a/src/osysmon.h
+++ b/src/osysmon.h
@@ -15,6 +15,8 @@
 	struct rtnl_client_state *rcs;
 	/* list of 'struct ctrl client' */
 	struct llist_head ctrl_clients;
+	/* list of 'struct openvpn_client' */
+	struct llist_head openvpn_clients;
 	/* list of 'struct netdev' */
 	struct llist_head netdevs;
 	/* list of 'struct osysmon_file' */
@@ -30,6 +32,7 @@
 	CTRL_CLIENT_NODE = _LAST_OSMOVTY_NODE + 1,
 	CTRL_CLIENT_GETVAR_NODE,
 	NETDEV_NODE,
+	OPENVPN_NODE,
 	PING_NODE,
 };
 
@@ -48,5 +51,8 @@
 int osysmon_ping_init();
 int osysmon_ping_poll(struct value_node *parent);
 
+int osysmon_openvpn_init();
+int osysmon_openvpn_poll(struct value_node *parent);
+
 int osysmon_file_init();
 int osysmon_file_poll(struct value_node *parent);
diff --git a/src/osysmon_main.c b/src/osysmon_main.c
index eb4f50b..6d72786 100644
--- a/src/osysmon_main.c
+++ b/src/osysmon_main.c
@@ -199,6 +199,7 @@
 
 	g_oss = talloc_zero(NULL, struct osysmon_state);
 	INIT_LLIST_HEAD(&g_oss->ctrl_clients);
+	INIT_LLIST_HEAD(&g_oss->openvpn_clients);
 	INIT_LLIST_HEAD(&g_oss->netdevs);
 	INIT_LLIST_HEAD(&g_oss->files);
 
@@ -206,6 +207,7 @@
 	handle_options(argc, argv);
 	osysmon_sysinfo_init();
 	osysmon_ctrl_init();
+	osysmon_openvpn_init();
 	osysmon_rtnl_init();
 	ping_init = osysmon_ping_init();
 	osysmon_file_init();
@@ -239,9 +241,11 @@
 			osysmon_ping_poll(root);
 
 		osysmon_file_poll(root);
+		osysmon_openvpn_poll(root);
 
 		display_update(root);
 		value_node_del(root);
+		osmo_select_main(0);
 		sleep(1);
 	}
 
diff --git a/src/osysmon_openvpn.c b/src/osysmon_openvpn.c
new file mode 100644
index 0000000..6edb143
--- /dev/null
+++ b/src/osysmon_openvpn.c
@@ -0,0 +1,295 @@
+/* Simple Osmocom System Monitor (osysmon): Support for OpenVPN monitoring */
+
+/* (C) 2019 by sysmocom - s.f.m.c. GmbH.
+ * Author: Max Suraev
+ * All Rights Reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ *  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; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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.
+ */
+
+#include <string.h>
+#include <stdbool.h>
+#include <ctype.h>
+#include <errno.h>
+
+#include <osmocom/core/utils.h>
+#include <osmocom/vty/vty.h>
+#include <osmocom/vty/command.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/netif/stream.h>
+
+#include "osysmon.h"
+#include "client.h"
+#include "value_node.h"
+
+/***********************************************************************
+ * Data model
+ ***********************************************************************/
+
+/* max number of csv in response */
+#define MAX_RESP_COMPONENTS 6
+
+/* a single OpenVPN management interface client */
+struct openvpn_client {
+	/* links to osysmon.openvpn_clients */
+	struct llist_head list;
+	struct host_cfg *cfg;
+	struct osmo_stream_cli *mgmt;
+	/* fields below are parsed from response to 'state' command on mgmt interface */
+	struct host_cfg *rem_cfg;
+	char *tun_ip;
+	bool connected;
+};
+
+static char *parse_state(const struct msgb *msg, struct openvpn_client *vpn)
+{
+	char tmp[128];
+	char *tok;
+	unsigned int i = 0;
+	uint8_t *m = msgb_data(msg);
+
+	if (msgb_length(msg) > 128)
+		fprintf(stderr, "[%s] received message too long (%d > %u), truncating...\n",
+			get_authority(NULL, vpn->cfg), msgb_length(msg), 128);
+
+	if (msgb_length(msg) > 0) {
+		if (!isdigit(m[0])) /* skip OpenVPN greetings and alike */
+			return NULL;
+	} else {
+		fprintf(stderr, "[%s] received message is empty, ignoring...\n", get_authority(NULL, vpn->cfg));
+		return NULL;
+	}
+
+	memcpy(tmp, m, OSMO_MIN(sizeof(tmp) - 1, msgb_length(msg)));
+	tmp[sizeof(tmp) - 1] = '\0';
+
+	for (tok = strtok(tmp, ","); tok && i < MAX_RESP_COMPONENTS; tok = strtok(NULL, ",")) {
+		if (tok)
+			switch (i++) {
+			case 1:
+				update_name(vpn->rem_cfg, tok);
+				break;
+			case 3:
+				osmo_talloc_replace_string(vpn->rem_cfg, &vpn->tun_ip, tok);
+				break;
+			case 4:
+				update_host(vpn->rem_cfg, tok);
+				break;
+			case 5:
+				vpn->rem_cfg->remote_port = atoi(tok);
+				break;
+			}
+	}
+	return NULL;
+}
+
+static struct openvpn_client *openvpn_client_find(const struct osysmon_state *os, const char *host, uint16_t port)
+{
+	struct openvpn_client *vpn;
+	llist_for_each_entry(vpn, &os->openvpn_clients, list) {
+		if (match_config(vpn->cfg, host, MATCH_HOST) && vpn->cfg->remote_port == port)
+			return vpn;
+	}
+
+	return NULL;
+}
+
+static int connect_cb(struct osmo_stream_cli *conn)
+{
+	struct openvpn_client *vpn = osmo_stream_cli_get_data(conn);
+
+	update_name(vpn->rem_cfg, "management interface incompatible");
+	vpn->connected = true; /* FIXME: there's no callback for lost connection to drop this flag */
+
+	return 0;
+}
+
+static int read_cb(struct osmo_stream_cli *conn)
+{
+	int bytes;
+	struct openvpn_client *vpn = osmo_stream_cli_get_data(conn);
+	struct msgb *msg = msgb_alloc(1024, "OpenVPN");
+	if (msg == NULL) {
+		fprintf(stderr, "[%s] unable to allocate message in callback\n", get_authority(NULL, vpn->cfg));
+		return 0;
+	}
+
+	bytes = osmo_stream_cli_recv(conn, msg);
+	if (bytes < 0) {
+		fprintf(stderr, "[%s] unable to receive message in callback\n", get_authority(NULL, vpn->cfg));
+		return 0;
+	}
+
+	parse_state(msg, vpn);
+
+	msgb_free(msg);
+
+	return 0;
+}
+
+static bool openvpn_client_create(struct osysmon_state *os, const char *name, const char *host, uint16_t port)
+{
+	struct openvpn_client *vpn = openvpn_client_find(os, host, port);
+	if (vpn)
+		return true;
+
+	vpn = talloc_zero(os, struct openvpn_client);
+	if (!vpn)
+		return false;
+
+	vpn->connected = false;
+
+	vpn->cfg = make_config(vpn, name, host, port);
+	if (!vpn->cfg) {
+		talloc_free(vpn);
+		return false;
+	}
+
+	vpn->rem_cfg = make_config(vpn, "management interface unavailable", NULL, 0);
+	if (!vpn->cfg) {
+		talloc_free(vpn);
+		return false;
+	}
+
+	vpn->mgmt = make_tcp_client(vpn->cfg);
+	if (!vpn->mgmt)
+	{//FIXME: log error via same macro as ctrl client?
+		talloc_free(vpn);
+		fprintf(stderr, "OpenVPN: ERROR: failed to create TCP client for %s:%u\n", host, port);
+		return false;
+	}
+
+	/* Wait for 1 minute before attempting to reconnect to management interface */
+	osmo_stream_cli_set_reconnect_timeout(vpn->mgmt, 60); /* FIXME: this seems to be ignored by libosmo-netif */
+	osmo_stream_cli_set_read_cb(vpn->mgmt, read_cb);
+	osmo_stream_cli_set_connect_cb(vpn->mgmt, connect_cb);
+
+	if (osmo_stream_cli_open(vpn->mgmt) < 0) {
+		fprintf(stderr, "OpenVPN: ERROR: failed to connect to %s:%u\n", host, port);
+		talloc_free(vpn);
+		return false;
+	}
+
+	osmo_stream_cli_set_data(vpn->mgmt, vpn);
+	llist_add_tail(&vpn->list, &os->openvpn_clients);
+
+	return true;
+}
+
+static void openvpn_client_destroy(struct openvpn_client *vpn)
+{
+	if (!vpn)
+		return;
+
+	osmo_stream_cli_destroy(vpn->mgmt);
+	llist_del(&vpn->list);
+	talloc_free(vpn);
+}
+
+
+/***********************************************************************
+ * VTY
+ ***********************************************************************/
+
+#define OPENVPN_STR "Configure OpenVPN management interface address\n"
+
+DEFUN(cfg_openvpn, cfg_openvpn_cmd,
+      "openvpn HOST <1-65535>",
+      OPENVPN_STR "Name of the host to connect to\n" "Management interface port\n")
+{
+	uint16_t port = atoi(argv[1]);
+
+	if (!openvpn_client_create(g_oss, "OpenVPN", argv[0], port)) {
+		vty_out(vty, "Failed to create OpenVPN client for %s:%u%s", argv[0], port, VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_no_openvpn, cfg_no_openvpn_cmd,
+      "no openvpn HOST <1-65535>",
+      NO_STR OPENVPN_STR "Name of the host to connect to\n" "Management interface port\n")
+{
+	uint16_t port = atoi(argv[1]);
+	struct openvpn_client *vpn = openvpn_client_find(g_oss, argv[0], port);
+	if (!vpn) {
+		vty_out(vty, "OpenVPN client %s:%u doesn't exist%s", argv[0], port, VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+
+	openvpn_client_destroy(vpn);
+
+	return CMD_SUCCESS;
+}
+
+
+/***********************************************************************
+ * Runtime Code
+ ***********************************************************************/
+
+static int openvpn_client_poll(struct openvpn_client *vpn, struct value_node *parent)
+{
+	char *remote = get_authority(parent, vpn->rem_cfg);
+	struct value_node *vn_host = value_node_find_or_add(parent, get_authority(parent, vpn->cfg));
+	struct msgb *msg = msgb_alloc(128, "state");
+	if (!msg) {
+		value_node_add(vn_host, "msgb", "memory allocation failure");
+		return 0;
+	}
+
+	if (vpn->rem_cfg->name)
+		value_node_add(vn_host, "status", vpn->rem_cfg->name);
+
+	if (vpn->tun_ip)
+		value_node_add(vn_host, "tunnel", vpn->tun_ip);
+
+	if (remote)
+		value_node_add(vn_host, "remote", remote);
+
+	/* FIXME: there's no way to check client state so this might be triggered even while it's reconnecting */
+	if (vpn->connected) { /* re-trigger state command */
+		msgb_printf(msg, "state\n");
+		osmo_stream_cli_send(vpn->mgmt, msg);
+	}
+
+	return 0;
+}
+
+/* called once on startup before config file parsing */
+int osysmon_openvpn_init()
+{
+	install_element(CONFIG_NODE, &cfg_openvpn_cmd);
+	install_element(CONFIG_NODE, &cfg_no_openvpn_cmd);
+
+	return 0;
+}
+
+/* called periodically */
+int osysmon_openvpn_poll(struct value_node *parent)
+{
+	struct value_node *vn_vpn;
+	if (llist_count(&g_oss->openvpn_clients)) {
+		struct openvpn_client *vpn;
+		vn_vpn = value_node_add(parent, "OpenVPN", NULL);
+		llist_for_each_entry(vpn, &g_oss->openvpn_clients, list)
+			openvpn_client_poll(vpn, vn_vpn);
+	}
+
+	return 0;
+}

-- 
To view, visit https://gerrit.osmocom.org/12763
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-sysmon
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4493e19b9a09dcebd289457eacd1719f7f8cc31c
Gerrit-Change-Number: 12763
Gerrit-PatchSet: 1
Gerrit-Owner: Max <msuraev at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190131/e4d1baeb/attachment.htm>


More information about the gerrit-log mailing list