Annotate

plugins/mod_admin_shell.lua @ 10859:8de0057b4279

mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result Fixes the client pausing for input after output from commands.
author Matthew Wild <mwild1@gmail.com>
date Mon, 01 Jun 2020 16:14:06 +0100
parent 10856:c99711eda0d1
child 10860:934dca972f2c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 -- luacheck: ignore 212/self
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 module:set_global();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 module:depends("admin_socket");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local hostmanager = require "core.hostmanager";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local modulemanager = require "core.modulemanager";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local s2smanager = require "core.s2smanager";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local portmanager = require "core.portmanager";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local helpers = require "util.helpers";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local server = require "net.server";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local st = require "util.stanza";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local _G = _G;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local prosody = _G.prosody;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local unpack = table.unpack or unpack; -- luacheck: ignore 113
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local iterators = require "util.iterators";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local keys, values = iterators.keys, iterators.values;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local jid_bare, jid_split, jid_join = import("util.jid", "bare", "prepped_split", "join");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local set, array = require "util.set", require "util.array";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local cert_verify_identity = require "util.x509".verify_identity;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local envload = require "util.envload".envload;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local envloadfile = require "util.envload".envloadfile;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local has_pposix, pposix = pcall(require, "util.pposix");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local async = require "util.async";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local serialization = require "util.serialization";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local serialize_config = serialization.new ({ fatal = false, unquoted = true});
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local time = require "util.time";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local commands = module:shared("commands")
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local def_env = module:shared("env");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local default_env_mt = { __index = def_env };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local function redirect_output(target, session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local env = setmetatable({ print = session.print }, { __index = function (_, k) return rawget(target, k); end });
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 env.dofile = function(name)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local f, err = envloadfile(name, env);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if not f then return f, err; end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return f();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 return env;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 console = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local runner_callbacks = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 function runner_callbacks:error(err)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 module:log("error", "Traceback[shell]: %s", err);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 self.data.print("Fatal error while running command, it did not complete");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 self.data.print("Error: "..tostring(err));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
64 local function send_repl_output(session, line)
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
65 return session.send(st.stanza("repl-output"):text(tostring(line)));
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 function console:new_session(admin_session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 local session = {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 send = function (t)
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
71 return send_repl_output(admin_session, t);
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 print = function (...)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local t = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 for i=1,select("#", ...) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 t[i] = tostring(select(i, ...));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
78 return send_repl_output(admin_session, table.concat(t, "\t"));
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 serialize = tostring;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 disconnect = function () admin_session:close(); end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 session.env = setmetatable({}, default_env_mt);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 session.thread = async.runner(function (line)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 console:process_line(session, line);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end, runner_callbacks, session);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 -- Load up environment with helper objects
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 for name, t in pairs(def_env) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 if type(t) == "table" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 session.env[name] = setmetatable({ session = session }, { __index = t });
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 session.env.output:configure();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 return session;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 local function handle_line(event)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 local session = event.origin.shell_session;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 if not session then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 session = console:new_session(event.origin);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 event.origin.shell_session = session;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 local line = event.stanza:get_text();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 local useglobalenv;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
110 local result = st.stanza("repl-result");
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 if line:match("^>") then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 line = line:gsub("^>", "");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 useglobalenv = true;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 local command = line:match("^%w+") or line:match("%p");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 if commands[command] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 commands[command](session, line);
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
119 event.origin.send(result);
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 return;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 session.env._ = line;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 if not useglobalenv and commands[line:lower()] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 commands[line:lower()](session, line);
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
128 event.origin.send(result);
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 return;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local chunkname = "=console";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 -- luacheck: ignore 311/err
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 local chunk, err = envload("return "..line, chunkname, env);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 if not chunk then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 chunk, err = envload(line, chunkname, env);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 if not chunk then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 err = err:gsub("^%[string .-%]:%d+: ", "");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 err = err:gsub("^:%d+: ", "");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 err = err:gsub("'<eof>'", "the end of the line");
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
142 result.attr.type = "error";
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
143 result:text("Sorry, I couldn't understand that... "..err);
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
144 event.origin.send(result);
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 return;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 local taskok, message = chunk();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
151 local result = st.stanza("repl-result");
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
152
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 if not message then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 if type(taskok) ~= "string" and useglobalenv then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 taskok = session.serialize(taskok);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 end
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
157 result:text("Result: "..tostring(taskok));
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 elseif (not taskok) and message then
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
159 result.attr.type = "error";
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
160 result:text("Error: "..tostring(message));
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
161 else
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
162 result:text("OK: "..tostring(message));
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
165 event.origin.send(result);
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167
10859
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10856
diff changeset
168 module:hook("admin/repl-input", function (event)
10856
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 local ok, err = pcall(handle_line, event);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 if not ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 event.origin.send(st.stanza("repl-result", { type = "error" }):text(err));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 -- Console commands --
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 -- These are simple commands, not valid standalone in Lua
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 function commands.help(session, data)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 local print = session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 local section = data:match("^help (%w+)");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 if not section then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 print [[Commands are divided into multiple sections. For help on a particular section, ]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 print [[type: help SECTION (for example, 'help c2s'). Sections are: ]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 print [[]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 print [[c2s - Commands to manage local client-to-server sessions]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 print [[s2s - Commands to manage sessions between this server and others]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 print [[http - Commands to inspect HTTP services]] -- XXX plural but there is only one so far
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 print [[module - Commands to load/reload/unload modules/plugins]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 print [[host - Commands to activate, deactivate and list virtual hosts]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 print [[user - Commands to create and delete users, and change their passwords]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 print [[server - Uptime, version, shutting down, etc.]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 print [[port - Commands to manage ports the server is listening on]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 print [[dns - Commands to manage and inspect the internal DNS resolver]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 print [[xmpp - Commands for sending XMPP stanzas]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 print [[debug - Commands for debugging the server]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 print [[config - Reloading the configuration, etc.]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 print [[console - Help regarding the console itself]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 elseif section == "c2s" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 print [[c2s:show(jid) - Show all client sessions with the specified JID (or all if no JID given)]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 print [[c2s:show_insecure() - Show all unencrypted client connections]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 print [[c2s:show_secure() - Show all encrypted client connections]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 print [[c2s:show_tls() - Show TLS cipher info for encrypted sessions]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 print [[c2s:count() - Count sessions without listing them]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 print [[c2s:close(jid) - Close all sessions for the specified JID]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 print [[c2s:closeall() - Close all active c2s connections ]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 elseif section == "s2s" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 print [[s2s:show(domain) - Show all s2s connections for the given domain (or all if no domain given)]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 print [[s2s:show_tls(domain) - Show TLS cipher info for encrypted sessions]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 print [[s2s:close(from, to) - Close a connection from one domain to another]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 print [[s2s:closeall(host) - Close all the incoming/outgoing s2s sessions to specified host]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 elseif section == "http" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 print [[http:list(hosts) - Show HTTP endpoints]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 elseif section == "module" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 print [[module:load(module, host) - Load the specified module on the specified host (or all hosts if none given)]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 print [[module:reload(module, host) - The same, but unloads and loads the module (saving state if the module supports it)]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 print [[module:unload(module, host) - The same, but just unloads the module from memory]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 print [[module:list(host) - List the modules loaded on the specified host]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 elseif section == "host" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 print [[host:activate(hostname) - Activates the specified host]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 print [[host:deactivate(hostname) - Disconnects all clients on this host and deactivates]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 print [[host:list() - List the currently-activated hosts]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 elseif section == "user" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 print [[user:create(jid, password) - Create the specified user account]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 print [[user:password(jid, password) - Set the password for the specified user account]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 print [[user:delete(jid) - Permanently remove the specified user account]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 print [[user:list(hostname, pattern) - List users on the specified host, optionally filtering with a pattern]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 elseif section == "server" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 print [[server:version() - Show the server's version number]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 print [[server:uptime() - Show how long the server has been running]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 print [[server:memory() - Show details about the server's memory usage]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 print [[server:shutdown(reason) - Shut down the server, with an optional reason to be broadcast to all connections]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 elseif section == "port" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 print [[port:list() - Lists all network ports prosody currently listens on]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 print [[port:close(port, interface) - Close a port]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 elseif section == "dns" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 print [[dns:lookup(name, type, class) - Do a DNS lookup]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 print [[dns:addnameserver(nameserver) - Add a nameserver to the list]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 print [[dns:setnameserver(nameserver) - Replace the list of name servers with the supplied one]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 print [[dns:purge() - Clear the DNS cache]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 print [[dns:cache() - Show cached records]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 elseif section == "xmpp" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 print [[xmpp:ping(localhost, remotehost) -- Sends a ping to a remote XMPP server and reports the response]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 elseif section == "config" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 print [[config:reload() - Reload the server configuration. Modules may need to be reloaded for changes to take effect.]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 print [[config:get([host,] option) - Show the value of a config option.]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 elseif section == "stats" then -- luacheck: ignore 542
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 -- TODO describe how stats:show() works
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 elseif section == "debug" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 print [[debug:logevents(host) - Enable logging of fired events on host]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 print [[debug:events(host, event) - Show registered event handlers]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 print [[debug:timers() - Show information about scheduled timers]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 elseif section == "console" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 print [[Hey! Welcome to Prosody's admin console.]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 print [[First thing, if you're ever wondering how to get out, simply type 'quit'.]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 print [[Secondly, note that we don't support the full telnet protocol yet (it's coming)]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 print [[so you may have trouble using the arrow keys, etc. depending on your system.]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 print [[]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 print [[For now we offer a couple of handy shortcuts:]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 print [[!! - Repeat the last command]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 print [[!old!new! - repeat the last command, but with 'old' replaced by 'new']]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 print [[]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 print [[For those well-versed in Prosody's internals, or taking instruction from those who are,]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 print [[you can prefix a command with > to escape the console sandbox, and access everything in]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 print [[the running server. Great fun, but be careful not to break anything :)]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 print [[]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 -- Session environment --
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 -- Anything in def_env will be accessible within the session as a global variable
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 --luacheck: ignore 212/self
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 local serialize_defaults = module:get_option("console_prettyprint_settings", { fatal = false, unquoted = true, maxdepth = 2})
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 def_env.output = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 function def_env.output:configure(opts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 if type(opts) ~= "table" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 opts = { preset = opts };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 if not opts.fallback then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 -- XXX Error message passed to fallback is lost, does it matter?
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 opts.fallback = tostring;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 for k,v in pairs(serialize_defaults) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 if opts[k] == nil then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 opts[k] = v;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 self.session.serialize = serialization.new(opts);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 def_env.server = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 function def_env.server:insane_reload()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 prosody.unlock_globals();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 dofile "prosody"
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 prosody = _G.prosody;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 return true, "Server reloaded";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 function def_env.server:version()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 return true, tostring(prosody.version or "unknown");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 function def_env.server:uptime()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 local t = os.time()-prosody.start_time;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 local seconds = t%60;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 t = (t - seconds)/60;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 local minutes = t%60;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 t = (t - minutes)/60;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 local hours = t%24;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 t = (t - hours)/24;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 local days = t;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 return true, string.format("This server has been running for %d day%s, %d hour%s and %d minute%s (since %s)",
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "",
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 minutes, (minutes ~= 1 and "s") or "", os.date("%c", prosody.start_time));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 function def_env.server:shutdown(reason)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 prosody.shutdown(reason);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 return true, "Shutdown initiated";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 local function human(kb)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 local unit = "K";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 if kb > 1024 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 kb, unit = kb/1024, "M";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 return ("%0.2f%sB"):format(kb, unit);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 function def_env.server:memory()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 if not has_pposix or not pposix.meminfo then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 return true, "Lua is using "..human(collectgarbage("count"));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 local mem, lua_mem = pposix.meminfo(), collectgarbage("count");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 print("Process: "..human((mem.allocated+mem.allocated_mmap)/1024));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 print(" Used: "..human(mem.used/1024).." ("..human(lua_mem).." by Lua)");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 print(" Free: "..human(mem.unused/1024).." ("..human(mem.returnable/1024).." returnable)");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 return true, "OK";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 def_env.module = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 local function get_hosts_set(hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 if type(hosts) == "table" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 if hosts[1] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349 return set.new(hosts);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 elseif hosts._items then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 return hosts;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 elseif type(hosts) == "string" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 return set.new { hosts };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 elseif hosts == nil then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 return set.new(array.collect(keys(prosody.hosts)));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 -- Hosts with a module or all virtualhosts if no module given
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 -- matching modules_enabled in the global section
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 local function get_hosts_with_module(hosts, module)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 local hosts_set = get_hosts_set(hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 / function (host)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 if module then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 -- Module given, filter in hosts with this module loaded
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 if modulemanager.is_loaded(host, module) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 return host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 return nil;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 if not hosts then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374 -- No hosts given, filter in VirtualHosts
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
375 if prosody.hosts[host].type == "local" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 return host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
378 return nil
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 -- No module given, but hosts are, don't filter at all
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 return host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383 end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384 if module and modulemanager.get_module("*", module) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 hosts_set:add("*");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 return hosts_set;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
390 function def_env.module:load(name, hosts, config)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391 hosts = get_hosts_with_module(hosts);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
392
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
393 -- Load the module for each host
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 local ok, err, count, mod = true, nil, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
395 for host in hosts do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 if (not modulemanager.is_loaded(host, name)) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397 mod, err = modulemanager.load(host, name, config);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
398 if not mod then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 ok = false;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 if err == "global-module-already-loaded" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
401 if count > 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 ok, err, count = true, nil, 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 break;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
406 self.session.print(err or "Unknown error loading module");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
407 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
408 count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
409 self.session.print("Loaded for "..mod.module.host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
410 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
411 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
412 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
413
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
414 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
415 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
416
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
417 function def_env.module:unload(name, hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
418 hosts = get_hosts_with_module(hosts, name);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
419
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
420 -- Unload the module for each host
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
421 local ok, err, count = true, nil, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
422 for host in hosts do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
423 if modulemanager.is_loaded(host, name) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
424 ok, err = modulemanager.unload(host, name);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
425 if not ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
426 ok = false;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
427 self.session.print(err or "Unknown error unloading module");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
428 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
429 count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
430 self.session.print("Unloaded from "..host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
431 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
432 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
433 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
434 return ok, (ok and "Module unloaded from "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
435 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
436
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
437 local function _sort_hosts(a, b)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
438 if a == "*" then return true
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
439 elseif b == "*" then return false
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
440 else return a:gsub("[^.]+", string.reverse):reverse() < b:gsub("[^.]+", string.reverse):reverse(); end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
441 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
442
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
443 function def_env.module:reload(name, hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
444 hosts = array.collect(get_hosts_with_module(hosts, name)):sort(_sort_hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
445
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
446 -- Reload the module for each host
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
447 local ok, err, count = true, nil, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
448 for _, host in ipairs(hosts) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
449 if modulemanager.is_loaded(host, name) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
450 ok, err = modulemanager.reload(host, name);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
451 if not ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
452 ok = false;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
453 self.session.print(err or "Unknown error reloading module");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
454 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
455 count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
456 if ok == nil then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
457 ok = true;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
458 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
459 self.session.print("Reloaded on "..host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
460 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
461 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
462 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
463 return ok, (ok and "Module reloaded on "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
464 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
465
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
466 function def_env.module:list(hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
467 hosts = array.collect(set.new({ not hosts and "*" or nil }) + get_hosts_set(hosts)):sort(_sort_hosts);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
468
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
469 local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
470 for _, host in ipairs(hosts) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
471 print((host == "*" and "Global" or host)..":");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
472 local modules = array.collect(keys(modulemanager.get_modules(host) or {})):sort();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
473 if #modules == 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
474 if prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
475 print(" No modules loaded");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
476 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
477 print(" Host not found");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
478 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
479 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
480 for _, name in ipairs(modules) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
481 local status, status_text = modulemanager.get_module(host, name).module:get_status();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
482 local status_summary = "";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
483 if status == "warn" or status == "error" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
484 status_summary = (" (%s: %s)"):format(status, status_text);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
485 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
486 print((" %s%s"):format(name, status_summary));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
487 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
488 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
489 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
490 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
491
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
492 def_env.config = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
493 function def_env.config:load(filename, format)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
494 local config_load = require "core.configmanager".load;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
495 local ok, err = config_load(filename, format);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
496 if not ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
497 return false, err or "Unknown error loading config";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
498 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
499 return true, "Config loaded";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
500 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
501
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
502 function def_env.config:get(host, key)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
503 if key == nil then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
504 host, key = "*", host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
505 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
506 local config_get = require "core.configmanager".get
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
507 return true, serialize_config(config_get(host, key));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
508 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
509
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
510 function def_env.config:reload()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
511 local ok, err = prosody.reload_config();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
512 return ok, (ok and "Config reloaded (you may need to reload modules to take effect)") or tostring(err);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
513 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
514
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
515 local function common_info(session, line)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
516 if session.id then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
517 line[#line+1] = "["..session.id.."]"
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
518 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
519 line[#line+1] = "["..session.type..(tostring(session):match("%x*$")).."]"
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
520 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
521 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
522
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
523 local function session_flags(session, line)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
524 line = line or {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
525 common_info(session, line);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
526 if session.type == "c2s" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
527 local status, priority = "unavailable", tostring(session.priority or "-");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
528 if session.presence then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
529 status = session.presence:get_child_text("show") or "available";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
530 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
531 line[#line+1] = status.."("..priority..")";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
532 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
533 if session.cert_identity_status == "valid" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
534 line[#line+1] = "(authenticated)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
535 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
536 if session.dialback_key then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
537 line[#line+1] = "(dialback)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
538 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
539 if session.external_auth then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
540 line[#line+1] = "(SASL)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
541 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
542 if session.secure then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
543 line[#line+1] = "(encrypted)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
544 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
545 if session.compressed then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
546 line[#line+1] = "(compressed)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
547 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
548 if session.smacks then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
549 line[#line+1] = "(sm)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
550 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
551 if session.ip and session.ip:match(":") then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
552 line[#line+1] = "(IPv6)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
553 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
554 if session.remote then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
555 line[#line+1] = "(remote)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
556 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
557 if session.incoming and session.outgoing then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
558 line[#line+1] = "(bidi)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
559 elseif session.is_bidi or session.bidi_session then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
560 line[#line+1] = "(bidi)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
561 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
562 if session.bosh_version then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
563 line[#line+1] = "(bosh)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
564 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
565 if session.websocket_request then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
566 line[#line+1] = "(websocket)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
567 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
568 return table.concat(line, " ");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
569 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
570
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
571 local function tls_info(session, line)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
572 line = line or {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
573 common_info(session, line);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
574 if session.secure then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
575 local sock = session.conn and session.conn.socket and session.conn:socket();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
576 if sock then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
577 local info = sock.info and sock:info();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
578 if info then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
579 line[#line+1] = ("(%s with %s)"):format(info.protocol, info.cipher);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
580 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
581 -- TLS session might not be ready yet
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
582 line[#line+1] = "(cipher info unavailable)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
583 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
584 if sock.getsniname then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
585 local name = sock:getsniname();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
586 if name then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
587 line[#line+1] = ("(SNI:%q)"):format(name);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
588 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
589 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
590 if sock.getalpn then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
591 local proto = sock:getalpn();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
592 if proto then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
593 line[#line+1] = ("(ALPN:%q)"):format(proto);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
594 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
595 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
596 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
597 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
598 line[#line+1] = "(insecure)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
599 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
600 return table.concat(line, " ");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
601 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
602
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
603 def_env.c2s = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
604
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
605 local function get_jid(session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
606 if session.username then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
607 return session.full_jid or jid_join(session.username, session.host, session.resource);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
608 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
609
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
610 local conn = session.conn;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
611 local ip = session.ip or "?";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
612 local clientport = conn and conn:clientport() or "?";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
613 local serverip = conn and conn.server and conn:server():ip() or "?";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
614 local serverport = conn and conn:serverport() or "?"
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
615 return jid_join("["..ip.."]:"..clientport, session.host or "["..serverip.."]:"..serverport);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
616 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
617
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
618 local function get_c2s()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
619 local c2s = array.collect(values(prosody.full_sessions));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
620 c2s:append(array.collect(values(module:shared"/*/c2s/sessions")));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
621 c2s:append(array.collect(values(module:shared"/*/bosh/sessions")));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
622 c2s:unique();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
623 return c2s;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
624 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
625
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
626 local function show_c2s(callback)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
627 get_c2s():sort(function(a, b)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
628 if a.host == b.host then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
629 if a.username == b.username then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
630 return (a.resource or "") > (b.resource or "");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
631 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
632 return (a.username or "") > (b.username or "");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
633 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
634 return _sort_hosts(a.host or "", b.host or "");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
635 end):map(function (session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
636 callback(get_jid(session), session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
637 end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
638 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
639
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
640 function def_env.c2s:count()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
641 local c2s = get_c2s();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
642 return true, "Total: ".. #c2s .." clients";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
643 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
644
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
645 function def_env.c2s:show(match_jid, annotate)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
646 local print, count = self.session.print, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
647 annotate = annotate or session_flags;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
648 local curr_host = false;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
649 show_c2s(function (jid, session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
650 if curr_host ~= session.host then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
651 curr_host = session.host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
652 print(curr_host or "(not connected to any host yet)");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
653 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
654 if (not match_jid) or jid:match(match_jid) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
655 count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
656 print(annotate(session, { " ", jid }));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
657 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
658 end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
659 return true, "Total: "..count.." clients";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
660 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
661
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
662 function def_env.c2s:show_insecure(match_jid)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
663 local print, count = self.session.print, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
664 show_c2s(function (jid, session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
665 if ((not match_jid) or jid:match(match_jid)) and not session.secure then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
666 count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
667 print(jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
668 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
669 end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
670 return true, "Total: "..count.." insecure client connections";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
671 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
672
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
673 function def_env.c2s:show_secure(match_jid)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
674 local print, count = self.session.print, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
675 show_c2s(function (jid, session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
676 if ((not match_jid) or jid:match(match_jid)) and session.secure then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
677 count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
678 print(jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
679 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
680 end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
681 return true, "Total: "..count.." secure client connections";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
682 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
683
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
684 function def_env.c2s:show_tls(match_jid)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
685 return self:show(match_jid, tls_info);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
686 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
687
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
688 local function build_reason(text, condition)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
689 if text or condition then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
690 return {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
691 text = text,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
692 condition = condition or "undefined-condition",
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
693 };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
694 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
695 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
696
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
697 function def_env.c2s:close(match_jid, text, condition)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
698 local count = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
699 show_c2s(function (jid, session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
700 if jid == match_jid or jid_bare(jid) == match_jid then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
701 count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
702 session:close(build_reason(text, condition));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
703 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
704 end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
705 return true, "Total: "..count.." sessions closed";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
706 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
707
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
708 function def_env.c2s:closeall(text, condition)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
709 local count = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
710 --luacheck: ignore 212/jid
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
711 show_c2s(function (jid, session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
712 count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
713 session:close(build_reason(text, condition));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
714 end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
715 return true, "Total: "..count.." sessions closed";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
716 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
717
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
718
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
719 def_env.s2s = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
720 function def_env.s2s:show(match_jid, annotate)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
721 local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
722 annotate = annotate or session_flags;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
723
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
724 local count_in, count_out = 0,0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
725 local s2s_list = { };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
726
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
727 local s2s_sessions = module:shared"/*/s2s/sessions";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
728 for _, session in pairs(s2s_sessions) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
729 local remotehost, localhost, direction;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
730 if session.direction == "outgoing" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
731 direction = "->";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
732 count_out = count_out + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
733 remotehost, localhost = session.to_host or "?", session.from_host or "?";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
734 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
735 direction = "<-";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
736 count_in = count_in + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
737 remotehost, localhost = session.from_host or "?", session.to_host or "?";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
738 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
739 local sess_lines = { l = localhost, r = remotehost,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
740 annotate(session, { "", direction, remotehost or "?" })};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
741
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
742 if (not match_jid) or remotehost:match(match_jid) or localhost:match(match_jid) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
743 table.insert(s2s_list, sess_lines);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
744 -- luacheck: ignore 421/print
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
745 local print = function (s) table.insert(sess_lines, " "..s); end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
746 if session.sendq then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
747 print("There are "..#session.sendq.." queued outgoing stanzas for this connection");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
748 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
749 if session.type == "s2sout_unauthed" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
750 if session.connecting then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
751 print("Connection not yet established");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
752 if not session.srv_hosts then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
753 if not session.conn then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
754 print("We do not yet have a DNS answer for this host's SRV records");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
755 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
756 print("This host has no SRV records, using A record instead");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
757 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
758 elseif session.srv_choice then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
759 print("We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
760 local srv_choice = session.srv_hosts[session.srv_choice];
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
761 print("Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
762 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
763 elseif session.notopen then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
764 print("The <stream> has not yet been opened");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
765 elseif not session.dialback_key then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
766 print("Dialback has not been initiated yet");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
767 elseif session.dialback_key then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
768 print("Dialback has been requested, but no result received");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
769 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
770 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
771 if session.type == "s2sin_unauthed" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
772 print("Connection not yet authenticated");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
773 elseif session.type == "s2sin" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
774 for name in pairs(session.hosts) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
775 if name ~= session.from_host then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
776 print("also hosts "..tostring(name));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
777 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
778 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
779 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
780 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
781 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
782
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
783 -- Sort by local host, then remote host
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
784 table.sort(s2s_list, function(a,b)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
785 if a.l == b.l then return _sort_hosts(a.r, b.r); end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
786 return _sort_hosts(a.l, b.l);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
787 end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
788 local lasthost;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
789 for _, sess_lines in ipairs(s2s_list) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
790 if sess_lines.l ~= lasthost then print(sess_lines.l); lasthost=sess_lines.l end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
791 for _, line in ipairs(sess_lines) do print(line); end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
792 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
793 return true, "Total: "..count_out.." outgoing, "..count_in.." incoming connections";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
794 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
795
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
796 function def_env.s2s:show_tls(match_jid)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
797 return self:show(match_jid, tls_info);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
798 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
799
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
800 local function print_subject(print, subject)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
801 for _, entry in ipairs(subject) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
802 print(
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
803 (" %s: %q"):format(
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
804 entry.name or entry.oid,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
805 entry.value:gsub("[\r\n%z%c]", " ")
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
806 )
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
807 );
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
808 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
809 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
810
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
811 -- As much as it pains me to use the 0-based depths that OpenSSL does,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
812 -- I think there's going to be more confusion among operators if we
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
813 -- break from that.
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
814 local function print_errors(print, errors)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
815 for depth, t in pairs(errors) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
816 print(
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
817 (" %d: %s"):format(
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
818 depth-1,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
819 table.concat(t, "\n| ")
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
820 )
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
821 );
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
822 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
823 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
824
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
825 function def_env.s2s:showcert(domain)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
826 local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
827 local s2s_sessions = module:shared"/*/s2s/sessions";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
828 local domain_sessions = set.new(array.collect(values(s2s_sessions)))
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
829 /function(session) return (session.to_host == domain or session.from_host == domain) and session or nil; end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
830 local cert_set = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
831 for session in domain_sessions do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
832 local conn = session.conn;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
833 conn = conn and conn:socket();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
834 if not conn.getpeerchain then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
835 if conn.dohandshake then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
836 error("This version of LuaSec does not support certificate viewing");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
837 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
838 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
839 local cert = conn:getpeercertificate();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
840 if cert then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
841 local certs = conn:getpeerchain();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
842 local digest = cert:digest("sha1");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
843 if not cert_set[digest] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
844 local chain_valid, chain_errors = conn:getpeerverification();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
845 cert_set[digest] = {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
846 {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
847 from = session.from_host,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
848 to = session.to_host,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
849 direction = session.direction
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
850 };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
851 chain_valid = chain_valid;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
852 chain_errors = chain_errors;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
853 certs = certs;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
854 };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
855 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
856 table.insert(cert_set[digest], {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
857 from = session.from_host,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
858 to = session.to_host,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
859 direction = session.direction
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
860 });
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
861 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
862 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
863 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
864 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
865 local domain_certs = array.collect(values(cert_set));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
866 -- Phew. We now have a array of unique certificates presented by domain.
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
867 local n_certs = #domain_certs;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
868
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
869 if n_certs == 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
870 return "No certificates found for "..domain;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
871 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
872
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
873 local function _capitalize_and_colon(byte)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
874 return string.upper(byte)..":";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
875 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
876 local function pretty_fingerprint(hash)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
877 return hash:gsub("..", _capitalize_and_colon):sub(1, -2);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
878 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
879
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
880 for cert_info in values(domain_certs) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
881 local certs = cert_info.certs;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
882 local cert = certs[1];
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
883 print("---")
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
884 print("Fingerprint (SHA1): "..pretty_fingerprint(cert:digest("sha1")));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
885 print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
886 local n_streams = #cert_info;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
887 print("Currently used on "..n_streams.." stream"..(n_streams==1 and "" or "s")..":");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
888 for _, stream in ipairs(cert_info) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
889 if stream.direction == "incoming" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
890 print(" "..stream.to.." <- "..stream.from);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
891 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
892 print(" "..stream.from.." -> "..stream.to);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
893 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
894 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
895 print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
896 local chain_valid, errors = cert_info.chain_valid, cert_info.chain_errors;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
897 local valid_identity = cert_verify_identity(domain, "xmpp-server", cert);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
898 if chain_valid then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
899 print("Trusted certificate: Yes");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
900 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
901 print("Trusted certificate: No");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
902 print_errors(print, errors);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
903 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
904 print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
905 print("Issuer: ");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
906 print_subject(print, cert:issuer());
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
907 print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
908 print("Valid for "..domain..": "..(valid_identity and "Yes" or "No"));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
909 print("Subject:");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
910 print_subject(print, cert:subject());
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
911 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
912 print("---");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
913 return ("Showing "..n_certs.." certificate"
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
914 ..(n_certs==1 and "" or "s")
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
915 .." presented by "..domain..".");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
916 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
917
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
918 function def_env.s2s:close(from, to, text, condition)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
919 local print, count = self.session.print, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
920 local s2s_sessions = module:shared"/*/s2s/sessions";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
921
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
922 local match_id;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
923 if from and not to then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
924 match_id, from = from, nil;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
925 elseif not to then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
926 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
927 elseif from == to then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
928 return false, "Both from and to are the same... you can't do that :)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
929 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
930
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
931 for _, session in pairs(s2s_sessions) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
932 local id = session.id or (session.type..tostring(session):match("[a-f0-9]+$"));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
933 if (match_id and match_id == id)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
934 or (session.from_host == from and session.to_host == to) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
935 print(("Closing connection from %s to %s [%s]"):format(session.from_host, session.to_host, id));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
936 (session.close or s2smanager.destroy_session)(session, build_reason(text, condition));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
937 count = count + 1 ;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
938 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
939 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
940 return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
941 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
942
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
943 function def_env.s2s:closeall(host, text, condition)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
944 local count = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
945 local s2s_sessions = module:shared"/*/s2s/sessions";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
946 for _,session in pairs(s2s_sessions) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
947 if not host or session.from_host == host or session.to_host == host then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
948 session:close(build_reason(text, condition));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
949 count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
950 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
951 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
952 if count == 0 then return false, "No sessions to close.";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
953 else return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s"); end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
954 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
955
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
956 def_env.host = {}; def_env.hosts = def_env.host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
957
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
958 function def_env.host:activate(hostname, config)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
959 return hostmanager.activate(hostname, config);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
960 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
961 function def_env.host:deactivate(hostname, reason)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
962 return hostmanager.deactivate(hostname, reason);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
963 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
964
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
965 function def_env.host:list()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
966 local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
967 local i = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
968 local type;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
969 for host, host_session in iterators.sorted_pairs(prosody.hosts, _sort_hosts) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
970 i = i + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
971 type = host_session.type;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
972 if type == "local" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
973 print(host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
974 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
975 type = module:context(host):get_option_string("component_module", type);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
976 if type ~= "component" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
977 type = type .. " component";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
978 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
979 print(("%s (%s)"):format(host, type));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
980 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
981 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
982 return true, i.." hosts";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
983 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
984
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
985 def_env.port = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
986
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
987 function def_env.port:list()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
988 local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
989 local services = portmanager.get_active_services().data;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
990 local n_services, n_ports = 0, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
991 for service, interfaces in iterators.sorted_pairs(services) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
992 n_services = n_services + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
993 local ports_list = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
994 for interface, ports in pairs(interfaces) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
995 for port in pairs(ports) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
996 table.insert(ports_list, "["..interface.."]:"..port);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
997 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
998 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
999 n_ports = n_ports + #ports_list;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1000 print(service..": "..table.concat(ports_list, ", "));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1001 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1002 return true, n_services.." services listening on "..n_ports.." ports";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1003 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1004
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1005 function def_env.port:close(close_port, close_interface)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1006 close_port = assert(tonumber(close_port), "Invalid port number");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1007 local n_closed = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1008 local services = portmanager.get_active_services().data;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1009 for service, interfaces in pairs(services) do -- luacheck: ignore 213
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1010 for interface, ports in pairs(interfaces) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1011 if not close_interface or close_interface == interface then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1012 if ports[close_port] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1013 self.session.print("Closing ["..interface.."]:"..close_port.."...");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1014 local ok, err = portmanager.close(interface, close_port)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1015 if not ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1016 self.session.print("Failed to close "..interface.." "..close_port..": "..err);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1017 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1018 n_closed = n_closed + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1019 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1020 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1021 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1022 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1023 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1024 return true, "Closed "..n_closed.." ports";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1025 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1026
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1027 def_env.muc = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1028
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1029 local console_room_mt = {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1030 __index = function (self, k) return self.room[k]; end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1031 __tostring = function (self)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1032 return "MUC room <"..self.room.jid..">";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1033 end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1034 };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1035
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1036 local function check_muc(jid)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1037 local room_name, host = jid_split(jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1038 if not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1039 return nil, "No such host: "..host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1040 elseif not prosody.hosts[host].modules.muc then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1041 return nil, "Host '"..host.."' is not a MUC service";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1042 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1043 return room_name, host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1044 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1045
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1046 function def_env.muc:create(room_jid, config)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1047 local room_name, host = check_muc(room_jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1048 if not room_name then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1049 return room_name, host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1050 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1051 if not room_name then return nil, host end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1052 if config ~= nil and type(config) ~= "table" then return nil, "Config must be a table"; end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1053 if prosody.hosts[host].modules.muc.get_room_from_jid(room_jid) then return nil, "Room exists already" end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1054 return prosody.hosts[host].modules.muc.create_room(room_jid, config);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1055 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1056
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1057 function def_env.muc:room(room_jid)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1058 local room_name, host = check_muc(room_jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1059 if not room_name then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1060 return room_name, host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1061 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1062 local room_obj = prosody.hosts[host].modules.muc.get_room_from_jid(room_jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1063 if not room_obj then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1064 return nil, "No such room: "..room_jid;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1065 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1066 return setmetatable({ room = room_obj }, console_room_mt);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1067 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1068
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1069 function def_env.muc:list(host)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1070 local host_session = prosody.hosts[host];
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1071 if not host_session or not host_session.modules.muc then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1072 return nil, "Please supply the address of a local MUC component";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1073 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1074 local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1075 local c = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1076 for room in host_session.modules.muc.each_room() do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1077 print(room.jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1078 c = c + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1079 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1080 return true, c.." rooms";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1081 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1082
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1083 local um = require"core.usermanager";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1084
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1085 def_env.user = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1086 function def_env.user:create(jid, password)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1087 local username, host = jid_split(jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1088 if not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1089 return nil, "No such host: "..host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1090 elseif um.user_exists(username, host) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1091 return nil, "User exists";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1092 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1093 local ok, err = um.create_user(username, password, host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1094 if ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1095 return true, "User created";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1096 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1097 return nil, "Could not create user: "..err;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1098 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1099 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1100
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1101 function def_env.user:delete(jid)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1102 local username, host = jid_split(jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1103 if not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1104 return nil, "No such host: "..host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1105 elseif not um.user_exists(username, host) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1106 return nil, "No such user";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1107 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1108 local ok, err = um.delete_user(username, host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1109 if ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1110 return true, "User deleted";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1111 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1112 return nil, "Could not delete user: "..err;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1113 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1114 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1115
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1116 function def_env.user:password(jid, password)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1117 local username, host = jid_split(jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1118 if not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1119 return nil, "No such host: "..host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1120 elseif not um.user_exists(username, host) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1121 return nil, "No such user";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1122 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1123 local ok, err = um.set_password(username, password, host, nil);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1124 if ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1125 return true, "User password changed";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1126 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1127 return nil, "Could not change password for user: "..err;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1128 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1129 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1130
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1131 function def_env.user:list(host, pat)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1132 if not host then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1133 return nil, "No host given";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1134 elseif not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1135 return nil, "No such host";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1136 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1137 local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1138 local total, matches = 0, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1139 for user in um.users(host) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1140 if not pat or user:match(pat) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1141 print(user.."@"..host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1142 matches = matches + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1143 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1144 total = total + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1145 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1146 return true, "Showing "..(pat and (matches.." of ") or "all " )..total.." users";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1147 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1148
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1149 def_env.xmpp = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1150
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1151 local new_id = require "util.id".medium;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1152 function def_env.xmpp:ping(localhost, remotehost, timeout)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1153 localhost = select(2, jid_split(localhost));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1154 remotehost = select(2, jid_split(remotehost));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1155 if not localhost then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1156 return nil, "Invalid sender hostname";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1157 elseif not prosody.hosts[localhost] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1158 return nil, "No such local host";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1159 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1160 if not remotehost then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1161 return nil, "Invalid destination hostname";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1162 elseif prosody.hosts[remotehost] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1163 return nil, "Both hosts are local";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1164 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1165 local iq = st.iq{ from=localhost, to=remotehost, type="get", id=new_id()}
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1166 :tag("ping", {xmlns="urn:xmpp:ping"});
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1167 local time_start = time.now();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1168 local ret, err = async.wait(module:context(localhost):send_iq(iq, nil, timeout));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1169 if ret then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1170 return true, ("pong from %s in %gs"):format(ret.stanza.attr.from, time.now() - time_start);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1171 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1172 return false, tostring(err);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1173 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1174 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1175
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1176 def_env.dns = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1177 local adns = require"net.adns";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1178
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1179 local function get_resolver(session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1180 local resolver = session.dns_resolver;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1181 if not resolver then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1182 resolver = adns.resolver();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1183 session.dns_resolver = resolver;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1184 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1185 return resolver;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1186 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1187
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1188 function def_env.dns:lookup(name, typ, class)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1189 local resolver = get_resolver(self.session);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1190 local ret, err = async.wait(resolver:lookup_promise(name, typ, class));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1191 if ret then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1192 return true, ret;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1193 elseif err then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1194 return false, err;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1195 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1196 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1197
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1198 function def_env.dns:addnameserver(...)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1199 local resolver = get_resolver(self.session);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1200 resolver._resolver:addnameserver(...)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1201 return true
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1202 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1203
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1204 function def_env.dns:setnameserver(...)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1205 local resolver = get_resolver(self.session);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1206 resolver._resolver:setnameserver(...)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1207 return true
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1208 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1209
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1210 function def_env.dns:purge()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1211 local resolver = get_resolver(self.session);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1212 resolver._resolver:purge()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1213 return true
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1214 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1215
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1216 function def_env.dns:cache()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1217 local resolver = get_resolver(self.session);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1218 return true, "Cache:\n"..tostring(resolver._resolver.cache)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1219 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1220
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1221 def_env.http = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1222
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1223 function def_env.http:list(hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1224 local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1225
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1226 for host in get_hosts_set(hosts) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1227 local http_apps = modulemanager.get_items("http-provider", host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1228 if #http_apps > 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1229 local http_host = module:context(host):get_option_string("http_host");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1230 print("HTTP endpoints on "..host..(http_host and (" (using "..http_host.."):") or ":"));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1231 for _, provider in ipairs(http_apps) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1232 local url = module:context(host):http_url(provider.name, provider.default_path);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1233 print("", url);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1234 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1235 print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1236 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1237 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1238
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1239 local default_host = module:get_option_string("http_default_host");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1240 if not default_host then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1241 print("HTTP requests to unknown hosts will return 404 Not Found");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1242 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1243 print("HTTP requests to unknown hosts will be handled by "..default_host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1244 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1245 return true;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1246 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1247
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1248 def_env.debug = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1249
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1250 function def_env.debug:logevents(host)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1251 helpers.log_host_events(host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1252 return true;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1253 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1254
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1255 function def_env.debug:events(host, event)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1256 local events_obj;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1257 if host and host ~= "*" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1258 if host == "http" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1259 events_obj = require "net.http.server"._events;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1260 elseif not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1261 return false, "Unknown host: "..host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1262 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1263 events_obj = prosody.hosts[host].events;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1264 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1265 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1266 events_obj = prosody.events;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1267 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1268 return true, helpers.show_events(events_obj, event);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1269 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1270
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1271 function def_env.debug:timers()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1272 local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1273 local add_task = require"util.timer".add_task;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1274 local h, params = add_task.h, add_task.params;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1275 if h then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1276 print("-- util.timer");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1277 for i, id in ipairs(h.ids) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1278 if not params[id] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1279 print(os.date("%F %T", h.priorities[i]), h.items[id]);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1280 elseif not params[id].callback then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1281 print(os.date("%F %T", h.priorities[i]), h.items[id], unpack(params[id]));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1282 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1283 print(os.date("%F %T", h.priorities[i]), params[id].callback, unpack(params[id]));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1284 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1285 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1286 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1287 if server.event_base then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1288 local count = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1289 for _, v in pairs(debug.getregistry()) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1290 if type(v) == "function" and v.callback and v.callback == add_task._on_timer then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1291 count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1292 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1293 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1294 print(count .. " libevent callbacks");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1295 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1296 if h then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1297 local next_time = h:peek();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1298 if next_time then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1299 return true, os.date("Next event at %F %T (in %%.6fs)", next_time):format(next_time - time.now());
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1300 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1301 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1302 return true;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1303 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1304
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1305 -- COMPAT: debug:timers() was timer:info() for some time in trunk
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1306 def_env.timer = { info = def_env.debug.timers };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1307
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1308 def_env.stats = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1309
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1310 local function format_stat(type, value, ref_value)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1311 ref_value = ref_value or value;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1312 --do return tostring(value) end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1313 if type == "duration" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1314 if ref_value < 0.001 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1315 return ("%g µs"):format(value*1000000);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1316 elseif ref_value < 0.9 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1317 return ("%0.2f ms"):format(value*1000);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1318 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1319 return ("%0.2f"):format(value);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1320 elseif type == "size" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1321 if ref_value > 1048576 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1322 return ("%d MB"):format(value/1048576);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1323 elseif ref_value > 1024 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1324 return ("%d KB"):format(value/1024);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1325 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1326 return ("%d bytes"):format(value);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1327 elseif type == "rate" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1328 if ref_value < 0.9 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1329 return ("%0.2f/min"):format(value*60);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1330 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1331 return ("%0.2f/sec"):format(value);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1332 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1333 return tostring(value);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1334 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1335
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1336 local stats_methods = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1337 function stats_methods:bounds(_lower, _upper)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1338 for _, stat_info in ipairs(self) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1339 local data = stat_info[4];
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1340 if data then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1341 local lower = _lower or data.min;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1342 local upper = _upper or data.max;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1343 local new_data = {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1344 min = lower;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1345 max = upper;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1346 samples = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1347 sample_count = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1348 count = data.count;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1349 units = data.units;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1350 };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1351 local sum = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1352 for _, v in ipairs(data.samples) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1353 if v > upper then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1354 break;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1355 elseif v>=lower then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1356 table.insert(new_data.samples, v);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1357 sum = sum + v;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1358 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1359 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1360 new_data.sample_count = #new_data.samples;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1361 stat_info[4] = new_data;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1362 stat_info[3] = sum/new_data.sample_count;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1363 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1364 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1365 return self;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1366 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1367
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1368 function stats_methods:trim(lower, upper)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1369 upper = upper or (100-lower);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1370 local statistics = require "util.statistics";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1371 for _, stat_info in ipairs(self) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1372 -- Strip outliers
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1373 local data = stat_info[4];
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1374 if data then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1375 local new_data = {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1376 min = statistics.get_percentile(data, lower);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1377 max = statistics.get_percentile(data, upper);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1378 samples = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1379 sample_count = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1380 count = data.count;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1381 units = data.units;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1382 };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1383 local sum = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1384 for _, v in ipairs(data.samples) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1385 if v > new_data.max then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1386 break;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1387 elseif v>=new_data.min then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1388 table.insert(new_data.samples, v);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1389 sum = sum + v;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1390 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1391 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1392 new_data.sample_count = #new_data.samples;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1393 stat_info[4] = new_data;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1394 stat_info[3] = sum/new_data.sample_count;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1395 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1396 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1397 return self;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1398 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1399
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1400 function stats_methods:max(upper)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1401 return self:bounds(nil, upper);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1402 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1403
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1404 function stats_methods:min(lower)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1405 return self:bounds(lower, nil);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1406 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1407
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1408 function stats_methods:summary()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1409 local statistics = require "util.statistics";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1410 for _, stat_info in ipairs(self) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1411 local type, value, data = stat_info[2], stat_info[3], stat_info[4];
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1412 if data and data.samples then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1413 table.insert(stat_info.output, string.format("Count: %d (%d captured)",
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1414 data.count,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1415 data.sample_count
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1416 ));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1417 table.insert(stat_info.output, string.format("Min: %s Mean: %s Max: %s",
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1418 format_stat(type, data.min),
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1419 format_stat(type, value),
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1420 format_stat(type, data.max)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1421 ));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1422 table.insert(stat_info.output, string.format("Q1: %s Median: %s Q3: %s",
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1423 format_stat(type, statistics.get_percentile(data, 25)),
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1424 format_stat(type, statistics.get_percentile(data, 50)),
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1425 format_stat(type, statistics.get_percentile(data, 75))
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1426 ));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1427 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1428 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1429 return self;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1430 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1431
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1432 function stats_methods:cfgraph()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1433 for _, stat_info in ipairs(self) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1434 local name, type, value, data = unpack(stat_info, 1, 4); -- luacheck: ignore 211
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1435 local function print(s)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1436 table.insert(stat_info.output, s);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1437 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1438
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1439 if data and data.sample_count and data.sample_count > 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1440 local raw_histogram = require "util.statistics".get_histogram(data);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1441
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1442 local graph_width, graph_height = 50, 10;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1443 local eighth_chars = " ▁▂▃▄▅▆▇█";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1444
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1445 local range = data.max - data.min;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1446
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1447 if range > 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1448 local x_scaling = #raw_histogram/graph_width;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1449 local histogram = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1450 for i = 1, graph_width do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1451 histogram[i] = math.max(raw_histogram[i*x_scaling-1] or 0, raw_histogram[i*x_scaling] or 0);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1452 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1453
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1454 print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1455 print(("_"):rep(52)..format_stat(type, data.max));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1456 for row = graph_height, 1, -1 do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1457 local row_chars = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1458 local min_eighths, max_eighths = 8, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1459 for i = 1, #histogram do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1460 local char_eighths = math.ceil(math.max(math.min((graph_height/(data.max/histogram[i]))-(row-1), 1), 0)*8);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1461 if char_eighths < min_eighths then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1462 min_eighths = char_eighths;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1463 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1464 if char_eighths > max_eighths then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1465 max_eighths = char_eighths;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1466 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1467 if char_eighths == 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1468 row_chars[i] = "-";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1469 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1470 local char = eighth_chars:sub(char_eighths*3+1, char_eighths*3+3);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1471 row_chars[i] = char;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1472 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1473 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1474 print(table.concat(row_chars).."|-"..format_stat(type, data.max/(graph_height/(row-0.5))));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1475 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1476 print(("\\ "):rep(11));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1477 local x_labels = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1478 for i = 1, 11 do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1479 local s = ("%-4s"):format((i-1)*10);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1480 if #s > 4 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1481 s = s:sub(1, 3).."…";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1482 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1483 x_labels[i] = s;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1484 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1485 print(" "..table.concat(x_labels, " "));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1486 local units = "%";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1487 local margin = math.floor((graph_width-#units)/2);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1488 print((" "):rep(margin)..units);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1489 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1490 print("[range too small to graph]");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1491 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1492 print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1493 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1494 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1495 return self;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1496 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1497
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1498 function stats_methods:histogram()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1499 for _, stat_info in ipairs(self) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1500 local name, type, value, data = unpack(stat_info, 1, 4); -- luacheck: ignore 211
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1501 local function print(s)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1502 table.insert(stat_info.output, s);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1503 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1504
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1505 if not data then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1506 print("[no data]");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1507 return self;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1508 elseif not data.sample_count then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1509 print("[not a sampled metric type]");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1510 return self;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1511 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1512
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1513 local graph_width, graph_height = 50, 10;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1514 local eighth_chars = " ▁▂▃▄▅▆▇█";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1515
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1516 local range = data.max - data.min;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1517
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1518 if range > 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1519 local n_buckets = graph_width;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1520
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1521 local histogram = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1522 for i = 1, n_buckets do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1523 histogram[i] = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1524 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1525 local max_bin_samples = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1526 for _, d in ipairs(data.samples) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1527 local bucket = math.floor(1+(n_buckets-1)/(range/(d-data.min)));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1528 histogram[bucket] = histogram[bucket] + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1529 if histogram[bucket] > max_bin_samples then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1530 max_bin_samples = histogram[bucket];
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1531 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1532 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1533
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1534 print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1535 print(("_"):rep(52)..max_bin_samples);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1536 for row = graph_height, 1, -1 do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1537 local row_chars = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1538 local min_eighths, max_eighths = 8, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1539 for i = 1, #histogram do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1540 local char_eighths = math.ceil(math.max(math.min((graph_height/(max_bin_samples/histogram[i]))-(row-1), 1), 0)*8);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1541 if char_eighths < min_eighths then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1542 min_eighths = char_eighths;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1543 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1544 if char_eighths > max_eighths then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1545 max_eighths = char_eighths;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1546 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1547 if char_eighths == 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1548 row_chars[i] = "-";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1549 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1550 local char = eighth_chars:sub(char_eighths*3+1, char_eighths*3+3);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1551 row_chars[i] = char;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1552 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1553 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1554 print(table.concat(row_chars).."|-"..math.ceil((max_bin_samples/graph_height)*(row-0.5)));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1555 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1556 print(("\\ "):rep(11));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1557 local x_labels = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1558 for i = 1, 11 do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1559 local s = ("%-4s"):format(format_stat(type, data.min+range*i/11, data.min):match("^%S+"));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1560 if #s > 4 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1561 s = s:sub(1, 3).."…";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1562 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1563 x_labels[i] = s;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1564 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1565 print(" "..table.concat(x_labels, " "));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1566 local units = format_stat(type, data.min):match("%s+(.+)$") or data.units or "";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1567 local margin = math.floor((graph_width-#units)/2);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1568 print((" "):rep(margin)..units);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1569 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1570 print("[range too small to graph]");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1571 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1572 print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1573 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1574 return self;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1575 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1576
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1577 local function stats_tostring(stats)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1578 local print = stats.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1579 for _, stat_info in ipairs(stats) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1580 if #stat_info.output > 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1581 print("\n#"..stat_info[1]);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1582 print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1583 for _, v in ipairs(stat_info.output) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1584 print(v);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1585 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1586 print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1587 else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1588 print(("%-50s %s"):format(stat_info[1], format_stat(stat_info[2], stat_info[3])));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1589 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1590 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1591 return #stats.." statistics displayed";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1592 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1593
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1594 local stats_mt = {__index = stats_methods, __tostring = stats_tostring }
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1595 local function new_stats_context(self)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1596 return setmetatable({ session = self.session, stats = true }, stats_mt);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1597 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1598
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1599 function def_env.stats:show(filter)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1600 -- luacheck: ignore 211/changed
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1601 local stats, changed, extra = require "core.statsmanager".get_stats();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1602 local available, displayed = 0, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1603 local displayed_stats = new_stats_context(self);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1604 for name, value in iterators.sorted_pairs(stats) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1605 available = available + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1606 if not filter or name:match(filter) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1607 displayed = displayed + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1608 local type = name:match(":(%a+)$");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1609 table.insert(displayed_stats, {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1610 name, type, value, extra[name];
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1611 output = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1612 });
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1613 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1614 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1615 return displayed_stats;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1616 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1617
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1618
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1619
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1620 -------------
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1621
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1622 function printbanner(session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1623 local option = module:get_option_string("console_banner", "full");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1624 if option == "full" or option == "graphic" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1625 session.print [[
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1626 ____ \ / _
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1627 | _ \ _ __ ___ ___ _-_ __| |_ _
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1628 | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1629 | __/| | | (_) \__ \ |_| | (_| | |_| |
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1630 |_| |_| \___/|___/\___/ \__,_|\__, |
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1631 A study in simplicity |___/
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1632
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1633 ]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1634 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1635 if option == "short" or option == "full" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1636 session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1637 session.print("You may find more help on using this console in our online documentation at ");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1638 session.print("https://prosody.im/doc/console\n");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1639 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1640 if option ~= "short" and option ~= "full" and option ~= "graphic" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1641 session.print(option);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1642 end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1643 end