添加监控的报警接口

master
zcy 2022-04-07 23:18:03 +08:00
parent d42d5f1330
commit d47c777a26
3 changed files with 145 additions and 49 deletions

View File

@ -108,6 +108,7 @@ int CProcessMonitor::GetMemoryUsage(uint64_t* mem, uint64_t* vmem)
CProcessMonitor::~CProcessMonitor()
{
clear();
}
@ -122,6 +123,14 @@ CProcessMonitor::CProcessMonitor()
init();
_processor = get_processor_number();
this->m_log_status = STATUS_STOP;
this->m_alert = false;
this->m_cpu_threshold = 30;
this->m_virtual_memory_threshold = uint64_t(2048) * 1024 * 1024;
this->m_physical_memory_threshold = uint64_t(2048) * 1024 * 1024;
}
std::list<CProcessMonitor::ProcessStatus *> & CProcessMonitor::Logs(){
return this->m_logs;
}
CProcessMonitor::CProcessMonitor(DWORD ProcessID)
@ -130,6 +139,10 @@ CProcessMonitor::CProcessMonitor(DWORD ProcessID)
_processor = get_processor_number();
this->SetPID(ProcessID);
this->m_log_status = STATUS_STOP;
this->m_alert = false;
this->m_cpu_threshold = 30;
this->m_virtual_memory_threshold = uint64_t(2048) * 1024 * 1024;
this->m_physical_memory_threshold = uint64_t(2048) * 1024 * 1024;
}
float CProcessMonitor::GetCpuUsage()
@ -205,24 +218,51 @@ int CProcessMonitor::StopLog(){
return 0;
}
int CProcessMonitor::StartLog(uint64_t milisecond){
if(m_log_status != STATUS_STOP )
return -1;
if(m_logs.size() > 0){
for(auto itr = m_logs.begin();itr != m_logs.end();itr++){
delete *itr;
}
m_logs.clear();
}
this->m_log_status = STATUS_STARTING;
m_thread = new std::thread([this,milisecond](){
if(m_log_status == CProcessMonitor::STATUS_STARTING){
int CProcessMonitor::SetAlert(bool){
m_mux.lock();
m_log_status = CProcessMonitor::STATUS_LOGGING;
this->m_alert = true;
m_mux.unlock();
return 0;
}
int CProcessMonitor::SetAlertThreshold(int cpu, uint64_t mem,uint64_t mem2){
m_mux.lock();
m_cpu_threshold = cpu;
m_virtual_memory_threshold = mem;
m_physical_memory_threshold = mem2;
m_mux.unlock();
return 0;
}
int CProcessMonitor::SetMemoryAlertNotify(OnMemoryAlert alert){
m_mux.lock();
this->m_on_memory_alert = alert;
m_mux.unlock();
return 0;
}
int CProcessMonitor::SetCpuAlertNotify(OnCpuAlert alert){
m_mux.lock();
this->m_on_cpu_alert = alert;
m_mux.unlock();
return 0;
}
void MonitorProcess(CProcessMonitor *pr,uint64_t milisecond) {
if(pr->m_log_status == CProcessMonitor::STATUS_STARTING){
pr->m_mux.lock();
pr->m_log_status = CProcessMonitor::STATUS_LOGGING;
pr->m_mux.unlock();
}
while(m_log_status == CProcessMonitor::STATUS_LOGGING){
while(pr->m_log_status == CProcessMonitor::STATUS_LOGGING){
Sleep(milisecond);
SYSTEMTIME stTime;
GetLocalTime(&stTime);
WORD wYear = stTime.wYear;
@ -233,21 +273,56 @@ int CProcessMonitor::StartLog(uint64_t milisecond){
WORD wSecond = stTime.wSecond;
CString m_date;
//m_date字符串即为当前时间。如2010年4月23日 11:12:45
m_date.Format("%4d-%2d-%2d %2d:%2d:%2d", wYear, wMonth, wDay, wHour, wMinute, wSecond);
m_date.Format("%4d-%2d-%2d %2d:%2d:%2d", wYear, wMonth, wDay,
wHour, wMinute, wSecond);
USES_CONVERSION;
std::string strS(m_date); //CString-->std::string
ProcessStatus *p = new ProcessStatus;
CProcessMonitor::ProcessStatus *p = new CProcessMonitor::ProcessStatus;
p->time = strS;
uint64_t vmem,pmem;
this->GetMemoryUsage(&pmem,&vmem);
pr->GetMemoryUsage(&pmem,&vmem);
p->phsical_memory = pmem;
p->virtual_memory = vmem;
p->cpu = this->GetCpuUsage();
this->m_logs.push_back(p);
p->cpu = pr->GetCpuUsage();
pr->m_logs.push_back(p);
if(pr->m_alert){
bool flag = false;
// 物理内存大于阀值
if(p->phsical_memory > pr->m_physical_memory_threshold){
if(pr->m_on_memory_alert){
pr->m_on_memory_alert(p->phsical_memory, p->virtual_memory);
}
});
}
// 虚拟内存大于监控阀值
if(p->virtual_memory > pr->m_virtual_memory_threshold) {
if(pr->m_on_memory_alert)
pr->m_on_memory_alert(p->phsical_memory, p->virtual_memory);
}
// cpu大于被监控cpu
if(p->cpu > pr->m_cpu_threshold){
if(pr->m_on_cpu_alert){
pr->m_on_cpu_alert(p->cpu);
}
}
}
}
}
int CProcessMonitor::StartLog(uint64_t milisecond){
if(m_log_status != STATUS_STOP )
return -1;
if(m_logs.size() > 0){
for(auto itr = m_logs.begin();itr != m_logs.end();itr++){
delete *itr;
}
m_logs.clear();
}
this->m_log_status = STATUS_STARTING;
m_thread = new std::thread(MonitorProcess,this,milisecond);
return 0;
}
#endif

View File

@ -34,7 +34,6 @@ using namespace std;
#include <mutex>
class CProcessMonitor {
#ifdef _WIN64 || _WIN32
public:
@ -44,13 +43,16 @@ public:
STATUS_STOP, // 停止记录
}LOG_STATUS;
typedef std::function<void (float)> OnCpuAlert; // cpu告警
typedef std::function<void (uint64_t,uint64_t)> OnMemoryAlert; // 内存告警
typedef struct {
std::string time;
float cpu;
uint64_t phsical_memory;
uint64_t virtual_memory;
}ProcessStatus; // 某一个时刻的进程状态
std::list<ProcessStatus *> &Logs();
CProcessMonitor(DWORD ProcessID);
CProcessMonitor();
~CProcessMonitor();
@ -61,11 +63,22 @@ public:
int GetMemoryUsage(uint64_t* mem, uint64_t* vmem);
int StartLog(uint64_t milisecond);
int StopLog();
LOG_STATUS m_log_status;
std::list<ProcessStatus*> m_logs;
int SetAlert(bool);
int SetAlertThreshold(int cpu, uint64_t mem,uint64_t mem2);
// 设置内存报警的回调函数
int SetMemoryAlertNotify(OnMemoryAlert );
// 设置cpu报警的回调函数
int SetCpuAlertNotify(OnCpuAlert );
friend void MonitorProcess(CProcessMonitor *,uint64_t );
private:
std::list<ProcessStatus*> m_logs;
LOG_STATUS m_log_status;
uint64_t m_virtual_memory_threshold;
uint64_t m_physical_memory_threshold;
int m_cpu_threshold;
OnMemoryAlert m_on_memory_alert;
OnCpuAlert m_on_cpu_alert;
bool m_alert;
std::mutex m_mux;
std::thread *m_thread;
typedef long long int64_t;

View File

@ -2,24 +2,32 @@
#include "function/cpu_usage.h"
#include <Windows.h>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(int argc,char **argv){
CProcessMonitor usage1(19728);
int main(int argc, char **argv)
{
CProcessMonitor usage1(26644);
usage1.StartLog(1000);
usage1.SetAlert(true);
usage1.SetCpuAlertNotify([](float cpu){
std::cout<<"cpu alert detected "<<cpu<<std::endl;
});
while(true){
uint64_t vmem,pmem;
usage1.GetMemoryUsage(&pmem,&vmem);
std::cout<<"cpu usage is "<< usage1.GetCpuUsage() << " "<<pmem <<
"byte "<< vmem <<"byte seq "<< usage1.m_logs.size() << std::endl;
// std::cout<<"cpu usage is "<< usage1.GetCpuUsage() << " "<<pmem <<
// " byte "<< vmem <<" byte seq "<< usage1.Logs().size() << std::endl;
Sleep(1000);
for(auto itr = usage1.m_logs.begin();itr != usage1.m_logs.end();itr++){
std::cout<<(*itr)->time<<" "<<(*itr)->cpu;
}
// for (auto itr = usage1.m_logs.begin(); itr != usage1.m_logs.end(); itr++)
// {
// std::cout<<(*itr)->time<<" cpu: "<<(*itr)->cpu
// << " physical mem: " << (*itr)->phsical_memory
// << " virtual mem: " << (*itr)->virtual_memory
// <<std::endl;
// }
}
}