Newer
Older
nextcloud-monitoring-dashboard / zabbix-agent-scripts / get_storage_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 json

import io

import pandas as pd


datadir = subprocess.check_output(
				  ["sudo", "-u", "www-data", "/usr/bin/php", "/var/www/nextcloud/occ", "config:system:get", "datadirectory"]
                                 ).strip(b"\n").decode()

total_size = subprocess.check_output(
                                     ["du","-b", "-d0", datadir]
                                    ).split(b"\t")[0].decode()

gb_conversion_factor = 1./(1024.*1024.*1024.)

datadirectory_total_size_gb = int(total_size)*gb_conversion_factor

df_command_output = subprocess.check_output(
                                            ["df", "-B1", "--output=size,avail,pcent", datadir]
                                           ).strip(b"\n").decode()

df =  pd.read_csv(
				  io.StringIO(df_command_output),
				  sep="\s+",
				  names=["total","avail","pused"],
				  skiprows=1
				 )

partition_total_capacity_gb = (df.total.values[0])*gb_conversion_factor

partition_available_capacity_gb = (df.avail.values[0])*gb_conversion_factor

partition_used_capacity_percent = int(df.pused.values[0].strip('%'))

storage_metrics = { 
                   "datadirectory_total_size_gb": datadirectory_total_size_gb,
                   "partition_total_capacity_gb":     partition_total_capacity_gb,
                   "partition_available_capacity_gb": partition_available_capacity_gb,
                   "partition_used_capacity_percent": partition_used_capacity_percent
                  }

with open("/var/lib/zabbix/output/storage_metrics.json","w") as fl:
    
    json.dump(storage_metrics,fl)