From 73a3e472d815b424005114c192acf41789cee665 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sat, 11 Jan 2020 13:35:48 +0500 Subject: [PATCH] Driver: Add timestamp to traffic stats. --- src/common/fortlog.c | 10 ++++++---- src/common/version.h | 2 +- src/driver/fortdrv.c | 15 ++++++++++++++- src/ui/fortcommon.cpp | 3 ++- src/ui/fortcommon.h | 1 + src/ui/log/logbuffer.cpp | 4 +++- src/ui/log/logentrystattraf.cpp | 5 +++++ src/ui/log/logentrystattraf.h | 4 ++++ src/ui/log/model/appstatmodel.cpp | 1 + src/ui/stat/statmanager.cpp | 5 ++--- src/ui/stat/statmanager.h | 3 ++- 11 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/common/fortlog.c b/src/common/fortlog.c index 5a567b96..206cf755 100644 --- a/src/common/fortlog.c +++ b/src/common/fortlog.c @@ -24,7 +24,7 @@ ((FORT_LOG_PROC_NEW_HEADER_SIZE + (path_len) \ + (FORT_LOG_ALIGN - 1)) & ~(FORT_LOG_ALIGN - 1)) -#define FORT_LOG_STAT_HEADER_SIZE sizeof(UINT32) +#define FORT_LOG_STAT_HEADER_SIZE (sizeof(UINT32) + sizeof(INT64)) #define FORT_LOG_STAT_TRAF_SIZE(proc_count) \ (proc_count * 3 * sizeof(UINT32)) @@ -114,19 +114,21 @@ fort_log_proc_new_header_read (const char *p, UINT32 *pid, } static void -fort_log_stat_traf_header_write (char *p, UINT16 proc_count) +fort_log_stat_traf_header_write (char *p, INT64 unix_time, UINT16 proc_count) { UINT32 *up = (UINT32 *) p; *up++ = FORT_LOG_FLAG_STAT_TRAF | proc_count; + *((INT64 *) up) = unix_time; } static void -fort_log_stat_traf_header_read (const char *p, UINT16 *proc_count) +fort_log_stat_traf_header_read (const char *p, INT64 *unix_time, UINT16 *proc_count) { const UINT32 *up = (const UINT32 *) p; - *proc_count = (UINT16) *up; + *proc_count = (UINT16) *up++; + *unix_time = *((INT64 *) up); } static void diff --git a/src/common/version.h b/src/common/version.h index e3188c7c..da68f0cb 100644 --- a/src/common/version.h +++ b/src/common/version.h @@ -14,6 +14,6 @@ #define APP_UPDATES_URL "https://github.com/tnodir/fort/releases" #define APP_UPDATES_API_URL "https://api.github.com/repos/tnodir/fort/releases/latest" -#define DRIVER_VERSION 17 +#define DRIVER_VERSION 18 #endif // VERSION_H diff --git a/src/driver/fortdrv.c b/src/driver/fortdrv.c index f31fae12..0631b062 100644 --- a/src/driver/fortdrv.c +++ b/src/driver/fortdrv.c @@ -796,6 +796,7 @@ fort_callout_timer (void) (stat->proc_active_count < FORT_LOG_STAT_BUFFER_PROC_COUNT) ? stat->proc_active_count : FORT_LOG_STAT_BUFFER_PROC_COUNT; const UINT32 len = FORT_LOG_STAT_SIZE(proc_count); + INT64 unix_time; PCHAR out; NTSTATUS status; @@ -806,7 +807,19 @@ fort_callout_timer (void) break; } - fort_log_stat_traf_header_write(out, proc_count); + /* Get current Unix time */ + { + LARGE_INTEGER system_time; + + KeQuerySystemTime(&system_time); + + /* Convert system time to seconds since 1970 */ + #define SECSPERDAY 86400 + #define SECS_1601_TO_1970 ((369 * 365 + 89) * (INT64) SECSPERDAY) /* 1601 to 1970 is 369 years plus 89 leap days */ + unix_time = system_time.QuadPart / 10000000 - SECS_1601_TO_1970; + } + + fort_log_stat_traf_header_write(out, unix_time, proc_count); out += FORT_LOG_STAT_HEADER_SIZE; fort_stat_dpc_traf_flush(stat, proc_count, out); diff --git a/src/ui/fortcommon.cpp b/src/ui/fortcommon.cpp index ad6d229f..ea7fc28d 100644 --- a/src/ui/fortcommon.cpp +++ b/src/ui/fortcommon.cpp @@ -137,9 +137,10 @@ void FortCommon::logProcNewHeaderRead(const char *input, } void FortCommon::logStatTrafHeaderRead(const char *input, + qint64 *unixTime, quint16 *procCount) { - fort_log_stat_traf_header_read(input, procCount); + fort_log_stat_traf_header_read(input, unixTime, procCount); } void FortCommon::logHeartbeatRead(const char *input, quint16 *tick) diff --git a/src/ui/fortcommon.h b/src/ui/fortcommon.h index b943efc5..97ecc132 100644 --- a/src/ui/fortcommon.h +++ b/src/ui/fortcommon.h @@ -51,6 +51,7 @@ public: quint32 *pid, quint32 *pathLen); static void logStatTrafHeaderRead(const char *input, + qint64 *unixTime, quint16 *procCount); static void logHeartbeatRead(const char *input, quint16 *tick); diff --git a/src/ui/log/logbuffer.cpp b/src/ui/log/logbuffer.cpp index 1c5b193b..13a0ebc7 100644 --- a/src/ui/log/logbuffer.cpp +++ b/src/ui/log/logbuffer.cpp @@ -149,10 +149,12 @@ void LogBuffer::readEntryStatTraf(LogEntryStatTraf *logEntry) const char *input = this->input(); + qint64 unixTime; quint16 procCount; - FortCommon::logStatTrafHeaderRead(input, &procCount); + FortCommon::logStatTrafHeaderRead(input, &unixTime, &procCount); logEntry->setProcCount(procCount); + logEntry->setUnixTime(unixTime); if (procCount) { input += FortCommon::logStatHeaderSize(); diff --git a/src/ui/log/logentrystattraf.cpp b/src/ui/log/logentrystattraf.cpp index 8c64a70f..ccc52475 100644 --- a/src/ui/log/logentrystattraf.cpp +++ b/src/ui/log/logentrystattraf.cpp @@ -12,6 +12,11 @@ void LogEntryStatTraf::setProcCount(quint16 procCount) m_procCount = procCount; } +void LogEntryStatTraf::setUnixTime(qint64 unixTime) +{ + m_unixTime = unixTime; +} + void LogEntryStatTraf::setProcTrafBytes(const quint32 *procTrafBytes) { m_procTrafBytes = procTrafBytes; diff --git a/src/ui/log/logentrystattraf.h b/src/ui/log/logentrystattraf.h index 1db2acdc..8b5b06b5 100644 --- a/src/ui/log/logentrystattraf.h +++ b/src/ui/log/logentrystattraf.h @@ -14,11 +14,15 @@ public: quint16 procCount() const { return m_procCount; } void setProcCount(quint16 procCount); + qint64 unixTime() const { return m_unixTime; } + void setUnixTime(qint64 unixTime); + const quint32 *procTrafBytes() const { return m_procTrafBytes; } void setProcTrafBytes(const quint32 *procTrafBytes); private: quint16 m_procCount = 0; + qint64 m_unixTime = 0; const quint32 *m_procTrafBytes = nullptr; }; diff --git a/src/ui/log/model/appstatmodel.cpp b/src/ui/log/model/appstatmodel.cpp index 89c73171..26293fad 100644 --- a/src/ui/log/model/appstatmodel.cpp +++ b/src/ui/log/model/appstatmodel.cpp @@ -87,6 +87,7 @@ void AppStatModel::handleProcNew(const LogEntryProcNew &procNewEntry) void AppStatModel::handleStatTraf(const LogEntryStatTraf &statTrafEntry) { m_statManager->logStatTraf(statTrafEntry.procCount(), + statTrafEntry.unixTime(), statTrafEntry.procTrafBytes()); } diff --git a/src/ui/stat/statmanager.cpp b/src/ui/stat/statmanager.cpp index 870d1a7c..bb528dc3 100644 --- a/src/ui/stat/statmanager.cpp +++ b/src/ui/stat/statmanager.cpp @@ -248,13 +248,12 @@ void StatManager::logProcNew(quint32 pid, const QString &appPath) m_appIndexes.insert(pid, procIndex); } -void StatManager::logStatTraf(quint16 procCount, const quint32 *procTrafBytes) +void StatManager::logStatTraf(quint16 procCount, qint64 unixTime, + const quint32 *procTrafBytes) { if (!m_conf || !m_conf->logStat()) return; - const qint64 unixTime = DateUtil::getUnixTime(); - const qint32 trafHour = DateUtil::getUnixHour(unixTime); const bool isNewHour = (trafHour != m_lastTrafHour); diff --git a/src/ui/stat/statmanager.h b/src/ui/stat/statmanager.h index 6d17cf7d..74eb36c3 100644 --- a/src/ui/stat/statmanager.h +++ b/src/ui/stat/statmanager.h @@ -35,7 +35,8 @@ public: bool initialize(); void logProcNew(quint32 pid, const QString &appPath); - void logStatTraf(quint16 procCount, const quint32 *procTrafBytes); + void logStatTraf(quint16 procCount, qint64 unixTime, + const quint32 *procTrafBytes); void getAppList(QStringList &list, QVector &appIds);