2016-02-26 00:25:13 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
2016-03-05 14:39:35 -06:00
|
|
|
#include <mutex>
|
2016-02-26 00:25:13 -06:00
|
|
|
#include <sys/poll.h>
|
|
|
|
|
2016-03-21 04:06:54 -05:00
|
|
|
#include <Poco/Net/HTTPCookie.h>
|
|
|
|
#include <Poco/Net/HTTPBasicCredentials.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
2016-02-26 00:25:13 -06:00
|
|
|
#include <Poco/Net/HTTPRequestHandler.h>
|
|
|
|
#include <Poco/Net/HTTPServerParams.h>
|
|
|
|
#include <Poco/Net/HTTPServerRequest.h>
|
|
|
|
#include <Poco/Net/HTTPServerResponse.h>
|
|
|
|
#include <Poco/Net/NetException.h>
|
2016-03-21 04:06:54 -05:00
|
|
|
#include <Poco/Net/SecureServerSocket.h>
|
2016-03-08 01:31:29 -06:00
|
|
|
#include <Poco/Net/WebSocket.h>
|
2016-02-26 00:25:13 -06:00
|
|
|
#include <Poco/StringTokenizer.h>
|
2016-03-21 04:06:54 -05:00
|
|
|
#include <Poco/Util/ServerApplication.h>
|
2016-03-05 14:39:35 -06:00
|
|
|
#include <Poco/Util/Timer.h>
|
2016-02-26 00:25:13 -06:00
|
|
|
|
2016-03-21 04:06:54 -05:00
|
|
|
#include "Auth.hpp"
|
2016-02-26 00:25:13 -06:00
|
|
|
#include "Admin.hpp"
|
|
|
|
#include "AdminModel.hpp"
|
|
|
|
#include "Common.hpp"
|
2016-04-06 13:14:03 -05:00
|
|
|
#include "FileServer.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
#include "TileCache.hpp"
|
|
|
|
#include "Storage.hpp"
|
2016-02-26 00:25:13 -06:00
|
|
|
#include "LOOLProtocol.hpp"
|
2016-03-05 09:21:25 -06:00
|
|
|
#include "LOOLWSD.hpp"
|
2016-03-27 14:22:24 -05:00
|
|
|
#include "IoUtil.hpp"
|
2016-02-26 00:25:13 -06:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
|
|
|
using namespace LOOLProtocol;
|
|
|
|
|
2016-03-21 04:06:54 -05:00
|
|
|
using Poco::StringTokenizer;
|
|
|
|
using Poco::Net::HTTPBasicCredentials;
|
|
|
|
using Poco::Net::HTTPCookie;
|
|
|
|
using Poco::Net::HTTPRequest;
|
2016-02-26 00:25:13 -06:00
|
|
|
using Poco::Net::HTTPRequestHandler;
|
|
|
|
using Poco::Net::HTTPRequestHandlerFactory;
|
|
|
|
using Poco::Net::HTTPResponse;
|
|
|
|
using Poco::Net::HTTPServerParams;
|
|
|
|
using Poco::Net::HTTPServerRequest;
|
|
|
|
using Poco::Net::HTTPServerResponse;
|
2016-03-21 04:06:54 -05:00
|
|
|
using Poco::Net::SecureServerSocket;
|
2016-02-26 00:25:13 -06:00
|
|
|
using Poco::Net::ServerSocket;
|
2016-03-08 01:31:29 -06:00
|
|
|
using Poco::Net::Socket;
|
2016-02-26 00:25:13 -06:00
|
|
|
using Poco::Net::WebSocket;
|
|
|
|
using Poco::Net::WebSocketException;
|
2016-03-21 04:06:54 -05:00
|
|
|
using Poco::Util::Application;
|
2016-02-26 00:25:13 -06:00
|
|
|
|
|
|
|
/// Handle admin requests.
|
2016-03-20 05:59:32 -05:00
|
|
|
void AdminRequestHandler::handleWSRequests(HTTPServerRequest& request, HTTPServerResponse& response, int nSessionId)
|
2016-02-26 00:25:13 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
try
|
2016-02-26 00:25:13 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
auto ws = std::make_shared<WebSocket>(request, response);
|
|
|
|
|
2016-02-26 00:25:13 -06:00
|
|
|
{
|
2016-04-06 07:43:44 -05:00
|
|
|
std::unique_lock<std::mutex> modelLock(_admin->getLock());
|
2016-03-20 05:59:32 -05:00
|
|
|
// Subscribe the websocket of any AdminModel updates
|
|
|
|
AdminModel& model = _admin->getModel();
|
|
|
|
model.subscribe(nSessionId, ws);
|
|
|
|
}
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
const Poco::Timespan waitTime(POLL_TIMEOUT_MS * 1000);
|
|
|
|
int flags = 0;
|
|
|
|
int n = 0;
|
|
|
|
ws->setReceiveTimeout(0);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char buffer[200000]; //FIXME: Dynamic?
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
if (ws->poll(waitTime, Socket::SELECT_READ))
|
2016-02-26 00:25:13 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
n = ws->receiveFrame(buffer, sizeof(buffer), flags);
|
2016-02-26 00:25:13 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
if ((flags & WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_PING)
|
2016-02-26 00:25:13 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
// Echo back the ping payload as pong.
|
|
|
|
// Technically, we should send back a PONG control frame.
|
|
|
|
// However Firefox (probably) or Node.js (possibly) doesn't
|
|
|
|
// like that and closes the socket when we do.
|
|
|
|
// Echoing the payload as a normal frame works with Firefox.
|
|
|
|
ws->sendFrame(buffer, n /*, WebSocket::FRAME_OP_PONG*/);
|
|
|
|
}
|
|
|
|
else if ((flags & WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_PONG)
|
|
|
|
{
|
|
|
|
// In case we do send pings in the future.
|
|
|
|
}
|
|
|
|
else if (n <= 0)
|
|
|
|
{
|
|
|
|
// Connection closed.
|
|
|
|
Log::warn() << "Received " << n
|
|
|
|
<< " bytes. Connection closed. Flags: "
|
|
|
|
<< std::hex << flags << Log::end;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(n > 0);
|
|
|
|
const std::string firstLine = getFirstLine(buffer, n);
|
|
|
|
StringTokenizer tokens(firstLine, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
2016-03-22 18:50:42 -05:00
|
|
|
Log::trace("Recv: " + firstLine);
|
2016-02-26 00:25:13 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
if (tokens.count() < 1)
|
|
|
|
continue;
|
2016-02-26 00:25:13 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
// Lock the model mutex before interacting with it
|
2016-04-06 07:43:44 -05:00
|
|
|
std::unique_lock<std::mutex> modelLock(_admin->getLock());
|
2016-03-20 05:59:32 -05:00
|
|
|
AdminModel& model = _admin->getModel();
|
2016-02-26 00:25:13 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
if (tokens[0] == "stats")
|
|
|
|
{
|
|
|
|
//TODO: Collect stats and reply back to admin.
|
2016-04-07 03:27:43 -05:00
|
|
|
// We need to ask ForKit to give us some numbers on docs/clients/etc.
|
2016-03-20 05:59:32 -05:00
|
|
|
// But we can also collect some memory info using system calls.
|
2016-03-04 12:49:01 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
std::string statsResponse;
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
const auto cmd = "pstree -a -c -h -A -p " + std::to_string(getpid());
|
|
|
|
FILE* fp = popen(cmd.c_str(), "r");
|
|
|
|
if (fp == nullptr)
|
2016-02-26 00:25:13 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
statsResponse = "error: failed to collect stats.";
|
2016-02-26 00:25:13 -06:00
|
|
|
ws->sendFrame(statsResponse.data(), statsResponse.size());
|
2016-03-20 05:59:32 -05:00
|
|
|
continue;
|
2016-02-26 00:25:13 -06:00
|
|
|
}
|
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
char treeBuffer[1024];
|
2016-03-22 18:50:42 -05:00
|
|
|
while (fgets(treeBuffer, sizeof(treeBuffer)-1, fp) != nullptr &&
|
|
|
|
!TerminationFlag)
|
2016-03-04 12:49:01 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
statsResponse += treeBuffer;
|
|
|
|
statsResponse += "</ BR>\n";
|
2016-03-04 12:49:01 -06:00
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
|
|
|
|
pclose(fp);
|
|
|
|
|
|
|
|
ws->sendFrame(statsResponse.data(), statsResponse.size());
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "subscribe" && tokens.count() > 1)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < tokens.count() - 1; i++)
|
2016-03-04 12:49:01 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
model.subscribe(nSessionId, tokens[i + 1]);
|
2016-03-04 12:49:01 -06:00
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
}
|
|
|
|
else if (tokens[0] == "unsubscribe" && tokens.count() > 1)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < tokens.count() - 1; i++)
|
2016-03-04 12:49:01 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
model.unsubscribe(nSessionId, tokens[i + 1]);
|
2016-03-04 12:49:01 -06:00
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
}
|
|
|
|
else if (tokens[0] == "documents")
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string responseString = "documents " + model.query("documents");
|
|
|
|
ws->sendFrame(responseString.data(), responseString.size());
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "total_mem")
|
|
|
|
{
|
|
|
|
unsigned totalMem = _admin->getTotalMemoryUsage(model);
|
|
|
|
std::string responseFrame = "total_mem " + std::to_string(totalMem);
|
|
|
|
ws->sendFrame(responseFrame.data(), responseFrame.size());
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "active_users_count")
|
|
|
|
{
|
|
|
|
std::string responseFrame = tokens[0] + " " + model.query(tokens[0]);
|
|
|
|
ws->sendFrame(responseFrame.data(), responseFrame.size());
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "active_docs_count")
|
|
|
|
{
|
|
|
|
std::string responseFrame = tokens[0] + " " + model.query(tokens[0]);
|
|
|
|
ws->sendFrame(responseFrame.data(), responseFrame.size());
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "kill" && tokens.count() == 2)
|
|
|
|
{
|
|
|
|
try
|
2016-03-04 15:08:37 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
if (std::stoi(tokens[1]))
|
2016-03-04 15:08:37 -06:00
|
|
|
{
|
2016-04-15 04:00:22 -05:00
|
|
|
LOOLWSD::killKit(std::stoi(tokens[1]));
|
2016-03-04 15:08:37 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-27 14:22:24 -05:00
|
|
|
catch(std::exception& e)
|
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
Log::warn() << "Could not kill given PID" << Log::end;
|
2016-03-05 14:39:35 -06:00
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
}
|
|
|
|
else if (tokens[0] == "mem_stats")
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << tokens[0] << " "
|
|
|
|
<< model.query(tokens[0]);
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
std::string responseFrame = oss.str();
|
|
|
|
ws->sendFrame(responseFrame.data(), responseFrame.size());
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "cpu_stats")
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << tokens[0] << " "
|
|
|
|
<< model.query(tokens[0]);
|
|
|
|
|
|
|
|
std::string responseFrame = oss.str();
|
|
|
|
ws->sendFrame(responseFrame.data(), responseFrame.size());
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "settings")
|
|
|
|
{
|
|
|
|
// for now, we have only these settings
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << tokens[0] << " "
|
|
|
|
<< "mem_stats_size=" << model.query("mem_stats_size") << " "
|
|
|
|
<< "mem_stats_interval=" << std::to_string(_admin->getMemStatsInterval()) << " "
|
|
|
|
<< "cpu_stats_size=" << model.query("cpu_stats_size") << " "
|
|
|
|
<< "cpu_stats_interval=" << std::to_string(_admin->getCpuStatsInterval());
|
|
|
|
|
|
|
|
std::string responseFrame = oss.str();
|
|
|
|
ws->sendFrame(responseFrame.data(), responseFrame.size());
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "set" && tokens.count() > 1)
|
|
|
|
{
|
|
|
|
for (unsigned i = 1; i < tokens.count(); i++)
|
2016-03-05 14:39:35 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
StringTokenizer setting(tokens[i], "=", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
unsigned settingVal = 0;
|
|
|
|
try
|
2016-03-05 14:39:35 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
settingVal = std::stoi(setting[1]);
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
Log::warn() << "Invalid setting value: "
|
|
|
|
<< setting[1] << " for "
|
|
|
|
<< setting[0] << Log::end;
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
if (setting[0] == "mem_stats_size")
|
|
|
|
{
|
|
|
|
if (settingVal != static_cast<unsigned>(std::stoi(model.query(setting[0]))))
|
2016-03-05 14:39:35 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
model.setMemStatsSize(settingVal);
|
2016-03-05 14:39:35 -06:00
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
}
|
|
|
|
else if (setting[0] == "mem_stats_interval")
|
|
|
|
{
|
|
|
|
if (settingVal != _admin->getMemStatsInterval())
|
2016-03-05 14:39:35 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
_admin->rescheduleMemTimer(settingVal);
|
|
|
|
model.clearMemStats();
|
|
|
|
model.notify("settings mem_stats_interval=" + std::to_string(settingVal));
|
2016-03-05 14:39:35 -06:00
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
}
|
|
|
|
else if (setting[0] == "cpu_stats_size")
|
|
|
|
{
|
|
|
|
if (settingVal != static_cast<unsigned>(std::stoi(model.query(setting[0]))))
|
2016-03-05 14:39:35 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
model.setCpuStatsSize(settingVal);
|
2016-03-05 14:39:35 -06:00
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
}
|
|
|
|
else if (setting[0] == "cpu_stats_interval")
|
|
|
|
{
|
|
|
|
if (settingVal != _admin->getCpuStatsInterval())
|
2016-03-05 14:39:35 -06:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
_admin->rescheduleCpuTimer(settingVal);
|
|
|
|
model.clearCpuStats();
|
|
|
|
model.notify("settings cpu_stats_interval=" + std::to_string(settingVal));
|
2016-03-05 14:39:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-26 00:25:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
while (!TerminationFlag &&
|
|
|
|
(flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
|
|
|
|
Log::debug() << "Finishing AdminProcessor. TerminationFlag: " << TerminationFlag
|
|
|
|
<< ", payload size: " << n
|
|
|
|
<< ", flags: " << std::hex << flags << Log::end;
|
|
|
|
}
|
|
|
|
catch (const WebSocketException& exc)
|
|
|
|
{
|
|
|
|
Log::error("AdminRequestHandler::handleRequest: WebSocketException: " + exc.message());
|
|
|
|
switch (exc.code())
|
2016-03-21 04:06:54 -05:00
|
|
|
{
|
2016-03-20 05:59:32 -05:00
|
|
|
case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION:
|
|
|
|
response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION);
|
|
|
|
// fallthrough
|
|
|
|
case WebSocket::WS_ERR_NO_HANDSHAKE:
|
|
|
|
case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION:
|
|
|
|
case WebSocket::WS_ERR_HANDSHAKE_NO_KEY:
|
|
|
|
response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST);
|
2016-03-21 04:06:54 -05:00
|
|
|
response.setContentLength(0);
|
|
|
|
response.send();
|
2016-03-20 05:59:32 -05:00
|
|
|
break;
|
2016-02-26 00:25:13 -06:00
|
|
|
}
|
2016-03-21 04:06:54 -05:00
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
catch (const Poco::Net::NotAuthenticatedException& exc)
|
|
|
|
{
|
|
|
|
Log::info("NotAuthenticatedException");
|
|
|
|
response.set("WWW-Authenticate", "Basic realm=\"ws-online\"");
|
|
|
|
response.setStatus(HTTPResponse::HTTP_UNAUTHORIZED);
|
|
|
|
response.setContentLength(0);
|
|
|
|
response.send();
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
Log::error(std::string("AdminRequestHandler::handleRequest: Exception: ") + exc.what());
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 04:06:54 -05:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
AdminRequestHandler::AdminRequestHandler(Admin* adminManager)
|
|
|
|
: _admin(adminManager)
|
|
|
|
{ }
|
2016-03-21 04:06:54 -05:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
void AdminRequestHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
|
|
|
|
{
|
|
|
|
// Different session id pool for admin sessions (?)
|
|
|
|
const auto nSessionId = Util::decodeId(LOOLWSD::GenSessionId());
|
2016-03-21 04:06:54 -05:00
|
|
|
|
2016-04-07 11:31:56 -05:00
|
|
|
Util::setThreadName("admin_ws_" + std::to_string(nSessionId));
|
2016-03-21 04:06:54 -05:00
|
|
|
|
2016-04-07 07:51:49 -05:00
|
|
|
Log::debug("Thread started.");
|
2016-02-26 00:25:13 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string requestURI = request.getURI();
|
|
|
|
StringTokenizer pathTokens(requestURI, "/", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
2016-03-21 04:06:54 -05:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
if (request.find("Upgrade") != request.end() && Poco::icompare(request["Upgrade"], "websocket") == 0)
|
2016-03-21 04:06:54 -05:00
|
|
|
{
|
2016-04-06 13:14:03 -05:00
|
|
|
if (!FileServerRequestHandler::isAdminLoggedIn(request, response))
|
|
|
|
throw Poco::Net::NotAuthenticatedException("Invalid admin login");
|
2016-03-20 05:59:32 -05:00
|
|
|
|
2016-04-06 13:14:03 -05:00
|
|
|
handleWSRequests(request, response, nSessionId);
|
2016-03-21 04:06:54 -05:00
|
|
|
}
|
2016-02-26 00:25:13 -06:00
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
catch(const Poco::Net::NotAuthenticatedException& exc)
|
2016-02-26 00:25:13 -06:00
|
|
|
{
|
2016-04-08 06:30:48 -05:00
|
|
|
Log::info("Admin::NotAuthenticated");
|
2016-03-20 05:59:32 -05:00
|
|
|
response.set("WWW-Authenticate", "Basic realm=\"online\"");
|
|
|
|
response.setStatus(HTTPResponse::HTTP_UNAUTHORIZED);
|
|
|
|
response.setContentLength(0);
|
|
|
|
response.send();
|
2016-02-26 00:25:13 -06:00
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
Log::info("Unknown Exception caught");
|
|
|
|
response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST);
|
|
|
|
response.setContentLength(0);
|
|
|
|
response.send();
|
|
|
|
}
|
2016-04-07 07:51:49 -05:00
|
|
|
Log::debug("Thread finished.");
|
2016-03-20 05:59:32 -05:00
|
|
|
}
|
2016-02-26 00:25:13 -06:00
|
|
|
|
|
|
|
/// An admin command processor.
|
2016-04-06 07:43:44 -05:00
|
|
|
Admin::Admin() :
|
2016-02-26 00:25:13 -06:00
|
|
|
_model(AdminModel())
|
|
|
|
{
|
2016-04-13 03:14:54 -05:00
|
|
|
Log::info("Admin ctor.");
|
|
|
|
|
|
|
|
_memStatsTask = new MemoryStats(this);
|
|
|
|
_memStatsTimer.schedule(_memStatsTask, _memStatsTaskInterval, _memStatsTaskInterval);
|
|
|
|
|
|
|
|
_cpuStatsTask = new CpuStats(this);
|
|
|
|
_cpuStatsTimer.schedule(_cpuStatsTask, _cpuStatsTaskInterval, _cpuStatsTaskInterval);
|
2016-02-26 00:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Admin::~Admin()
|
|
|
|
{
|
|
|
|
Log::info("~Admin dtor.");
|
2016-04-13 03:14:54 -05:00
|
|
|
|
|
|
|
_memStatsTask->cancel();
|
|
|
|
_cpuStatsTask->cancel();
|
2016-02-26 00:25:13 -06:00
|
|
|
}
|
|
|
|
|
2016-04-15 04:00:22 -05:00
|
|
|
void Admin::addDoc(const std::string& docKey, Poco::Process::PID pid, const std::string& filename, const int sessionId)
|
2016-02-26 00:25:13 -06:00
|
|
|
{
|
2016-04-13 03:16:57 -05:00
|
|
|
std::unique_lock<std::mutex> modelLock(_modelMutex);
|
2016-04-15 04:00:22 -05:00
|
|
|
_model.addDocument(docKey, pid, filename, sessionId);
|
2016-04-14 15:29:31 -05:00
|
|
|
}
|
|
|
|
|
2016-04-15 04:00:22 -05:00
|
|
|
void Admin::rmDoc(const std::string& docKey, const int sessionId)
|
2016-04-14 15:29:31 -05:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> modelLock(_modelMutex);
|
2016-04-15 04:00:22 -05:00
|
|
|
_model.removeDocument(docKey, sessionId);
|
2016-02-26 00:25:13 -06:00
|
|
|
}
|
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
void MemoryStats::run()
|
|
|
|
{
|
2016-04-06 07:43:44 -05:00
|
|
|
std::unique_lock<std::mutex> modelLock(_admin->getLock());
|
2016-03-05 14:39:35 -06:00
|
|
|
AdminModel& model = _admin->getModel();
|
|
|
|
unsigned totalMem = _admin->getTotalMemoryUsage(model);
|
|
|
|
|
2016-04-14 15:29:31 -05:00
|
|
|
Log::info("Total memory used: " + std::to_string(totalMem));
|
2016-03-05 14:39:35 -06:00
|
|
|
model.addMemStats(totalMem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuStats::run()
|
|
|
|
{
|
|
|
|
//TODO: Implement me
|
2016-04-06 07:43:44 -05:00
|
|
|
//std::unique_lock<std::mutex> modelLock(_admin->getLock());
|
2016-03-05 14:39:35 -06:00
|
|
|
//model.addCpuStats(totalMem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Admin::rescheduleMemTimer(unsigned interval)
|
|
|
|
{
|
|
|
|
_memStatsTask->cancel();
|
|
|
|
_memStatsTaskInterval = interval;
|
|
|
|
_memStatsTask = new MemoryStats(this);
|
|
|
|
_memStatsTimer.schedule(_memStatsTask, _memStatsTaskInterval, _memStatsTaskInterval);
|
|
|
|
Log::info("Memory stats interval changed - New interval: " + std::to_string(interval));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Admin::rescheduleCpuTimer(unsigned interval)
|
|
|
|
{
|
|
|
|
_cpuStatsTask->cancel();
|
|
|
|
_cpuStatsTaskInterval = interval;
|
|
|
|
_cpuStatsTask = new CpuStats(this);
|
|
|
|
_cpuStatsTimer.schedule(_cpuStatsTask, _cpuStatsTaskInterval, _cpuStatsTaskInterval);
|
|
|
|
Log::info("CPU stats interval changed - New interval: " + std::to_string(interval));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Admin::getTotalMemoryUsage(AdminModel& model)
|
|
|
|
{
|
2016-04-07 03:27:43 -05:00
|
|
|
unsigned totalMem = Util::getMemoryUsage(_forKitPid);
|
2016-03-05 14:39:35 -06:00
|
|
|
totalMem += model.getTotalMemoryUsage();
|
|
|
|
totalMem += Util::getMemoryUsage(Poco::Process::id());
|
|
|
|
|
|
|
|
return totalMem;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Admin::getMemStatsInterval()
|
|
|
|
{
|
|
|
|
return _memStatsTaskInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Admin::getCpuStatsInterval()
|
|
|
|
{
|
|
|
|
return _cpuStatsTaskInterval;
|
|
|
|
}
|
|
|
|
|
2016-03-03 12:01:37 -06:00
|
|
|
AdminModel& Admin::getModel()
|
|
|
|
{
|
|
|
|
return _model;
|
|
|
|
}
|
|
|
|
|
2016-02-26 00:25:13 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|