Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / prosody / prosody_ @ 26e7ae9e

Historique | Voir | Annoter | Télécharger (7,4 ko)

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Copyright (c) 2010 Christoph Heer (Christoph.Heer@googlemail.com)
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining a
6
# copy of this software and associated documentation files (the \"Software\"),
7
# to deal in the Software without restriction, including without limitation
8
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
# and/or sell copies of the Software, and to permit persons to whom the
10
# Software is furnished to do so, subject to the following conditions:
11
#
12
# The above copyright notice and this permission notice shall be included in
13
# all copies or substantial portions of the Software.
14
#
15
# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
# DEALINGS IN THE SOFTWARE.
22

    
23
import sys
24
import os
25
import telnetlib
26
import re
27

    
28
def main(): 
29
    try:
30
        mode = sys.argv[1]
31
    except IndexError:
32
        mode = ""
33
    wildcard = sys.argv[0].split("prosody_")[1].split("_")[0]
34
    host = os.environ.get('host', 'localhost')
35
    port = int(os.environ.get('port', 5582))
36
    
37
    if mode == "suggest":
38
        print "c2s"
39
        print "s2s"
40
        print "presence"
41
        print "uptime"
42
        print "users"
43
        sys.exit(0)
44

    
45
    if wildcard == "c2s":
46
        if mode == "config":
47
            print "graph_title Prosody C2S Connections"
48
            print "graph_vlabel users"
49
            print "graph_category Prosody"
50
        
51
            print "all_client_connections.label client connections"
52
            print "secure_client_connections.label secure client connections"
53
            print "insecure_client_connections.label insecure client " \
54
                  "connections"
55
            sys.exit(0)
56

    
57
        else:
58
            connection_count_re = re.compile(r"Total:\s(\d+)\s")
59
            telnet = telnetlib.Telnet(host, port)
60
            telnet.write("c2s:show_secure()\n")
61
            telnet_response = telnet.read_until("secure client connections",
62
                                                5)
63
            parsed_info = connection_count_re.findall(telnet_response)
64
            secure_client_connections = int(parsed_info[0])
65
            print "secure_client_connections.value %s" % \
66
                  (secure_client_connections)
67
            
68
            telnet.write("c2s:show_insecure()\n")
69
            telnet_response = telnet.read_until("insecure client connections",
70
                                                5)
71
            parsed_info = connection_count_re.findall(telnet_response)
72
            insecure_client_connections = int(parsed_info[0])
73
            print "insecure_client_connections.value %s" % \
74
                  (insecure_client_connections)
75
            all_client_connections = secure_client_connections + \
76
                                     insecure_client_connections
77
            print "all_client_connections.value %s" % (all_client_connections)
78
            telnet.write("quit")
79
            
80
    elif wildcard == "s2s":
81
        if mode == "config":
82
            print "graph_title Prosody S2S Connections"
83
            print "graph_vlabel servers"
84
            print "graph_category Prosody"
85
        
86
            print "outgoing_connections.label outgoing connections"
87
            print "incoming_connections.label incoming connections"
88
            sys.exit(0)
89
            
90
        else:   
91
            server_connections_re = re.compile(r"(\d+) outgoing, (\d+)")
92
            telnet = telnetlib.Telnet(host, port)
93
            telnet.write("s2s:show()\n")
94
            telnet_response = telnet.read_until("connections", 5)
95
            parsed_info = server_connections_re.findall(telnet_response)
96
            print "outgoing_connections.value %s" % (parsed_info[0][0])
97
            print "incoming_connections.value %s" % (parsed_info[0][1])
98
            telnet.write("quit")
99
            
100
    elif wildcard == "presence":
101
        if mode == "config":
102
            print "graph_title Prosody Client Presence"
103
            print "graph_vlabel clients"
104
            print "graph_category Prosody"
105

    
106
            print "available.label Avaible Clients"
107
            print "chat.label Ready for Chat Clients"
108
            print "away.label Away Clients"
109
            print "xa.label Extended Away Clients"
110
            print "dnd.label Do Not Disturb Clients"
111
            sys.exit(0)
112

    
113
        else:   
114
            client_presence_re = re.compile(r"- (.*?)\(\d+\)")
115
            telnet = telnetlib.Telnet(host, port)
116
            telnet.write("c2s:show()\n")
117
            telnet_response = telnet.read_until("clients", 5)
118
            parsed_info = client_presence_re.findall(telnet_response)
119
            print "available.value %s" % (parsed_info.count("available"))
120
            print "chat.value %s" % (parsed_info.count("chat"))
121
            print "away.value %s" % (parsed_info.count("away"))
122
            print "xa.value %s" % (parsed_info.count("xa"))
123
            print "dnd.value %s" % (parsed_info.count("dnd"))
124
            telnet.write("quit")
125

    
126
    elif wildcard == "uptime":
127
        if mode == "config":
128
            print "graph_title Prosody Uptime"
129
            print "graph_args --base 1000 -l 0"
130
            print "graph_scale no"
131
            print "graph_vlabel uptime in days"
132
            print "graph_category Prosody"
133
            print "graph_order uptime"
134
            print "uptime.draw AREA"
135
            print "uptime.min U"
136
            print "uptime.max U"
137
            print "uptime.label uptime"
138
            print "uptime.type GAUGE"
139
            sys.exit(0)
140

    
141
        else:
142
            uptime_re = re.compile(r"\d+")
143
            telnet = telnetlib.Telnet(host, port)
144
            telnet.write("server:uptime()\n")
145
            telnet_response = telnet.read_until("minutes (", 5)
146
            parsed_info = uptime_re.findall(telnet_response)
147
            uptime_value = float(parsed_info[0]) + float(parsed_info[1])/24 +\
148
                           float(parsed_info[2])/60/24
149
            print "uptime.value %s" % (uptime_value)
150
            telnet.write("quit")
151

    
152
    elif wildcard == "users":
153
        if mode == "config":
154
            print "graph_title Prosody Registered Users"
155
            print "graph_vlabel users"
156
            print "graph_category Prosody"
157

    
158
        base_dir = os.environ.get('internal_storage_path', "/var/lib/prosody")
159
        if os.path.isdir(base_dir):
160
            vhosts = listdirs(base_dir)
161
            for vhost in vhosts:
162
                account_dir = os.path.join(base_dir, vhost, "accounts")
163
                if os.path.isdir(account_dir):
164
                    vhost = vhost.replace("%2e",".")
165
                    munin_var = vhost.replace(".","_")
166
                    if mode == "config":
167
                        print "%s.label %s" % (munin_var, vhost)
168
                    else:
169
                        accounts = len(list(listfiles(account_dir)))
170
                        print "%s.value %s" % (munin_var, accounts)
171

    
172
def listdirs(folder):
173
    for x in os.listdir(folder):
174
        if os.path.isdir(os.path.join(folder, x)):
175
            yield x
176

    
177
def listfiles(folder):
178
    for x in os.listdir(folder):
179
        if os.path.isfile(os.path.join(folder, x)):
180
            yield x
181

    
182
if __name__ == '__main__':
183
    main()