- All component locks moved to memory
[public/netxms.git] / src / server / core / nms_core.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003, 2004 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: nms_core.h
20 **
21 **/
22
23 #ifndef _nms_core_h_
24 #define _nms_core_h_
25
26 #define LIBNXCL_NO_DECLARATIONS 1
27
28 #include <nms_common.h>
29
30 #ifndef _WIN32
31
32 #if HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #if HAVE_DLFCN_H
37 #include <dlfcn.h>
38 #endif
39
40 #define WSAGetLastError() (errno)
41
42 #endif /* _WIN32 */
43
44 #include <time.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdarg.h>
48
49 #ifdef _WIN32
50 #define _GETOPT_H_ 1 /* Prevent including getopt.h from net-snmp */
51 #define HAVE_SOCKLEN_T /* Prevent defining socklen_t in net-snmp */
52 #endif /* _WIN32 */
53
54 //#include <net-snmp/net-snmp-config.h>
55 //#include <net-snmp/net-snmp-includes.h>
56
57 #include <nms_threads.h>
58 #include <dbdrv.h>
59 #include <nms_cscp.h>
60 #include <nms_util.h>
61 #include <nxsrvapi.h>
62 #include <nxcscpapi.h>
63 #include <nximage.h>
64 #include <nxqueue.h>
65 #include <nxsnmp.h>
66 #include "nms_dcoll.h"
67 #include "nms_users.h"
68 #include "nms_objects.h"
69 #include "messages.h"
70 #include "nms_locks.h"
71
72
73 //
74 // Common constants and macros
75 //
76
77 #ifdef _WIN32
78
79 #define DEFAULT_SHELL "cmd.exe"
80 #define DEFAULT_LOG_FILE "C:\\NetXMS.log"
81 #define DEFAULT_DATA_DIR "C:\\NetXMS\\var"
82
83 #define DDIR_MIBS "\\mibs"
84 #define DDIR_IMAGES "\\images"
85
86 #else /* _WIN32 */
87
88 #define DEFAULT_SHELL "/bin/sh"
89 #define DEFAULT_LOG_FILE "/var/log/netxmsd.log"
90 #define DEFAULT_DATA_DIR "/var/netxms"
91
92 #define DDIR_MIBS "/mibs"
93 #define DDIR_IMAGES "/images"
94
95 #endif /* _WIN32 */
96
97 #define MAX_LINE_SIZE 4096
98
99 #define GROUP_FLAG_BIT ((DWORD)0x80000000)
100
101 typedef void * HSNMPSESSION;
102
103
104 //
105 // Unique identifier group codes
106 //
107
108 #define IDG_NETWORK_OBJECT 0
109 #define IDG_CONTAINER_CAT 1
110 #define IDG_EVENT 2
111 #define IDG_ITEM 3
112 //#define IDG_DCT 4
113 #define IDG_IMAGE 5
114 #define IDG_ACTION 6
115 #define IDG_EVENT_GROUP 7
116 #define IDG_THRESHOLD 8
117 #define IDG_USER 9
118 #define IDG_USER_GROUP 10
119 #define IDG_ALARM 11
120 #define IDG_ALARM_NOTE 12
121
122
123 //
124 // Application flags
125 //
126
127 #define AF_STANDALONE 0x00000001
128 #define AF_USE_EVENT_LOG 0x00000002
129 #define AF_ENABLE_ACCESS_CONTROL 0x00000004
130 #define AF_ENABLE_EVENTS_ACCESS_CONTROL 0x00000008
131 #define AF_LOG_SQL_ERRORS 0x00000010
132 #define AF_DELETE_EMPTY_SUBNETS 0x00000020
133 #define AF_ENABLE_SNMP_TRAPD 0x00000040
134 #define AF_DEBUG_EVENTS 0x00000100
135 #define AF_DEBUG_CSCP 0x00000200
136 #define AF_DEBUG_DISCOVERY 0x00000400
137 #define AF_DEBUG_DC 0x00000800
138 #define AF_DEBUG_HOUSEKEEPER 0x00001000
139 #define AF_DEBUG_LOCKS 0x00002000
140 #define AF_DEBUG_ACTIONS 0x00004000
141 #define AF_DEBUG_MISC 0x00008000
142 #define AF_DEBUG_SQL 0x00010000
143 #define AF_DEBUG_SNMP 0x00020000
144 #define AF_DEBUG_ALL 0x0002FF00
145 #define AF_DB_LOCKED 0x01000000
146 #define AF_SHUTDOWN 0x80000000
147
148 #define IsStandalone() (g_dwFlags & AF_STANDALONE)
149 #define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
150
151
152 //
153 // Client session flags
154 //
155
156 #define CSF_EVENT_DB_LOCKED ((DWORD)0x0001)
157 #define CSF_EPP_LOCKED ((DWORD)0x0002)
158 #define CSF_EVENT_DB_MODIFIED ((DWORD)0x0004)
159 #define CSF_USER_DB_LOCKED ((DWORD)0x0008)
160 #define CSF_EPP_UPLOAD ((DWORD)0x0010)
161 #define CSF_ACTION_DB_LOCKED ((DWORD)0x0020)
162
163
164 //
165 // Information categories for UPDATE_INFO structure
166 //
167
168 #define INFO_CAT_EVENT 1
169 #define INFO_CAT_OBJECT_CHANGE 2
170 #define INFO_CAT_ALARM 3
171 #define INFO_CAT_ACTION 4
172
173
174 //
175 // Event handling subsystem definitions
176 //
177
178 #include "nms_events.h"
179 #include "nms_actions.h"
180 #include "nms_alarm.h"
181
182
183 //
184 // Data update structure for client sessions
185 //
186
187 typedef struct
188 {
189 DWORD dwCategory; // Data category - event, network object, etc.
190 DWORD dwCode; // Data-specific update code
191 void *pData; // Pointer to data block
192 } UPDATE_INFO;
193
194
195 //
196 // Client session
197 //
198
199 class ClientSession
200 {
201 private:
202 SOCKET m_hSocket;
203 Queue *m_pSendQueue;
204 Queue *m_pMessageQueue;
205 Queue *m_pUpdateQueue;
206 DWORD m_dwIndex;
207 int m_iState;
208 DWORD m_dwUserId;
209 DWORD m_dwSystemAccess; // User's system access rights
210 DWORD m_dwFlags; // Session flags
211 CSCP_BUFFER *m_pMsgBuffer;
212 THREAD m_hWriteThread;
213 THREAD m_hProcessingThread;
214 THREAD m_hUpdateThread;
215 MUTEX m_mutexSendEvents;
216 MUTEX m_mutexSendObjects;
217 MUTEX m_mutexSendAlarms;
218 MUTEX m_mutexSendActions;
219 MUTEX m_mutexPollerInit;
220 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
221 char m_szUserName[256]; // String in form login_name@host
222 DWORD m_dwOpenDCIListSize; // Number of open DCI lists
223 DWORD *m_pOpenDCIList; // List of nodes with DCI lists open
224 DWORD m_dwNumRecordsToUpload; // Number of records to be uploaded
225 DWORD m_dwRecordsUploaded;
226 EPRule **m_ppEPPRuleList; // List of loaded EPP rules
227
228 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
229 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
230 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
231 static THREAD_RESULT THREAD_CALL UpdateThreadStarter(void *);
232 static THREAD_RESULT THREAD_CALL PollerThreadStarter(void *);
233
234 void ReadThread(void);
235 void WriteThread(void);
236 void ProcessingThread(void);
237 void UpdateThread(void);
238 void PollerThread(Node *pNode, int iPollType, DWORD dwRqId);
239
240 BOOL CheckSysAccessRights(DWORD dwRequiredAccess)
241 {
242 return m_dwUserId == 0 ? TRUE :
243 ((dwRequiredAccess & m_dwSystemAccess) ? TRUE : FALSE);
244 }
245
246 void DebugPrintf(char *szFormat, ...);
247 void Login(CSCPMessage *pRequest);
248 void SendAllObjects(DWORD dwRqId);
249 void SendAllEvents(DWORD dwRqId);
250 void SendAllConfigVars(void);
251 void SendUserDB(DWORD dwRqId);
252 void SendAllAlarms(DWORD dwRqId, BOOL bIncludeAck);
253 void CreateUser(CSCPMessage *pRequest);
254 void UpdateUser(CSCPMessage *pRequest);
255 void DeleteUser(CSCPMessage *pRequest);
256 void SetPassword(CSCPMessage *pRequest);
257 void LockUserDB(DWORD dwRqId, BOOL bLock);
258 void SetConfigVariable(CSCPMessage *pRequest);
259 void SendEventDB(DWORD dwRqId);
260 void LockEventDB(DWORD dwRqId);
261 void UnlockEventDB(DWORD dwRqId);
262 void SetEventInfo(CSCPMessage *pRequest);
263 void DeleteEventTemplate(CSCPMessage *pRequest);
264 void GenerateEventId(DWORD dwRqId);
265 void ModifyObject(CSCPMessage *pRequest);
266 void ChangeObjectMgmtStatus(CSCPMessage *pRequest);
267 void OpenNodeDCIList(CSCPMessage *pRequest);
268 void CloseNodeDCIList(CSCPMessage *pRequest);
269 void ModifyNodeDCI(CSCPMessage *pRequest);
270 void CopyDCI(CSCPMessage *pRequest);
271 void GetCollectedData(CSCPMessage *pRequest);
272 void OpenEPP(DWORD dwRqId);
273 void CloseEPP(DWORD dwRqId);
274 void SaveEPP(CSCPMessage *pRequest);
275 void ProcessEPPRecord(CSCPMessage *pRequest);
276 void SendMIBList(DWORD dwRqId);
277 void SendMIB(CSCPMessage *pRequest);
278 void CreateObject(CSCPMessage *pRequest);
279 void ChangeObjectBinding(CSCPMessage *pRequest, BOOL bBind);
280 void DeleteObject(CSCPMessage *pRequest);
281 void AcknowlegeAlarm(CSCPMessage *pRequest);
282 void CreateAction(CSCPMessage *pRequest);
283 void UpdateAction(CSCPMessage *pRequest);
284 void DeleteAction(CSCPMessage *pRequest);
285 void LockActionDB(DWORD dwRqId, BOOL bLock);
286 void SendAllActions(DWORD dwRqId);
287 void SendContainerCategories(DWORD dwRqId);
288 void ForcedNodePoll(CSCPMessage *pRequest);
289 void OnTrap(CSCPMessage *pRequest);
290 void OnWakeUpNode(CSCPMessage *pRequest);
291 void CreateTrap(CSCPMessage *pRequest);
292 void UpdateTrap(CSCPMessage *pRequest);
293 void DeleteTrap(CSCPMessage *pRequest);
294 void LockTrapCfg(DWORD dwRqId, BOOL bLock);
295 void SendAllTraps(DWORD dwRqId);
296
297 public:
298 ClientSession(SOCKET hSocket, DWORD dwHostAddr);
299 ~ClientSession();
300
301 void Run(void);
302
303 void SendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
304 void SendPollerMsg(DWORD dwRqId, TCHAR *pszMsg);
305
306 DWORD GetIndex(void) { return m_dwIndex; }
307 void SetIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
308 int GetState(void) { return m_iState; }
309 const char *GetUserName(void) { return m_szUserName; }
310 DWORD GetUserId(void) { return m_dwUserId; }
311
312 void Kill(void);
313 void Notify(DWORD dwCode, DWORD dwData = 0);
314
315 void OnNewEvent(Event *pEvent);
316 void OnObjectChange(NetObj *pObject);
317 void OnUserDBUpdate(int iCode, DWORD dwUserId, NMS_USER *pUser, NMS_USER_GROUP *pGroup);
318 void OnAlarmUpdate(DWORD dwCode, NXC_ALARM *pAlarm);
319 void OnActionDBUpdate(DWORD dwCode, NXC_ACTION *pAction);
320 };
321
322
323 //
324 // Functions
325 //
326
327 BOOL ConfigReadStr(char *szVar, char *szBuffer, int iBufSize, const char *szDefault);
328 int ConfigReadInt(char *szVar, int iDefault);
329 DWORD ConfigReadULong(char *szVar, DWORD dwDefault);
330 BOOL ConfigWriteStr(char *szVar, char *szValue, BOOL bCreate);
331 BOOL ConfigWriteInt(char *szVar, int iValue, BOOL bCreate);
332 BOOL ConfigWriteULong(char *szVar, DWORD dwValue, BOOL bCreate);
333
334 BOOL ParseCommandLine(int argc, char *argv[]);
335 BOOL LoadConfig(void);
336
337 void Shutdown(void);
338 BOOL Initialize(void);
339 void Main(void);
340
341 BOOL SleepAndCheckForShutdown(int iSeconds);
342
343 void SaveObjects(void);
344
345 void QueueSQLRequest(char *szQuery);
346 void StartDBWriter(void);
347 void StopDBWriter(void);
348
349 void SnmpInit(void);
350 BOOL SnmpGet(DWORD dwVersion, DWORD dwAddr, const char *szCommunity, const char *szOidStr,
351 const DWORD *oidBinary, DWORD dwOidLen, void *pValue,
352 DWORD dwBufferSize, BOOL bVerbose, BOOL bStringResult);
353 BOOL SnmpEnumerate(DWORD dwVersion, DWORD dwAddr, const char *szCommunity, const char *szRootOid,
354 void (* pHandler)(DWORD, DWORD, const char *, SNMP_Variable *, void *),
355 void *pUserArg, BOOL bVerbose);
356 void StrToMac(char *pszStr, BYTE *pBuffer);
357 DWORD OidToType(TCHAR *pszOid, DWORD *pdwFlags);
358
359 void InitLocalNetInfo(void);
360
361 ARP_CACHE *GetLocalArpCache(void);
362 ARP_CACHE *SnmpGetArpCache(DWORD dwVersion, DWORD dwAddr, const char *szCommunity);
363
364 INTERFACE_LIST *SnmpGetInterfaceList(DWORD dwVersion, DWORD dwAddr, const char *szCommunity, DWORD dwNodeType);
365 INTERFACE_LIST *GetLocalInterfaceList(void);
366 void CleanInterfaceList(INTERFACE_LIST *pIfList);
367
368 void WatchdogInit(void);
369 DWORD WatchdogAddThread(char *szName, time_t tNotifyInterval);
370 void WatchdogNotify(DWORD dwId);
371 void WatchdogPrintStatus(void);
372
373 void CheckForMgmtNode(void);
374 NetObj *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwFlags, TCHAR *pszName);
375
376 void EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
377 void NotifyClient(ClientSession *pSession, void *pArg);
378
379 void GetSysInfoStr(char *pszBuffer);
380 DWORD GetLocalIpAddr(void);
381
382 BOOL ExecCommand(char *pszCommand);
383 BOOL SendMagicPacket(DWORD dwIpAddr, BYTE *pbMacAddr, int iNumPackets);
384
385 BOOL InitIdTable(void);
386 DWORD CreateUniqueId(int iGroup);
387
388 BYTE *LoadFile(char *pszFileName, DWORD *pdwFileSize);
389
390 void UpdateImageHashes(void);
391 void SendImageCatalogue(ClientSession *pSession, DWORD dwRqId, WORD wFormat);
392 void SendImageFile(ClientSession *pSession, DWORD dwRqId, DWORD dwImageId, WORD wFormat);
393 void SendDefaultImageList(ClientSession *pSession, DWORD dwRqId);
394
395 char *EncodeSQLString(const char *pszIn);
396 void DecodeSQLString(char *pszStr);
397
398 void InitMailer(void);
399 void ShutdownMailer(void);
400 void PostMail(char *pszRcpt, char *pszSubject, char *pszText);
401
402 void GetAccelarVLANIfList(DWORD dwVersion, DWORD dwIpAddr, const TCHAR *pszCommunity, INTERFACE_LIST *pIfList);
403
404 #ifdef _WIN32
405
406 void InitService(void);
407 void InstallService(char *execName, char *dllName);
408 void RemoveService(void);
409 void StartCoreService(void);
410 void StopCoreService(void);
411 void InstallEventSource(char *path);
412 void RemoveEventSource(void);
413
414 char *GetSystemErrorText(DWORD error);
415
416 #endif /* _WIN32 */
417
418 void DbgTestMutex(MUTEX hMutex, TCHAR *szName);
419 void DbgTestRWLock(RWLOCK hLock, TCHAR *szName);
420 void DbgPrintf(DWORD dwFlags, TCHAR *szFormat, ...);
421 void DumpSessions(void);
422
423
424 //
425 // Global variables
426 //
427
428 extern DWORD g_dwFlags;
429 extern char g_szConfigFile[];
430 extern char g_szLogFile[];
431 #ifndef _WIN32
432 extern char g_szPIDFile[];
433 #endif
434 extern char g_szDataDir[];
435
436 extern DB_HANDLE g_hCoreDB;
437 extern Queue *g_pLazyRequestQueue;
438
439 extern char *g_pszStatusName[];
440
441 #endif /* _nms_core_h_ */