"Map Appearance" poperty page shown for clusters
[public/netxms.git] / src / agent / libnxagent / bridge.cpp
CommitLineData
359784de
VK
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 */
28static void (* s_fpWriteLog)(int, int, const TCHAR *) = NULL;
29static void (* s_fpSendTrap1)(UINT32, const TCHAR *, const char *, va_list) = NULL;
30static void (* s_fpSendTrap2)(UINT32, const TCHAR *, int, TCHAR **) = NULL;
6fbaa926 31static AbstractCommSession *(* s_fpFindServerSession)(UINT64) = NULL;
34b9a3e7 32static bool (* s_fpEnumerateSessions)(EnumerationCallbackResult (*)(AbstractCommSession *, void *), void *) = NULL;
359784de 33static bool (* s_fpSendFile)(void *, UINT32, const TCHAR *, long) = NULL;
b60584cf 34static bool (* s_fpPushData)(const TCHAR *, const TCHAR *, UINT32, time_t) = NULL;
359784de 35static CONDITION s_agentShutdownCondition = INVALID_CONDITION_HANDLE;
359784de 36static const TCHAR *s_dataDirectory = NULL;
bb48198e 37static DB_HANDLE (*s_fpGetLocalDatabaseHandle)() = NULL;
359784de
VK
38
39/**
40 * Initialize subagent API
41 */
42void LIBNXAGENT_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
6fbaa926
VK
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),
48 bool (* pushData)(const TCHAR *, const TCHAR *, UINT32, time_t),
7f6ecb6d
TD
49 DB_HANDLE (* getLocalDatabaseHandle)(),
50 CONDITION shutdownCondition, const TCHAR *dataDirectory)
359784de
VK
51{
52 s_fpWriteLog = writeLog;
53 s_fpSendTrap1 = sendTrap1;
54 s_fpSendTrap2 = sendTrap2;
55 s_fpEnumerateSessions = enumerateSessions;
6fbaa926 56 s_fpFindServerSession = findServerSession;
359784de
VK
57 s_fpSendFile = sendFile;
58 s_fpPushData = pushData;
59 s_agentShutdownCondition = shutdownCondition;
359784de 60 s_dataDirectory = dataDirectory;
bb48198e 61 s_fpGetLocalDatabaseHandle = getLocalDatabaseHandle;
359784de
VK
62}
63
64/**
65 * Write message to agent's log
66 */
67void 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);
94851bf0 77 szBuffer[4095] = 0;
359784de
VK
78 s_fpWriteLog(logLevel, 0, szBuffer);
79 }
80}
81
82/**
83 * Write message to agent's log
84 */
85void 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);
94851bf0 92 szBuffer[4095] = 0;
359784de
VK
93 s_fpWriteLog(logLevel, 0, szBuffer);
94 }
95}
96
97/**
98 * Write debug message to agent's log
99 */
100void 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);
94851bf0 110 szBuffer[4095] = 0;
359784de
VK
111 s_fpWriteLog(EVENTLOG_DEBUG_TYPE, level, szBuffer);
112 }
113}
114
115/**
116 * Write debug message to agent's log
117 */
118void 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);
94851bf0 125 szBuffer[4095] = 0;
359784de
VK
126 s_fpWriteLog(EVENTLOG_DEBUG_TYPE, level, szBuffer);
127 }
128}
129
130/**
131 * Send trap from agent to server
132 */
133void 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 */
148void 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/**
34b9a3e7
VK
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
359784de 159 */
34b9a3e7 160bool LIBNXAGENT_EXPORTABLE AgentEnumerateSessions(EnumerationCallbackResult (* callback)(AbstractCommSession *, void *), void *data)
359784de 161{
34b9a3e7 162 return (s_fpEnumerateSessions != NULL) ? s_fpEnumerateSessions(callback, data) : false;
359784de
VK
163}
164
165/**
166 * Send file to server
167 */
168bool LIBNXAGENT_EXPORTABLE AgentSendFileToServer(void *session, UINT32 requestId, const TCHAR *file, long offset)
169{
170 if ((s_fpSendFile == NULL) || (session == NULL) || (file == NULL))
171 return FALSE;
172 return s_fpSendFile(session, requestId, file, offset);
173}
174
175/**
176 * Push parameter's value
177 */
178bool LIBNXAGENT_EXPORTABLE AgentPushParameterData(const TCHAR *parameter, const TCHAR *value)
179{
180 if (s_fpPushData == NULL)
181 return FALSE;
b60584cf 182 return s_fpPushData(parameter, value, 0, 0);
359784de
VK
183}
184
185/**
186 * Push parameter's value
187 */
188bool 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 */
199bool 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 */
210bool 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 */
221bool 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 */
232bool 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 */
243CONDITION LIBNXAGENT_EXPORTABLE AgentGetShutdownCondition()
244{
245 return s_agentShutdownCondition;
246}
247
248/**
249 * Sleep and check for agent shutdown
250 */
251bool LIBNXAGENT_EXPORTABLE AgentSleepAndCheckForShutdown(UINT32 sleepTime)
252{
253 return ConditionWait(s_agentShutdownCondition, sleepTime);
254}
255
359784de
VK
256/**
257 * Get data directory
258 */
259const TCHAR LIBNXAGENT_EXPORTABLE *AgentGetDataDirectory()
260{
261 return s_dataDirectory;
262}
6fbaa926
VK
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 */
270AbstractCommSession LIBNXAGENT_EXPORTABLE *AgentFindServerSession(UINT64 serverId)
271{
272 return (s_fpFindServerSession != NULL) ? s_fpFindServerSession(serverId) : NULL;
273}
bb48198e
VK
274
275/**
276 * Get handle to local database.
277 *
278 * @return database handle or NULL if not available
279 */
280DB_HANDLE LIBNXAGENT_EXPORTABLE AgentGetLocalDatabaseHandle()
281{
282 return (s_fpGetLocalDatabaseHandle != NULL) ? s_fpGetLocalDatabaseHandle() : NULL;
283}