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