aboutsummaryrefslogtreecommitdiffstats
path: root/calls.py
blob: 94e8ddeb50372b889e964b4aed0dc89b826e86f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import re

from packaging import version

from api import EjabberdApi

log = logging.getLogger(__name__)


class EjabberdApiCalls(EjabberdApi):
    @property
    def nodename(self):
        if self._login is not None:
            status = self.cmd("status", {})
            # "The node ejabberd@localhost is started with status: startedejabberd 20.07 is running in that node"

            try:
                tmp = status.split()[2]

            except AttributeError:
                # emtpy response or None obj
                log.warning("nodename not found on split")
                raise SystemExit(17)

            except IndexError:
                # status string differs from what we expect
                log.warning("status string is different then expected")
                tmp = "ejabberd@status-string-split-error"
                pass

            # strip double quotations
            if tmp.startswith("'"):
                tmp = tmp.strip("'")

            log.debug(f"fetched node string: {tmp}")
            return tmp

        return None

    @property
    def verstring(self):
        if self._login is not None:
            ver_str = re.compile("([1-9][0-9.]+(?![.a-z]))\\b")
            status = self.cmd("status", {})

            # matches
            try:
                tmp = ver_str.findall(status)[0]
            # raise SystemExit code 17 if no status message is received
            except TypeError:
                log.warning("ver sting not parsed")
                raise SystemExit(17)

            # return parsed version string
            log.debug(f"fetched version string: {tmp}")
            return version.parse(tmp)

        return None

    def fetch_onlineuser(self):
        tmp = self.cmd("connected_users_info", {})
        if "connected_users_info" not in tmp:
            return tmp
        data = []
        for c in tmp["connected_users_info"]:
            if "session" not in c:
                continue
            user = {}
            for attrs in c["session"]:
                for k, v in attrs.items():
                    user[k] = v
            data.append(user)
        return data

    def fetch_nodes(self):
        result = self.cmd("list_cluster", {})
        if "nodes" not in result:
            return result
        data = []
        for node in result["nodes"]:
            data.append(node["node"])
        return data

    def fetch_vhosts(self):
        result = self.cmd("registered_vhosts", {})
        if "vhosts" not in result:
            return result
        data = []
        for vhost in result["vhosts"]:
            data.append(vhost["vhost"])
        return data

    def fetch_s2s_in(self):
        result = self.cmd("incoming_s2s_number", {})
        if isinstance(result, dict):
            if "s2s_incoming" in result:
                return result["s2s_incoming"]
            log.warning("fetch_s2s_in: error empty result " + str(result))
            return 0
        return result

    def fetch_s2s_out(self):
        result = self.cmd("outgoing_s2s_number", {})
        if isinstance(result, dict):
            if "s2s_outgoing" in result:
                return result["s2s_outgoing"]
            log.warning("fetch_s2s_out: error empty result " + str(result))
            return 0
        return result

    def fetch_uptime(self):
        result = self.cmd("stats", {"name": "uptimeseconds"})
        if isinstance(result, dict):
            if "stat" in result:
                return result["stat"]
            log.warning("uptime: error empty result " + str(result))
            return 0
        return result

    def fetch_processes(self):
        result = self.cmd("stats", {"name": "processes"})
        if isinstance(result, dict):
            if "stat" in result:
                return result["stat"]
            log.warning("processes: error empty result " + str(result))
            return 0
        return result

    def fetch_registered_count(self, vhost=None):
        if vhost is None:
            result = self.cmd("stats", {"name": "registeredusers"})
            if "stat" in result:
                return result["stat"]
        else:
            result = self.cmd("stats_host", {"name": "registeredusers", "host": vhost})
            if "stat" in result:
                return result["stat"]

    def fetch_muc_count(self, vhost=None, muc_host="conference"):
        host = "global"
        if vhost is not None:
            if self.verstring.major >= 19:
                host = ".".join([muc_host, vhost])
            else:
                host = vhost
        result = self.cmd("muc_online_rooms", {"host": host})
        if "rooms" in result:
            return len(result["rooms"])
        return len(result)