-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.c
425 lines (355 loc) · 15.4 KB
/
report.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "include.h"
Server *pServerSC = NULL;
struct ReportListHead *Head; /* List head. */
static pthread_mutex_t *send_mutex = NULL;
struct timeval tv;
void addICMPReport(struct Task * task) {
static pthread_mutex_t *mutex = NULL;
if (!mutex) {
mutex = calloc(1, sizeof (*mutex));
pthread_mutex_init(mutex, NULL);
}
debug("mutex_lock")
pthread_mutex_lock(mutex);
pthread_mutex_lock(send_mutex);
struct _chg_ping *chg;
struct stICMPInfo * poll = (struct stICMPInfo *) (task->poll);
incStat(task->Record.ModType, task->code);
ReportEntryPtr = getNulledMemory(sizeof (struct ReportListEntry)); /* Insert at the head. */
ReportEntryPtr->ModType = MODULE_PING;
ReportEntryPtr->Len = sizeof (struct _chg_ping);
chg = getNulledMemory(ReportEntryPtr->Len);
chg->LObjId = task->LObjId;
chg->IP = task->Record.IP;
chg->CheckDt = LocalToRemoteTime(poll->CheckDt.tv_sec);
chg->CheckRes = task->code == STATE_DONE ? 0 : -1;
#ifdef CFG_3
chg->CheckStatus=(chg->CheckRes)?0:1;
#endif
gettimeofday(&tv, NULL);
chg->DelayMS = task->code == STATE_DONE ? timeDiffMS(tv, poll->CheckDt) : 0xffff;
chg->ProblemIP = poll->ProblemIP;
chg->ProblemICMP_Code = poll->ProblemICMP_Code;
chg->ProblemICMP_Type = poll->ProblemICMP_Type;
if (task->isVerifyTask) {
chg->Flags |= TESTER_FLAG_CHECK_TASK;
}
chg->CFGVer = task->Record.CFGVer;
if (task->isCfgChange) {
task->isCfgChange = 0;
chg->Flags |= TESTER_FLAG_CHANGE_CFGVER;
task->Record.CFGVer = task->Record.CFGVer == 249 ? 1 : task->Record.CFGVer + 1;
}
debug("%d -> Module %s for %s [%s:%d] [ms: %d] [time test:%d] Result %s",
task->Record.LObjId,
getModuleText(task->Record.ModType),
task->Record.HostName, ipString(task->Record.IP), task->Record.Port,
poll->DelayMS,
poll->CheckDt.tv_sec,
getStatusText(task->code));
ReportEntryPtr->Data = (char *) chg;
if (pServerSC) {
ReportEntryPtrForDC = getNulledMemory(sizeof (struct ReportListEntry));
memcpy(ReportEntryPtrForDC, ReportEntryPtr, sizeof (struct ReportListEntry));
ReportEntryPtrForDC->Data = getNulledMemory(ReportEntryPtr->Len);
memcpy(ReportEntryPtrForDC->Data, ReportEntryPtr->Data, ReportEntryPtr->Len);
if (task->code == STATE_DONE) {
pServerSC->report.counterReport++;
} else {
pServerSC->report.counterReportError++;
}
if (chg->CheckRes > -1) {
LIST_INSERT_HEAD(&pServerSC->report.ReportHead, ReportEntryPtrForDC, entries);
} else {
LIST_INSERT_HEAD(&pServerSC->report.ReportErrorHead, ReportEntryPtrForDC, entries);
}
}
if (task->code == STATE_DONE) {
task->pServer->report.counterReport++;
} else {
task->pServer->report.counterReportError++;
}
if (chg->CheckRes > -1) {
LIST_INSERT_HEAD(&task->pServer->report.ReportHead, ReportEntryPtr, entries);
} else {
LIST_INSERT_HEAD(&task->pServer->report.ReportErrorHead, ReportEntryPtr, entries);
}
debug("mutex_unlock")
pthread_mutex_unlock(send_mutex);
pthread_mutex_unlock(mutex);
}
void addTCPReport(struct Task * task) {
static pthread_mutex_t *mutex;
if (!mutex) {
mutex = calloc(1, sizeof (*mutex));
pthread_mutex_init(mutex, NULL);
}
pthread_mutex_lock(mutex);
pthread_mutex_lock(send_mutex);
debug("mutex_lock");
struct _chg_tcp *chg;
struct stTCPUDPInfo * poll = (struct stTCPUDPInfo *) (task->poll);
incStat(task->Record.ModType, task->code);
ReportEntryPtr = calloc(1, sizeof (struct ReportListEntry)); /* Insert at the head. */
ReportEntryPtr->ModType = task->Record.ModType;
ReportEntryPtr->Len = sizeof (struct _chg_tcp);
chg = getNulledMemory(ReportEntryPtr->Len);
chg->LObjId = task->LObjId;
chg->IP = task->Record.IP;
chg->CheckDt = LocalToRemoteTime(poll->CheckDt.tv_sec);
chg->CheckRes = task->code == STATE_DONE ? 0 : -1;
#ifdef CFG_3
chg->CheckStatus=(chg->CheckRes)?0:1;
#endif
if (task->isVerifyTask) {
chg->Flags |= TESTER_FLAG_CHECK_TASK;
}
chg->CFGVer = task->Record.CFGVer;
if (task->isCfgChange) {
task->isCfgChange = 0;
chg->Flags |= TESTER_FLAG_CHANGE_CFGVER;
task->Record.CFGVer = task->Record.CFGVer == 249 ? 1 : task->Record.CFGVer + 1;
}
gettimeofday(&tv, NULL);
chg->DelayMS = task->code == STATE_DONE ? timeDiffMS(tv, poll->CheckDt) : 0xffff;
chg->ProblemIP = poll->ProblemIP;
chg->ProblemICMP_Code = poll->ProblemICMP_Code;
chg->ProblemICMP_Type = poll->ProblemICMP_Type;
chg->Port = task->Record.Port;
debug("%d -> Module %s for %s [%s:%d] [ms: %d] [time test:%d] Flags is %d, Result %s, to %s",
task->Record.LObjId,
getModuleText(task->Record.ModType),
task->Record.HostName, ipString(task->Record.IP), task->Record.Port,
chg->DelayMS,
poll->CheckDt.tv_sec,
chg->Flags,
getStatusText(task->code),
task->pServer->host
);
ReportEntryPtr->Data = (char *) chg;
if (pServerSC) {
ReportEntryPtrForDC = getNulledMemory(sizeof (struct ReportListEntry));
memcpy(ReportEntryPtrForDC, ReportEntryPtr, sizeof (struct ReportListEntry));
ReportEntryPtrForDC->Data = getNulledMemory(ReportEntryPtr->Len);
memcpy(ReportEntryPtrForDC->Data, ReportEntryPtr->Data, ReportEntryPtr->Len);
if (task->code == STATE_DONE) {
pServerSC->report.counterReport++;
} else {
pServerSC->report.counterReportError++;
}
if (chg->CheckRes > -1) {
LIST_INSERT_HEAD(&pServerSC->report.ReportHead, ReportEntryPtrForDC, entries);
} else {
LIST_INSERT_HEAD(&pServerSC->report.ReportErrorHead, ReportEntryPtrForDC, entries);
}
}
if (task->code == STATE_DONE) {
task->pServer->report.counterReport++;
} else {
task->pServer->report.counterReportError++;
}
if (chg->CheckRes > -1) {
LIST_INSERT_HEAD(&task->pServer->report.ReportHead, ReportEntryPtr, entries);
} else {
LIST_INSERT_HEAD(&task->pServer->report.ReportErrorHead, ReportEntryPtr, entries);
}
pthread_mutex_unlock(send_mutex);
pthread_mutex_unlock(mutex);
debug("mutex_unlock")
}
void addDNSReport(struct Task * task) {
static pthread_mutex_t *mutex;
if (!mutex) {
mutex = calloc(1, sizeof (*mutex));
pthread_mutex_init(mutex, NULL);
}
debug("mutex_lock")
pthread_mutex_lock(mutex);
pthread_mutex_lock(send_mutex);
struct _chg_tcp *chg;
struct DNSTask * dnstask = (struct DNSTask *) (task->poll);
incStat(task->Record.ModType, task->code);
ReportEntryPtr = calloc(1, sizeof (struct ReportListEntry)); /* Insert at the head. */
ReportEntryPtr->ModType = task->Record.ModType;
ReportEntryPtr->Len = sizeof (struct _chg_tcp);
chg = getNulledMemory(ReportEntryPtr->Len);
chg->LObjId = task->LObjId;
chg->IP = task->Record.IP;
if (task->isVerifyTask) {
chg->Flags |= TESTER_FLAG_CHECK_TASK;
}
chg->CheckDt = LocalToRemoteTime(dnstask->CheckDt.tv_sec);
chg->CheckRes = task->code == STATE_DONE ? 0 : -1;
#ifdef CFG_3
chg->CheckStatus=(chg->CheckRes)?0:1;
#endif
gettimeofday(&tv, NULL);
chg->DelayMS = task->code == STATE_DONE ? timeDiffMS(tv, dnstask->CheckDt) : 0xffff;
chg->Port = task->Record.Port;
debug("%d -> Module %s for %s [%s:%d] [ms: %d] [time test:%d] Flags is %d Result %s, to %s",
task->Record.LObjId,
getModuleText(task->Record.ModType),
task->Record.HostName, ipString(task->Record.IP), task->Record.Port,
chg->DelayMS,
dnstask->CheckDt.tv_sec,
chg->Flags,
getStatusText(task->code),
task->pServer->host);
ReportEntryPtr->Data = (char *) chg;
if (pServerSC) {
ReportEntryPtrForDC = getNulledMemory(sizeof (struct ReportListEntry));
memcpy(ReportEntryPtrForDC, ReportEntryPtr, sizeof (struct ReportListEntry));
ReportEntryPtrForDC->Data = getNulledMemory(ReportEntryPtr->Len);
memcpy(ReportEntryPtrForDC->Data, ReportEntryPtr->Data, ReportEntryPtr->Len);
if (task->code == STATE_DONE) {
pServerSC->report.counterReport++;
} else {
pServerSC->report.counterReportError++;
}
if (chg->CheckRes > -1) {
LIST_INSERT_HEAD(&pServerSC->report.ReportHead, ReportEntryPtrForDC, entries);
} else {
LIST_INSERT_HEAD(&pServerSC->report.ReportErrorHead, ReportEntryPtrForDC, entries);
}
}
if (task->code == STATE_DONE) {
task->pServer->report.counterReport++;
} else {
task->pServer->report.counterReportError++;
}
if (chg->CheckRes > -1) {
LIST_INSERT_HEAD(&task->pServer->report.ReportHead, ReportEntryPtr, entries);
} else {
LIST_INSERT_HEAD(&task->pServer->report.ReportErrorHead, ReportEntryPtr, entries);
}
pthread_mutex_unlock(send_mutex);
pthread_mutex_unlock(mutex);
debug("mutex_unlock")
}
u32 countReport(Server *pServer) {
return pServer->report.counterReport;
}
u32 countReportError(Server *pServer) {
return pServer->report.counterReportError;
}
void inline debugPrintReport(struct ReportListEntry * ReportEntry) {
debug("\tтип модуля тестирования (пинг-порт-хттп-...) = %s", getModuleText(ReportEntry->ModType));
if (ReportEntry->ModType == MODULE_PING) {
struct _chg_ping *report = (struct _chg_ping *) ReportEntry->Data;
debug("\tid объекта тестирования = %d", report->LObjId);
debug("\tдата проверки = %x", report->CheckDt);
debug("\tип объекта тестирования = %s", ipString(report->IP));
#ifdef CFG_3
debug("\tстатус = %d", report->CheckStatus);
#endif
debug("\tрезультат = %d", report->CheckRes);
debug("\tвремя ответа в мс = %u", report->DelayMS);
debug("\tИП сообщивший о недоступности/проблемах или 0 = %x", report->ProblemIP);
debug("\ticmp.type проблемы от хоста ProblemIP = %x", report->ProblemICMP_Type);
debug("\ticmp.code проблемы от хоста ProblemIP = %x", report->ProblemICMP_Code);
debug("\tфлаги = %d", report->Flags);
debug("\tверсия конфига = %d", report->CFGVer);
} else if (ReportEntry->ModType == MODULE_READ_OBJECTS_CFG) {
} else {
struct _chg_tcp *report = (struct _chg_tcp *) ReportEntry->Data;
debug("\tid объекта тестирования = %d", report->LObjId);
debug("\tдата проверки = %x", report->CheckDt);
debug("\tип объекта тестирования = %s", ipString(report->IP));
#ifdef CFG_3
debug("\tстатус = %d", report->CheckStatus);
#endif
debug("\tрезультат = %d", report->CheckRes);
debug("\tвремя ответа в мс = %d", report->DelayMS);
debug("\tИП сообщивший о недоступности/проблемах или 0 = %x", report->ProblemIP);
debug("\ticmp.type проблемы от хоста ProblemIP = %x", report->ProblemICMP_Type);
debug("\ticmp.code проблемы от хоста ProblemIP = %x", report->ProblemICMP_Code);
debug("\tфлаги = %d", report->Flags);
debug("\tверсия конфига = %d", report->CFGVer);
debug("\tпорт првоерки = %hd", (short) report->Port);
}
/*
debug("\tid объекта тестирования = %d", Cfg_Record->LObjId);
debug("\tтип модуля тестирования (пинг-порт-хттп-...) = %s", getModuleText(Cfg_Record->ModType));
debug("\tпорт првоерки = %hd", (short) Cfg_Record->Port);
debug("\tпериодичность проверки объекта в секундах = %d", Cfg_Record->CheckPeriod);
debug("\tпериодичность проверки ИП-адреса в секундах, или 0, если проверку делать не нужно = %d", Cfg_Record->ResolvePeriod);
debug("\tип объекта тестирования = %s", ipString(Cfg_Record->IP));
debug("\tдата ближайшей проверки. unixtime utc = %d", Cfg_Record->NextCheckDt);
debug("\tимя хоста = %s", Cfg_Record->HostName);
debug("\tLObjId предыдущего хоста в folded-цепочке или 0 = %d", Cfg_Record->FoldedNext);
debug("\tLObjId следующего хоста в folded-цепочке или 0 = %d", Cfg_Record->FoldedPrev);
debug("\tТаймаут для проверки = %d", Cfg_Record->TimeOut);
debug("\tРазмер дополнительных данных = %d", Cfg_Record->ConfigLen);
*/
}
void _SendReport(Server *pServer, short error) {
debug("mutex_lock")
pthread_mutex_lock(send_mutex);
struct evbuffer * ptr[MODULE_LAST];
short counter;
bzero(ptr, MODULE_LAST * sizeof (*ptr));
if (error) {
Head = &pServer->report.ReportErrorHead;
} else {
Head = &pServer->report.ReportHead;
}
#ifdef ONEPACKET
LIST_FOREACH(ReportEntryPtr, Head, entries) {
if (ptr[ReportEntryPtr->ModType] == 0) {
ptr[ReportEntryPtr->ModType] = evbuffer_new();
}
/*
#ifdef DEBUG
debugPrintReport(ReportEntryPtr);
#endif
*/
evbuffer_add(ptr[ReportEntryPtr->ModType], ReportEntryPtr->Data, ReportEntryPtr->Len);
LIST_REMOVE(ReportEntryPtr, entries);
free(ReportEntryPtr->Data);
free(ReportEntryPtr);
}
for (counter = 0; counter < MODULE_LAST; counter++) {
if (ptr[counter] > 0) {
RequestSend(pServer, counter, ptr[counter]);
evbuffer_free(ptr[counter]);
}
}
debug("mutex_unlock")
pthread_mutex_unlock(send_mutex);
#else
LIST_FOREACH(ReportEntryPtr, Head, entries) {
RequestSend(poll, ReportEntryPtr->ModType, ReportEntryPtr->Data, ReportEntryPtr->Len);
LIST_REMOVE(ReportEntryPtr, entries);
free(ReportEntryPtr->Data);
free(ReportEntryPtr);
}
pthread_mutex_unlock(send_mutex);
#endif
}
void SendReport(Server *pServer) {
debug("%s:%d -> Send %d report", pServer->host, pServer->port, pServer->report.counterReport);
if (pServer->report.counterReport == 0)return;
_SendReport(pServer, FALSE);
pServer->report.counterReport = 0;
pServer->flagSendReport = 0;
}
void SendReportError(Server *pServer) {
debug("%s:%d -> Send %d report", pServer->host, pServer->port, pServer->report.counterReportError);
if (pServer->report.counterReportError == 0) return;
_SendReport(pServer, TRUE);
pServer->report.counterReportError = 0;
pServer->flagSendReportError = 0;
}
void initReport(Server *pServer) {
if (!send_mutex) {
send_mutex = calloc(1, sizeof (*send_mutex));
pthread_mutex_init(send_mutex, NULL);
}
if (pServer->isSC) {
pServerSC = pServer;
}
pServer->report.counterReport = 0;
pServer->report.counterReportError = 0;
LIST_INIT(&pServer->report.ReportHead);
LIST_INIT(&pServer->report.ReportErrorHead);
}