Commit fd78e572 authored by Kilo Code's avatar Kilo Code

Add continuous monitoring with periodic status logging

parent 63bf10d8
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <tlhelp32.h> #include <tlhelp32.h>
#include <psapi.h>
// #pragma comment(lib, "ws2_32.lib") // #pragma comment(lib, "ws2_32.lib")
// #pragma comment(lib, "iphlpapi.lib") // #pragma comment(lib, "iphlpapi.lib")
...@@ -137,6 +138,7 @@ int main() { ...@@ -137,6 +138,7 @@ int main() {
FILE* internal_log = fopen(internal_log_path, "a"); FILE* internal_log = fopen(internal_log_path, "a");
FILE* external_log = fopen(external_log_path, "a"); FILE* external_log = fopen(external_log_path, "a");
// Initial snapshot of connections
PMIB_TCPTABLE_OWNER_PID tcpTable; PMIB_TCPTABLE_OWNER_PID tcpTable;
DWORD size = 0; DWORD size = 0;
GetExtendedTcpTable(NULL, &size, FALSE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0); GetExtendedTcpTable(NULL, &size, FALSE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
...@@ -165,6 +167,79 @@ int main() { ...@@ -165,6 +167,79 @@ int main() {
fclose(internal_log); fclose(internal_log);
fclose(external_log); fclose(external_log);
free(tcpTable); free(tcpTable);
// Continuous monitoring loop
DWORD lastStatusTime = GetTickCount();
const DWORD STATUS_INTERVAL = 5000; // 5 seconds
while (1) {
DWORD currentTime = GetTickCount();
// Periodic status logging
if (currentTime - lastStatusTime >= STATUS_INTERVAL) {
// Get current TCP connections for the monitored process
size = 0;
GetExtendedTcpTable(NULL, &size, FALSE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
tcpTable = (PMIB_TCPTABLE_OWNER_PID)malloc(size);
DWORD socketCount = 0;
if (GetExtendedTcpTable(tcpTable, &size, FALSE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0) == NO_ERROR) {
for (DWORD i = 0; i < tcpTable->dwNumEntries; i++) {
if (tcpTable->table[i].dwOwningPid == pid) {
socketCount++;
}
}
}
// Get process information
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
DWORD threadCount = 0;
DWORD memoryUsage = 0;
if (hProcess) {
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) {
memoryUsage = pmc.WorkingSetSize / 1024; // KB
}
// Get thread count
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hSnapshot != INVALID_HANDLE_VALUE) {
THREADENTRY32 te32;
te32.dwSize = sizeof(THREADENTRY32);
if (Thread32First(hSnapshot, &te32)) {
do {
if (te32.th32OwnerProcessID == pid) {
threadCount++;
}
} while (Thread32Next(hSnapshot, &te32));
}
CloseHandle(hSnapshot);
}
CloseHandle(hProcess);
}
// Get system time for logging
SYSTEMTIME st;
GetSystemTime(&st);
// Print status to console
printf("[%04d-%02d-%02d %02d:%02d:%02d] STATUS - Sockets: %lu, Threads: %lu, Memory: %lu KB\n",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
socketCount, threadCount, memoryUsage);
free(tcpTable);
lastStatusTime = currentTime;
}
// Check if process is still running
DWORD exitCode;
if (GetExitCodeProcess(pi.hProcess, &exitCode) && exitCode != STILL_ACTIVE) {
printf("Monitored process has terminated.\n");
break;
}
Sleep(1000); // Check every second
}
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
return 0; return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment