Newer
Older
nextcloud-monitoring-dashboard / zabbix-agent-scripts / get_nc_quota_metrics.py
#
# @copyright Copyright (c) 2024, Pietro Marini (pmarini@rcasys.com)
#
# @license GNU AGPL version 3 or any later version
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import subprocess

import pandas as pd

import io

import json

## The command usage-report:generate is provided by the app
## user_usage_report, that must be installed and enabled in
## the system.
## JSON output would be easier to parse but it actually isn't
## as the JSON file created is not correctly formatted as a JSON
## file.
result_set = subprocess.check_output(
                                     ["occ", "usage-report:generate"]
                                    )

result_set = result_set.decode()

column_names = [
                "userid","extract_date","assigned_quota", "used_quota",
                "files", "shares","uploads", "downloads"
               ]

df = pd.read_csv(
                 io.StringIO(result_set),
                 names=column_names
                )

## Transformation to int is necessary as json library cannot serialize numpy
## data types
results = {
            "total_number_of_users": int(df.userid.count()),
            "total_assigned_quota_gb": df.assigned_quota.agg("sum")/1024/1024/1024,
            "total_used_quota_gb": df.used_quota.agg("sum")/1024/1024/1024
          }

json.dump(results,
          open("/var/lib/zabbix/output/nc_quota_metrics.json","w")
         )