infra/roles/nextcloud/templates/nextcloud-influx.py.j2

60 lines
1.5 KiB
Django/Jinja

#!/usr/bin/env python3
import json
import socket
from datetime import datetime
import requests
from requests.auth import HTTPBasicAuth
from influxdb import InfluxDBClient
name = "{{ nextcloud_url }}"
username = "{{ nextcloud_monitor_user }}"
passwd = "{{ nextcloud_monitor_pass }}"
auth = HTTPBasicAuth(username, passwd)
client = InfluxDBClient(
host='{{ influxdb_url }}',
port=443,
username='metrics',
password="{{ influxdb_pass.metrics }}",
ssl=True,
verify_ssl=True,
database='metrics'
)
r = requests.get(
f"https://{name}/ocs/v2.php/apps/serverinfo/api/v1/info?format=json",
auth=auth)
r.raise_for_status()
j = r.json()
data = j['ocs']['data']
nextcloud = j['ocs']['data']['nextcloud']
fields = dict()
tags = dict()
fields['status'] = j['ocs']['meta']['status']
fields['apps_installed'] = nextcloud['system']['apps']['num_installed']
fields['apps_updates_available'] = nextcloud['system']['apps']['num_updates_available']
fields['db_size'] = data['server']['database']['size']
fields['active_users'] = data['activeUsers']['last5minutes']
fields['active_users_24h'] = data['activeUsers']['last24hours']
fields.update(nextcloud['storage'])
fields.update(nextcloud['shares'])
tags['host'] = socket.gethostname()
tags['webserver'] = data['server']['webserver']
tags['php'] = data['server']['php']['version']
tags['name'] = name
datapoints = [{
"measurement": "nextcloud",
"tags": tags,
"time": datetime.utcnow().isoformat(),
"fields": fields
}]
client.write_points(datapoints, time_precision='m')