MXS-1775 Add helper function for parsing disk_space_threshold
The function will be used by the functions
server_set_disk_space_threshold() and
monitor_set_disk_space_threshold()
that will be introduced.
This commit is contained in:
@ -28,6 +28,15 @@
|
|||||||
#include <maxscale/pcre2.h>
|
#include <maxscale/pcre2.h>
|
||||||
#include <maxscale/query_classifier.h>
|
#include <maxscale/query_classifier.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#include <tr1/unordered_map>
|
||||||
|
#include <string>
|
||||||
|
// A mapping from a path to a percentage, e.g.: "/disk" -> 80.
|
||||||
|
typedef std::tr1::unordered_map<std::string, int32_t> MxsDiskSpaceThreshold;
|
||||||
|
#else
|
||||||
|
typedef void MxsDiskSpaceThreshold;
|
||||||
|
#endif
|
||||||
|
|
||||||
MXS_BEGIN_DECLS
|
MXS_BEGIN_DECLS
|
||||||
|
|
||||||
/** Default port where the REST API listens */
|
/** Default port where the REST API listens */
|
||||||
@ -102,6 +111,7 @@ extern const char CN_DUMP_LAST_STATEMENTS[];
|
|||||||
extern const char CN_DATA[];
|
extern const char CN_DATA[];
|
||||||
extern const char CN_DEFAULT[];
|
extern const char CN_DEFAULT[];
|
||||||
extern const char CN_DESCRIPTION[];
|
extern const char CN_DESCRIPTION[];
|
||||||
|
extern const char CN_DISK_SPACE_THRESHOLD[];
|
||||||
extern const char CN_ENABLE_ROOT_USER[];
|
extern const char CN_ENABLE_ROOT_USER[];
|
||||||
extern const char CN_FILTERS[];
|
extern const char CN_FILTERS[];
|
||||||
extern const char CN_FILTER[];
|
extern const char CN_FILTER[];
|
||||||
@ -545,4 +555,16 @@ uint32_t config_writeq_high_water();
|
|||||||
*/
|
*/
|
||||||
uint32_t config_writeq_low_water();
|
uint32_t config_writeq_low_water();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Interpret a @disk_space_threshold configuration string.
|
||||||
|
*
|
||||||
|
* @param disk_space_threshold Data structure for holding disk space configuration.
|
||||||
|
* @param config_value Configuration value from the configuration file.
|
||||||
|
*
|
||||||
|
* @return True, if @ config_value was valid, false otherwise.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
bool config_parse_disk_space_threshold(MxsDiskSpaceThreshold* disk_space_threshold,
|
||||||
|
const char* config_value);
|
||||||
|
|
||||||
MXS_END_DECLS
|
MXS_END_DECLS
|
||||||
|
|||||||
@ -33,25 +33,26 @@
|
|||||||
|
|
||||||
#include <maxscale/adminusers.h>
|
#include <maxscale/adminusers.h>
|
||||||
#include <maxscale/alloc.h>
|
#include <maxscale/alloc.h>
|
||||||
|
#include <maxscale/clock.h>
|
||||||
#include <maxscale/housekeeper.h>
|
#include <maxscale/housekeeper.h>
|
||||||
|
#include <maxscale/http.hh>
|
||||||
|
#include <maxscale/json_api.h>
|
||||||
#include <maxscale/limits.h>
|
#include <maxscale/limits.h>
|
||||||
#include <maxscale/log_manager.h>
|
#include <maxscale/log_manager.h>
|
||||||
|
#include <maxscale/maxscale.h>
|
||||||
|
#include <maxscale/paths.h>
|
||||||
#include <maxscale/pcre2.h>
|
#include <maxscale/pcre2.h>
|
||||||
|
#include <maxscale/router.h>
|
||||||
#include <maxscale/spinlock.h>
|
#include <maxscale/spinlock.h>
|
||||||
#include <maxscale/utils.h>
|
#include <maxscale/utils.h>
|
||||||
#include <maxscale/paths.h>
|
#include <maxscale/utils.hh>
|
||||||
#include <maxscale/router.h>
|
|
||||||
#include <maxscale/json_api.h>
|
|
||||||
#include <maxscale/http.hh>
|
|
||||||
#include <maxscale/version.h>
|
#include <maxscale/version.h>
|
||||||
#include <maxscale/maxscale.h>
|
|
||||||
#include <maxscale/clock.h>
|
|
||||||
|
|
||||||
#include "internal/config.h"
|
#include "internal/config.h"
|
||||||
#include "internal/filter.h"
|
#include "internal/filter.h"
|
||||||
#include "internal/service.h"
|
|
||||||
#include "internal/monitor.h"
|
|
||||||
#include "internal/modules.h"
|
#include "internal/modules.h"
|
||||||
|
#include "internal/monitor.h"
|
||||||
|
#include "internal/service.h"
|
||||||
|
|
||||||
using std::set;
|
using std::set;
|
||||||
using std::string;
|
using std::string;
|
||||||
@ -81,6 +82,7 @@ const char CN_CONNECTION_TIMEOUT[] = "connection_timeout";
|
|||||||
const char CN_DATA[] = "data";
|
const char CN_DATA[] = "data";
|
||||||
const char CN_DEFAULT[] = "default";
|
const char CN_DEFAULT[] = "default";
|
||||||
const char CN_DESCRIPTION[] = "description";
|
const char CN_DESCRIPTION[] = "description";
|
||||||
|
const char CN_DISK_SPACE_THRESHOLD[] = "disk_space_threshold";
|
||||||
const char CN_DUMP_LAST_STATEMENTS[] = "dump_last_statements";
|
const char CN_DUMP_LAST_STATEMENTS[] = "dump_last_statements";
|
||||||
const char CN_ENABLE_ROOT_USER[] = "enable_root_user";
|
const char CN_ENABLE_ROOT_USER[] = "enable_root_user";
|
||||||
const char CN_FILTERS[] = "filters";
|
const char CN_FILTERS[] = "filters";
|
||||||
@ -4645,3 +4647,75 @@ static uint64_t get_suffixed_size(const char* value)
|
|||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool config_parse_disk_space_threshold(MxsDiskSpaceThreshold* pDisk_space_threshold,
|
||||||
|
const char* zDisk_space_threshold)
|
||||||
|
{
|
||||||
|
ss_dassert(pDisk_space_threshold);
|
||||||
|
ss_dassert(zDisk_space_threshold);
|
||||||
|
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
MxsDiskSpaceThreshold disk_space_threshold;
|
||||||
|
string s(zDisk_space_threshold);
|
||||||
|
|
||||||
|
// Somewhat simplified, this is what we expect: [^:]+:[:digit:]+(,[^:]+:[:digit:]+)*
|
||||||
|
// So, e.g. the following are fine "/data:20", "/data1:50,/data2:60", "*:80".
|
||||||
|
|
||||||
|
while (success && !s.empty())
|
||||||
|
{
|
||||||
|
size_t i = s.find_first_of(',');
|
||||||
|
string entry = s.substr(0, i);
|
||||||
|
|
||||||
|
s.erase(0, i != string::npos ? i + 1 : i);
|
||||||
|
|
||||||
|
size_t j = entry.find_first_of(':');
|
||||||
|
|
||||||
|
if (j != string::npos)
|
||||||
|
{
|
||||||
|
string path = entry.substr(0, j);
|
||||||
|
string tail = entry.substr(j + 1);
|
||||||
|
|
||||||
|
mxs::trim(path);
|
||||||
|
mxs::trim(tail);
|
||||||
|
|
||||||
|
if (!path.empty() && !tail.empty())
|
||||||
|
{
|
||||||
|
char* end;
|
||||||
|
int32_t percentage = strtol(tail.c_str(), &end, 0);
|
||||||
|
|
||||||
|
if ((*end == 0) && (percentage >= 0) && (percentage <= 100))
|
||||||
|
{
|
||||||
|
disk_space_threshold[path] = percentage;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MXS_ERROR("The value following the ':' must be a percentage: %s",
|
||||||
|
entry.c_str());
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MXS_ERROR("The %s parameter '%s' contains an invalid entry: '%s'",
|
||||||
|
CN_DISK_SPACE_THRESHOLD, zDisk_space_threshold, entry.c_str());
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MXS_ERROR("The %s parameter '%s' contains an invalid entry: '%s'",
|
||||||
|
CN_DISK_SPACE_THRESHOLD, zDisk_space_threshold, entry.c_str());
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
pDisk_space_threshold->swap(disk_space_threshold);
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|||||||
@ -16,8 +16,12 @@
|
|||||||
#define SS_DEBUG
|
#define SS_DEBUG
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <maxscale/log_manager.h>
|
||||||
#include "../config.cc"
|
#include "../config.cc"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
#define TEST(a) do{if(!(a)){printf("Error: `" #a "` was not true\n");return 1;}}while(false)
|
#define TEST(a) do{if(!(a)){printf("Error: `" #a "` was not true\n");return 1;}}while(false)
|
||||||
|
|
||||||
int test_validity()
|
int test_validity()
|
||||||
@ -213,13 +217,174 @@ int test_required_parameters()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
struct DISK_SPACE_THRESHOLD_RESULT
|
||||||
|
{
|
||||||
|
const char* zPath;
|
||||||
|
int32_t size;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DISK_SPACE_THRESHOLD_TEST
|
||||||
|
{
|
||||||
|
const char* zValue;
|
||||||
|
bool valid;
|
||||||
|
DISK_SPACE_THRESHOLD_RESULT results[5];
|
||||||
|
};
|
||||||
|
|
||||||
|
int dst_report(const DISK_SPACE_THRESHOLD_TEST& test,
|
||||||
|
bool parsed,
|
||||||
|
MxsDiskSpaceThreshold& result)
|
||||||
|
{
|
||||||
|
int nErrors = 0;
|
||||||
|
|
||||||
|
cout << test.zValue << endl;
|
||||||
|
|
||||||
|
if (test.valid)
|
||||||
|
{
|
||||||
|
if (parsed)
|
||||||
|
{
|
||||||
|
const DISK_SPACE_THRESHOLD_RESULT* pResult = test.results;
|
||||||
|
|
||||||
|
while (pResult->zPath)
|
||||||
|
{
|
||||||
|
auto i = result.find(pResult->zPath);
|
||||||
|
|
||||||
|
if (i != result.end())
|
||||||
|
{
|
||||||
|
result.erase(i);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << "error: Expected " << pResult->zPath << " to be found, but it wasn't." << endl;
|
||||||
|
++nErrors;
|
||||||
|
}
|
||||||
|
|
||||||
|
++pResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.size() != 0)
|
||||||
|
{
|
||||||
|
for (auto i = result.begin(); i != result.end(); ++i)
|
||||||
|
{
|
||||||
|
cout << "error: " << i->first << " was found, although not expected." << endl;
|
||||||
|
++nErrors;
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << "error: Expected value to be parsed, but it wasn't." << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (parsed)
|
||||||
|
{
|
||||||
|
cout << "error: Expected value not to be parsed, but it was." << endl;
|
||||||
|
++nErrors;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nErrors == 0)
|
||||||
|
{
|
||||||
|
cout << "OK, ";
|
||||||
|
if (test.valid)
|
||||||
|
{
|
||||||
|
cout << "was valid and was parsed as such.";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << "was not valid, and was not parsed either.";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nErrors;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_disk_space_threshold()
|
||||||
|
{
|
||||||
|
int nErrors = 0;
|
||||||
|
|
||||||
|
static const DISK_SPACE_THRESHOLD_TEST tests[] =
|
||||||
|
{
|
||||||
|
{
|
||||||
|
"/data:80", true,
|
||||||
|
{
|
||||||
|
{ "/data", 80 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/data1", false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
":50", false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/data1:", false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/data1:abc", false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/data1:120", false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/data1:-50", false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/data1,/data2:50", false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/data1:50,/data2", false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
" /data1 : 40, /data2 :50, /data3: 70 ", true,
|
||||||
|
{
|
||||||
|
{ "/data1", 40 },
|
||||||
|
{ "/data2", 50 },
|
||||||
|
{ "/data3", 70 },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const int nTests = sizeof(tests) / sizeof(tests[0]);
|
||||||
|
|
||||||
|
for (int i = 0; i < nTests; ++i)
|
||||||
|
{
|
||||||
|
const DISK_SPACE_THRESHOLD_TEST& test = tests[i];
|
||||||
|
|
||||||
|
MxsDiskSpaceThreshold dst;
|
||||||
|
|
||||||
|
bool parsed = config_parse_disk_space_threshold(&dst, test.zValue);
|
||||||
|
|
||||||
|
nErrors += dst_report(test, parsed, dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
return nErrors;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
|
||||||
|
if (mxs_log_init(NULL, ".", MXS_LOG_TARGET_FS))
|
||||||
|
{
|
||||||
result += test_validity();
|
result += test_validity();
|
||||||
result += test_add_parameter();
|
result += test_add_parameter();
|
||||||
result += test_required_parameters();
|
result += test_required_parameters();
|
||||||
|
result += test_disk_space_threshold();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cerr << "Could not initialize log manager." << endl;
|
||||||
|
++result;
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user