|
|
@@ -3556,7 +3556,7 @@ ULONGLONG ResourceWatcherFSM::GetDiskUsedByte()
|
|
|
return ulTotalBytes.QuadPart - ulTotalFree.QuadPart;
|
|
|
}
|
|
|
|
|
|
-__int64 Filetime2Int64(const FILETIME& ftime)
|
|
|
+long long Filetime2Int64(const FILETIME& ftime)
|
|
|
{
|
|
|
LARGE_INTEGER li;
|
|
|
li.LowPart = ftime.dwLowDateTime;
|
|
|
@@ -3564,7 +3564,7 @@ __int64 Filetime2Int64(const FILETIME& ftime)
|
|
|
return li.QuadPart;
|
|
|
}
|
|
|
|
|
|
-__int64 CompareFileTime2(const FILETIME& preTime, const FILETIME& nowTime)
|
|
|
+long long CompareFileTime2(const FILETIME& preTime, const FILETIME& nowTime)
|
|
|
{
|
|
|
return Filetime2Int64(nowTime) - Filetime2Int64(preTime);
|
|
|
}
|
|
|
@@ -3653,85 +3653,94 @@ void ResourceWatcherFSM::GetSystemCPUStatus()
|
|
|
}
|
|
|
CloseHandle(hProcessSnapNew);
|
|
|
|
|
|
- auto idle = CompareFileTime2(preIdleTime, idleTime);
|
|
|
- auto kernel = CompareFileTime2(preKernelTime, kernelTime);
|
|
|
- auto user = CompareFileTime2(preUserTime, userTime);
|
|
|
+ long long idle = CompareFileTime2(preIdleTime, idleTime);
|
|
|
+ long long kernel = CompareFileTime2(preKernelTime, kernelTime);
|
|
|
+ long long user = CompareFileTime2(preUserTime, userTime);
|
|
|
|
|
|
if (kernel + user == 0)
|
|
|
return;
|
|
|
|
|
|
- CSystemRunInfo runInfo = { 0 };
|
|
|
- GetEntityBase()->GetFunction()->GetSystemRunInfo(runInfo);
|
|
|
- for (int i = 0; i < runInfo.strRunningEntityNames.GetCount(); i++) //实体进程的名称默认全为sphost,需转换为对应实体名
|
|
|
- {
|
|
|
- runInfo.strRunningEntityNames[i];
|
|
|
- CEntityRunInfo entityInfo = { 0 };
|
|
|
- GetEntityBase()->GetFunction()->GetEntityRunInfo(runInfo.strRunningEntityNames[i].GetData(), entityInfo);
|
|
|
- processName[entityInfo.dwProcessID] = runInfo.strRunningEntityNames[i];
|
|
|
- }
|
|
|
-
|
|
|
- unordered_map<int, long long>::iterator it;
|
|
|
- for (it = newProcessTime.begin(); it != newProcessTime.end(); ++it)
|
|
|
- {
|
|
|
- int tPid = it->first;
|
|
|
- //进程的占用率 = 单位时间间隔里进程的CPU时间片占用 / 单位时间间隔里CPU的整体时间片
|
|
|
- if (oldProcessTime.find(tPid) == oldProcessTime.end())
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (sysProcName.find(processName[tPid].GetData()) != std::string::npos)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- // 转换为百分比
|
|
|
- double tRatio = 100.0 * (newProcessTime[tPid] - oldProcessTime[tPid]) / (kernel + user);
|
|
|
- if (tRatio > 0.0)
|
|
|
- {
|
|
|
- processCpu[tPid] = tRatio;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- vector<pair<int, double>> vec(processCpu.begin(), processCpu.end());
|
|
|
- sort(vec.begin(), vec.end(), cmp); //根据CPU使用率从大到小进行排序
|
|
|
- int len = min(10, vec.size());
|
|
|
- CSimpleStringA procWarn = "";
|
|
|
- for (int i = 0; i < len; ++i) //构建进程的CPU使用告警信息
|
|
|
- {
|
|
|
- procWarn = procWarn + ",\"" + processName[vec[i].first] + "\":\"" + CSimpleStringA::Format("%.2f", vec[i].second) + "%\"";
|
|
|
- }
|
|
|
-
|
|
|
+ //计算总CPU使用率
|
|
|
double cpuRatio = 100.0 * (kernel + user - idle) / (kernel + user);
|
|
|
-
|
|
|
if (m_cpuHighPercent <= 0)
|
|
|
{
|
|
|
- m_cpuHighPercent = 80; //默认告警百分比80%
|
|
|
+ m_cpuHighPercent = 80; //默认告警线:80%
|
|
|
}
|
|
|
-
|
|
|
if (cpuWarnThreshold <= 0)
|
|
|
{
|
|
|
cpuWarnThreshold = 10; //默认满10次告一次警
|
|
|
}
|
|
|
|
|
|
+ //超过设置的告警线时进行告警
|
|
|
if (cpuRatio > (double)m_cpuHighPercent)
|
|
|
{
|
|
|
- cpuWarnTime = cpuWarnTime % cpuWarnThreshold;
|
|
|
- if (cpuWarnTime == 0) //每达到一次阈值告警一次
|
|
|
+ cpuWarnTime = cpuWarnTime % cpuWarnThreshold;
|
|
|
+ if (cpuWarnTime == 0) //CPU使用率过高达到一定次数告警一次
|
|
|
{
|
|
|
+ //终端进程名称转换
|
|
|
+ CSystemRunInfo runInfo = { 0 };
|
|
|
+ GetEntityBase()->GetFunction()->GetSystemRunInfo(runInfo);
|
|
|
+ for (int i = 0; i < runInfo.strRunningEntityNames.GetCount(); i++) //实体进程的名称默认全为sphost,需转换为对应实体名
|
|
|
+ {
|
|
|
+ runInfo.strRunningEntityNames[i];
|
|
|
+ CEntityRunInfo entityInfo = { 0 };
|
|
|
+ GetEntityBase()->GetFunction()->GetEntityRunInfo(runInfo.strRunningEntityNames[i].GetData(), entityInfo);
|
|
|
+ processName[entityInfo.dwProcessID] = runInfo.strRunningEntityNames[i];
|
|
|
+ }
|
|
|
+
|
|
|
+ //进程占用率计算
|
|
|
+ unordered_map<int, long long>::iterator it;
|
|
|
+ for (it = newProcessTime.begin(); it != newProcessTime.end(); ++it)
|
|
|
+ {
|
|
|
+ int tPid = it->first;
|
|
|
+ //进程的占用率 = 单位时间间隔里进程的CPU时间片占用 / 单位时间间隔里CPU的整体时间片
|
|
|
+ if (oldProcessTime.find(tPid) == oldProcessTime.end())
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sysProcName.find(processName[tPid].GetData()) != std::string::npos)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转换为百分比
|
|
|
+ if (newProcessTime[tPid] >= oldProcessTime[tPid]) //数据合法
|
|
|
+ {
|
|
|
+ double tRatio = 100.0 * (newProcessTime[tPid] - oldProcessTime[tPid]) / (kernel + user);
|
|
|
+ processCpu[tPid] = tRatio;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据CPU使用率对进程从大到小进行排序
|
|
|
+ vector<pair<int, double>> vec(processCpu.begin(), processCpu.end());
|
|
|
+ sort(vec.begin(), vec.end(), cmp);
|
|
|
+ int len = min(10, vec.size());
|
|
|
+ CSimpleStringA procWarn = "";
|
|
|
+ for (int i = 0; i < len; ++i) //构建进程的CPU使用告警信息
|
|
|
+ {
|
|
|
+ procWarn = procWarn + ",\"" + processName[vec[i].first] + "\":\"" + CSimpleStringA::Format("%.2f", processCpu[vec[i].first]) + "%\"";
|
|
|
+ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM) //上传校验信息
|
|
|
+ ("CPU use info. [vec[%d].second]:%.2f; [processCpu[%d]]:%.2f",
|
|
|
+ i, vec[i].second, vec[i].first, processCpu[vec[i].first]);
|
|
|
+ }
|
|
|
+
|
|
|
+ //构建告警信息
|
|
|
CSimpleStringA warn = CSimpleStringA::Format("{\"type\":\"cpu\", \"total_used\":\" %.2f", cpuRatio);
|
|
|
warn = warn + "%\"" + procWarn + "}";
|
|
|
- LogWarn(Severity_Middle, Error_Resource, LOG_EVT_RESOURCE_CPU_TOO_HIGH, warn.GetData());
|
|
|
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA5A03").setAPI("CpuUsedCheck")(warn.GetData());
|
|
|
if (cpuRatio > 98) //cpu极端异常情况
|
|
|
{
|
|
|
LogWarn(Severity_Middle, Error_Resource, LOG_EVT_RESOURCE_CPU_ERROR, warn.GetData());
|
|
|
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA5A02")(warn.GetData());
|
|
|
+ DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA5A02").setAPI("CpuUsedCheck")(warn.GetData());
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LogWarn(Severity_Middle, Error_Resource, LOG_EVT_RESOURCE_CPU_TOO_HIGH, warn.GetData());
|
|
|
+ DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA5A03").setAPI("CpuUsedCheck")(warn.GetData());
|
|
|
}
|
|
|
}
|
|
|
cpuWarnTime++;
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
unsigned long long convert_time_format(const FILETIME* ftime)
|