Skip to content

Commit 388938e

Browse files
committed
解决了网络闪断导致的错误报警
1 parent f912794 commit 388938e

File tree

2 files changed

+106
-8
lines changed

2 files changed

+106
-8
lines changed

server/src/main.cpp

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,9 @@ void CMain::OnDelClient(int ClientNetID)
117117
Client(ClientID)->m_ClientNetType = NETTYPE_INVALID;
118118
mem_zero(&Client(ClientID)->m_Stats, sizeof(CClient::CStats));
119119
}
120-
//copy offline message for watchdog
121-
WatchdogMessage(ClientNetID,
122-
0, 0, 0, 0, 0, 0,
123-
0, 0, 0,0, 0, 0,
124-
0, 0, 0, 0, 0, 0,
125-
0, 0, 0,0, 0, 0,
126-
0, 0, 0, 0);
120+
m_OfflineAlarmThreadData.pClients = m_aClients;
121+
m_OfflineAlarmThreadData.pWatchDogs = m_aCWatchDogs;
122+
thread_create(offlineAlarmThread, &m_OfflineAlarmThreadData);
127123
}
128124

129125
int CMain::HandleMessage(int ClientNetID, char *pMessage)
@@ -503,6 +499,105 @@ void CMain::JSONUpdateThread(void *pUser)
503499
fs_rename(pConfig->m_aJSONFile, aJSONFileTmp);
504500
}
505501

502+
void CMain::offlineAlarmThread(void *pUser)
503+
{
504+
CJSONUpdateThreadData *m_OfflineAlarmThreadData = (CJSONUpdateThreadData *)pUser;
505+
CClient *pClients = m_OfflineAlarmThreadData->pClients;
506+
CWatchDog *pWatchDogs = m_OfflineAlarmThreadData->pWatchDogs;
507+
thread_sleep(6000);
508+
if(!pClients->m_Connected)
509+
{
510+
int ID = 0;
511+
while (strcmp(pWatchDogs[ID].m_aName, "NULL"))
512+
{
513+
typedef exprtk::symbol_table<double> symbol_table_t;
514+
typedef exprtk::expression<double> expression_t;
515+
typedef exprtk::parser<double> parser_t;
516+
const std::string expression_string = pWatchDogs[ID].m_aRule;
517+
std::string username = pClients->m_aUsername;
518+
std::string name = pClients->m_aName;
519+
std::string type = pClients->m_aType;
520+
std::string host = pClients->m_aHost;
521+
std::string location = pClients->m_aLocation;
522+
std::double_t online4 = pClients->m_Stats.m_Online4;
523+
std::double_t online6 = pClients->m_Stats.m_Online6;
524+
525+
symbol_table_t symbol_table;
526+
symbol_table.add_stringvar("username", username);
527+
symbol_table.add_stringvar("name", name);
528+
symbol_table.add_stringvar("type", type);
529+
symbol_table.add_stringvar("host", host);
530+
symbol_table.add_stringvar("location", location);
531+
symbol_table.add_variable("online4",online4);
532+
symbol_table.add_variable("online6",online6);
533+
symbol_table.add_constants();
534+
535+
expression_t expression;
536+
expression.register_symbol_table(symbol_table);
537+
538+
parser_t parser;
539+
parser.compile(expression_string,expression);
540+
541+
if (expression.value() > 0)
542+
{
543+
time_t currentStamp = (long long)time(/*ago*/0);
544+
if ((currentStamp-pClients->m_AlarmLastTime) > pWatchDogs[ID].m_aInterval)
545+
{
546+
printf("客户端下线, Client disconnects and sends alert information\n");
547+
pClients->m_AlarmLastTime = currentStamp;
548+
CURL *curl;
549+
CURLcode res;
550+
curl_global_init(CURL_GLOBAL_ALL);
551+
552+
curl = curl_easy_init();
553+
if(curl) {
554+
//standard time
555+
char standardTime[32]= { 0 };
556+
strftime(standardTime, sizeof(standardTime), "%Y-%m-%d %H:%M:%S",localtime(&currentStamp));
557+
558+
//url encode, Rules conflict with url special characters,eg:&, del rules, by https://cpp.la, 2023-10-09
559+
char encodeBuffer[2048] = { 0 };
560+
sprintf(encodeBuffer, "【告警名称】 %s \n\n【告警时间】 %s \n\n【用户名】 %s \n\n【节点名】 %s \n\n【虚拟化】 %s \n\n【主机名】 %s \n\n【位 置】 %s",
561+
pWatchDogs[ID].m_aName,
562+
standardTime,
563+
pClients->m_aUsername,
564+
pClients->m_aName,
565+
pClients->m_aType,
566+
pClients->m_aHost,
567+
pClients->m_aLocation);
568+
char *encodeUrl = curl_easy_escape(curl, encodeBuffer, strlen(encodeBuffer));
569+
570+
//standard url
571+
char urlBuffer[2048] = { 0 };
572+
sprintf(urlBuffer, "%s%s",pWatchDogs[ID].m_aCallback, encodeUrl);
573+
574+
575+
curl_easy_setopt(curl, CURLOPT_POST, 1L);
576+
curl_easy_setopt(curl, CURLOPT_URL, urlBuffer);
577+
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,"signature=ServerStatus");
578+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
579+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
580+
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3L);
581+
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 6L);
582+
res = curl_easy_perform(curl);
583+
if(res != CURLE_OK)
584+
fprintf(stderr, "watchdog failed: %s\n", curl_easy_strerror(res));
585+
if(encodeUrl)
586+
curl_free(encodeUrl);
587+
curl_easy_cleanup(curl);
588+
}
589+
curl_global_cleanup();
590+
}
591+
}
592+
ID++;
593+
}
594+
}
595+
else
596+
{
597+
printf("网络波动,No alarm information is sent due to network fluctuations\n");
598+
}
599+
}
600+
506601
int CMain::ReadConfig()
507602
{
508603
// read and parse config
@@ -701,6 +796,7 @@ int CMain::Run()
701796
m_JSONUpdateThreadData.m_ReloadRequired = 2;
702797
m_JSONUpdateThreadData.pClients = m_aClients;
703798
m_JSONUpdateThreadData.pConfig = &m_Config;
799+
m_JSONUpdateThreadData.pWatchDogs = m_aCWatchDogs;
704800
void *LoadThread = thread_create(JSONUpdateThread, &m_JSONUpdateThreadData);
705801
//thread_detach(LoadThread);
706802

server/src/main.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@ class CMain
101101
{
102102
CClient *pClients;
103103
CConfig *pConfig;
104+
CWatchDog *pWatchDogs;
104105
volatile short m_ReloadRequired;
105-
} m_JSONUpdateThreadData;
106+
} m_JSONUpdateThreadData, m_OfflineAlarmThreadData;
106107

107108
static void JSONUpdateThread(void *pUser);
109+
static void offlineAlarmThread(void *pUser);
108110
public:
109111
CMain(CConfig Config);
110112

0 commit comments

Comments
 (0)