systemd compliance

+ add possibility to customize db name via config file
+ file path configurations are dealt with dynamically
* at least Debian does not feature a group called nobody it is called nogroup

I am not sure if other systems feature a group called nobody.
This commit is contained in:
nico 2019-12-07 14:48:25 +01:00 committed by Martin/Geno
parent e3e3222f20
commit 9c33cf856b
4 changed files with 20 additions and 10 deletions

View File

@ -4,7 +4,7 @@ Description=ejabberd2influxdb
[Service] [Service]
Type=simple Type=simple
User=nobody User=nobody
Group=nobody Group=nogroup
ExecStart=/opt/ejabberd-metrics/influx.py ExecStart=/opt/ejabberd-metrics/influx.py
Restart=always Restart=always
RestartSec=5s RestartSec=5s

8
ejabberdrpc.py Executable file → Normal file
View File

@ -29,10 +29,16 @@ class EjabberdMetrics():
"Conv6ations for Sum7": ["Conversations with IPv6"], "Conv6ations for Sum7": ["Conversations with IPv6"],
"Conversations": [], "Conversations": [],
"Pix-Art Messenger": [], "Pix-Art Messenger": [],
"Gajim": ["gajim"],
"Psi+": [],
"jitsi": [], "jitsi": [],
"dino": [], "Dino": ["dino"],
"poezio": [], "poezio": [],
"profanity": [],
"Xabber": [],
"ChatSecure": ["chatsecure"]
} }
for client, names in clientmap.items(): for client, names in clientmap.items():
for c in names: for c in names:
if c in resource: if c in resource:

View File

@ -3,6 +3,7 @@
import json import json
import time import time
import os
from influxdb import InfluxDBClient from influxdb import InfluxDBClient
@ -57,7 +58,7 @@ class Influx:
data.append("{m},vhost={vh} registered={v}i {ts}".format(m=name, vh= vhost, v=self._metrics.get_registered(vhost),ts=cur_ts)) data.append("{m},vhost={vh} registered={v}i {ts}".format(m=name, vh= vhost, v=self._metrics.get_registered(vhost),ts=cur_ts))
data.append("{m},vhost={vh} muc={v}i {ts}".format(m=name, vh= vhost, v=self._metrics.get_muc(vhost), ts=cur_ts)) data.append("{m},vhost={vh} muc={v}i {ts}".format(m=name, vh= vhost, v=self._metrics.get_muc(vhost), ts=cur_ts))
# vhost statistics on their repsective node # vhost statistics on their respective node
for node in self._metrics.get_nodes(): for node in self._metrics.get_nodes():
cur_ts = self._timestamp() cur_ts = self._timestamp()
for k, v in self._metrics.get_online_by_status(node=node, vhost=vhost).items(): for k, v in self._metrics.get_online_by_status(node=node, vhost=vhost).items():
@ -73,24 +74,26 @@ class Influx:
data.append(self._parse("ejabberd_online_client_ipversion", k, v, cur_ts, {"vhost": vhost, "node": node, "ipversion": k, "client": cl})) data.append(self._parse("ejabberd_online_client_ipversion", k, v, cur_ts, {"vhost": vhost, "node": node, "ipversion": k, "client": cl}))
# write output to database # write output to database
self.client.write_points(data, database='custom', time_precision='ms', batch_size=10000, protocol='line') self.client.write_points(data, time_precision='ms', batch_size=10000, protocol='line')
if __name__ == "__main__": if __name__ == "__main__":
with open("config.json", "r", encoding="utf-8") as f: # load config
login = json.load(f) path = os.path.dirname(__file__)
with open("/".join([path, "config.json"]), "r", encoding="utf-8") as f:
config = json.load(f)
# init global handler # init global handler
metrics = EjabberdMetrics("http://localhost:4560", login) metrics = EjabberdMetrics("http://localhost:4560", config['login'])
client = InfluxDBClient(host='localhost', port=8086, database='custom') client = InfluxDBClient(host='localhost', port=8086, database=config['database'], retries=5)
# create database only once # create database only once
client.create_database('custom') client.create_database(config['database'])
# init influx class # init influx class
influx = Influx(metrics, client) influx = Influx(metrics, client)
while True: while True:
# TODO this will fail when the connection drops try except maybe?
metrics.update() metrics.update()
influx.writeMetrics() influx.writeMetrics()

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
influxdb>=5.2.0