#
# @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(
["sudo", "-u", "www-data", "/usr/bin/php", "/var/www/nextcloud/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
)
## In some cases assigned_quota get the value of -2 or 'none'. In these cases, we are replacing with 0.
## If the field is not of type string don't do anything.
if df.assigned_quota.dtype.name == 'object':
df["assigned_quota"] = df.assigned_quota.str.replace('none','0').str.replace('-2','0')
# See https://github.com/nextcloud/user_usage_report/issues/200
def trf_to_nr(elem):
if 'GB' in elem:
return int(elem.split()[0])*1024*1024*1024
else:
return int(elem)
df["assigned_quota"] = df.assigned_quota.apply(trf_to_nr)
## 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")
)