mirror of https://github.com/Icinga/icinga2.git
Merge pull request #7127 from Icinga/bugfix/replay-log
ApiListener#RotateLogFile(): don't overwrite previous log
This commit is contained in:
commit
20d51d21dc
|
@ -33,6 +33,7 @@
|
|||
#include <openssl/tls1.h>
|
||||
#include <openssl/x509.h>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
|
@ -229,7 +230,6 @@ void ApiListener::Start(bool runtimeCreated)
|
|||
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_LogLock);
|
||||
RotateLogFile();
|
||||
OpenLogFile();
|
||||
}
|
||||
|
||||
|
@ -280,6 +280,7 @@ void ApiListener::Stop(bool runtimeDeleted)
|
|||
{
|
||||
boost::mutex::scoped_lock lock(m_LogLock);
|
||||
CloseLogFile();
|
||||
RotateLogFile();
|
||||
}
|
||||
|
||||
RemoveStatusFile();
|
||||
|
@ -1156,13 +1157,11 @@ void ApiListener::RotateLogFile()
|
|||
String oldpath = GetApiDir() + "log/current";
|
||||
String newpath = GetApiDir() + "log/" + Convert::ToString(static_cast<int>(ts)+1);
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
_unlink(newpath.CStr());
|
||||
#endif /* _WIN32 */
|
||||
|
||||
|
||||
// If the log is being rotated more than once per second,
|
||||
// don't overwrite the previous one, but silently deny rotation.
|
||||
if (!Utility::PathExists(newpath)) {
|
||||
(void) rename(oldpath.CStr(), newpath.CStr());
|
||||
}
|
||||
}
|
||||
|
||||
void ApiListener::LogGlobHandler(std::vector<int>& files, const String& file)
|
||||
|
@ -1215,7 +1214,6 @@ void ApiListener::ReplayLog(const JsonRpcConnection::Ptr& client)
|
|||
boost::mutex::scoped_lock lock(m_LogLock);
|
||||
|
||||
CloseLogFile();
|
||||
RotateLogFile();
|
||||
|
||||
if (count == -1 || count > 50000) {
|
||||
OpenLogFile();
|
||||
|
@ -1230,16 +1228,21 @@ void ApiListener::ReplayLog(const JsonRpcConnection::Ptr& client)
|
|||
Utility::Glob(GetApiDir() + "log/*", std::bind(&ApiListener::LogGlobHandler, std::ref(files), _1), GlobFile);
|
||||
std::sort(files.begin(), files.end());
|
||||
|
||||
std::vector<std::pair<int, String>> allFiles;
|
||||
|
||||
for (int ts : files) {
|
||||
String path = GetApiDir() + "log/" + Convert::ToString(ts);
|
||||
if (ts >= peer_ts) {
|
||||
allFiles.emplace_back(ts, GetApiDir() + "log/" + Convert::ToString(ts));
|
||||
}
|
||||
}
|
||||
|
||||
if (ts < peer_ts)
|
||||
continue;
|
||||
allFiles.emplace_back(Utility::GetTime() + 1, GetApiDir() + "log/current");
|
||||
|
||||
for (auto& file : allFiles) {
|
||||
Log(LogNotice, "ApiListener")
|
||||
<< "Replaying log: " << path;
|
||||
<< "Replaying log: " << file.second;
|
||||
|
||||
auto *fp = new std::fstream(path.CStr(), std::fstream::in | std::fstream::binary);
|
||||
auto *fp = new std::fstream(file.second.CStr(), std::fstream::in | std::fstream::binary);
|
||||
StdioStream::Ptr logStream = new StdioStream(fp, true);
|
||||
|
||||
String message;
|
||||
|
@ -1259,7 +1262,7 @@ void ApiListener::ReplayLog(const JsonRpcConnection::Ptr& client)
|
|||
pmessage = JsonDecode(message);
|
||||
} catch (const std::exception&) {
|
||||
Log(LogWarning, "ApiListener")
|
||||
<< "Unexpected end-of-file for cluster log: " << path;
|
||||
<< "Unexpected end-of-file for cluster log: " << file.second;
|
||||
|
||||
/* Log files may be incomplete or corrupted. This is perfectly OK. */
|
||||
break;
|
||||
|
@ -1295,8 +1298,8 @@ void ApiListener::ReplayLog(const JsonRpcConnection::Ptr& client)
|
|||
|
||||
peer_ts = pmessage->Get("timestamp");
|
||||
|
||||
if (ts > logpos_ts + 10) {
|
||||
logpos_ts = ts;
|
||||
if (file.first > logpos_ts + 10) {
|
||||
logpos_ts = file.first;
|
||||
|
||||
Dictionary::Ptr lmessage = new Dictionary({
|
||||
{ "jsonrpc", "2.0" },
|
||||
|
|
Loading…
Reference in New Issue