2019-07-27 22:44:32 +08:00
|
|
|
|
#include "stdafx.h"
|
2016-04-12 20:11:15 +08:00
|
|
|
|
#include "redis_util.h"
|
|
|
|
|
#include "redis_monitor.h"
|
|
|
|
|
|
|
|
|
|
redis_monitor::redis_monitor(const char* addr, int conn_timeout,
|
|
|
|
|
int rw_timeout, const char* passwd, bool prefer_master)
|
|
|
|
|
{
|
|
|
|
|
addr_ = addr;
|
|
|
|
|
conn_timeout_ = conn_timeout;
|
|
|
|
|
rw_timeout_ = rw_timeout;
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (passwd && *passwd) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
passwd_ = passwd;
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
2016-04-12 20:11:15 +08:00
|
|
|
|
prefer_master_ = prefer_master;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
redis_monitor::~redis_monitor(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void redis_monitor::status(void)
|
|
|
|
|
{
|
|
|
|
|
acl::redis_client client(addr_, conn_timeout_, rw_timeout_);
|
2017-12-27 10:26:06 +08:00
|
|
|
|
client.set_password(passwd_);
|
2016-04-12 20:11:15 +08:00
|
|
|
|
acl::redis redis(&client);
|
|
|
|
|
|
|
|
|
|
std::vector<acl::redis_node*> nodes;
|
|
|
|
|
redis_util::get_nodes(redis, prefer_master_, nodes);
|
|
|
|
|
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (nodes.empty()) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
logger_error("no redis nodes available");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<acl::redis_client*> conns;
|
|
|
|
|
for (std::vector<acl::redis_node*>::const_iterator
|
2019-07-08 11:49:19 +08:00
|
|
|
|
cit = nodes.begin(); cit != nodes.end(); ++cit) {
|
|
|
|
|
|
2016-04-12 20:11:15 +08:00
|
|
|
|
const char* addr = (*cit)->get_addr();
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (addr == NULL || *addr == 0) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
logger_warn("addr NULL, skip it");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acl::redis_client* conn = new acl::redis_client(
|
|
|
|
|
addr, conn_timeout_, rw_timeout_);
|
2017-12-27 10:26:06 +08:00
|
|
|
|
conn->set_password(passwd_);
|
2016-04-12 20:11:15 +08:00
|
|
|
|
conns.push_back(conn);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 11:49:19 +08:00
|
|
|
|
while (true) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
show_status(conns);
|
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (std::vector<acl::redis_client*>::iterator it = conns.begin();
|
2019-07-08 11:49:19 +08:00
|
|
|
|
it != conns.end(); ++it) {
|
|
|
|
|
|
2016-04-12 20:11:15 +08:00
|
|
|
|
delete *it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int redis_monitor::check(const std::map<acl::string, acl::string>& info,
|
|
|
|
|
const char* name, std::vector<int>& res)
|
|
|
|
|
{
|
|
|
|
|
std::map<acl::string, acl::string>::const_iterator cit
|
|
|
|
|
= info.find(name);
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (cit == info.end()) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
logger_error("no %s", name);
|
|
|
|
|
res.push_back(0);
|
|
|
|
|
return 0;
|
2019-07-08 11:49:19 +08:00
|
|
|
|
} else {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
int n = atoi(cit->second.c_str());
|
|
|
|
|
res.push_back(n);
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long long redis_monitor::check(const std::map<acl::string, acl::string>& info,
|
|
|
|
|
const char* name, std::vector<long long>& res)
|
|
|
|
|
{
|
|
|
|
|
std::map<acl::string, acl::string>::const_iterator cit
|
|
|
|
|
= info.find(name);
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (cit == info.end()) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
logger_error("no %s", name);
|
|
|
|
|
res.push_back(0);
|
|
|
|
|
return 0;
|
2019-07-08 11:49:19 +08:00
|
|
|
|
} else {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
long long n = acl_atoll(cit->second.c_str());
|
|
|
|
|
res.push_back(n);
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-10 21:58:47 +08:00
|
|
|
|
double redis_monitor::check(const std::map<acl::string, acl::string>& info,
|
|
|
|
|
const char* name, std::vector<double>& res)
|
|
|
|
|
{
|
|
|
|
|
std::map<acl::string, acl::string>::const_iterator cit
|
|
|
|
|
= info.find(name);
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (cit == info.end()) {
|
2016-05-10 21:58:47 +08:00
|
|
|
|
logger_error("no %s", name);
|
|
|
|
|
res.push_back(0.0);
|
|
|
|
|
return 0.0;
|
2019-07-08 11:49:19 +08:00
|
|
|
|
} else {
|
2016-05-10 21:58:47 +08:00
|
|
|
|
double n = atof(cit->second.c_str());
|
|
|
|
|
res.push_back(n);
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-12 20:11:15 +08:00
|
|
|
|
int redis_monitor::check_keys(const char* name, const char* value)
|
|
|
|
|
{
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (strncmp(name, "db", 2) != 0 || *value == 0) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
return 0;
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
2016-04-12 20:11:15 +08:00
|
|
|
|
|
|
|
|
|
name += 2;
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (*name == 0 || !acl_alldig(name)) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
return 0;
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
2016-04-12 20:11:15 +08:00
|
|
|
|
|
|
|
|
|
acl::string buf(value);
|
|
|
|
|
buf.trim_space();
|
|
|
|
|
|
|
|
|
|
int keys = 0;
|
|
|
|
|
std::vector<acl::string>& tokens = buf.split2(";,");
|
|
|
|
|
for (std::vector<acl::string>::iterator it = tokens.begin();
|
2019-07-08 11:49:19 +08:00
|
|
|
|
it != tokens.end(); ++it) {
|
|
|
|
|
|
2016-04-12 20:11:15 +08:00
|
|
|
|
std::vector<acl::string>& tokens2 = (*it).split2("=");
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (tokens2.size() != 2) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
continue;
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
|
|
|
|
if (tokens2[0] == "keys" && acl_alldig(tokens2[1])) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
keys = atoi(tokens2[1].c_str());
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
2016-04-12 20:11:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int redis_monitor::check_keys(const std::map<acl::string, acl::string>& info,
|
|
|
|
|
std::vector<int>& keys)
|
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
for (std::map<acl::string, acl::string>::const_iterator cit
|
2019-07-08 11:49:19 +08:00
|
|
|
|
= info.begin(); cit != info.end(); ++cit) {
|
|
|
|
|
|
2016-04-12 20:11:15 +08:00
|
|
|
|
int n = check_keys(cit->first, cit->second);
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (n > 0) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
count += n;
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
2016-04-12 20:11:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keys.push_back(count);
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-10 21:58:47 +08:00
|
|
|
|
static double to_human(long long n, acl::string& units)
|
|
|
|
|
{
|
|
|
|
|
double msize;
|
|
|
|
|
|
|
|
|
|
#define KB 1024
|
|
|
|
|
#define MB (1024 * 1024)
|
|
|
|
|
#define GB (1024 * 1024 * 1024)
|
|
|
|
|
long long TB = (long long) GB * 1024;
|
|
|
|
|
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if ((msize = (double) (n / TB)) >= 1) {
|
2016-05-10 21:58:47 +08:00
|
|
|
|
units = "T";
|
2019-07-08 11:49:19 +08:00
|
|
|
|
} else if ((msize = ((double) n) / GB) >= 1) {
|
2016-05-10 21:58:47 +08:00
|
|
|
|
units = "G";
|
2019-07-08 11:49:19 +08:00
|
|
|
|
} else if ((msize = ((double) n) / MB) >= 1) {
|
2016-05-10 21:58:47 +08:00
|
|
|
|
units = "M";
|
2019-07-08 11:49:19 +08:00
|
|
|
|
} else if ((msize = ((double) n) / KB) >= 1) {
|
2016-05-10 21:58:47 +08:00
|
|
|
|
units = "K";
|
2019-07-08 11:49:19 +08:00
|
|
|
|
} else {
|
2016-05-10 21:58:47 +08:00
|
|
|
|
msize = (double) n;
|
|
|
|
|
units = "B";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return msize;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-12 20:11:15 +08:00
|
|
|
|
void redis_monitor::show_status(std::vector<acl::redis_client*>& conns)
|
|
|
|
|
{
|
|
|
|
|
std::vector<acl::string> addrs;
|
|
|
|
|
std::vector<int> tpses;
|
|
|
|
|
std::vector<int> clients;
|
|
|
|
|
std::vector<int> keys;
|
|
|
|
|
std::vector<long long> memory;
|
2016-05-10 21:58:47 +08:00
|
|
|
|
std::vector<long long> memory_rss;
|
|
|
|
|
std::vector<long long> memory_peak;
|
|
|
|
|
std::vector<double> memory_frag;
|
2016-04-12 20:11:15 +08:00
|
|
|
|
|
|
|
|
|
for (std::vector<acl::redis_client*>::iterator it = conns.begin();
|
2019-07-08 11:49:19 +08:00
|
|
|
|
it != conns.end(); ++it) {
|
|
|
|
|
|
2016-04-12 20:11:15 +08:00
|
|
|
|
acl::redis cmd(*it);
|
|
|
|
|
std::map<acl::string, acl::string> info;
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (cmd.info(info) < 0) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
logger_error("cmd info error: %s, addr: %s",
|
|
|
|
|
cmd.result_error(), (*it)->get_addr());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addrs.push_back((*it)->get_addr());
|
|
|
|
|
check(info, "instantaneous_ops_per_sec", tpses);
|
|
|
|
|
check(info, "connected_clients", clients);
|
|
|
|
|
check_keys(info, keys);
|
|
|
|
|
check(info, "used_memory", memory);
|
2016-05-10 21:58:47 +08:00
|
|
|
|
check(info, "used_memory_rss", memory_rss);
|
|
|
|
|
check(info, "used_memory_peak", memory_peak);
|
|
|
|
|
check(info, "mem_fragmentation_ratio", memory_frag);
|
2016-04-12 20:11:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int all_tps = 0, all_client = 0, all_keys = 0;
|
|
|
|
|
size_t size = addrs.size();
|
|
|
|
|
|
|
|
|
|
|
2016-05-10 21:58:47 +08:00
|
|
|
|
acl::string units, rss_units, peak_units;
|
|
|
|
|
double msize, mrss, mpeak;
|
|
|
|
|
long long total_msize = 0, total_rss = 0, total_peak = 0;
|
2016-04-12 20:11:15 +08:00
|
|
|
|
|
2019-07-08 11:49:19 +08:00
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
2016-05-10 21:58:47 +08:00
|
|
|
|
msize = to_human(memory[i], units);
|
|
|
|
|
mrss = to_human(memory_rss[i], rss_units);
|
|
|
|
|
mpeak = to_human(memory_peak[i], peak_units);
|
2016-04-12 20:11:15 +08:00
|
|
|
|
#ifdef ACL_UNIX
|
|
|
|
|
printf("\033[1;32;40mredis:\033[0m"
|
|
|
|
|
" \033[1;36;40m%20s\033[0m,"
|
|
|
|
|
" \033[1;33;40mtps:\033[0m"
|
|
|
|
|
" \033[1;36;40m%8d\033[0m,"
|
|
|
|
|
" \033[1;33;40mclients:\033[0m"
|
2016-05-10 21:58:47 +08:00
|
|
|
|
" \033[1;36;40m%6d\033[0m,"
|
2016-04-12 20:11:15 +08:00
|
|
|
|
" \033[1;33;40mkeys:\033[0m"
|
2016-05-10 21:58:47 +08:00
|
|
|
|
" \033[1;36;40m%10d\033[0m,"
|
|
|
|
|
" \033[1;33;40mmem:\033[0m"
|
|
|
|
|
" \033[1;36;40m%8.2f %s\033[0m,"
|
|
|
|
|
" \033[1;33;40mmem rss:\033[0m"
|
|
|
|
|
" \033[1;36;40m%8.2f %s\033[0m,"
|
|
|
|
|
" \033[1;33;40mmem peak:\033[0m"
|
|
|
|
|
" \033[1;36;40m%8.2f %s\033[0m,"
|
|
|
|
|
" \033[1;33;40mfrag ratio:\033[0m"
|
|
|
|
|
" \033[1;36;40m%4.2f\033[0m\r\n",
|
2016-04-12 20:11:15 +08:00
|
|
|
|
#else
|
2016-05-10 21:58:47 +08:00
|
|
|
|
printf("redis: %15s, tps: %8d, clients: %6d, keys: %10d,"
|
|
|
|
|
" mem: %8.2f %s, mem rss: %8.2f %s,"
|
|
|
|
|
" mem peak: %8.2f %s, frag ratio: %4.2f\r\n",
|
2016-04-12 20:11:15 +08:00
|
|
|
|
#endif
|
2016-05-10 21:58:47 +08:00
|
|
|
|
addrs[i].c_str(), tpses[i], clients[i], keys[i],
|
|
|
|
|
msize, units.c_str(), mrss, rss_units.c_str(),
|
|
|
|
|
mpeak, peak_units.c_str(), memory_frag[i]);
|
|
|
|
|
|
2019-07-08 11:49:19 +08:00
|
|
|
|
if (tpses[i] > 0) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
all_tps += tpses[i];
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
|
|
|
|
if (clients[i] > 0) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
all_client += clients[i];
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
|
|
|
|
if (keys[i] > 0) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
all_keys += keys[i];
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
|
|
|
|
if (memory[i] > 0) {
|
2016-04-12 20:11:15 +08:00
|
|
|
|
total_msize += memory[i];
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
|
|
|
|
if (memory_rss[i] > 0) {
|
2016-05-10 21:58:47 +08:00
|
|
|
|
total_rss += memory_rss[i];
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
|
|
|
|
if (memory_peak[i] > 0) {
|
2016-05-10 21:58:47 +08:00
|
|
|
|
total_peak += memory_peak[i];
|
2019-07-08 11:49:19 +08:00
|
|
|
|
}
|
2016-04-12 20:11:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-10 21:58:47 +08:00
|
|
|
|
msize = to_human(total_msize, units);
|
|
|
|
|
mrss = to_human(total_rss, rss_units);
|
|
|
|
|
mpeak = to_human(total_peak, peak_units);
|
2016-04-12 20:11:15 +08:00
|
|
|
|
|
|
|
|
|
#ifdef ACL_UNIX
|
|
|
|
|
printf("\033[1;34;40mtotal masters:\033[0m"
|
|
|
|
|
" \033[1;36;40m%12d\033[0m,"
|
|
|
|
|
" \033[1;34;40mtps:\033[0m"
|
|
|
|
|
" \033[1;36;40m%8d\033[0m,"
|
|
|
|
|
" \033[1;34;40mclients:\033[0m"
|
2016-05-10 21:58:47 +08:00
|
|
|
|
" \033[1;36;40m%6d\033[0m,"
|
2016-04-12 20:11:15 +08:00
|
|
|
|
" \033[1;34;40mkeys:\033[0m"
|
2016-05-10 21:58:47 +08:00
|
|
|
|
" \033[1;36;40m%10d\033[0m,"
|
|
|
|
|
" \033[1;34;40mmem:\033[0m"
|
|
|
|
|
" \033[1;36;40m%8.2f %s\033[0m,"
|
|
|
|
|
" \033[1;34;40mmem rss:\033[0m"
|
|
|
|
|
" \033[1;36;40m%8.2f %s\033[0m,"
|
|
|
|
|
" \033[1;34;40mmem peak:\033[0m"
|
2016-04-12 20:11:15 +08:00
|
|
|
|
" \033[1;36;40m%8.2f %s\033[0m\r\n",
|
|
|
|
|
#else
|
2016-05-10 21:58:47 +08:00
|
|
|
|
printf("total masters: %12d, tps: %8d, clients: %6d,"
|
|
|
|
|
" keys: %10d, mem: %8.2f %s, mem rss: %8.2f %s,"
|
|
|
|
|
" mem peak: %8.2f %s\r\n",
|
2016-04-12 20:11:15 +08:00
|
|
|
|
#endif
|
2016-05-10 21:58:47 +08:00
|
|
|
|
(int) size, all_tps, all_client, all_keys,
|
|
|
|
|
msize, units.c_str(), mrss, rss_units.c_str(),
|
|
|
|
|
mpeak, peak_units.c_str());
|
2016-04-12 20:11:15 +08:00
|
|
|
|
}
|