Changeset

4897:42a362a2bf51

mod_portcheck: Shell command to check if ports are open
author Kim Alvefur <zash@zash.se>
date Fri, 18 Feb 2022 18:35:45 +0100
parents 4896:54fa2116bbf3
children 4898:999e7cb7f6d9
files mod_portcheck/README.md mod_portcheck/mod_portcheck.lua
diffstat 2 files changed, 38 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_portcheck/README.md	Fri Feb 18 18:35:45 2022 +0100
@@ -0,0 +1,20 @@
+This module adds a `portcheck` command to the [shell][doc:console]
+intended for use with health checks, i.e. to check whether Prosody is
+listening to all expected ports.
+
+# Usage
+
+After installing and enabling the module a command like this becomes
+available:
+
+``` bash
+prosodyctl shell "portcheck [::]:5222 *:5222 [::]:5269 *:5269"
+```
+
+This would check if the c2s (`5222`) and s2s (`5269`) ports are
+available on both IPv6 (`*`) and *Legacy IP*^[often referred to as IPv4].
+
+# Compatibility
+
+Compatible with Prosody **trunk**, will **not** work with 0.11.x or
+earlier.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_portcheck/mod_portcheck.lua	Fri Feb 18 18:35:45 2022 +0100
@@ -0,0 +1,18 @@
+module:set_global();
+local portmanager = require "core.portmanager";
+
+local commands = module:shared("admin_shell/commands")
+
+function commands.portcheck(session, line)
+	for desc, interface, port in line:gmatch("%s(%[?([%x:.]+)%]?:(%d+))") do
+		if interface == "*" then
+			interface = "0.0.0.0";
+		end
+		assert(portmanager.get_service_at(interface, tonumber(port)), desc);
+	end
+	session.print "OK";
+end
+
+function module.unload()
+	commands.portcheck = nil;
+end