- Added component locks
[public/netxms.git] / src / server / core / evproc.cpp
1 /*
2 ** Project X - Network Management System
3 ** Copyright (C) 2003 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** $module: evproc.cpp
20 **
21 **/
22
23 #include "nms_core.h"
24
25
26 //
27 // Handler for EnumerateSessions()
28 //
29
30 static void BroadcastEvent(ClientSession *pSession, void *pArg)
31 {
32 if (pSession->GetState() == STATE_AUTHENTICATED)
33 pSession->OnNewEvent((Event *)pArg);
34 }
35
36
37 //
38 // Event processing thread
39 //
40
41 void EventProcessor(void *arg)
42 {
43 Event *pEvent;
44
45 while(!ShutdownInProgress())
46 {
47 pEvent = (Event *)g_pEventQueue->GetOrBlock();
48 if (pEvent == INVALID_POINTER_VALUE)
49 break; // Shutdown indicator
50
51 // Write event to log if required
52 if (pEvent->Flags() & EF_LOG)
53 {
54 char szQuery[1024];
55
56 sprintf(szQuery, "INSERT INTO eventlog (event_id,timestamp,source,severity,message) "
57 "VALUES (%d,%d,%d,%d,'%s')", pEvent->Id(), pEvent->TimeStamp(),
58 pEvent->SourceId(), pEvent->Severity(), pEvent->Message());
59 DBQuery(g_hCoreDB, szQuery);
60 }
61
62 // Send event to all connected clients
63 EnumerateClientSessions(BroadcastEvent, pEvent);
64
65 // Write event information to screen if event debugging is on
66 if (IsStandalone() && (g_dwFlags & AF_DEBUG_EVENTS))
67 {
68 NetObj *pObject = FindObjectById(pEvent->SourceId());
69 if (pObject == NULL)
70 pObject = g_pEntireNet;
71 printf("EVENT %d (F:0x%04X S:%d) FROM %s: %s\n", pEvent->Id(),
72 pEvent->Flags(), pEvent->Severity(), pObject->Name(), pEvent->Message());
73 }
74
75 // Destroy event
76 delete pEvent;
77 }
78 }