Newer
Older
nextcloud-monitoring-dashboard / zabbix-agent-scripts / get_db_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 pandas as pd

import mariadb as mdb

import configparser

from functions import get_param

config = configparser.ConfigParser()

config.read("/var/lib/zabbix/.my.cnf")

nc_usr	   = config["client"].get("user")

nc_usr_pwd = config["client"].get("password")

param_file = "/var/lib/zabbix/params.json"

nc_db      = get_param(param_file,"nc_db")

nc_host    = get_param(param_file,"nc_host")

nc_db_port = get_param(param_file,"nc_db_port")

conn = mdb.connect( user=nc_usr,
                    password=nc_usr_pwd,
                    host=nc_host,
                    port=nc_db_port,
                    database=nc_db
                   )

cur = conn.cursor()

cur.execute(
        """
        SELECT TABLE_NAME AS table_name, 
               TABLE_ROWS AS nr_rows,
               (DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024 AS size_mb     
             FROM information_schema.TABLES 
             WHERE TABLE_SCHEMA='%s' 
             ORDER BY size_mb DESC
             LIMIT 5
        """ % nc_db
)

df = pd.DataFrame( cur.fetchall(),
		   columns=["table_name","nr_rows","size_mb"]
		 )

df.to_json( "/var/lib/zabbix/output/db_metrics.json",
			orient='columns')