annotate mod_server_status/mod_server_status.lua @ 564:0b52d82ea0b3

mod_server_status: added http cleanup code from mod_register_json
author Marco Cirillo <maranda@lightwitch.org>
date Tue, 17 Jan 2012 01:40:58 +0000
parents 0afe88f9bdde
children fe85e18a06cb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
1 -- (C) 2011, Marco Cirillo (LW.Org)
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
2 -- Display server stats in readable XML or JSON format
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
3
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
4 module:set_global()
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
5
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
6 local ports = module:get_option_array("server_status_http_ports", {{ port = 5280 }})
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
7 local show_hosts = module:get_option_array("server_status_show_hosts", nil)
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
8 local show_comps = module:get_option_array("server_status_show_comps", nil)
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
9 local json_output = module:get_option_boolean("server_status_json", false)
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
10
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
11 local httpserver = require "net.httpserver"
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
12 local json_encode = require "util.json".encode
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
13
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
14 -- code begin
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
15
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
16 if not prosody.stanza_counter and not show_hosts and not show_comps then
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
17 module:log ("error", "mod_server_status requires at least one of the following things:")
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
18 module:log ("error", "mod_stanza_counter loaded, or either server_status_show_hosts or server_status_show_comps configuration values set.")
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
19 module:log ("error", "check the module wiki at: http://code.google.com/p/prosody-modules/wiki/mod_server_status")
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
20 return false
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
21 end
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
22
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
23 local r_err = "\n<html>\n<head>\n<title>Prosody's Server Status - Error %s</title>\n<meta name=\"robots\" content=\"noindex, nofollow\" />\n</head>\n\n<body>\n<h3>%s</h3>\n</body>\n\n</html>\n"
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
24
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
25 local response_table = {}
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
26 response_table.header = '<?xml version="1.0" encoding="UTF-8" ?>'
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
27 response_table.doc_header = '<document>'
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
28 response_table.doc_closure = '</document>'
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
29 response_table.stanzas = {
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
30 elem_header = ' <stanzas>', elem_closure = ' </stanzas>',
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
31 incoming = ' <incoming iq="%d" message="%d" presence="%d" />',
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
32 outgoing = ' <outgoing iq="%d" message="%d" presence="%d" />'
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
33 }
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
34 response_table.hosts = {
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
35 elem_header = ' <hosts>', elem_closure = ' </hosts>',
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
36 status = ' <status name="%s" current="%s" />'
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
37 }
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
38 response_table.comps = {
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
39 elem_header = ' <components>', elem_closure = ' </components>',
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
40 status = ' <status name="%s" current="%s" />'
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
41 }
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
42
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
43 local function forge_response_xml()
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
44 local hosts_s = {}; local components = {}; local stats = {}; local hosts_stats = {}; local comps_stats = {}
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
45
526
0529658efd1e mod_xml_status: last code cleanup and optimization.
Marco Cirillo <maranda@lightwitch.org>
parents: 525
diff changeset
46 local function t_builder(t,r) for _, bstring in ipairs(t) do r[#r+1] = bstring end end
0529658efd1e mod_xml_status: last code cleanup and optimization.
Marco Cirillo <maranda@lightwitch.org>
parents: 525
diff changeset
47
0529658efd1e mod_xml_status: last code cleanup and optimization.
Marco Cirillo <maranda@lightwitch.org>
parents: 525
diff changeset
48 if show_hosts then t_builder(show_hosts, hosts_s) end
0529658efd1e mod_xml_status: last code cleanup and optimization.
Marco Cirillo <maranda@lightwitch.org>
parents: 525
diff changeset
49 if show_comps then t_builder(show_comps, components) end
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
50
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
51 -- build stanza stats if there
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
52 if prosody.stanza_counter then
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
53 stats[1] = response_table.stanzas.elem_header
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
54 stats[2] = response_table.stanzas.incoming:format(prosody.stanza_counter.iq["incoming"],
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
55 prosody.stanza_counter.message["incoming"],
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
56 prosody.stanza_counter.presence["incoming"])
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
57 stats[3] = response_table.stanzas.outgoing:format(prosody.stanza_counter.iq["outgoing"],
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
58 prosody.stanza_counter.message["outgoing"],
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
59 prosody.stanza_counter.presence["outgoing"])
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
60 stats[4] = response_table.stanzas.elem_closure
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
61 end
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
62
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
63 -- build hosts stats if there
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
64 if hosts_s[1] then
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
65 hosts_stats[1] = response_table.hosts.elem_header
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
66 for _, name in ipairs(hosts_s) do
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
67 hosts_stats[#hosts_stats+1] = response_table.hosts.status:format(
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
68 name, hosts[name] and "online" or "offline")
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
69 end
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
70 hosts_stats[#hosts_stats+1] = response_table.hosts.elem_closure
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
71 end
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
72
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
73 -- build components stats if there
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
74 if components[1] then
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
75 comps_stats[1] = response_table.comps.elem_header
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
76 for _, name in ipairs(components) do
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
77 comps_stats[#comps_stats+1] = response_table.comps.status:format(
546
0afe88f9bdde mod_server_status: fixed trace case when an user, mistakenly or not, sets a component which doesn't exist into the plugin config. (thanks chris)
Marco Cirillo <maranda@lightwitch.org>
parents: 544
diff changeset
78 name, hosts[name] and hosts[name].modules.component and hosts[name].modules.component.connected and "online" or
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
79 hosts[name] and hosts[name].modules.component == nil and "online" or "offline")
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
80 end
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
81 comps_stats[#comps_stats+1] = response_table.comps.elem_closure
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
82 end
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
83
526
0529658efd1e mod_xml_status: last code cleanup and optimization.
Marco Cirillo <maranda@lightwitch.org>
parents: 525
diff changeset
84 -- build xml document
0529658efd1e mod_xml_status: last code cleanup and optimization.
Marco Cirillo <maranda@lightwitch.org>
parents: 525
diff changeset
85 local result = {}
0529658efd1e mod_xml_status: last code cleanup and optimization.
Marco Cirillo <maranda@lightwitch.org>
parents: 525
diff changeset
86 result[#result+1] = response_table.header; result[#result+1] = response_table.doc_header -- start
0529658efd1e mod_xml_status: last code cleanup and optimization.
Marco Cirillo <maranda@lightwitch.org>
parents: 525
diff changeset
87 t_builder(stats, result); t_builder(hosts_stats, result); t_builder(comps_stats, result)
0529658efd1e mod_xml_status: last code cleanup and optimization.
Marco Cirillo <maranda@lightwitch.org>
parents: 525
diff changeset
88 result[#result+1] = response_table.doc_closure -- end
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
89
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
90 return table.concat(result, "\n")
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
91 end
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
92
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
93 local function forge_response_json()
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
94 local result = {}
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
95
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
96 if prosody.stanza_counter then result.stanzas = {} ; result.stanzas = prosody.stanza_counter end
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
97 if show_hosts then
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
98 result.hosts = {}
544
f78a95aa23c4 mod_server_status: corrected mistake in the code.
Marco Cirillo <maranda@lightwitch.org>
parents: 543
diff changeset
99 for _,n in ipairs(show_hosts) do result.hosts[n] = hosts[n] and "online" or "offline" end
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
100 end
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
101 if show_comps then
542
32d9fd110cb1 mod_server_status: corrected error.
Marco Cirillo <maranda@lightwitch.org>
parents: 541
diff changeset
102 result.components = {}
32d9fd110cb1 mod_server_status: corrected error.
Marco Cirillo <maranda@lightwitch.org>
parents: 541
diff changeset
103 for _,n in ipairs(show_comps) do
546
0afe88f9bdde mod_server_status: fixed trace case when an user, mistakenly or not, sets a component which doesn't exist into the plugin config. (thanks chris)
Marco Cirillo <maranda@lightwitch.org>
parents: 544
diff changeset
104 result.components[n] = hosts[n] and hosts[n].modules.component and hosts[n].modules.component.connected and "online" or
544
f78a95aa23c4 mod_server_status: corrected mistake in the code.
Marco Cirillo <maranda@lightwitch.org>
parents: 543
diff changeset
105 hosts[n] and hosts[n].modules.component == nil and "online" or "offline"
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
106 end
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
107 end
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
108
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
109 return json_encode(result)
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
110 end
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
111
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
112 -- http handlers
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
113
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
114 local function response(code, r, h)
532
967ba17b1d2a mod_xml_status: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 527
diff changeset
115 local response = { status = code, body = r }
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
116
532
967ba17b1d2a mod_xml_status: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 527
diff changeset
117 if h then response.headers = h end
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
118 return response
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
119 end
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
120
526
0529658efd1e mod_xml_status: last code cleanup and optimization.
Marco Cirillo <maranda@lightwitch.org>
parents: 525
diff changeset
121 local function request(method, body, request)
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
122 if method == "GET" then
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
123 if not json_output then return response(200, forge_response_xml()) else return response(200, forge_response_json()) end
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
124 else
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
125 local err405 = r_err:format("405", "Only GET is supported")
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
126 return response(405, err405, {["Allow"] = "GET"})
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
127 end
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
128 end
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
129
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
130 -- initialization.
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
131 -- init http interface
564
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
132
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
133 function cleanup() -- handy, recycled from mod_register_json
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
134 module:log("debug", "Cleaning up handlers and stuff as module is being unloaded.")
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
135 for _, options in ipairs(ports) do
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
136 if options.port then
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
137 httpserver.new.http_servers[options.port].handlers[options.path or "server-status"] = nil
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
138 end
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
139 end
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
140
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
141 local event = require "core.configmanager".get("*", "core", "use_libevent");
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
142
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
143 -- if there're no handlers left clean the socket, not sure if it works with server_select
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
144 if not event then
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
145 for _, options in ipairs(ports) do
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
146 if options.port and not next(httpserver.new.http_servers[options.port].handlers) then
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
147 httpserver.new.http_servers[options.port] = nil
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
148 if options.interface then
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
149 for _, value in ipairs(options.interface) do
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
150 if server.getserver(value, options.port) then server.removeserver(value, options.port) end
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
151 end
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
152 else if server.getserver("*", options.port) then server.removeserver("*", options.port) end end
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
153 end
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
154 end
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
155 end
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
156
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
157 prosody.events.remove_handler("module-unloaded", cleanup)
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
158 end
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
159
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
160 local function setup()
541
947103177844 mod_server_status: renamed mod_xml_status to mod_server_status as the option to output the stats in JSON was added.
Marco Cirillo <maranda@lightwitch.org>
parents: 532
diff changeset
161 httpserver.new_from_config(ports, request, { base = "server-status" })
564
0b52d82ea0b3 mod_server_status: added http cleanup code from mod_register_json
Marco Cirillo <maranda@lightwitch.org>
parents: 546
diff changeset
162 prosody.events.add_handler("module-unloaded", cleanup)
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
163 end
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
164
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
165 if prosody.start_time then
532
967ba17b1d2a mod_xml_status: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 527
diff changeset
166 setup()
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
167 else
532
967ba17b1d2a mod_xml_status: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 527
diff changeset
168 module:hook("server-started", setup)
524
ff3ea8735d61 mod_xml_status: initial commit. Expose server status in xml format to be used on external webapps.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
169 end