- Changes in logging and debug output
[public/netxms.git] / src / server / core / nms_core.h
CommitLineData
9a19737f 1/*
b54b2b11
VK
2** NetXMS - Network Management System
3** Copyright (C) 2003, 2004 Victor Kirhenshtein
9a19737f
VK
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
3c55b85d
VK
26#define LIBNXCL_NO_DECLARATIONS 1
27
e31784c0 28#include <nms_common.h>
0ee0c807 29
e31784c0 30#ifndef _WIN32
0ee0c807 31
c9ccd685
AK
32#if HAVE_SYS_TYPES_H
33# include <sys/types.h>
34#endif
35
e31784c0 36#if HAVE_DLFCN_H
3a2f672c 37#include <dlfcn.h>
e31784c0 38#endif
0ee0c807 39
5af4c41b 40#define WSAGetLastError() (errno)
0ee0c807 41
9a19737f
VK
42#endif /* _WIN32 */
43
4385fa12
VK
44#include <time.h>
45#include <stdio.h>
46#include <string.h>
f9b7e653 47#include <stdarg.h>
469b937c 48
e31784c0 49#ifdef _WIN32
66c04cc7 50#define _GETOPT_H_ 1 /* Prevent including getopt.h from net-snmp */
53512272 51#define HAVE_SOCKLEN_T /* Prevent defining socklen_t in net-snmp */
e31784c0 52#endif /* _WIN32 */
66c04cc7 53
469b937c
VK
54#include <net-snmp/net-snmp-config.h>
55#include <net-snmp/net-snmp-includes.h>
56
9a19737f 57#include <nms_threads.h>
3a2f672c 58#include <dbdrv.h>
5af4c41b 59#include <nms_cscp.h>
4a4afbd0 60#include <nms_util.h>
f11f4edc 61#include <nxsrvapi.h>
b02279eb 62#include <nxcscpapi.h>
e641b7d0 63#include <nximage.h>
77312b6f 64#include <nxqueue.h>
1275c750 65#include "nms_dcoll.h"
23a32988 66#include "nms_users.h"
3ea35b38 67#include "nms_objects.h"
3a2f672c 68#include "messages.h"
b54b2b11 69#include "nms_locks.h"
9a19737f
VK
70
71
72//
d2a1dd18 73// Common constants and macros
9a19737f
VK
74//
75
9a19737f 76#ifdef _WIN32
06e7be2f 77
4d5a05a0 78#define DEFAULT_SHELL "cmd.exe"
1275c750
VK
79#define DEFAULT_CONFIG_FILE "C:\\NetXMS.conf"
80#define DEFAULT_LOG_FILE "C:\\NetXMS.log"
06e7be2f
VK
81#define DEFAULT_DATA_DIR "C:\\NetXMS\\var"
82
83#define DDIR_MIBS "\\mibs"
e641b7d0 84#define DDIR_IMAGES "\\images"
06e7be2f 85
9a19737f 86#else /* _WIN32 */
06e7be2f 87
4d5a05a0 88#define DEFAULT_SHELL "/bin/sh"
1275c750
VK
89#define DEFAULT_CONFIG_FILE "/etc/netxms.conf"
90#define DEFAULT_LOG_FILE "/var/log/netxms.log"
06e7be2f
VK
91#define DEFAULT_DATA_DIR "/var/netxms"
92
93#define DDIR_MIBS "/mibs"
e641b7d0 94#define DDIR_IMAGES "/images"
06e7be2f 95
9a19737f
VK
96#endif /* _WIN32 */
97
64584976
VK
98#define MAX_DB_LOGIN 64
99#define MAX_DB_PASSWORD 64
100#define MAX_DB_NAME 32
101#define MAX_LINE_SIZE 4096
3a2f672c 102
4d5a05a0
VK
103#define UNLOCKED ((DWORD)0xFFFFFFFF)
104
105#define GROUP_FLAG_BIT ((DWORD)0x80000000)
c7ca9142 106
f92869c9
VK
107typedef void * HSNMPSESSION;
108
b7271ccb 109
3ea35b38
VK
110//
111// Unique identifier group codes
112//
113
114#define IDG_NETWORK_OBJECT 0
ef44d5ea 115#define IDG_CONTAINER_CAT 1
3ea35b38
VK
116#define IDG_EVENT 2
117#define IDG_ITEM 3
118#define IDG_DCT 4
a60d4f27 119#define IDG_IMAGE 5
3ea35b38
VK
120#define IDG_ACTION 6
121#define IDG_EVENT_GROUP 7
122#define IDG_THRESHOLD 8
de78f964
VK
123#define IDG_USER 9
124#define IDG_USER_GROUP 10
06e7be2f
VK
125#define IDG_ALARM 11
126#define IDG_ALARM_NOTE 12
3ea35b38
VK
127
128
9a19737f 129//
4385fa12 130// Application flags
9a19737f
VK
131//
132
3e4e127f
VK
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
78bf9c68 137#define AF_LOG_SQL_ERRORS 0x00000010
3ea35b38 138#define AF_DELETE_EMPTY_SUBNETS 0x00000020
5add75d8 139#define AF_ENABLE_SNMP_TRAPD 0x00000040
3e4e127f
VK
140#define AF_DEBUG_EVENTS 0x00000100
141#define AF_DEBUG_CSCP 0x00000200
142#define AF_DEBUG_DISCOVERY 0x00000400
143#define AF_DEBUG_DC 0x00000800
83f0529c 144#define AF_DEBUG_HOUSEKEEPER 0x00001000
b54b2b11 145#define AF_DEBUG_LOCKS 0x00002000
4d5a05a0 146#define AF_DEBUG_ACTIONS 0x00004000
0c598d0e 147#define AF_DEBUG_MISC 0x00008000
b1dd534d
VK
148#define AF_DEBUG_SQL 0x00010000
149#define AF_DEBUG_ALL 0x0001FF00
2260ffe5 150#define AF_DB_LOCKED 0x01000000
3e4e127f 151#define AF_SHUTDOWN 0x80000000
a68e217b 152
1a26164f 153#define IsStandalone() (g_dwFlags & AF_STANDALONE)
a68e217b 154#define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
9a19737f
VK
155
156
157//
158// Win32 service constants
159//
160
161#ifdef _WIN32
162
da54bd8d
VK
163#define CORE_SERVICE_NAME "NetXMSCore"
164#define CORE_EVENT_SOURCE "NetXMSCore"
9a19737f
VK
165
166#endif /* _WIN32 */
167
168
62f5857f 169//
b54b2b11 170// Client session flags
62f5857f
VK
171//
172
b8bad201
VK
173#define CSF_EVENT_DB_LOCKED ((DWORD)0x0001)
174#define CSF_EPP_LOCKED ((DWORD)0x0002)
175#define CSF_EVENT_DB_MODIFIED ((DWORD)0x0004)
ff550544 176#define CSF_USER_DB_LOCKED ((DWORD)0x0008)
8f7e2482 177#define CSF_EPP_UPLOAD ((DWORD)0x0010)
c9363772 178#define CSF_ACTION_DB_LOCKED ((DWORD)0x0020)
62f5857f
VK
179
180
c7ca9142 181//
b54b2b11 182// Information categories for UPDATE_INFO structure
c7ca9142
VK
183//
184
53512272
VK
185#define INFO_CAT_EVENT 1
186#define INFO_CAT_OBJECT_CHANGE 2
03482383 187#define INFO_CAT_ALARM 3
c9363772 188#define INFO_CAT_ACTION 4
c7ca9142
VK
189
190
f9b7e653
VK
191//
192// Event handling subsystem definitions
193//
194
195#include "nms_events.h"
7e495d97 196#include "nms_actions.h"
2260ffe5 197#include "nms_alarm.h"
f9b7e653
VK
198
199
62f5857f
VK
200//
201// Data update structure for client sessions
202//
203
204typedef struct
205{
206 DWORD dwCategory; // Data category - event, network object, etc.
c9363772 207 DWORD dwCode; // Data-specific update code
62f5857f
VK
208 void *pData; // Pointer to data block
209} UPDATE_INFO;
210
211
5af4c41b
VK
212//
213// Client session
214//
215
216class ClientSession
217{
218private:
219 SOCKET m_hSocket;
220 Queue *m_pSendQueue;
4a4afbd0 221 Queue *m_pMessageQueue;
62f5857f 222 Queue *m_pUpdateQueue;
da54bd8d
VK
223 DWORD m_dwIndex;
224 int m_iState;
225 DWORD m_dwUserId;
4b8ef715 226 DWORD m_dwSystemAccess; // User's system access rights
b54b2b11 227 DWORD m_dwFlags; // Session flags
3664c5ba 228 CSCP_BUFFER *m_pMsgBuffer;
ccdbbb52
VK
229 THREAD m_hWriteThread;
230 THREAD m_hProcessingThread;
231 THREAD m_hUpdateThread;
03482383
VK
232 MUTEX m_mutexSendEvents;
233 MUTEX m_mutexSendObjects;
234 MUTEX m_mutexSendAlarms;
c9363772 235 MUTEX m_mutexSendActions;
d966d18a 236 MUTEX m_mutexPollerInit;
b54b2b11
VK
237 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
238 char m_szUserName[256]; // String in form login_name@host
338493a0
VK
239 DWORD m_dwOpenDCIListSize; // Number of open DCI lists
240 DWORD *m_pOpenDCIList; // List of nodes with DCI lists open
8f7e2482
VK
241 DWORD m_dwNumRecordsToUpload; // Number of records to be uploaded
242 DWORD m_dwRecordsUploaded;
243 EPRule **m_ppEPPRuleList; // List of loaded EPP rules
da54bd8d 244
ccdbbb52
VK
245 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
246 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
247 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
248 static THREAD_RESULT THREAD_CALL UpdateThreadStarter(void *);
d966d18a 249 static THREAD_RESULT THREAD_CALL PollerThreadStarter(void *);
ccdbbb52
VK
250
251 void ReadThread(void);
252 void WriteThread(void);
253 void ProcessingThread(void);
254 void UpdateThread(void);
d966d18a 255 void PollerThread(Node *pNode, int iPollType, DWORD dwRqId);
ccdbbb52 256
605d2931
VK
257 BOOL CheckSysAccessRights(DWORD dwRequiredAccess)
258 {
259 return m_dwUserId == 0 ? TRUE :
260 ((dwRequiredAccess & m_dwSystemAccess) ? TRUE : FALSE);
261 }
262
da54bd8d 263 void DebugPrintf(char *szFormat, ...);
7257eb7d 264 void Login(CSCPMessage *pRequest);
e100f091 265 void SendAllObjects(DWORD dwRqId);
74a5aa17 266 void SendAllEvents(DWORD dwRqId);
4b8ef715 267 void SendAllConfigVars(void);
23a32988 268 void SendUserDB(DWORD dwRqId);
03482383 269 void SendAllAlarms(DWORD dwRqId, BOOL bIncludeAck);
7257eb7d
VK
270 void CreateUser(CSCPMessage *pRequest);
271 void UpdateUser(CSCPMessage *pRequest);
272 void DeleteUser(CSCPMessage *pRequest);
017e1e13 273 void SetPassword(CSCPMessage *pRequest);
ff550544 274 void LockUserDB(DWORD dwRqId, BOOL bLock);
7257eb7d 275 void SetConfigVariable(CSCPMessage *pRequest);
b54b2b11 276 void SendEventDB(DWORD dwRqId);
3421c063 277 void CloseEventDB(DWORD dwRqId);
7257eb7d
VK
278 void SetEventInfo(CSCPMessage *pRequest);
279 void ModifyObject(CSCPMessage *pRequest);
c1c39152 280 void ChangeObjectMgmtStatus(CSCPMessage *pRequest);
7257eb7d
VK
281 void OpenNodeDCIList(CSCPMessage *pRequest);
282 void CloseNodeDCIList(CSCPMessage *pRequest);
9ed4eaff 283 void ModifyNodeDCI(CSCPMessage *pRequest);
ebebc8ea 284 void GetCollectedData(CSCPMessage *pRequest);
06e7be2f
VK
285 void OpenEPP(DWORD dwRqId);
286 void CloseEPP(DWORD dwRqId);
287 void SaveEPP(CSCPMessage *pRequest);
8f7e2482 288 void ProcessEPPRecord(CSCPMessage *pRequest);
06e7be2f
VK
289 void SendMIBList(DWORD dwRqId);
290 void SendMIB(CSCPMessage *pRequest);
3421c063 291 void SendEventNames(DWORD dwRqId);
e6b958c4
VK
292 void CreateObject(CSCPMessage *pRequest);
293 void ChangeObjectBinding(CSCPMessage *pRequest, BOOL bBind);
88039e6c 294 void DeleteObject(CSCPMessage *pRequest);
ee20e615 295 void AcknowlegeAlarm(CSCPMessage *pRequest);
c9363772
VK
296 void CreateAction(CSCPMessage *pRequest);
297 void UpdateAction(CSCPMessage *pRequest);
298 void DeleteAction(CSCPMessage *pRequest);
299 void LockActionDB(DWORD dwRqId, BOOL bLock);
e91cb822 300 void SendAllActions(DWORD dwRqId);
6849d9be 301 void SendContainerCategories(DWORD dwRqId);
0b89f5fb 302 void ForcedNodePoll(CSCPMessage *pRequest);
5af4c41b
VK
303
304public:
b54b2b11 305 ClientSession(SOCKET hSocket, DWORD dwHostAddr);
5af4c41b
VK
306 ~ClientSession();
307
ccdbbb52 308 void Run(void);
da54bd8d 309
3c55b85d 310 void SendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
d966d18a 311 void SendPollerMsg(DWORD dwRqId, TCHAR *pszMsg);
3c55b85d 312
da54bd8d
VK
313 DWORD GetIndex(void) { return m_dwIndex; }
314 void SetIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
62f5857f 315 int GetState(void) { return m_iState; }
c868331f 316 const char *GetUserName(void) { return m_szUserName; }
03482383 317 DWORD GetUserId(void) { return m_dwUserId; }
20177e8e
VK
318
319 void Kill(void);
4de204a3 320 void Notify(DWORD dwCode, DWORD dwData = 0);
62f5857f
VK
321
322 void OnNewEvent(Event *pEvent);
53512272 323 void OnObjectChange(NetObj *pObject);
984542f8 324 void OnUserDBUpdate(int iCode, DWORD dwUserId, NMS_USER *pUser, NMS_USER_GROUP *pGroup);
03482383 325 void OnAlarmUpdate(DWORD dwCode, NXC_ALARM *pAlarm);
c9363772 326 void OnActionDBUpdate(DWORD dwCode, NXC_ACTION *pAction);
5af4c41b
VK
327};
328
329
9a19737f
VK
330//
331// Functions
332//
333
7084ac81
VK
334BOOL ConfigReadStr(char *szVar, char *szBuffer, int iBufSize, char *szDefault);
335int ConfigReadInt(char *szVar, int iDefault);
e77547c2 336DWORD ConfigReadULong(char *szVar, DWORD dwDefault);
7084ac81
VK
337BOOL ConfigWriteStr(char *szVar, char *szValue, BOOL bCreate);
338BOOL ConfigWriteInt(char *szVar, int iValue, BOOL bCreate);
e77547c2 339BOOL ConfigWriteULong(char *szVar, DWORD dwValue, BOOL bCreate);
7084ac81 340
3a2f672c
VK
341void InitLog(void);
342void CloseLog(void);
343void WriteLog(DWORD msg, WORD wType, char *format, ...);
344
4385fa12
VK
345BOOL ParseCommandLine(int argc, char *argv[]);
346BOOL LoadConfig(void);
9a19737f
VK
347
348void Shutdown(void);
349BOOL Initialize(void);
350void Main(void);
351
d627ae40
VK
352BOOL SleepAndCheckForShutdown(int iSeconds);
353
d627ae40
VK
354void SaveObjects(void);
355
3a2f672c
VK
356BOOL DBInit(void);
357DB_HANDLE DBConnect(void);
358void DBDisconnect(DB_HANDLE hConn);
359BOOL DBQuery(DB_HANDLE hConn, char *szQuery);
360DB_RESULT DBSelect(DB_HANDLE hConn, char *szQuery);
20177e8e
VK
361DB_ASYNC_RESULT DBAsyncSelect(DB_HANDLE hConn, char *szQuery);
362BOOL DBFetch(DB_ASYNC_RESULT hResult);
3a2f672c 363char *DBGetField(DB_RESULT hResult, int iRow, int iColumn);
a68e217b 364long DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn);
9f20696e 365double DBGetFieldDouble(DB_RESULT hResult, int iRow, int iColumn);
a68e217b 366DWORD DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn);
20177e8e
VK
367char *DBGetFieldAsync(DB_ASYNC_RESULT hResult, int iColumn, char *pBuffer, int iBufSize);
368long DBGetFieldAsyncLong(DB_RESULT hResult, int iColumn);
9f20696e 369double DBGetFieldAsyncDouble(DB_RESULT hResult, int iColumn);
20177e8e 370DWORD DBGetFieldAsyncULong(DB_ASYNC_RESULT hResult, int iColumn);
3a2f672c
VK
371int DBGetNumRows(DB_RESULT hResult);
372void DBFreeResult(DB_RESULT hResult);
20177e8e 373void DBFreeAsyncResult(DB_ASYNC_RESULT hResult);
cc140cce 374void DBUnloadDriver(void);
3a2f672c 375
067f2689 376void QueueSQLRequest(char *szQuery);
ccdbbb52 377void StartDBWriter(void);
83f0529c 378void StopDBWriter(void);
067f2689 379
be0a5a53 380void SnmpInit(void);
a7b85168
VK
381BOOL SnmpGet(DWORD dwAddr, const char *szCommunity, const char *szOidStr,
382 const oid *oidBinary, size_t iOidLen, void *pValue,
f92869c9 383 DWORD dwBufferSize, BOOL bVerbose, BOOL bStringResult);
3ea35b38
VK
384BOOL SnmpEnumerate(DWORD dwAddr, const char *szCommunity, const char *szRootOid,
385 void (* pHandler)(DWORD, const char *, variable_list *, void *),
4ea67c99 386 void *pUserArg, BOOL bVerbose);
f92869c9 387void OidToStr(oid *pOid, int iOidLen, char *szBuffer, DWORD dwBufferSize);
d7d76e50 388
910ca685 389ARP_CACHE *GetLocalArpCache(void);
910ca685
VK
390ARP_CACHE *SnmpGetArpCache(DWORD dwAddr, const char *szCommunity);
391
3ea35b38 392INTERFACE_LIST *SnmpGetInterfaceList(DWORD dwAddr, const char *szCommunity);
910ca685 393INTERFACE_LIST *GetLocalInterfaceList(void);
48b1c0ac 394void CleanInterfaceList(INTERFACE_LIST *pIfList);
a0495b6e 395
f91fa4c2 396void WatchdogInit(void);
a97797f8 397DWORD WatchdogAddThread(char *szName, time_t tNotifyInterval);
f91fa4c2
VK
398void WatchdogNotify(DWORD dwId);
399void WatchdogPrintStatus(void);
400
3e4e127f 401void CheckForMgmtNode(void);
b900a78b 402NetObj *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwFlags, TCHAR *pszName);
3e4e127f 403
62f5857f 404void EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
3421c063 405void NotifyClient(ClientSession *pSession, void *pArg);
62f5857f 406
0fdc2761
VK
407void BinToStr(BYTE *pData, DWORD dwSize, char *pStr);
408DWORD StrToBin(char *pStr, BYTE *pData, DWORD dwSize);
e77547c2 409
c7ca9142
VK
410void GetSysInfoStr(char *pszBuffer);
411DWORD GetLocalIpAddr(void);
412
4d5a05a0
VK
413BOOL ExecCommand(char *pszCommand);
414
3ea35b38
VK
415BOOL InitIdTable(void);
416DWORD CreateUniqueId(int iGroup);
417
06e7be2f
VK
418BYTE *LoadFile(char *pszFileName, DWORD *pdwFileSize);
419
e641b7d0 420void UpdateImageHashes(void);
aa3743b2
VK
421void SendImageCatalogue(ClientSession *pSession, DWORD dwRqId, WORD wFormat);
422void SendImageFile(ClientSession *pSession, DWORD dwRqId, DWORD dwImageId, WORD wFormat);
8bff53f6 423void SendDefaultImageList(ClientSession *pSession, DWORD dwRqId);
e641b7d0 424
8f7e2482
VK
425char *EncodeSQLString(const char *pszIn);
426void DecodeSQLString(char *pszStr);
427
b86ba1c0
VK
428void InitMailer(void);
429void ShutdownMailer(void);
430void PostMail(char *pszRcpt, char *pszSubject, char *pszText);
431
9a19737f
VK
432#ifdef _WIN32
433
434void InitService(void);
435void InstallService(char *execName);
436void RemoveService(void);
437void StartCoreService(void);
438void StopCoreService(void);
439void InstallEventSource(char *path);
440void RemoveEventSource(void);
441
442char *GetSystemErrorText(DWORD error);
443
444#endif /* _WIN32 */
445
be0a5a53 446void DbgTestMutex(MUTEX hMutex, char *szName);
7fdc8a45 447void DbgPrintf(DWORD dwFlags, char *szFormat, ...);
c868331f 448void DumpSessions(void);
be0a5a53 449
9a19737f
VK
450
451//
452// Global variables
453//
454
4385fa12
VK
455extern DWORD g_dwFlags;
456extern char g_szConfigFile[];
457extern char g_szLogFile[];
06e7be2f 458extern char g_szDataDir[];
3a2f672c
VK
459
460extern char g_szDbDriver[];
4385fa12 461extern char g_szDbDriver[];
3a2f672c
VK
462extern char g_szDbDrvParams[];
463extern char g_szDbServer[];
464extern char g_szDbLogin[];
465extern char g_szDbPassword[];
466extern char g_szDbName[];
467
468extern DB_HANDLE g_hCoreDB;
067f2689 469extern Queue *g_pLazyRequestQueue;
9a19737f 470
eb8b1960
VK
471extern char *g_pszStatusName[];
472
9a19737f 473#endif /* _nms_core_h_ */