a0ede4698187ee8fa35d38428652b509284877c5
[public/netxms.git] / src / libnetxms / agent.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Foundation Library
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: agent.cpp
21 **
22 **/
23
24 #include "libnetxms.h"
25
26
27 //
28 // Static data
29 //
30
31 static void (* s_fpWriteLog)(int, int, const TCHAR *) = NULL;
32 static void (* s_fpSendTrap1)(DWORD, const char *, va_list) = NULL;
33 static void (* s_fpSendTrap2)(DWORD, int, TCHAR **) = NULL;
34 static BOOL (* s_fpSendFile)(void *, DWORD, const TCHAR *, long) = NULL;
35 static BOOL (* s_fpPushData)(const TCHAR *, const TCHAR *) = NULL;
36
37
38 //
39 // Initialize subagent API
40 //
41
42 void LIBNETXMS_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
43 void (* sendTrap1)(DWORD, const char *, va_list),
44 void (* sendTrap2)(DWORD, int, TCHAR **),
45 BOOL (* sendFile)(void *, DWORD, const TCHAR *, long),
46 BOOL (* pushData)(const TCHAR *, const TCHAR *))
47 {
48 s_fpWriteLog = writeLog;
49 s_fpSendTrap1 = sendTrap1;
50 s_fpSendTrap2 = sendTrap2;
51 s_fpSendFile = sendFile;
52 s_fpPushData = pushData;
53 }
54
55
56 //
57 // Write message to agent's log
58 //
59
60 void LIBNETXMS_EXPORTABLE AgentWriteLog(int logLevel, const TCHAR *format, ...)
61 {
62 TCHAR szBuffer[4096];
63 va_list args;
64
65 if (s_fpWriteLog != NULL)
66 {
67 va_start(args, format);
68 _vsntprintf(szBuffer, 4096, format, args);
69 va_end(args);
70 s_fpWriteLog(logLevel, 0, szBuffer);
71 }
72 }
73
74 void LIBNETXMS_EXPORTABLE AgentWriteLog2(int logLevel, const TCHAR *format, va_list args)
75 {
76 TCHAR szBuffer[4096];
77
78 if (s_fpWriteLog != NULL)
79 {
80 _vsntprintf(szBuffer, 4096, format, args);
81 s_fpWriteLog(logLevel, 0, szBuffer);
82 }
83 }
84
85 void LIBNETXMS_EXPORTABLE AgentWriteDebugLog(int level, const TCHAR *format, ...)
86 {
87 TCHAR szBuffer[4096];
88 va_list args;
89
90 if (s_fpWriteLog != NULL)
91 {
92 va_start(args, format);
93 _vsntprintf(szBuffer, 4096, format, args);
94 va_end(args);
95 s_fpWriteLog(EVENTLOG_DEBUG_TYPE, level, szBuffer);
96 }
97 }
98
99 void LIBNETXMS_EXPORTABLE AgentWriteDebugLog2(int level, const TCHAR *format, va_list args)
100 {
101 TCHAR szBuffer[4096];
102
103 if (s_fpWriteLog != NULL)
104 {
105 _vsntprintf(szBuffer, 4096, format, args);
106 s_fpWriteLog(EVENTLOG_DEBUG_TYPE, level, szBuffer);
107 }
108 }
109
110
111 //
112 // Send trap from agent to server
113 //
114
115 void LIBNETXMS_EXPORTABLE AgentSendTrap(DWORD dwEvent, const char *pszFormat, ...)
116 {
117 va_list args;
118
119 if (s_fpSendTrap1 != NULL)
120 {
121 va_start(args, pszFormat);
122 s_fpSendTrap1(dwEvent, pszFormat, args);
123 va_end(args);
124 }
125 }
126
127 void LIBNETXMS_EXPORTABLE AgentSendTrap2(DWORD dwEvent, int nCount, TCHAR **ppszArgList)
128 {
129 if (s_fpSendTrap2 != NULL)
130 s_fpSendTrap2(dwEvent, nCount, ppszArgList);
131 }
132
133
134 //
135 // Get arguments for parameters like name(arg1,...)
136 // Returns FALSE on processing error
137 //
138
139 BOOL LIBNETXMS_EXPORTABLE AgentGetParameterArg(const TCHAR *param, int index, TCHAR *arg, int maxSize)
140 {
141 const TCHAR *ptr1, *ptr2;
142 int state, currIndex, pos;
143 BOOL bResult = TRUE;
144
145 arg[0] = 0; // Default is empty string
146 ptr1 = _tcschr(param, _T('('));
147 if (ptr1 == NULL)
148 return TRUE; // No arguments at all
149 for(ptr2 = ptr1 + 1, currIndex = 1, state = 0, pos = 0; state != -1; ptr2++)
150 {
151 switch(state)
152 {
153 case 0: // Normal
154 switch(*ptr2)
155 {
156 case _T(')'):
157 if (currIndex == index)
158 arg[pos] = 0;
159 state = -1; // Finish processing
160 break;
161 case _T('"'):
162 state = 1; // String
163 break;
164 case _T('\''): // String, type 2
165 state = 2;
166 break;
167 case _T(','):
168 if (currIndex == index)
169 {
170 arg[pos] = 0;
171 state = -1;
172 }
173 else
174 {
175 currIndex++;
176 }
177 break;
178 case 0:
179 state = -1; // Finish processing
180 bResult = FALSE; // Set error flag
181 break;
182 default:
183 if ((currIndex == index) && (pos < maxSize - 1))
184 arg[pos++] = *ptr2;
185 }
186 break;
187 case 1: // String in ""
188 switch(*ptr2)
189 {
190 case _T('"'):
191 state = 0; // Normal
192 break;
193 case 0:
194 state = -1; // Finish processing
195 bResult = FALSE; // Set error flag
196 break;
197 default:
198 if ((currIndex == index) && (pos < maxSize - 1))
199 arg[pos++] = *ptr2;
200 }
201 break;
202 case 2: // String in ''
203 switch(*ptr2)
204 {
205 case _T('\''):
206 state = 0; // Normal
207 break;
208 case 0:
209 state = -1; // Finish processing
210 bResult = FALSE; // Set error flag
211 break;
212 default:
213 if ((currIndex == index) && (pos < maxSize - 1))
214 arg[pos++] = *ptr2;
215 }
216 break;
217 }
218 }
219
220 if (bResult)
221 StrStrip(arg);
222 return bResult;
223 }
224
225
226 //
227 // Send file to server
228 //
229
230 BOOL LIBNETXMS_EXPORTABLE AgentSendFileToServer(void *session, DWORD requestId, const TCHAR *file, long offset)
231 {
232 if ((s_fpSendFile == NULL) || (session == NULL) || (file == NULL))
233 return FALSE;
234 return s_fpSendFile(session, requestId, file, offset);
235 }
236
237
238 //
239 // Push parameter's value
240 //
241
242 BOOL LIBNETXMS_EXPORTABLE AgentPushParameterData(const TCHAR *parameter, const TCHAR *value)
243 {
244 if (s_fpPushData == NULL)
245 return FALSE;
246 return s_fpPushData(parameter, value);
247 }
248
249 BOOL LIBNETXMS_EXPORTABLE AgentPushParameterDataInt32(const TCHAR *parameter, LONG value)
250 {
251 TCHAR buffer[64];
252
253 _sntprintf(buffer, sizeof(buffer), _T("%d"), value);
254 return AgentPushParameterData(parameter, buffer);
255 }
256
257 BOOL LIBNETXMS_EXPORTABLE AgentPushParameterDataUInt32(const TCHAR *parameter, DWORD value)
258 {
259 TCHAR buffer[64];
260
261 _sntprintf(buffer, sizeof(buffer), _T("%u"), value);
262 return AgentPushParameterData(parameter, buffer);
263 }
264
265 BOOL LIBNETXMS_EXPORTABLE AgentPushParameterDataInt64(const TCHAR *parameter, INT64 value)
266 {
267 TCHAR buffer[64];
268
269 _sntprintf(buffer, sizeof(buffer), INT64_FMT, value);
270 return AgentPushParameterData(parameter, buffer);
271 }
272
273 BOOL LIBNETXMS_EXPORTABLE AgentPushParameterDataUInt64(const TCHAR *parameter, QWORD value)
274 {
275 TCHAR buffer[64];
276
277 _sntprintf(buffer, sizeof(buffer), UINT64_FMT, value);
278 return AgentPushParameterData(parameter, buffer);
279 }
280
281 BOOL LIBNETXMS_EXPORTABLE AgentPushParameterDataDouble(const TCHAR *parameter, double value)
282 {
283 TCHAR buffer[64];
284
285 _sntprintf(buffer, sizeof(buffer), _T("%f"), value);
286 return AgentPushParameterData(parameter, buffer);
287 }