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:
@ -33,25 +33,26 @@
|
||||
|
||||
#include <maxscale/adminusers.h>
|
||||
#include <maxscale/alloc.h>
|
||||
#include <maxscale/clock.h>
|
||||
#include <maxscale/housekeeper.h>
|
||||
#include <maxscale/http.hh>
|
||||
#include <maxscale/json_api.h>
|
||||
#include <maxscale/limits.h>
|
||||
#include <maxscale/log_manager.h>
|
||||
#include <maxscale/maxscale.h>
|
||||
#include <maxscale/paths.h>
|
||||
#include <maxscale/pcre2.h>
|
||||
#include <maxscale/router.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
#include <maxscale/utils.h>
|
||||
#include <maxscale/paths.h>
|
||||
#include <maxscale/router.h>
|
||||
#include <maxscale/json_api.h>
|
||||
#include <maxscale/http.hh>
|
||||
#include <maxscale/utils.hh>
|
||||
#include <maxscale/version.h>
|
||||
#include <maxscale/maxscale.h>
|
||||
#include <maxscale/clock.h>
|
||||
|
||||
#include "internal/config.h"
|
||||
#include "internal/filter.h"
|
||||
#include "internal/service.h"
|
||||
#include "internal/monitor.h"
|
||||
#include "internal/modules.h"
|
||||
#include "internal/monitor.h"
|
||||
#include "internal/service.h"
|
||||
|
||||
using std::set;
|
||||
using std::string;
|
||||
@ -81,6 +82,7 @@ const char CN_CONNECTION_TIMEOUT[] = "connection_timeout";
|
||||
const char CN_DATA[] = "data";
|
||||
const char CN_DEFAULT[] = "default";
|
||||
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_ENABLE_ROOT_USER[] = "enable_root_user";
|
||||
const char CN_FILTERS[] = "filters";
|
||||
@ -4645,3 +4647,75 @@ static uint64_t get_suffixed_size(const char* value)
|
||||
|
||||
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
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <maxscale/log_manager.h>
|
||||
#include "../config.cc"
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define TEST(a) do{if(!(a)){printf("Error: `" #a "` was not true\n");return 1;}}while(false)
|
||||
|
||||
int test_validity()
|
||||
@ -213,13 +217,174 @@ int test_required_parameters()
|
||||
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 result = 0;
|
||||
|
||||
result += test_validity();
|
||||
result += test_add_parameter();
|
||||
result += test_required_parameters();
|
||||
if (mxs_log_init(NULL, ".", MXS_LOG_TARGET_FS))
|
||||
{
|
||||
result += test_validity();
|
||||
result += test_add_parameter();
|
||||
result += test_required_parameters();
|
||||
result += test_disk_space_threshold();
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "Could not initialize log manager." << endl;
|
||||
++result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
Reference in New Issue
Block a user