Mercurial > prosody-modules
comparison mod_http_admin_api/mod_http_admin_api.lua @ 4576:cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Thu, 27 May 2021 16:18:10 +0200 |
parents | d6a3201a65c0 |
children | bd83c9ada037 |
comparison
equal
deleted
inserted
replaced
4575:5b4f43b90766 | 4576:cade5dac1003 |
---|---|
1 local usermanager = require "core.usermanager"; | 1 local usermanager = require "core.usermanager"; |
2 | 2 |
3 local json = require "util.json"; | 3 local json = require "util.json"; |
4 local st = require "util.stanza"; | 4 local st = require "util.stanza"; |
5 local array = require "util.array"; | 5 local array = require "util.array"; |
6 local statsmanager = require "core.statsmanager"; | |
6 | 7 |
7 module:depends("http"); | 8 module:depends("http"); |
8 | 9 |
10 local announce = module:depends("announce"); | |
9 local invites = module:depends("invites"); | 11 local invites = module:depends("invites"); |
10 local tokens = module:depends("tokenauth"); | 12 local tokens = module:depends("tokenauth"); |
11 local mod_pep = module:depends("pep"); | 13 local mod_pep = module:depends("pep"); |
12 local mod_groups = module:depends("groups_internal"); | 14 local mod_groups = module:depends("groups_internal"); |
13 | 15 |
576 site_name = site_name; | 578 site_name = site_name; |
577 version = prosody.version; | 579 version = prosody.version; |
578 }); | 580 }); |
579 end | 581 end |
580 | 582 |
583 local function maybe_export_plain_gauge(mf) | |
584 if mf == nil then | |
585 return nil | |
586 end | |
587 return mf.data.value | |
588 end | |
589 | |
590 local function maybe_export_plain_counter(mf) | |
591 if mf == nil then | |
592 return nil | |
593 end | |
594 return { | |
595 since = mf.data._created, | |
596 value = mf.data.value, | |
597 } | |
598 end | |
599 | |
600 local function maybe_export_summed_gauge(mf) | |
601 if mf == nil then | |
602 return nil | |
603 end | |
604 local sum = 0; | |
605 for _, metric in mf:iter_metrics() do | |
606 sum = sum + metric.value; | |
607 end | |
608 return sum; | |
609 end | |
610 | |
611 local function get_server_metrics(event) | |
612 event.response.headers["Content-Type"] = json_content_type; | |
613 local result = {}; | |
614 local families = statsmanager.get_metric_registry():get_metric_families(); | |
615 result.memory = maybe_export_plain_gauge(families.process_resident_memory_bytes); | |
616 result.cpu = maybe_export_plain_counter(families.process_cpu_seconds); | |
617 result.c2s = maybe_export_summed_gauge(families["prosody_mod_c2s/connections"]) | |
618 return json.encode(result); | |
619 end | |
620 | |
621 local function post_server_announcement(event) | |
622 local request = event.request; | |
623 if request.headers.content_type ~= json_content_type | |
624 or (not request.body or #request.body == 0) then | |
625 return 400; | |
626 end | |
627 local body = json.decode(event.request.body); | |
628 if not body then | |
629 return 400; | |
630 end | |
631 | |
632 if type(body.recipients) ~= "table" and body.recipients ~= "online" and body.recipients ~= "all" then | |
633 return 400; | |
634 end | |
635 | |
636 if not body.body or #body.body == 0 then | |
637 return 400; | |
638 end | |
639 | |
640 local message = st.message():tag("body"):text(body.body):up(); | |
641 local host = module.host | |
642 message.attr.from = host | |
643 if body.recipients == "online" then | |
644 announce.send_to_online(message, host); | |
645 elseif body.recipients == "all" then | |
646 for username in usermanager.users(host) do | |
647 message.attr.to = username .. "@" .. host | |
648 module:send(st.clone(message)) | |
649 end | |
650 else | |
651 for _, addr in ipairs(body.recipients) do | |
652 message.attr.to = addr | |
653 module:send(message) | |
654 end | |
655 end | |
656 | |
657 return 201; | |
658 end | |
659 | |
581 module:provides("http", { | 660 module:provides("http", { |
582 route = check_auth { | 661 route = check_auth { |
583 ["GET /invites"] = list_invites; | 662 ["GET /invites"] = list_invites; |
584 ["GET /invites/*"] = get_invite_by_id; | 663 ["GET /invites/*"] = get_invite_by_id; |
585 ["POST /invites/*"] = create_invite_type; | 664 ["POST /invites/*"] = create_invite_type; |
595 ["POST /groups"] = create_group; | 674 ["POST /groups"] = create_group; |
596 ["PUT /groups/*"] = update_group; | 675 ["PUT /groups/*"] = update_group; |
597 ["DELETE /groups/*"] = delete_group; | 676 ["DELETE /groups/*"] = delete_group; |
598 | 677 |
599 ["GET /server/info"] = get_server_info; | 678 ["GET /server/info"] = get_server_info; |
679 | |
680 ["GET /server/metrics"] = get_server_metrics; | |
681 ["POST /server/announcement"] = post_server_announcement; | |
600 }; | 682 }; |
601 }); | 683 }); |