f7af26fae1fec084592189168777e29299dd110f
[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 "nms_dcoll.h"
66 #include "nms_users.h"
67 #include "nms_objects.h"
68 #include "messages.h"
69 #include "nms_locks.h"
70
71
72 //
73 // Common constants and macros
74 //
75
76 #ifdef _WIN32
77
78 #define DEFAULT_SHELL "cmd.exe"
79 #define DEFAULT_CONFIG_FILE "C:\\NetXMS.conf"
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_CONFIG_FILE "/etc/netxms.conf"
90 #define DEFAULT_LOG_FILE "/var/log/netxms.log"
91 #define DEFAULT_DATA_DIR "/var/netxms"
92
93 #define DDIR_MIBS "/mibs"
94 #define DDIR_IMAGES "/images"
95
96 #endif /* _WIN32 */
97
98 #define MAX_DB_LOGIN 64
99 #define MAX_DB_PASSWORD 64
100 #define MAX_DB_NAME 32
101 #define MAX_LINE_SIZE 4096
102
103 #define UNLOCKED ((DWORD)0xFFFFFFFF)
104
105 #define GROUP_FLAG_BIT ((DWORD)0x80000000)
106
107 typedef void * HSNMPSESSION;
108
109
110 //
111 // Unique identifier group codes
112 //
113
114 #define IDG_NETWORK_OBJECT 0
115 #define IDG_CONTAINER_CAT 1
116 #define IDG_EVENT 2
117 #define IDG_ITEM 3
118 #define IDG_DCT 4
119 #define IDG_IMAGE 5
120 #define IDG_ACTION 6
121 #define IDG_EVENT_GROUP 7
122 #define IDG_THRESHOLD 8
123 #define IDG_USER 9
124 #define IDG_USER_GROUP 10
125 #define IDG_ALARM 11
126 #define IDG_ALARM_NOTE 12
127
128
129 //
130 // Application flags
131 //
132
133 #define AF_STANDALONE 0x00000001
134 #define AF_USE_EVENT_LOG 0x00000002
135 #define AF_ENABLE_ACCESS_CONTROL 0x00000004
136 #define AF_ENABLE_EVENTS_ACCESS_CONTROL 0x00000008
137 #define AF_LOG_SQL_ERRORS 0x00000010
138 #define AF_DELETE_EMPTY_SUBNETS 0x00000020
139 #define AF_ENABLE_SNMP_TRAPD 0x00000040
140 #define AF_DEBUG_EVENTS 0x00000100
141 #define AF_DEBUG_CSCP 0x00000200
142 #define AF_DEBUG_DISCOVERY 0x00000400
143 #define AF_DEBUG_DC 0x00000800
144 #define AF_DEBUG_HOUSEKEEPER 0x00001000
145 #define AF_DEBUG_LOCKS 0x00002000
146 #define AF_DEBUG_ACTIONS 0x00004000
147 #define AF_DEBUG_MISC 0x00008000
148 #define AF_DEBUG_ALL 0x0000FF00
149 #define AF_DB_LOCKED 0x01000000
150 #define AF_SHUTDOWN 0x80000000
151
152 #define IsStandalone() (g_dwFlags & AF_STANDALONE)
153 #define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
154
155
156 //
157 // Win32 service constants
158 //
159
160 #ifdef _WIN32
161
162 #define CORE_SERVICE_NAME "NetXMSCore"
163 #define CORE_EVENT_SOURCE "NetXMSCore"
164
165 #endif /* _WIN32 */
166
167
168 //
169 // Client session flags
170 //
171
172 #define CSF_EVENT_DB_LOCKED ((DWORD)0x0001)
173 #define CSF_EPP_LOCKED ((DWORD)0x0002)
174 #define CSF_EVENT_DB_MODIFIED ((DWORD)0x0004)
175 #define CSF_USER_DB_LOCKED ((DWORD)0x0008)
176 #define CSF_EPP_UPLOAD ((DWORD)0x0010)
177 #define CSF_ACTION_DB_LOCKED ((DWORD)0x0020)
178
179
180 //
181 // Information categories for UPDATE_INFO structure
182 //
183
184 #define INFO_CAT_EVENT 1
185 #define INFO_CAT_OBJECT_CHANGE 2
186 #define INFO_CAT_ALARM 3
187 #define INFO_CAT_ACTION 4
188
189
190 //
191 // Event handling subsystem definitions
192 //
193
194 #include "nms_events.h"
195 #include "nms_actions.h"
196 #include "nms_alarm.h"
197
198
199 //
200 // Data update structure for client sessions
201 //
202
203 typedef struct
204 {
205 DWORD dwCategory; // Data category - event, network object, etc.
206 DWORD dwCode; // Data-specific update code
207 void *pData; // Pointer to data block
208 } UPDATE_INFO;
209
210
211 //
212 // Client session
213 //
214
215 class ClientSession
216 {
217 private:
218 SOCKET m_hSocket;
219 Queue *m_pSendQueue;
220 Queue *m_pMessageQueue;
221 Queue *m_pUpdateQueue;
222 DWORD m_dwIndex;
223 int m_iState;
224 DWORD m_dwUserId;
225 DWORD m_dwSystemAccess; // User's system access rights
226 DWORD m_dwFlags; // Session flags
227 CSCP_BUFFER *m_pMsgBuffer;
228 THREAD m_hWriteThread;
229 THREAD m_hProcessingThread;
230 THREAD m_hUpdateThread;
231 MUTEX m_mutexSendEvents;
232 MUTEX m_mutexSendObjects;
233 MUTEX m_mutexSendAlarms;
234 MUTEX m_mutexSendActions;
235 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
236 char m_szUserName[256]; // String in form login_name@host
237 DWORD m_dwOpenDCIListSize; // Number of open DCI lists
238 DWORD *m_pOpenDCIList; // List of nodes with DCI lists open
239 DWORD m_dwNumRecordsToUpload; // Number of records to be uploaded
240 DWORD m_dwRecordsUploaded;
241 EPRule **m_ppEPPRuleList; // List of loaded EPP rules
242
243 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
244 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
245 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
246 static THREAD_RESULT THREAD_CALL UpdateThreadStarter(void *);
247
248 void ReadThread(void);
249 void WriteThread(void);
250 void ProcessingThread(void);
251 void UpdateThread(void);
252
253 BOOL CheckSysAccessRights(DWORD dwRequiredAccess)
254 {
255 return m_dwUserId == 0 ? TRUE :
256 ((dwRequiredAccess & m_dwSystemAccess) ? TRUE : FALSE);
257 }
258
259 void DebugPrintf(char *szFormat, ...);
260 void Login(CSCPMessage *pRequest);
261 void SendAllObjects(DWORD dwRqId);
262 void SendAllEvents(DWORD dwRqId);
263 void SendAllConfigVars(void);
264 void SendUserDB(DWORD dwRqId);
265 void SendAllAlarms(DWORD dwRqId, BOOL bIncludeAck);
266 void CreateUser(CSCPMessage *pRequest);
267 void UpdateUser(CSCPMessage *pRequest);
268 void DeleteUser(CSCPMessage *pRequest);
269 void SetPassword(CSCPMessage *pRequest);
270 void LockUserDB(DWORD dwRqId, BOOL bLock);
271 void SetConfigVariable(CSCPMessage *pRequest);
272 void SendEventDB(DWORD dwRqId);
273 void CloseEventDB(DWORD dwRqId);
274 void SetEventInfo(CSCPMessage *pRequest);
275 void ModifyObject(CSCPMessage *pRequest);
276 void ChangeObjectMgmtStatus(CSCPMessage *pRequest);
277 void OpenNodeDCIList(CSCPMessage *pRequest);
278 void CloseNodeDCIList(CSCPMessage *pRequest);
279 void ModifyNodeDCI(CSCPMessage *pRequest);
280 void GetCollectedData(CSCPMessage *pRequest);
281 void OpenEPP(DWORD dwRqId);
282 void CloseEPP(DWORD dwRqId);
283 void SaveEPP(CSCPMessage *pRequest);
284 void ProcessEPPRecord(CSCPMessage *pRequest);
285 void SendMIBList(DWORD dwRqId);
286 void SendMIB(CSCPMessage *pRequest);
287 void SendEventNames(DWORD dwRqId);
288 void CreateObject(CSCPMessage *pRequest);
289 void ChangeObjectBinding(CSCPMessage *pRequest, BOOL bBind);
290 void DeleteObject(CSCPMessage *pRequest);
291 void AcknowlegeAlarm(CSCPMessage *pRequest);
292 void CreateAction(CSCPMessage *pRequest);
293 void UpdateAction(CSCPMessage *pRequest);
294 void DeleteAction(CSCPMessage *pRequest);
295 void LockActionDB(DWORD dwRqId, BOOL bLock);
296 void SendAllActions(DWORD dwRqId);
297 void SendContainerCategories(DWORD dwRqId);
298
299 public:
300 ClientSession(SOCKET hSocket, DWORD dwHostAddr);
301 ~ClientSession();
302
303 void Run(void);
304
305 void SendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
306
307 DWORD GetIndex(void) { return m_dwIndex; }
308 void SetIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
309 int GetState(void) { return m_iState; }
310 const char *GetUserName(void) { return m_szUserName; }
311 DWORD GetUserId(void) { return m_dwUserId; }
312
313 void Kill(void);
314 void Notify(DWORD dwCode, DWORD dwData = 0);
315
316 void OnNewEvent(Event *pEvent);
317 void OnObjectChange(NetObj *pObject);
318 void OnUserDBUpdate(int iCode, DWORD dwUserId, NMS_USER *pUser, NMS_USER_GROUP *pGroup);
319 void OnAlarmUpdate(DWORD dwCode, NXC_ALARM *pAlarm);
320 void OnActionDBUpdate(DWORD dwCode, NXC_ACTION *pAction);
321 };
322
323
324 //
325 // Functions
326 //
327
328 BOOL ConfigReadStr(char *szVar, char *szBuffer, int iBufSize, char *szDefault);
329 int ConfigReadInt(char *szVar, int iDefault);
330 DWORD ConfigReadULong(char *szVar, DWORD dwDefault);
331 BOOL ConfigWriteStr(char *szVar, char *szValue, BOOL bCreate);
332 BOOL ConfigWriteInt(char *szVar, int iValue, BOOL bCreate);
333 BOOL ConfigWriteULong(char *szVar, DWORD dwValue, BOOL bCreate);
334
335 void InitLog(void);
336 void CloseLog(void);
337 void WriteLog(DWORD msg, WORD wType, char *format, ...);
338
339 BOOL ParseCommandLine(int argc, char *argv[]);
340 BOOL LoadConfig(void);
341
342 void Shutdown(void);
343 BOOL Initialize(void);
344 void Main(void);
345
346 BOOL SleepAndCheckForShutdown(int iSeconds);
347
348 void SaveObjects(void);
349
350 BOOL DBInit(void);
351 DB_HANDLE DBConnect(void);
352 void DBDisconnect(DB_HANDLE hConn);
353 BOOL DBQuery(DB_HANDLE hConn, char *szQuery);
354 DB_RESULT DBSelect(DB_HANDLE hConn, char *szQuery);
355 DB_ASYNC_RESULT DBAsyncSelect(DB_HANDLE hConn, char *szQuery);
356 BOOL DBFetch(DB_ASYNC_RESULT hResult);
357 char *DBGetField(DB_RESULT hResult, int iRow, int iColumn);
358 long DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn);
359 double DBGetFieldDouble(DB_RESULT hResult, int iRow, int iColumn);
360 DWORD DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn);
361 char *DBGetFieldAsync(DB_ASYNC_RESULT hResult, int iColumn, char *pBuffer, int iBufSize);
362 long DBGetFieldAsyncLong(DB_RESULT hResult, int iColumn);
363 double DBGetFieldAsyncDouble(DB_RESULT hResult, int iColumn);
364 DWORD DBGetFieldAsyncULong(DB_ASYNC_RESULT hResult, int iColumn);
365 int DBGetNumRows(DB_RESULT hResult);
366 void DBFreeResult(DB_RESULT hResult);
367 void DBFreeAsyncResult(DB_ASYNC_RESULT hResult);
368 void DBUnloadDriver(void);
369
370 void QueueSQLRequest(char *szQuery);
371 void StartDBWriter(void);
372 void StopDBWriter(void);
373
374 void SnmpInit(void);
375 BOOL SnmpGet(DWORD dwAddr, const char *szCommunity, const char *szOidStr,
376 const oid *oidBinary, size_t iOidLen, void *pValue,
377 DWORD dwBufferSize, BOOL bVerbose, BOOL bStringResult);
378 BOOL SnmpEnumerate(DWORD dwAddr, const char *szCommunity, const char *szRootOid,
379 void (* pHandler)(DWORD, const char *, variable_list *, void *),
380 void *pUserArg, BOOL bVerbose);
381 void OidToStr(oid *pOid, int iOidLen, char *szBuffer, DWORD dwBufferSize);
382
383 ARP_CACHE *GetLocalArpCache(void);
384 ARP_CACHE *SnmpGetArpCache(DWORD dwAddr, const char *szCommunity);
385
386 INTERFACE_LIST *SnmpGetInterfaceList(DWORD dwAddr, const char *szCommunity);
387 INTERFACE_LIST *GetLocalInterfaceList(void);
388 void CleanInterfaceList(INTERFACE_LIST *pIfList);
389
390 void WatchdogInit(void);
391 DWORD WatchdogAddThread(char *szName, time_t tNotifyInterval);
392 void WatchdogNotify(DWORD dwId);
393 void WatchdogPrintStatus(void);
394
395 void CheckForMgmtNode(void);
396 NetObj *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwFlags, TCHAR *pszName);
397
398 void EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
399 void NotifyClient(ClientSession *pSession, void *pArg);
400
401 void BinToStr(BYTE *pData, DWORD dwSize, char *pStr);
402 DWORD StrToBin(char *pStr, BYTE *pData, DWORD dwSize);
403
404 void GetSysInfoStr(char *pszBuffer);
405 DWORD GetLocalIpAddr(void);
406
407 BOOL ExecCommand(char *pszCommand);
408
409 BOOL InitIdTable(void);
410 DWORD CreateUniqueId(int iGroup);
411
412 BYTE *LoadFile(char *pszFileName, DWORD *pdwFileSize);
413
414 void UpdateImageHashes(void);
415 void SendImageCatalogue(ClientSession *pSession, DWORD dwRqId, WORD wFormat);
416 void SendImageFile(ClientSession *pSession, DWORD dwRqId, DWORD dwImageId, WORD wFormat);
417 void SendDefaultImageList(ClientSession *pSession, DWORD dwRqId);
418
419 char *EncodeSQLString(const char *pszIn);
420 void DecodeSQLString(char *pszStr);
421
422 void InitMailer(void);
423 void ShutdownMailer(void);
424 void PostMail(char *pszRcpt, char *pszSubject, char *pszText);
425
426 #ifdef _WIN32
427
428 void InitService(void);
429 void InstallService(char *execName);
430 void RemoveService(void);
431 void StartCoreService(void);
432 void StopCoreService(void);
433 void InstallEventSource(char *path);
434 void RemoveEventSource(void);
435
436 char *GetSystemErrorText(DWORD error);
437
438 #endif /* _WIN32 */
439
440 void DbgTestMutex(MUTEX hMutex, char *szName);
441 void DbgPrintf(DWORD dwFlags, char *szFormat, ...);
442 void DumpSessions(void);
443
444
445 //
446 // Global variables
447 //
448
449 extern DWORD g_dwFlags;
450 extern char g_szConfigFile[];
451 extern char g_szLogFile[];
452 extern char g_szDataDir[];
453
454 extern char g_szDbDriver[];
455 extern char g_szDbDriver[];
456 extern char g_szDbDrvParams[];
457 extern char g_szDbServer[];
458 extern char g_szDbLogin[];
459 extern char g_szDbPassword[];
460 extern char g_szDbName[];
461
462 extern DB_HANDLE g_hCoreDB;
463 extern Queue *g_pLazyRequestQueue;
464
465 #endif /* _nms_core_h_ */