This commit is contained in:
BenDr0id 2018-07-23 17:36:23 +02:00
parent 49680c282e
commit 527b557f9e
7 changed files with 58 additions and 42 deletions

View file

@ -90,7 +90,7 @@ App::App(int argc, char **argv) :
} }
if (m_options->ccUseRemoteLogging()) { if (m_options->ccUseRemoteLogging()) {
Log::add(new RemoteLog(m_options->ccRemoteLoggingMaxRows())); Log::add(new RemoteLog());
} }
# ifdef HAVE_SYSLOG_H # ifdef HAVE_SYSLOG_H

View file

@ -97,8 +97,7 @@ Options:\n"
--cc-access-token=T access token for CC Server\n\ --cc-access-token=T access token for CC Server\n\
--cc-worker-id=ID custom worker-id for CC Server\n\ --cc-worker-id=ID custom worker-id for CC Server\n\
--cc-update-interval-s=N status update interval in seconds (default: 10 min: 1)\n\ --cc-update-interval-s=N status update interval in seconds (default: 10 min: 1)\n\
--cc-use-remote-logging enable remote logging on CC Server\n\ --cc-use-remote-logging enable remote logging on CC Server\n"
--cc-remote-logging-max-rows=N maximum last n-log rows to send CC Server\n"
# endif # endif
# endif # endif
@ -183,7 +182,6 @@ static struct option const options[] = {
{ "cc-key-file", 1, nullptr, 4015 }, { "cc-key-file", 1, nullptr, 4015 },
{ "cc-use-tls", 0, nullptr, 4016 }, { "cc-use-tls", 0, nullptr, 4016 },
{ "cc-use-remote-logging", 0, nullptr, 4017 }, { "cc-use-remote-logging", 0, nullptr, 4017 },
{ "cc-remote-logging-max-rows", 1, nullptr, 4018 },
{ "daemonized", 0, nullptr, 4011 }, { "daemonized", 0, nullptr, 4011 },
{ "doublehash-thread-mask", 1, nullptr, 4013 }, { "doublehash-thread-mask", 1, nullptr, 4013 },
{ "multihash-thread-mask", 1, nullptr, 4013 }, { "multihash-thread-mask", 1, nullptr, 4013 },
@ -246,7 +244,6 @@ static struct option const cc_client_options[] = {
{ "update-interval-s", 1, nullptr, 4012 }, { "update-interval-s", 1, nullptr, 4012 },
{ "use-tls", 0, nullptr, 4016 }, { "use-tls", 0, nullptr, 4016 },
{ "use-remote-logging", 0, nullptr, 4017 }, { "use-remote-logging", 0, nullptr, 4017 },
{ "remote-logging-max-rows",1, nullptr, 4018 },
{ nullptr, 0, nullptr, 0 } { nullptr, 0, nullptr, 0 }
}; };
@ -349,7 +346,6 @@ Options::Options(int argc, char **argv) :
m_threads(0), m_threads(0),
m_ccUpdateInterval(10), m_ccUpdateInterval(10),
m_ccPort(0), m_ccPort(0),
m_ccRemoteLoggingMaxRows(25),
m_affinity(-1L), m_affinity(-1L),
m_multiHashThreadMask(-1L) m_multiHashThreadMask(-1L)
{ {
@ -562,8 +558,6 @@ bool Options::parseArg(int key, const char *arg)
case 4006: /* --cc-port */ case 4006: /* --cc-port */
case 4012: /* --cc-update-interval-c */ case 4012: /* --cc-update-interval-c */
return parseArg(key, strtol(arg, nullptr, 10)); return parseArg(key, strtol(arg, nullptr, 10));
case 4018: /* --cc-remote-logging-max-rows */
return parseArg(key, strtol(arg, nullptr, 25));
case 'B': /* --background */ case 'B': /* --background */
case 'k': /* --keepalive */ case 'k': /* --keepalive */
@ -762,15 +756,6 @@ bool Options::parseArg(int key, uint64_t arg)
} }
break; break;
case 4018: /* --cc-remote-logging-max-rows */
if (arg < 1) {
m_ccUseRemoteLogging = false;
}
else {
m_ccRemoteLoggingMaxRows = (int) arg;
}
break;
default: default:
break; break;
} }

View file

@ -101,7 +101,6 @@ public:
inline size_t threads() const { return m_threads; } inline size_t threads() const { return m_threads; }
inline int ccUpdateInterval() const { return m_ccUpdateInterval; } inline int ccUpdateInterval() const { return m_ccUpdateInterval; }
inline int ccPort() const { return m_ccPort; } inline int ccPort() const { return m_ccPort; }
inline size_t ccRemoteLoggingMaxRows() const { return m_ccRemoteLoggingMaxRows; }
inline int64_t affinity() const { return m_affinity; } inline int64_t affinity() const { return m_affinity; }
inline int64_t multiHashThreadMask() const { return m_multiHashThreadMask; } inline int64_t multiHashThreadMask() const { return m_multiHashThreadMask; }
inline void setColors(bool colors) { m_colors = colors; } inline void setColors(bool colors) { m_colors = colors; }
@ -176,7 +175,6 @@ private:
size_t m_threads; size_t m_threads;
int m_ccUpdateInterval; int m_ccUpdateInterval;
int m_ccPort; int m_ccPort;
size_t m_ccRemoteLoggingMaxRows;
int64_t m_affinity; int64_t m_affinity;
int64_t m_multiHashThreadMask; int64_t m_multiHashThreadMask;
std::vector<Url*> m_pools; std::vector<Url*> m_pools;

View file

@ -40,6 +40,8 @@
uv_mutex_t Service::m_mutex; uv_mutex_t Service::m_mutex;
std::map<std::string, ControlCommand> Service::m_clientCommand; std::map<std::string, ControlCommand> Service::m_clientCommand;
std::map<std::string, ClientStatus> Service::m_clientStatus; std::map<std::string, ClientStatus> Service::m_clientStatus;
std::map<std::string, std::list<std::string>> Service::m_remoteLog;
int Service::m_currentServerTime = 0; int Service::m_currentServerTime = 0;
bool Service::start() bool Service::start()
@ -55,6 +57,7 @@ void Service::release()
m_clientCommand.clear(); m_clientCommand.clear();
m_clientStatus.clear(); m_clientStatus.clear();
m_remoteLog.clear();
uv_mutex_unlock(&m_mutex); uv_mutex_unlock(&m_mutex);
} }
@ -77,6 +80,8 @@ unsigned Service::handleGET(const Options* options, const std::string& url, cons
resultCode = getClientConfig(options, clientId, resp); resultCode = getClientConfig(options, clientId, resp);
} else if (url.rfind("/admin/getClientCommand", 0) == 0) { } else if (url.rfind("/admin/getClientCommand", 0) == 0) {
resultCode = getClientCommand(clientId, resp); resultCode = getClientCommand(clientId, resp);
} else if (url.rfind("/admin/getClientLog", 0) == 0) {
//resultCode = getClientCommand(clientId, resp);
} }
} }
else { else {
@ -228,10 +233,6 @@ unsigned Service::setClientStatus(const std::string& clientIp, const std::string
clientStatus.parseFromJson(document); clientStatus.parseFromJson(document);
clientStatus.setExternalIp(clientIp); clientStatus.setExternalIp(clientIp);
if (m_clientStatus.find(clientId) == m_clientStatus.end()) {
m_clientCommand[clientId] = ControlCommand(ControlCommand::RESTART);
}
m_clientStatus[clientId] = clientStatus; m_clientStatus[clientId] = clientStatus;
resultCode = getClientCommand(clientId, resp); resultCode = getClientCommand(clientId, resp);
@ -293,6 +294,29 @@ unsigned Service::resetClientStatusList(const std::string& data, std::string& re
return MHD_HTTP_OK; return MHD_HTTP_OK;
} }
unsigned Service::getClientLog(const std::string& clientId, std::string& resp)
{
if (m_remoteLog.find(clientId) != m_remoteLog.end()) {
rapidjson::Document respDocument;
respDocument.SetObject();
auto& allocator = respDocument.GetAllocator();
rapidjson::Value controlCommand = m_clientCommand[clientId].toJson(allocator);
respDocument.AddMember("client_log", controlCommand, allocator);
rapidjson::StringBuffer buffer(0, 4096);
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.SetMaxDecimalPlaces(10);
respDocument.Accept(writer);
resp = buffer.GetString();
}
return MHD_HTTP_OK;
}
unsigned Service::getAdminPage(const Options* options, std::string& resp) unsigned Service::getAdminPage(const Options* options, std::string& resp)
{ {
std::stringstream data; std::stringstream data;

View file

@ -48,6 +48,7 @@ public:
private: private:
static unsigned getClientConfig(const Options* options, const std::string& clientId, std::string& resp); static unsigned getClientConfig(const Options* options, const std::string& clientId, std::string& resp);
static unsigned getClientCommand(const std::string& clientId, std::string& resp); static unsigned getClientCommand(const std::string& clientId, std::string& resp);
static unsigned getClientLog(const std::string& clientId, std::string& resp);
static unsigned getClientStatusList(std::string& resp); static unsigned getClientStatusList(std::string& resp);
static unsigned getAdminPage(const Options* options, std::string& resp); static unsigned getAdminPage(const Options* options, std::string& resp);
@ -63,6 +64,7 @@ private:
static std::map<std::string, ClientStatus> m_clientStatus; static std::map<std::string, ClientStatus> m_clientStatus;
static std::map<std::string, ControlCommand> m_clientCommand; static std::map<std::string, ControlCommand> m_clientCommand;
static std::map<std::string, std::list<std::string>> m_remoteLog;
static uv_mutex_t m_mutex; static uv_mutex_t m_mutex;

View file

@ -22,15 +22,19 @@
RemoteLog* RemoteLog::m_self = nullptr; RemoteLog* RemoteLog::m_self = nullptr;
RemoteLog::RemoteLog(size_t maxRows) RemoteLog::RemoteLog()
: maxRows_(maxRows) : m_maxRows(100)
{ {
uv_mutex_init(&m_mutex);
m_self = this; m_self = this;
} }
RemoteLog::~RemoteLog() RemoteLog::~RemoteLog()
{ {
m_self = nullptr; m_self = nullptr;
uv_mutex_destroy(&m_mutex);
} }
void RemoteLog::message(int level, const char* fmt, va_list args) void RemoteLog::message(int level, const char* fmt, va_list args)
@ -56,13 +60,17 @@ void RemoteLog::message(int level, const char* fmt, va_list args)
size = vsnprintf(buf + size, 512 - size - 1, fmt, args) + size; size = vsnprintf(buf + size, 512 - size - 1, fmt, args) + size;
buf[size] = '\n'; buf[size] = '\n';
if (rows_.size() == maxRows_) { uv_mutex_lock(&m_mutex);
rows_.pop_front();
if (m_rows.size() == m_maxRows) {
m_rows.pop_front();
} }
std::string row = std::regex_replace(std::string(buf, size+1), std::regex("\x1B\\[[0-9;]*[a-zA-Z]"), ""); std::string row = std::regex_replace(std::string(buf, size+1), std::regex("\x1B\\[[0-9;]*[a-zA-Z]"), "");
rows_.push_back(row); m_rows.push_back(row);
uv_mutex_unlock(&m_mutex);
delete[](buf); delete[](buf);
} }
@ -73,23 +81,21 @@ void RemoteLog::text(const char* fmt, va_list args)
message(0, fmt, args); message(0, fmt, args);
} }
void RemoteLog::flushRows()
{
if (m_self) {
m_self->rows_.clear();
}
}
std::string RemoteLog::getRows() std::string RemoteLog::getRows()
{ {
std::stringstream data; std::stringstream data;
uv_mutex_lock(&m_self->m_mutex);
if (m_self) { if (m_self) {
for (std::list<std::string>::iterator it = m_self->rows_.begin(); it != m_self->rows_.end(); it++) { for (auto& m_row : m_self->m_rows) {
data << it->c_str(); data << m_row.c_str();
} }
} }
m_self->m_rows.clear();
uv_mutex_unlock(&m_self->m_mutex);
return data.str(); return data.str();
} }

View file

@ -21,6 +21,7 @@
#include <list> #include <list>
#include <string> #include <string>
#include <uv.h>
#include "interfaces/ILogBackend.h" #include "interfaces/ILogBackend.h"
@ -28,21 +29,21 @@
class RemoteLog : public ILogBackend class RemoteLog : public ILogBackend
{ {
public: public:
RemoteLog(size_t maxRows); RemoteLog();
~RemoteLog(); ~RemoteLog();
void message(int level, const char* fmt, va_list args) override; void message(int level, const char* fmt, va_list args) override;
void text(const char* fmt, va_list args) override; void text(const char* fmt, va_list args) override;
static void flushRows();
static std::string getRows(); static std::string getRows();
private: private:
static RemoteLog* m_self; static RemoteLog* m_self;
size_t maxRows_; uv_mutex_t m_mutex;
std::list<std::string> rows_; size_t m_maxRows;
std::list<std::string> m_rows;
}; };
#endif /* __REMOTELOG_H__ */ #endif /* __REMOTELOG_H__ */