fc8cd1126eb8481435495004764a5bfc8dd5c57f
[public/netxms.git] / src / agent / libnxagent / bridge.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2015 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published
7 ** by the Free Software Foundation; either version 3 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 Lesser 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 ** File: bridge.cpp
20 **
21 **/
22
23 #include "libnxagent.h"
24
25 /**
26 * Static data
27 */
28 static void (* s_fpWriteLog)(int, int, const TCHAR *) = NULL;
29 static void (* s_fpSendTrap1)(UINT32, const TCHAR *, const char *, va_list) = NULL;
30 static void (* s_fpSendTrap2)(UINT32, const TCHAR *, int, TCHAR **) = NULL;
31 static AbstractCommSession *(* s_fpFindServerSession)(UINT64) = NULL;
32 static bool (* s_fpEnumerateSessions)(EnumerationCallbackResult (*)(AbstractCommSession *, void *), void *) = NULL;
33 static bool (* s_fpSendFile)(void *, UINT32, const TCHAR *, long, bool) = NULL;
34 static bool (* s_fpPushData)(const TCHAR *, const TCHAR *, UINT32, time_t) = NULL;
35 static CONDITION s_agentShutdownCondition = INVALID_CONDITION_HANDLE;
36 static const TCHAR *s_dataDirectory = NULL;
37 static DB_HANDLE (*s_fpGetLocalDatabaseHandle)() = NULL;
38
39 /**
40 * Initialize subagent API
41 */
42 void LIBNXAGENT_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
43 void (* sendTrap1)(UINT32, const TCHAR *, const char *, va_list),
44 void (* sendTrap2)(UINT32, const TCHAR *, int, TCHAR **),
45 bool (* enumerateSessions)(EnumerationCallbackResult (*)(AbstractCommSession *, void *), void*),
46 AbstractCommSession *(* findServerSession)(UINT64),
47 bool (* sendFile)(void *, UINT32, const TCHAR *, long, bool),
48 bool (* pushData)(const TCHAR *, const TCHAR *, UINT32, time_t),
49 DB_HANDLE (* getLocalDatabaseHandle)(),
50 CONDITION shutdownCondition, const TCHAR *dataDirectory)
51 {
52 s_fpWriteLog = writeLog;
53 s_fpSendTrap1 = sendTrap1;
54 s_fpSendTrap2 = sendTrap2;
55 s_fpEnumerateSessions = enumerateSessions;
56 s_fpFindServerSession = findServerSession;
57 s_fpSendFile = sendFile;
58 s_fpPushData = pushData;
59 s_agentShutdownCondition = shutdownCondition;
60 s_dataDirectory = dataDirectory;
61 s_fpGetLocalDatabaseHandle = getLocalDatabaseHandle;
62 }
63
64 /**
65 * Write message to agent's log
66 */
67 void LIBNXAGENT_EXPORTABLE AgentWriteLog(int logLevel, const TCHAR *format, ...)
68 {
69 TCHAR szBuffer[4096];
70 va_list args;
71
72 if (s_fpWriteLog != NULL)
73 {
74 va_start(args, format);
75 _vsntprintf(szBuffer, 4096, format, args);
76 va_end(args);
77 szBuffer[4095] = 0;
78 s_fpWriteLog(logLevel, 0, szBuffer);
79 }
80 }
81
82 /**
83 * Write message to agent's log
84 */
85 void LIBNXAGENT_EXPORTABLE AgentWriteLog2(int logLevel, const TCHAR *format, va_list args)
86 {
87 TCHAR szBuffer[4096];
88
89 if (s_fpWriteLog != NULL)
90 {
91 _vsntprintf(szBuffer, 4096, format, args);
92 szBuffer[4095] = 0;
93 s_fpWriteLog(logLevel, 0, szBuffer);
94 }
95 }
96
97 /**
98 * Write debug message to agent's log
99 */
100 void LIBNXAGENT_EXPORTABLE AgentWriteDebugLog(int level, const TCHAR *format, ...)
101 {
102 TCHAR szBuffer[4096];
103 va_list args;
104
105 if (s_fpWriteLog != NULL)
106 {
107 va_start(args, format);
108 _vsntprintf(szBuffer, 4096, format, args);
109 va_end(args);
110 szBuffer[4095] = 0;
111 s_fpWriteLog(EVENTLOG_DEBUG_TYPE, level, szBuffer);
112 }
113 }
114
115 /**
116 * Write debug message to agent's log
117 */
118 void LIBNXAGENT_EXPORTABLE AgentWriteDebugLog2(int level, const TCHAR *format, va_list args)
119 {
120 TCHAR szBuffer[4096];
121
122 if (s_fpWriteLog != NULL)
123 {
124 _vsntprintf(szBuffer, 4096, format, args);
125 szBuffer[4095] = 0;
126 s_fpWriteLog(EVENTLOG_DEBUG_TYPE, level, szBuffer);
127 }
128 }
129
130 /**
131 * Send trap from agent to server
132 */
133 void LIBNXAGENT_EXPORTABLE AgentSendTrap(UINT32 dwEvent, const TCHAR *eventName, const char *pszFormat, ...)
134 {
135 va_list args;
136
137 if (s_fpSendTrap1 != NULL)
138 {
139 va_start(args, pszFormat);
140 s_fpSendTrap1(dwEvent, eventName, pszFormat, args);
141 va_end(args);
142 }
143 }
144
145 /**
146 * Send trap from agent to server
147 */
148 void LIBNXAGENT_EXPORTABLE AgentSendTrap2(UINT32 dwEvent, const TCHAR *eventName, int nCount, TCHAR **ppszArgList)
149 {
150 if (s_fpSendTrap2 != NULL)
151 s_fpSendTrap2(dwEvent, eventName, nCount, ppszArgList);
152 }
153
154 /**
155 * Enumerates active agent sessions. Callback will be called for each valid session.
156 * Callback must return _STOP to stop enumeration or _CONTINUE to continue.
157 *
158 * @return true if enumeration was stopped by callback
159 */
160 bool LIBNXAGENT_EXPORTABLE AgentEnumerateSessions(EnumerationCallbackResult (* callback)(AbstractCommSession *, void *), void *data)
161 {
162 return (s_fpEnumerateSessions != NULL) ? s_fpEnumerateSessions(callback, data) : false;
163 }
164
165 /**
166 * Send file to server
167 */
168 bool LIBNXAGENT_EXPORTABLE AgentSendFileToServer(void *session, UINT32 requestId, const TCHAR *file, long offset, bool allowCompression)
169 {
170 if ((s_fpSendFile == NULL) || (session == NULL) || (file == NULL))
171 return FALSE;
172 return s_fpSendFile(session, requestId, file, offset, allowCompression);
173 }
174
175 /**
176 * Push parameter's value
177 */
178 bool LIBNXAGENT_EXPORTABLE AgentPushParameterData(const TCHAR *parameter, const TCHAR *value)
179 {
180 if (s_fpPushData == NULL)
181 return FALSE;
182 return s_fpPushData(parameter, value, 0, 0);
183 }
184
185 /**
186 * Push parameter's value
187 */
188 bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataInt32(const TCHAR *parameter, LONG value)
189 {
190 TCHAR buffer[64];
191
192 _sntprintf(buffer, sizeof(buffer), _T("%d"), (int)value);
193 return AgentPushParameterData(parameter, buffer);
194 }
195
196 /**
197 * Push parameter's value
198 */
199 bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataUInt32(const TCHAR *parameter, UINT32 value)
200 {
201 TCHAR buffer[64];
202
203 _sntprintf(buffer, sizeof(buffer), _T("%u"), (unsigned int)value);
204 return AgentPushParameterData(parameter, buffer);
205 }
206
207 /**
208 * Push parameter's value
209 */
210 bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataInt64(const TCHAR *parameter, INT64 value)
211 {
212 TCHAR buffer[64];
213
214 _sntprintf(buffer, sizeof(buffer), INT64_FMT, value);
215 return AgentPushParameterData(parameter, buffer);
216 }
217
218 /**
219 * Push parameter's value
220 */
221 bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataUInt64(const TCHAR *parameter, QWORD value)
222 {
223 TCHAR buffer[64];
224
225 _sntprintf(buffer, sizeof(buffer), UINT64_FMT, value);
226 return AgentPushParameterData(parameter, buffer);
227 }
228
229 /**
230 * Push parameter's value
231 */
232 bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataDouble(const TCHAR *parameter, double value)
233 {
234 TCHAR buffer[64];
235
236 _sntprintf(buffer, sizeof(buffer), _T("%f"), value);
237 return AgentPushParameterData(parameter, buffer);
238 }
239
240 /**
241 * Get shutdown condition
242 */
243 CONDITION LIBNXAGENT_EXPORTABLE AgentGetShutdownCondition()
244 {
245 return s_agentShutdownCondition;
246 }
247
248 /**
249 * Sleep and check for agent shutdown
250 */
251 bool LIBNXAGENT_EXPORTABLE AgentSleepAndCheckForShutdown(UINT32 sleepTime)
252 {
253 return ConditionWait(s_agentShutdownCondition, sleepTime);
254 }
255
256 /**
257 * Get data directory
258 */
259 const TCHAR LIBNXAGENT_EXPORTABLE *AgentGetDataDirectory()
260 {
261 return s_dataDirectory;
262 }
263
264 /**
265 * Find server session. Caller must call decRefCount() for session object when finished.
266 *
267 * @param serverId server ID
268 * @return server session object or NULL
269 */
270 AbstractCommSession LIBNXAGENT_EXPORTABLE *AgentFindServerSession(UINT64 serverId)
271 {
272 return (s_fpFindServerSession != NULL) ? s_fpFindServerSession(serverId) : NULL;
273 }
274
275 /**
276 * Get handle to local database.
277 *
278 * @return database handle or NULL if not available
279 */
280 DB_HANDLE LIBNXAGENT_EXPORTABLE AgentGetLocalDatabaseHandle()
281 {
282 return (s_fpGetLocalDatabaseHandle != NULL) ? s_fpGetLocalDatabaseHandle() : NULL;
283 }