Created
March 15, 2015 10:01
-
-
Save enko/58c24186ecb131095b81 to your computer and use it in GitHub Desktop.
Push prosody infos to zabbix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Copyright (c) 2010 Christoph Heer ([email protected]) | |
# Copyright (c) 2015 Tim Schumacher ([email protected]) | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a | |
# copy of this software and associated documentation files (the \"Software\"), | |
# to deal in the Software without restriction, including without limitation | |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
# and/or sell copies of the Software, and to permit persons to whom the | |
# Software is furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
# DEALINGS IN THE SOFTWARE. | |
import sys | |
import os | |
import telnetlib | |
import re | |
from subprocess import Popen, PIPE, STDOUT | |
import urllib | |
from pprint import pprint | |
config = { | |
'zabbix': { | |
'sender': '/usr/bin/zabbix_sender', | |
'server': 'monitor.int.datenknoten.me', | |
'host' : 'xmpp.int.datenknoten.me' | |
}, | |
'prosody': { | |
'host': 'localhost', | |
'port': 5582 | |
}, | |
'debug' : True | |
} | |
def zabbix_write(item,value): | |
send_str = "%s %s %s" % (config['zabbix']['host'],item,value) | |
proc = Popen([config['zabbix']['sender'],'--zabbix-server',config['zabbix']['server'],'--input-file','-'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) | |
output = proc.communicate(input=send_str)[0] | |
if (config['debug']): | |
print(send_str) | |
print(output) | |
def main(): | |
host = config['prosody']['host'] | |
port = config['prosody']['port'] | |
connection_count_re = re.compile(r"Total:\s(\d+)\s") | |
telnet = telnetlib.Telnet(host, port) | |
telnet.write("c2s:show_secure()\n") | |
telnet_response = telnet.read_until("secure client connections", | |
5) | |
parsed_info = connection_count_re.findall(telnet_response) | |
secure_client_connections = int(parsed_info[0]) | |
telnet.write("c2s:show_insecure()\n") | |
telnet_response = telnet.read_until("insecure client connections", | |
5) | |
parsed_info = connection_count_re.findall(telnet_response) | |
insecure_client_connections = int(parsed_info[0]) | |
all_client_connections = secure_client_connections + \ | |
insecure_client_connections | |
zabbix_write('prosody.users',all_client_connections) | |
server_connections_re = re.compile(r"(\d+) outgoing, (\d+)") | |
telnet.write("s2s:show()\n") | |
telnet_response = telnet.read_until("connections", 5) | |
parsed_info = server_connections_re.findall(telnet_response) | |
zabbix_write('prosody.s2s_incoming',parsed_info[0][1]) | |
zabbix_write('prosody.s2s_outgoing',parsed_info[0][0]) | |
memory_re = re.compile(r"(\d+\.\d\dMB)") | |
telnet.write("server:memory()\n") | |
telnet_response = telnet.read_until("OK: OK", 5) | |
parsed_info = memory_re.findall(telnet_response) | |
total_memory = float(parsed_info[0][:-2]) | |
used_memory = float(parsed_info[1][:-2]) | |
free_memory = float(parsed_info[3][:-2]) | |
zabbix_write('prosody.total_memory',total_memory) | |
zabbix_write('prosody.used_memory',used_memory) | |
zabbix_write('prosody.free_memory',free_memory) | |
base_dir = os.environ.get('internal_storage_path', "/var/lib/prosody") | |
if os.path.isdir(base_dir): | |
vhosts = listdirs(base_dir) | |
for vhost in vhosts: | |
account_dir = os.path.join(base_dir, vhost, "accounts") | |
if os.path.isdir(account_dir): | |
vhost = urllib.unquote(vhost) | |
munin_var = vhost.replace(".","_") | |
accounts = len(list(listfiles(account_dir))) | |
zabbix_write("prosody.%s.users" % (munin_var),accounts) | |
def listdirs(folder): | |
for x in os.listdir(folder): | |
if os.path.isdir(os.path.join(folder, x)): | |
yield x | |
def listfiles(folder): | |
for x in os.listdir(folder): | |
if os.path.isfile(os.path.join(folder, x)): | |
yield x | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment