Log and database functions moved from server core to libnxsrv
[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"
9e9d381c
VK
89#define DEFAULT_CONFIG_FILE "/etc/netxmsd.conf"
90#define DEFAULT_LOG_FILE "/var/log/netxmsd.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 98#define MAX_LINE_SIZE 4096
3a2f672c 99
4d5a05a0
VK
100#define UNLOCKED ((DWORD)0xFFFFFFFF)
101
102#define GROUP_FLAG_BIT ((DWORD)0x80000000)
c7ca9142 103
f92869c9
VK
104typedef void * HSNMPSESSION;
105
b7271ccb 106
3ea35b38
VK
107//
108// Unique identifier group codes
109//
110
111#define IDG_NETWORK_OBJECT 0
ef44d5ea 112#define IDG_CONTAINER_CAT 1
3ea35b38
VK
113#define IDG_EVENT 2
114#define IDG_ITEM 3
62d11997 115//#define IDG_DCT 4
a60d4f27 116#define IDG_IMAGE 5
3ea35b38
VK
117#define IDG_ACTION 6
118#define IDG_EVENT_GROUP 7
119#define IDG_THRESHOLD 8
de78f964
VK
120#define IDG_USER 9
121#define IDG_USER_GROUP 10
06e7be2f
VK
122#define IDG_ALARM 11
123#define IDG_ALARM_NOTE 12
3ea35b38
VK
124
125
9a19737f 126//
4385fa12 127// Application flags
9a19737f
VK
128//
129
3e4e127f
VK
130#define AF_STANDALONE 0x00000001
131#define AF_USE_EVENT_LOG 0x00000002
132#define AF_ENABLE_ACCESS_CONTROL 0x00000004
133#define AF_ENABLE_EVENTS_ACCESS_CONTROL 0x00000008
78bf9c68 134#define AF_LOG_SQL_ERRORS 0x00000010
3ea35b38 135#define AF_DELETE_EMPTY_SUBNETS 0x00000020
5add75d8 136#define AF_ENABLE_SNMP_TRAPD 0x00000040
3e4e127f
VK
137#define AF_DEBUG_EVENTS 0x00000100
138#define AF_DEBUG_CSCP 0x00000200
139#define AF_DEBUG_DISCOVERY 0x00000400
140#define AF_DEBUG_DC 0x00000800
83f0529c 141#define AF_DEBUG_HOUSEKEEPER 0x00001000
b54b2b11 142#define AF_DEBUG_LOCKS 0x00002000
4d5a05a0 143#define AF_DEBUG_ACTIONS 0x00004000
0c598d0e 144#define AF_DEBUG_MISC 0x00008000
b1dd534d 145#define AF_DEBUG_SQL 0x00010000
3679d89a 146#define AF_DEBUG_ALL 0x0000FF00
2260ffe5 147#define AF_DB_LOCKED 0x01000000
3e4e127f 148#define AF_SHUTDOWN 0x80000000
a68e217b 149
1a26164f 150#define IsStandalone() (g_dwFlags & AF_STANDALONE)
a68e217b 151#define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
9a19737f
VK
152
153
62f5857f 154//
b54b2b11 155// Client session flags
62f5857f
VK
156//
157
b8bad201
VK
158#define CSF_EVENT_DB_LOCKED ((DWORD)0x0001)
159#define CSF_EPP_LOCKED ((DWORD)0x0002)
160#define CSF_EVENT_DB_MODIFIED ((DWORD)0x0004)
ff550544 161#define CSF_USER_DB_LOCKED ((DWORD)0x0008)
8f7e2482 162#define CSF_EPP_UPLOAD ((DWORD)0x0010)
c9363772 163#define CSF_ACTION_DB_LOCKED ((DWORD)0x0020)
62f5857f
VK
164
165
c7ca9142 166//
b54b2b11 167// Information categories for UPDATE_INFO structure
c7ca9142
VK
168//
169
53512272
VK
170#define INFO_CAT_EVENT 1
171#define INFO_CAT_OBJECT_CHANGE 2
03482383 172#define INFO_CAT_ALARM 3
c9363772 173#define INFO_CAT_ACTION 4
c7ca9142
VK
174
175
f9b7e653
VK
176//
177// Event handling subsystem definitions
178//
179
180#include "nms_events.h"
7e495d97 181#include "nms_actions.h"
2260ffe5 182#include "nms_alarm.h"
f9b7e653
VK
183
184
62f5857f
VK
185//
186// Data update structure for client sessions
187//
188
189typedef struct
190{
191 DWORD dwCategory; // Data category - event, network object, etc.
c9363772 192 DWORD dwCode; // Data-specific update code
62f5857f
VK
193 void *pData; // Pointer to data block
194} UPDATE_INFO;
195
196
5af4c41b
VK
197//
198// Client session
199//
200
201class ClientSession
202{
203private:
204 SOCKET m_hSocket;
205 Queue *m_pSendQueue;
4a4afbd0 206 Queue *m_pMessageQueue;
62f5857f 207 Queue *m_pUpdateQueue;
da54bd8d
VK
208 DWORD m_dwIndex;
209 int m_iState;
210 DWORD m_dwUserId;
4b8ef715 211 DWORD m_dwSystemAccess; // User's system access rights
b54b2b11 212 DWORD m_dwFlags; // Session flags
3664c5ba 213 CSCP_BUFFER *m_pMsgBuffer;
ccdbbb52
VK
214 THREAD m_hWriteThread;
215 THREAD m_hProcessingThread;
216 THREAD m_hUpdateThread;
03482383
VK
217 MUTEX m_mutexSendEvents;
218 MUTEX m_mutexSendObjects;
219 MUTEX m_mutexSendAlarms;
c9363772 220 MUTEX m_mutexSendActions;
d966d18a 221 MUTEX m_mutexPollerInit;
b54b2b11
VK
222 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
223 char m_szUserName[256]; // String in form login_name@host
338493a0
VK
224 DWORD m_dwOpenDCIListSize; // Number of open DCI lists
225 DWORD *m_pOpenDCIList; // List of nodes with DCI lists open
8f7e2482
VK
226 DWORD m_dwNumRecordsToUpload; // Number of records to be uploaded
227 DWORD m_dwRecordsUploaded;
228 EPRule **m_ppEPPRuleList; // List of loaded EPP rules
da54bd8d 229
ccdbbb52
VK
230 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
231 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
232 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
233 static THREAD_RESULT THREAD_CALL UpdateThreadStarter(void *);
d966d18a 234 static THREAD_RESULT THREAD_CALL PollerThreadStarter(void *);
ccdbbb52
VK
235
236 void ReadThread(void);
237 void WriteThread(void);
238 void ProcessingThread(void);
239 void UpdateThread(void);
d966d18a 240 void PollerThread(Node *pNode, int iPollType, DWORD dwRqId);
ccdbbb52 241
605d2931
VK
242 BOOL CheckSysAccessRights(DWORD dwRequiredAccess)
243 {
244 return m_dwUserId == 0 ? TRUE :
245 ((dwRequiredAccess & m_dwSystemAccess) ? TRUE : FALSE);
246 }
247
da54bd8d 248 void DebugPrintf(char *szFormat, ...);
7257eb7d 249 void Login(CSCPMessage *pRequest);
e100f091 250 void SendAllObjects(DWORD dwRqId);
74a5aa17 251 void SendAllEvents(DWORD dwRqId);
4b8ef715 252 void SendAllConfigVars(void);
23a32988 253 void SendUserDB(DWORD dwRqId);
03482383 254 void SendAllAlarms(DWORD dwRqId, BOOL bIncludeAck);
7257eb7d
VK
255 void CreateUser(CSCPMessage *pRequest);
256 void UpdateUser(CSCPMessage *pRequest);
257 void DeleteUser(CSCPMessage *pRequest);
017e1e13 258 void SetPassword(CSCPMessage *pRequest);
ff550544 259 void LockUserDB(DWORD dwRqId, BOOL bLock);
7257eb7d 260 void SetConfigVariable(CSCPMessage *pRequest);
b54b2b11 261 void SendEventDB(DWORD dwRqId);
3421c063 262 void CloseEventDB(DWORD dwRqId);
7257eb7d
VK
263 void SetEventInfo(CSCPMessage *pRequest);
264 void ModifyObject(CSCPMessage *pRequest);
c1c39152 265 void ChangeObjectMgmtStatus(CSCPMessage *pRequest);
7257eb7d
VK
266 void OpenNodeDCIList(CSCPMessage *pRequest);
267 void CloseNodeDCIList(CSCPMessage *pRequest);
9ed4eaff 268 void ModifyNodeDCI(CSCPMessage *pRequest);
933aee5c 269 void CopyDCI(CSCPMessage *pRequest);
ebebc8ea 270 void GetCollectedData(CSCPMessage *pRequest);
06e7be2f
VK
271 void OpenEPP(DWORD dwRqId);
272 void CloseEPP(DWORD dwRqId);
273 void SaveEPP(CSCPMessage *pRequest);
8f7e2482 274 void ProcessEPPRecord(CSCPMessage *pRequest);
06e7be2f
VK
275 void SendMIBList(DWORD dwRqId);
276 void SendMIB(CSCPMessage *pRequest);
3421c063 277 void SendEventNames(DWORD dwRqId);
e6b958c4
VK
278 void CreateObject(CSCPMessage *pRequest);
279 void ChangeObjectBinding(CSCPMessage *pRequest, BOOL bBind);
88039e6c 280 void DeleteObject(CSCPMessage *pRequest);
ee20e615 281 void AcknowlegeAlarm(CSCPMessage *pRequest);
c9363772
VK
282 void CreateAction(CSCPMessage *pRequest);
283 void UpdateAction(CSCPMessage *pRequest);
284 void DeleteAction(CSCPMessage *pRequest);
285 void LockActionDB(DWORD dwRqId, BOOL bLock);
e91cb822 286 void SendAllActions(DWORD dwRqId);
6849d9be 287 void SendContainerCategories(DWORD dwRqId);
0b89f5fb 288 void ForcedNodePoll(CSCPMessage *pRequest);
5af4c41b
VK
289
290public:
b54b2b11 291 ClientSession(SOCKET hSocket, DWORD dwHostAddr);
5af4c41b
VK
292 ~ClientSession();
293
ccdbbb52 294 void Run(void);
da54bd8d 295
3c55b85d 296 void SendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
d966d18a 297 void SendPollerMsg(DWORD dwRqId, TCHAR *pszMsg);
3c55b85d 298
da54bd8d
VK
299 DWORD GetIndex(void) { return m_dwIndex; }
300 void SetIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
62f5857f 301 int GetState(void) { return m_iState; }
c868331f 302 const char *GetUserName(void) { return m_szUserName; }
03482383 303 DWORD GetUserId(void) { return m_dwUserId; }
20177e8e
VK
304
305 void Kill(void);
4de204a3 306 void Notify(DWORD dwCode, DWORD dwData = 0);
62f5857f
VK
307
308 void OnNewEvent(Event *pEvent);
53512272 309 void OnObjectChange(NetObj *pObject);
984542f8 310 void OnUserDBUpdate(int iCode, DWORD dwUserId, NMS_USER *pUser, NMS_USER_GROUP *pGroup);
03482383 311 void OnAlarmUpdate(DWORD dwCode, NXC_ALARM *pAlarm);
c9363772 312 void OnActionDBUpdate(DWORD dwCode, NXC_ACTION *pAction);
5af4c41b
VK
313};
314
315
9a19737f
VK
316//
317// Functions
318//
319
62d11997 320BOOL ConfigReadStr(char *szVar, char *szBuffer, int iBufSize, const char *szDefault);
7084ac81 321int ConfigReadInt(char *szVar, int iDefault);
e77547c2 322DWORD ConfigReadULong(char *szVar, DWORD dwDefault);
7084ac81
VK
323BOOL ConfigWriteStr(char *szVar, char *szValue, BOOL bCreate);
324BOOL ConfigWriteInt(char *szVar, int iValue, BOOL bCreate);
e77547c2 325BOOL ConfigWriteULong(char *szVar, DWORD dwValue, BOOL bCreate);
7084ac81 326
4385fa12
VK
327BOOL ParseCommandLine(int argc, char *argv[]);
328BOOL LoadConfig(void);
9a19737f
VK
329
330void Shutdown(void);
331BOOL Initialize(void);
332void Main(void);
333
d627ae40
VK
334BOOL SleepAndCheckForShutdown(int iSeconds);
335
d627ae40
VK
336void SaveObjects(void);
337
067f2689 338void QueueSQLRequest(char *szQuery);
ccdbbb52 339void StartDBWriter(void);
83f0529c 340void StopDBWriter(void);
067f2689 341
be0a5a53 342void SnmpInit(void);
a7b85168
VK
343BOOL SnmpGet(DWORD dwAddr, const char *szCommunity, const char *szOidStr,
344 const oid *oidBinary, size_t iOidLen, void *pValue,
f92869c9 345 DWORD dwBufferSize, BOOL bVerbose, BOOL bStringResult);
3ea35b38
VK
346BOOL SnmpEnumerate(DWORD dwAddr, const char *szCommunity, const char *szRootOid,
347 void (* pHandler)(DWORD, const char *, variable_list *, void *),
4ea67c99 348 void *pUserArg, BOOL bVerbose);
f92869c9 349void OidToStr(oid *pOid, int iOidLen, char *szBuffer, DWORD dwBufferSize);
b50f1100 350void StrToMac(char *pszStr, BYTE *pBuffer);
18e26ff8 351DWORD OidToType(TCHAR *pszOid, DWORD *pdwFlags);
d7d76e50 352
b688074e
VK
353void InitLocalNetInfo(void);
354
910ca685 355ARP_CACHE *GetLocalArpCache(void);
910ca685
VK
356ARP_CACHE *SnmpGetArpCache(DWORD dwAddr, const char *szCommunity);
357
18e26ff8 358INTERFACE_LIST *SnmpGetInterfaceList(DWORD dwAddr, const char *szCommunity, DWORD dwNodeType);
910ca685 359INTERFACE_LIST *GetLocalInterfaceList(void);
48b1c0ac 360void CleanInterfaceList(INTERFACE_LIST *pIfList);
a0495b6e 361
f91fa4c2 362void WatchdogInit(void);
a97797f8 363DWORD WatchdogAddThread(char *szName, time_t tNotifyInterval);
f91fa4c2
VK
364void WatchdogNotify(DWORD dwId);
365void WatchdogPrintStatus(void);
366
3e4e127f 367void CheckForMgmtNode(void);
b900a78b 368NetObj *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwFlags, TCHAR *pszName);
3e4e127f 369
62f5857f 370void EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
3421c063 371void NotifyClient(ClientSession *pSession, void *pArg);
62f5857f 372
c7ca9142
VK
373void GetSysInfoStr(char *pszBuffer);
374DWORD GetLocalIpAddr(void);
375
4d5a05a0
VK
376BOOL ExecCommand(char *pszCommand);
377
3ea35b38
VK
378BOOL InitIdTable(void);
379DWORD CreateUniqueId(int iGroup);
380
06e7be2f
VK
381BYTE *LoadFile(char *pszFileName, DWORD *pdwFileSize);
382
e641b7d0 383void UpdateImageHashes(void);
aa3743b2
VK
384void SendImageCatalogue(ClientSession *pSession, DWORD dwRqId, WORD wFormat);
385void SendImageFile(ClientSession *pSession, DWORD dwRqId, DWORD dwImageId, WORD wFormat);
8bff53f6 386void SendDefaultImageList(ClientSession *pSession, DWORD dwRqId);
e641b7d0 387
8f7e2482
VK
388char *EncodeSQLString(const char *pszIn);
389void DecodeSQLString(char *pszStr);
390
b86ba1c0
VK
391void InitMailer(void);
392void ShutdownMailer(void);
393void PostMail(char *pszRcpt, char *pszSubject, char *pszText);
394
18e26ff8
VK
395void GetAccelarVLANIfList(DWORD dwIpAddr, const TCHAR *pszCommunity, INTERFACE_LIST *pIfList);
396
9a19737f
VK
397#ifdef _WIN32
398
399void InitService(void);
9cc9ea72 400void InstallService(char *execName, char *dllName);
9a19737f
VK
401void RemoveService(void);
402void StartCoreService(void);
403void StopCoreService(void);
404void InstallEventSource(char *path);
405void RemoveEventSource(void);
406
407char *GetSystemErrorText(DWORD error);
408
409#endif /* _WIN32 */
410
dbe67493
VK
411void DbgTestMutex(MUTEX hMutex, TCHAR *szName);
412void DbgTestRWLock(RWLOCK hLock, TCHAR *szName);
413void DbgPrintf(DWORD dwFlags, TCHAR *szFormat, ...);
c868331f 414void DumpSessions(void);
be0a5a53 415
9a19737f
VK
416
417//
418// Global variables
419//
420
4385fa12
VK
421extern DWORD g_dwFlags;
422extern char g_szConfigFile[];
423extern char g_szLogFile[];
9e9d381c
VK
424#ifndef _WIN32
425extern char g_szPIDFile[];
426#endif
06e7be2f 427extern char g_szDataDir[];
3a2f672c 428
3a2f672c 429extern DB_HANDLE g_hCoreDB;
067f2689 430extern Queue *g_pLazyRequestQueue;
9a19737f 431
eb8b1960
VK
432extern char *g_pszStatusName[];
433
9a19737f 434#endif /* _nms_core_h_ */