05495f5d5f7a24fbb88e591e9dd8e7660bbb8367
[public/netxms.git] / src / agent / core / nxagentd.h
1 /*
2 ** NetXMS multiplatform core agent
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 usefu,,
11 ** but ITHOUT 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: nxagentd.h
20 **
21 **/
22
23 #ifndef _nxagentd_h_
24 #define _nxagentd_h_
25
26 #include <nms_common.h>
27 #include <nms_util.h>
28 #include <nms_threads.h>
29 #include <nms_agent.h>
30 #include <nms_cscp.h>
31 #include <stdio.h>
32 #include <nxqueue.h>
33 #include "messages.h"
34
35 #ifdef _WIN32
36 #include <psapi.h>
37 #endif
38
39 #define LIBNXCL_NO_DECLARATIONS
40 #include <nxclapi.h>
41
42
43 //
44 // Version
45 //
46
47 #ifdef _DEBUG
48 #define DEBUG_SUFFIX "-debug"
49 #else
50 #define DEBUG_SUFFIX
51 #endif
52 #define AGENT_VERSION_STRING NETXMS_VERSION_STRING DEBUG_SUFFIX
53
54
55 //
56 // Default files
57 //
58
59 #if defined(_WIN32)
60 #define AGENT_DEFAULT_CONFIG "C:\\nxagentd.conf"
61 #define AGENT_DEFAULT_LOG "C:\\nxagentd.log"
62 #elif defined(_NETWARE)
63 #define AGENT_DEFAULT_CONFIG "SYS:ETC/nxagentd.conf"
64 #define AGENT_DEFAULT_LOG "SYS:ETC/nxagentd.log"
65 #else
66 #define AGENT_DEFAULT_CONFIG "/etc/nxagentd.conf"
67 #define AGENT_DEFAULT_LOG "/var/log/nxagentd"
68 #endif
69
70
71 //
72 // Constants
73 //
74
75 #ifdef _WIN32
76 #define AGENT_SERVICE_NAME "NetXMSAgentdW32"
77 #define AGENT_EVENT_SOURCE "NetXMS Win32 Agent"
78 #endif
79
80 #define MAX_SERVERS 32
81
82 #define AF_DAEMON 0x0001
83 #define AF_USE_SYSLOG 0x0002
84 #define AF_DEBUG 0x0004
85 #define AF_REQUIRE_AUTH 0x0008
86 #define AF_LOG_UNRESOLVED_SYMBOLS 0x0010
87 #define AF_ENABLE_ACTIONS 0x0020
88 #define AF_SHUTDOWN 0x1000
89
90
91 #ifdef _WIN32
92
93 #define MAX_PROCESSES 4096
94 #define MAX_MODULES 512
95
96
97 //
98 // Attributes for H_ProcInfo
99 //
100
101 #define PROCINFO_GDI_OBJ 1
102 #define PROCINFO_IO_OTHER_B 2
103 #define PROCINFO_IO_OTHER_OP 3
104 #define PROCINFO_IO_READ_B 4
105 #define PROCINFO_IO_READ_OP 5
106 #define PROCINFO_IO_WRITE_B 6
107 #define PROCINFO_IO_WRITE_OP 7
108 #define PROCINFO_KTIME 8
109 #define PROCINFO_PF 9
110 #define PROCINFO_USER_OBJ 10
111 #define PROCINFO_UTIME 11
112 #define PROCINFO_VMSIZE 12
113 #define PROCINFO_WKSET 13
114
115 #define INFOTYPE_MIN 0
116 #define INFOTYPE_MAX 1
117 #define INFOTYPE_AVG 2
118 #define INFOTYPE_SUM 3
119
120
121 //
122 // Attributes for H_NetIPStats and H_NetInterfacStats
123 //
124
125 #ifdef _WIN32
126 #define NET_IP_FORWARDING 1
127
128 #define NET_IF_BYTES_IN 1
129 #define NET_IF_BYTES_OUT 2
130 #define NET_IF_DESCR 3
131 #define NET_IF_IN_ERRORS 4
132 #define NET_IF_LINK 5
133 #define NET_IF_OUT_ERRORS 6
134 #define NET_IF_PACKETS_IN 7
135 #define NET_IF_PACKETS_OUT 8
136 #define NET_IF_SPEED 9
137 #define NET_IF_ADMIN_STATUS 10
138 #endif
139
140
141 //
142 // Request types for H_MemoryInfo
143 //
144
145 #define MEMINFO_PHYSICAL_FREE 1
146 #define MEMINFO_PHYSICAL_TOTAL 2
147 #define MEMINFO_PHYSICAL_USED 3
148 #define MEMINFO_SWAP_FREE 4
149 #define MEMINFO_SWAP_TOTAL 5
150 #define MEMINFO_SWAP_USED 6
151 #define MEMINFO_VIRTUAL_FREE 7
152 #define MEMINFO_VIRTUAL_TOTAL 8
153 #define MEMINFO_VIRTUAL_USED 9
154
155 #endif /* _WIN32 */
156
157
158 //
159 // Action types
160 //
161
162 #define AGENT_ACTION_EXEC 1
163 #define AGENT_ACTION_SUBAGENT 2
164
165
166 //
167 // Action definition structure
168 //
169
170 typedef struct
171 {
172 char szName[MAX_PARAM_NAME];
173 int iType;
174 char *pszCmdLine;
175 } ACTION;
176
177
178 //
179 // Parameter definition structure
180 //
181
182 struct AGENT_PARAM
183 {
184 char name[MAX_PARAM_NAME]; // Parameter's name (wildcard)
185 LONG (* handler)(char *,char *,char *); // Handler
186 char *arg; // Optional argument passsed to the handler function
187 };
188
189
190 //
191 // Loaded subagent information
192 //
193
194 struct SUBAGENT
195 {
196 HMODULE hModule; // Subagent's module handle
197 NETXMS_SUBAGENT_INFO *pInfo; // Information provided by subagent
198 char szName[MAX_PATH]; // Name of the module
199 };
200
201
202 //
203 // Communication session
204 //
205
206 class CommSession
207 {
208 private:
209 SOCKET m_hSocket;
210 Queue *m_pSendQueue;
211 Queue *m_pMessageQueue;
212 CSCP_BUFFER *m_pMsgBuffer;
213 THREAD m_hWriteThread;
214 THREAD m_hProcessingThread;
215 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
216 DWORD m_dwIndex;
217 BOOL m_bIsAuthenticated;
218
219 void Authenticate(CSCPMessage *pRequest, CSCPMessage *pMsg);
220 void GetParameter(CSCPMessage *pRequest, CSCPMessage *pMsg);
221 void GetList(CSCPMessage *pRequest, CSCPMessage *pMsg);
222 void Action(CSCPMessage *pRequest, CSCPMessage *pMsg);
223
224 void ReadThread(void);
225 void WriteThread(void);
226 void ProcessingThread(void);
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
232 public:
233 CommSession(SOCKET hSocket, DWORD dwHostAddr);
234 ~CommSession();
235
236 void Run(void);
237
238 void SendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
239
240 DWORD GetIndex(void) { return m_dwIndex; }
241 void SetIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
242 };
243
244
245 //
246 // Functions
247 //
248
249 BOOL Initialize(void);
250 void Shutdown(void);
251 void Main(void);
252
253 void WriteLog(DWORD msg, WORD wType, char *format, ...);
254 void InitLog(void);
255 void CloseLog(void);
256
257 void ConsolePrintf(char *pszFormat, ...);
258 void DebugPrintf(char *pszFormat, ...);
259
260 BOOL InitParameterList(void);
261 void AddParameter(char *szName, LONG (* fpHandler)(char *,char *,char *), char *pArg);
262 void AddEnum(char *szName, LONG (* fpHandler)(char *,char *,NETXMS_VALUES_LIST *), char *pArg);
263 BOOL AddExternalParameter(char *pszCfgLine);
264 DWORD GetParameterValue(char *pszParam, char *pszValue);
265 DWORD GetEnumValue(char *pszParam, NETXMS_VALUES_LIST *pValue);
266
267 BOOL LoadSubAgent(char *szModuleName);
268 void UnloadAllSubAgents(void);
269
270 BOOL AddAction(char *pszName, int iType, char *pszCmdLine);
271 BOOL AddActionFromConfig(char *pszLine);
272 DWORD ExecAction(char *pszAction, NETXMS_VALUES_LIST *pArgs);
273
274 DWORD ExecuteCommand(char *pszCommand, NETXMS_VALUES_LIST *pArgs);
275
276 #ifdef _WIN32
277
278 void InitService(void);
279 void InstallService(char *execName, char *confFile);
280 void RemoveService(void);
281 void StartAgentService(void);
282 void StopAgentService(void);
283 void InstallEventSource(char *path);
284 void RemoveEventSource(void);
285
286 char *GetPdhErrorText(DWORD dwError, char *pszBuffer, int iBufSize);
287
288 #endif
289
290
291 //
292 // Global variables
293 //
294
295 extern DWORD g_dwFlags;
296 extern char g_szLogFile[];
297 extern char g_szSharedSecret[];
298 extern char g_szConfigFile[];
299 extern WORD g_wListenPort;
300 extern DWORD g_dwServerAddr[];
301 extern DWORD g_dwServerCount;
302 extern time_t g_dwAgentStartTime;
303
304 extern DWORD g_dwAcceptErrors;
305 extern DWORD g_dwAcceptedConnections;
306 extern DWORD g_dwRejectedConnections;
307
308 #ifdef _WIN32
309 extern DWORD (__stdcall *imp_GetGuiResources)(HANDLE, DWORD);
310 extern BOOL (__stdcall *imp_GetProcessIoCounters)(HANDLE, PIO_COUNTERS);
311 extern BOOL (__stdcall *imp_GetPerformanceInfo)(PPERFORMANCE_INFORMATION, DWORD);
312 extern BOOL (__stdcall *imp_GlobalMemoryStatusEx)(LPMEMORYSTATUSEX);
313 extern DWORD (__stdcall *imp_HrLanConnectionNameFromGuidOrPath)(LPWSTR, LPWSTR, LPWSTR, LPDWORD);
314 #endif /* _WIN32 */
315
316 #endif /* _nxagentd_h_ */