add network minitor.
Some checks failed
Deploy / PullDocker (push) Successful in 7s
Deploy / Build (push) Failing after 1m20s
Deploy Docker Images / Docusaurus build and Server deploy (push) Successful in 19s

This commit is contained in:
amass 2024-11-09 19:21:15 +08:00
parent 87a11914f8
commit 1a84bdc1a3
3 changed files with 93 additions and 1 deletions

View File

@ -39,6 +39,24 @@ jobs:
echo -n "提交消息: ${{ github.event.head_commit.message }}">> notify.tpl
cat notify.tpl | envsubst | jq -sR . | xargs -0 -I {} curl -H "Content-Type: application/json" -X POST -d '{"type":"text","msg":{} }' https://amass.fun/notify
- run: resources/build.sh build
- name: Copy files to server
uses: appleboy/scp-action@v0.1.3
with:
host: ${{ vars.YUYUN_SERVER }}
username: ${{ vars.YUYUN_USER }}
password: ${{ secrets.SERVER_ROOT_PASSWORD }}
source: build/Server/HttpServer
target: /root/Server/HttpServer
- name: Restart server
uses: appleboy/ssh-action@v0.1.3
with:
host: ${{ vars.YUYUN_SERVER }}
username: ${{ vars.YUYUN_USER }}
password: ${{ secrets.SERVER_ROOT_PASSWORD }}
script: |
pkill -f HttpServer || true
cp /root/Server/HttpServer /root/Server/HttpServer.bak
nohup /root/Server/HttpServer > /dev/null 2>&1 &
- name: Notify-End
if: ${{ always() }}
run: |

View File

@ -1,6 +1,11 @@
#include "SystemUsage.h"
#include "BoostLog.h"
#include "DateTime.h"
#include "NetworkUtility.h"
#include "ServiceManager.h"
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/json/object.hpp>
#include <boost/json/serialize.hpp>
#include <fstream>
@ -15,7 +20,8 @@ SystemUsage::SystemUsage(boost::asio::io_context &ioContext, const std::string &
}
void SystemUsage::start() {
m_timer->expires_after(std::chrono::seconds(10));
using namespace std::chrono;
m_timer->expires_after(seconds(10));
m_timer->async_wait([this](const boost::system::error_code &error) {
if (error) {
LOG(error) << error.message();
@ -27,6 +33,35 @@ void SystemUsage::start() {
publish("yuyun_disk_usage", static_cast<int>(100.0f * diskUsage("/")), "%", "磁盘占用率");
m_lastCpuStats = currentCpuStats;
m_networkStats.push_back(networkStats("eth0"));
while (m_networkStats.size() >= 2) {
constexpr auto SpeedInterval = seconds(60 * 15);
auto front = m_networkStats.front();
auto back = m_networkStats.back();
auto duration = duration_cast<seconds>(back.time - front.time);
if (duration > SpeedInterval) {
m_networkStats.pop_front();
} else {
constexpr auto gigabyte = 1024 * 1024 * 1024;
auto receiveGigabyte = static_cast<float>(back.receiveBytes - front.receiveBytes) / gigabyte;
auto transmitGigabyte = static_cast<float>(back.transmitBytes - front.transmitBytes) / gigabyte;
auto speed = (receiveGigabyte + transmitGigabyte) / duration.count() * SpeedInterval.count();
LOG(info) << "network speed: " << std::fixed << std::setprecision(2) << speed << "GB/h";
if (speed >= 1.f) { // 一个小时1GB的流量
std::ostringstream oss;
oss << "当前服务器流量存在异常, " << DateTime::toString(front.time) << " - "
<< DateTime::toString(back.time) << ": " << std::endl;
oss << "进出总速率: " << std::fixed << std::setprecision(2) << speed << "GB/h" << std::endl;
oss << "接收流量: " << std::fixed << std::setprecision(2) << receiveGigabyte << "GB" << std::endl;
oss << "发送流量: " << std::fixed << std::setprecision(2) << transmitGigabyte << "GB" << std::endl;
auto manager = Amass::Singleton<ServiceManager>::instance();
if (manager) manager->sendMessage(NotifyServerChan, oss.str());
LOG(warning) << oss.str();
}
break;
}
}
start();
});
}
@ -94,3 +129,32 @@ float SystemUsage::diskUsage(const std::string &disk) {
return result;
}
SystemUsage::NetworkStats SystemUsage::networkStats(const std::string &interface) {
NetworkStats ret;
try {
std::ifstream ifs("/proc/net/dev");
if (!ifs.is_open()) {
LOG(error) << "failed to open /proc/net/dev";
return ret;
}
std::string line;
while (std::getline(ifs, line)) {
if (line.find(interface) == std::string::npos) continue;
boost::trim(line);
std::vector<std::string> results;
boost::split(results, line, boost::algorithm::is_any_of(" "), boost::token_compress_on);
if (results.size() < 9) continue;
ret.interface = results.at(0);
ret.interface = ret.interface.substr(0, ret.interface.size() - 1); // Remove the trailing ':'
ret.receiveBytes = std::stoll(results.at(1));
ret.transmitBytes = std::stoll(results.at(9));
// LOG(info) << "interface: " << ret.interface << ", receive bytes: " << ret.receiveBytes
// << ", transmit bytes: " << ret.transmitBytes;
break;
}
} catch (const std::exception &e) {
LOG(error) << e.what();
}
return ret;
}

View File

@ -2,6 +2,7 @@
#define __SYSTEMUSAGE_H__
#include <boost/asio/system_timer.hpp>
#include <list>
/**
* @brief https://github.com/improvess/cpp-linux-system-stats
@ -29,6 +30,13 @@ public:
return user + nice + system + irq + softirq + steal + guest + guestNice;
}
};
struct NetworkStats {
std::string interface;
int64_t receiveBytes = 0;
int64_t transmitBytes = 0;
std::chrono::system_clock::time_point time = std::chrono::system_clock::now();
};
SystemUsage(boost::asio::io_context &ioContext, const std::string &accessToken);
void start();
@ -38,11 +46,13 @@ protected:
CpuStats readCpuData();
float cpuUsage(const CpuStats &first, const CpuStats &second);
float diskUsage(const std::string &disk);
NetworkStats networkStats(const std::string &interface);
private:
boost::asio::io_context &m_ioContext;
std::shared_ptr<boost::asio::system_timer> m_timer;
std::string m_accessToken;
CpuStats m_lastCpuStats;
std::list<NetworkStats> m_networkStats;
};
#endif // __SYSTEMUSAGE_H__