MXS-1775 Add function for obtaining the server disk space
From 10.1.32, 10.2.14 and 10.3.6 onwards, there is an information_schema plugin called DISKS using which information about the disk space situation on the server can be obtained. Subsequent commits will add configuration options for specifying limits and take the functionality in use in monitors where it makes sense.
This commit is contained in:
parent
4b988d99a1
commit
75b5f57d51
197
include/maxscale/mariadb.hh
Normal file
197
include/maxscale/mariadb.hh
Normal file
@ -0,0 +1,197 @@
|
||||
#pragma once
|
||||
/*
|
||||
* Copyright (c) 2016 MariaDB Corporation Ab
|
||||
*
|
||||
* Use of this software is governed by the Business Source License included
|
||||
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
|
||||
*
|
||||
* Change Date: 2020-01-01
|
||||
*
|
||||
* On the date above, in accordance with the Business Source License, use
|
||||
* of this software will be governed by version 2 or later of the General
|
||||
* Public License.
|
||||
*/
|
||||
|
||||
#include <maxscale/cppdefs.hh>
|
||||
#include <mysql.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace maxscale
|
||||
{
|
||||
|
||||
namespace disk
|
||||
{
|
||||
|
||||
/**
|
||||
* The size information of a particular disk.
|
||||
*/
|
||||
class Sizes
|
||||
{
|
||||
public:
|
||||
Sizes()
|
||||
: m_total(0)
|
||||
, m_used(0)
|
||||
, m_available(0)
|
||||
{
|
||||
}
|
||||
|
||||
Sizes(int64_t total,
|
||||
int64_t used,
|
||||
int64_t available)
|
||||
: m_total(total)
|
||||
, m_used(used)
|
||||
, m_available(available)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* The total size of a disk.
|
||||
*
|
||||
* @return The total size of the disk in bytes.
|
||||
*/
|
||||
int64_t total() const
|
||||
{
|
||||
return m_total;
|
||||
}
|
||||
|
||||
/**
|
||||
* The used amount of space of a disk.
|
||||
*
|
||||
* @return The size of the used amount of space of the disk in bytes.
|
||||
*/
|
||||
int64_t used() const
|
||||
{
|
||||
return m_used;
|
||||
}
|
||||
|
||||
/**
|
||||
* The available amount of space to non-root users.
|
||||
*
|
||||
* @attn As the reported size is what is available to non-root users,
|
||||
* @c available may be smaller than @total - @used.
|
||||
*
|
||||
* @return The size of the available amount of space of the disk in bytes.
|
||||
*/
|
||||
int64_t available() const
|
||||
{
|
||||
return m_available;
|
||||
}
|
||||
|
||||
private:
|
||||
int64_t m_total;
|
||||
int64_t m_used;
|
||||
int64_t m_available;
|
||||
};
|
||||
|
||||
/**
|
||||
* The size information of a particular named disk.
|
||||
*/
|
||||
class SizesAndName : public Sizes
|
||||
{
|
||||
public:
|
||||
SizesAndName()
|
||||
{
|
||||
}
|
||||
|
||||
SizesAndName(int64_t total,
|
||||
int64_t used,
|
||||
int64_t available,
|
||||
const std::string& name)
|
||||
: Sizes(total, used, available)
|
||||
, m_name(name)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The name of the disk. E.g. @c /dev/sda1
|
||||
*/
|
||||
const std::string& name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
};
|
||||
|
||||
/**
|
||||
* The size information of a particular disk, and the paths
|
||||
* on which that disk has been mounted.
|
||||
*/
|
||||
class SizesAndPaths : public Sizes
|
||||
{
|
||||
public:
|
||||
SizesAndPaths()
|
||||
{
|
||||
}
|
||||
|
||||
SizesAndPaths(int64_t total,
|
||||
int64_t used,
|
||||
int64_t available,
|
||||
const std::string& path)
|
||||
: Sizes(total, used, available)
|
||||
{
|
||||
m_paths.push_back(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The paths that referring to the disk for which the size is reported.
|
||||
*/
|
||||
const std::vector<std::string>& paths() const
|
||||
{
|
||||
return m_paths;
|
||||
}
|
||||
|
||||
void add_path(const std::string path)
|
||||
{
|
||||
m_paths.push_back(path);
|
||||
}
|
||||
|
||||
private:
|
||||
int64_t m_total;
|
||||
int64_t m_used;
|
||||
int64_t m_available;
|
||||
std::vector<std::string> m_paths;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get disk space information of a server.
|
||||
*
|
||||
* The information is obtained by accessing the @c information_schema.disks table,
|
||||
* which is available from 10.1.32, 10.2.14 and 10.3.6 onwards.
|
||||
*
|
||||
* @param pMysql A valid handle to some server.
|
||||
* @param pInfo [out] Filled with disk space information, ordered by path.
|
||||
*
|
||||
* @return 0 if successful.
|
||||
*
|
||||
* @attn If the function returns a non-zero value and @c mysql_errno(pMysql)
|
||||
* subsequently returns ER_UNKNOWN_TABLE(1109) then either the server
|
||||
* version is too old or the plugin @c DISKS has not been installed.
|
||||
*/
|
||||
int get_info_by_path(MYSQL* pMysql, std::map<std::string, disk::SizesAndName>* pInfo);
|
||||
|
||||
/**
|
||||
* @brief Get disk space information of a server.
|
||||
*
|
||||
* The information is obtained by accessing the @c information_schema.disks table,
|
||||
* which is available from 10.1.32, 10.2.14 and 10.3.6 onwards.
|
||||
*
|
||||
* @param pMysql A valid handle to some server.
|
||||
* @param pInfo [out] Filled with disk space information, ordered by disk.
|
||||
*
|
||||
* @return 0 if successful.
|
||||
*
|
||||
* @attn If the function returns a non-zero value and @c mysql_errno(pMysql)
|
||||
* subsequently returns ER_UNKNOWN_TABLE(1109) then either the server
|
||||
* version is too old or the plugin @c DISKS has not been installed.
|
||||
*/
|
||||
int get_info_by_disk(MYSQL* pMysql, std::map<std::string, disk::SizesAndPaths>* pInfo);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ add_library(maxscale-common SHARED
|
||||
listener.cc
|
||||
load_utils.cc
|
||||
log_manager.cc
|
||||
mariadb.cc
|
||||
maxscale_pcre2.cc
|
||||
misc.cc
|
||||
mlist.cc
|
||||
|
139
server/core/mariadb.cc
Normal file
139
server/core/mariadb.cc
Normal file
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (c) 2016 MariaDB Corporation Ab
|
||||
*
|
||||
* Use of this software is governed by the Business Source License included
|
||||
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
|
||||
*
|
||||
* Change Date: 2020-01-01
|
||||
*
|
||||
* On the date above, in accordance with the Business Source License, use
|
||||
* of this software will be governed by version 2 or later of the General
|
||||
* Public License.
|
||||
*/
|
||||
|
||||
#include <maxscale/mariadb.hh>
|
||||
#include <maxscale/debug.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using namespace maxscale;
|
||||
|
||||
typedef void (*Callback)(void* pCollection,
|
||||
const char* zDisk,
|
||||
const char* zPath,
|
||||
int64_t total,
|
||||
int64_t used,
|
||||
int64_t available);
|
||||
|
||||
int get_info(MYSQL* pMysql, Callback pCallback, void* pCollection)
|
||||
{
|
||||
int rv = 0;
|
||||
|
||||
rv = mysql_query(pMysql, "SELECT Disk, Path, Total, Used, Available FROM information_schema.disks");
|
||||
|
||||
if (rv == 0)
|
||||
{
|
||||
MYSQL_RES* pResult = mysql_store_result(pMysql);
|
||||
|
||||
if (pResult)
|
||||
{
|
||||
ss_dassert(mysql_field_count(pMysql) == 5);
|
||||
|
||||
MYSQL_ROW row;
|
||||
|
||||
while ((row = mysql_fetch_row(pResult)) != NULL)
|
||||
{
|
||||
ss_debug(char* pEnd);
|
||||
|
||||
int64_t total = strtoll(row[2], &pEnd, 0);
|
||||
ss_dassert(*pEnd == 0);
|
||||
int64_t used = strtoll(row[3], &pEnd, 0);
|
||||
ss_dassert(*pEnd == 0);
|
||||
int64_t available = strtoll(row[4], &pEnd, 0);
|
||||
ss_dassert(*pEnd == 0);
|
||||
|
||||
pCallback(pCollection, row[0], row[1], total, used, available);
|
||||
}
|
||||
|
||||
mysql_free_result(pResult);
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
template<class Collection>
|
||||
inline int get_info(MYSQL* pMysql,
|
||||
void (*pCallback)(Collection* pCollection,
|
||||
const char* zDisk,
|
||||
const char* zPath,
|
||||
int64_t total,
|
||||
int64_t used,
|
||||
int64_t available),
|
||||
Collection* pCollection)
|
||||
{
|
||||
pCollection->clear();
|
||||
|
||||
return get_info(pMysql, reinterpret_cast<Callback>(pCallback), pCollection);
|
||||
}
|
||||
|
||||
void add_info_by_path(std::map<std::string, disk::SizesAndName>* pSizes,
|
||||
const char* zDisk,
|
||||
const char* zPath,
|
||||
int64_t total,
|
||||
int64_t used,
|
||||
int64_t available)
|
||||
{
|
||||
pSizes->insert(std::make_pair(zPath, disk::SizesAndName(total, used, available, zDisk)));
|
||||
}
|
||||
|
||||
void add_info_by_disk(std::map<std::string, disk::SizesAndPaths>* pSizes,
|
||||
const char* zDisk,
|
||||
const char* zPath,
|
||||
int64_t total,
|
||||
int64_t used,
|
||||
int64_t available)
|
||||
{
|
||||
auto i = pSizes->find(zDisk);
|
||||
|
||||
if (i == pSizes->end())
|
||||
{
|
||||
disk::SizesAndPaths& item = i->second;
|
||||
|
||||
ss_dassert(total == item.total());
|
||||
ss_dassert(used == item.used());
|
||||
ss_dassert(available == item.available());
|
||||
|
||||
item.add_path(zPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
disk::SizesAndPaths item(total, used, available, zPath);
|
||||
|
||||
pSizes->insert(std::make_pair(zDisk, item));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
namespace maxscale
|
||||
{
|
||||
|
||||
namespace disk
|
||||
{
|
||||
|
||||
int get_info_by_path(MYSQL* pMysql, std::map<std::string, disk::SizesAndName>* pInfo)
|
||||
{
|
||||
return get_info(pMysql, add_info_by_path, pInfo);
|
||||
}
|
||||
|
||||
int get_info_by_disk(MYSQL* pMysql, std::map<std::string, disk::SizesAndPaths>* pInfo)
|
||||
{
|
||||
return get_info(pMysql, add_info_by_disk, pInfo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user