Skip to content

Commit c036576

Browse files
authored
Merge pull request #1 from Hsnmsri/multi_node
fix bugs
2 parents 96cdc5f + faae8bc commit c036576

8 files changed

Lines changed: 249 additions & 205 deletions

File tree

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
"variant": "cpp",
7878
"forward_list": "cpp",
7979
"ranges": "cpp",
80-
"valarray": "cpp"
80+
"valarray": "cpp",
81+
"bitset": "cpp",
82+
"cfenv": "cpp"
8183
}
8284
}

compile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ mkdir -p src/build
55
cp src/assets/settings.json src/build -n
66

77
# compile project
8-
g++ -I src/library -I src/library/log -I src/library/settings -I src/library/cpu -I src/library/memory -I src/library/telegram \
9-
src/library/log/Log.cpp src/library/settings/Settings.cpp src/library/cpu/CpuMonitor.cpp src/library/memory/MemoryMonitor.cpp src/library/telegram/TelegramMonitor.cpp \
8+
g++ -I src/library -I src/library/log -I src/library/settings -I src/library/cpu -I src/library/memory -I src/library/telegram -I src/library/app \
9+
src/library/log/Log.cpp src/library/settings/Settings.cpp src/library/cpu/CpuMonitor.cpp src/library/memory/MemoryMonitor.cpp src/library/telegram/TelegramMonitor.cpp src/library/app/App.cpp \
1010
src/main.cpp -o src/build/LinuxMonitoring \
1111
-pthread -lcurl --std=c++14 -I/usr/local/include -lTgBot -lboost_system -lssl -lcrypto -lpthread
1212

src/library/app/App.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "App.hpp"
2+
3+
App::App(){}
4+
5+
int App::execute()
6+
{
7+
this->checkSetting();
8+
9+
// set monitoring default status
10+
this->isMonitoringEnable = this->settings.getDefaultMonitoringStatus();
11+
12+
this->printWelcome();
13+
14+
// Monitoring Objects
15+
CpuMonitor cpu(settings.getCpuCheckDuration());
16+
MemoryMonitor memory(settings.getMemoryCheckDuration());
17+
TelegramMonitor telegram(isMonitoringEnable, cpu, memory, settings, logger);
18+
19+
// Start Monitoring
20+
telegram.startTelegramRequestThread();
21+
22+
// hold app
23+
this->hold(cpu, memory, telegram);
24+
}
25+
26+
bool App::checkSetting()
27+
{
28+
// load settings
29+
if (settings.getSetting())
30+
{
31+
return true;
32+
}
33+
34+
logger.logToConsole("Failed to load the settings file. Attempting to rebuild configuration...");
35+
36+
// build settings file
37+
if (settings.createSettingsFile())
38+
{
39+
logger.logToConsole("Settings initializing successfully!");
40+
settings.getSetting();
41+
return true;
42+
}
43+
44+
return false;
45+
}
46+
47+
void App::printWelcome()
48+
{
49+
this->logger.logToConsole("Linux Monitoring v" + this->settings.getAppVersion() + " Service Started");
50+
this->logger.logToConsole(this->settings.getServerName());
51+
}
52+
53+
void App::hold(CpuMonitor &cpu, MemoryMonitor &memory, TelegramMonitor &telegram)
54+
{
55+
while (true)
56+
{
57+
// Start Monitoring
58+
if (this->isMonitoringEnable)
59+
{
60+
cpu.startMonitoring();
61+
memory.startMonitoring();
62+
telegram.startTelegramNotificationWatchThread();
63+
}
64+
// Stop Monitoring
65+
if (!this->isMonitoringEnable)
66+
{
67+
cpu.stopMonitoring();
68+
memory.stopMonitoring();
69+
telegram.stopTelegramNotificationWatchThread();
70+
}
71+
sleep(1);
72+
continue;
73+
}
74+
}

src/library/app/App.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include "log/Log.hpp"
4+
#include "settings/Settings.hpp"
5+
#include "cpu/CpuMonitor.hpp"
6+
#include "memory/MemoryMonitor.hpp"
7+
#include "telegram/TelegramMonitor.hpp"
8+
9+
class App
10+
{
11+
public:
12+
App();
13+
int execute();
14+
bool checkSetting();
15+
void printWelcome();
16+
17+
private:
18+
Log logger;
19+
Settings settings;
20+
bool isMonitoringEnable;
21+
22+
void hold(CpuMonitor &cpu, MemoryMonitor &memory, TelegramMonitor &telegram);
23+
};

src/library/cpu/CpuMonitor.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ void CpuMonitor::startMonitoring()
2727
// if monitoring disabled
2828
this->monitoringCpuStatus = true;
2929
monitorThread = std::thread(&CpuMonitor::thread_getCPUUsage, this);
30-
monitorThread.detach(); // Detach the thread so it runs in the background
30+
31+
// Detach the thread so it runs in the background
32+
monitorThread.detach();
3133
}
3234

3335
/**
@@ -137,14 +139,14 @@ void CpuMonitor::thread_getCPUUsage()
137139

138140
/**
139141
* @brief Stops the CPU monitoring process.
140-
*
141-
* This function sets the `monitoringCpuStatus` flag to `false`,
142+
*
143+
* This function sets the `monitoringCpuStatus` flag to `false`,
142144
* indicating that the CPU monitoring should be stopped. Any ongoing
143145
* monitoring activities will cease, and the system will no longer
144146
* collect or process CPU usage data.
145-
*
147+
*
146148
* This method should be called when monitoring is no longer needed
147-
* to ensure that resources are released and the monitoring process
149+
* to ensure that resources are released and the monitoring process
148150
* is gracefully terminated.
149151
*/
150152
void CpuMonitor::stopMonitoring()

0 commit comments

Comments
 (0)