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