subagent specific API moved to separate library from libnetxms
[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;
31static bool (* s_fpEnumerateSessions)(bool (*)(AbstractCommSession *, void *), void *) = NULL;
32static bool (* s_fpSendFile)(void *, UINT32, const TCHAR *, long) = NULL;
33static bool (* s_fpPushData)(const TCHAR *, const TCHAR *, UINT32) = NULL;
34static CONDITION s_agentShutdownCondition = INVALID_CONDITION_HANDLE;
35static Config *s_registry = NULL;
36static void (* s_fpSaveRegistry)() = NULL;
37static const TCHAR *s_dataDirectory = NULL;
38
39/**
40 * Initialize subagent API
41 */
42void 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)(bool (*)(AbstractCommSession *, void *), void*),
46 bool (* sendFile)(void *, UINT32, const TCHAR *, long),
47 bool (* pushData)(const TCHAR *, const TCHAR *, UINT32),
48 CONDITION shutdownCondition, Config *registry,
49 void (* saveRegistry)(), const TCHAR *dataDirectory)
50{
51 s_fpWriteLog = writeLog;
52 s_fpSendTrap1 = sendTrap1;
53 s_fpSendTrap2 = sendTrap2;
54 s_fpEnumerateSessions = enumerateSessions;
55 s_fpSendFile = sendFile;
56 s_fpPushData = pushData;
57 s_agentShutdownCondition = shutdownCondition;
58 s_registry = registry;
59 s_fpSaveRegistry = saveRegistry;
60 s_dataDirectory = dataDirectory;
61}
62
63/**
64 * Write message to agent's log
65 */
66void LIBNXAGENT_EXPORTABLE AgentWriteLog(int logLevel, const TCHAR *format, ...)
67{
68 TCHAR szBuffer[4096];
69 va_list args;
70
71 if (s_fpWriteLog != NULL)
72 {
73 va_start(args, format);
74 _vsntprintf(szBuffer, 4096, format, args);
75 va_end(args);
76 s_fpWriteLog(logLevel, 0, szBuffer);
77 }
78}
79
80/**
81 * Write message to agent's log
82 */
83void LIBNXAGENT_EXPORTABLE AgentWriteLog2(int logLevel, const TCHAR *format, va_list args)
84{
85 TCHAR szBuffer[4096];
86
87 if (s_fpWriteLog != NULL)
88 {
89 _vsntprintf(szBuffer, 4096, format, args);
90 s_fpWriteLog(logLevel, 0, szBuffer);
91 }
92}
93
94/**
95 * Write debug message to agent's log
96 */
97void LIBNXAGENT_EXPORTABLE AgentWriteDebugLog(int level, const TCHAR *format, ...)
98{
99 TCHAR szBuffer[4096];
100 va_list args;
101
102 if (s_fpWriteLog != NULL)
103 {
104 va_start(args, format);
105 _vsntprintf(szBuffer, 4096, format, args);
106 va_end(args);
107 s_fpWriteLog(EVENTLOG_DEBUG_TYPE, level, szBuffer);
108 }
109}
110
111/**
112 * Write debug message to agent's log
113 */
114void LIBNXAGENT_EXPORTABLE AgentWriteDebugLog2(int level, const TCHAR *format, va_list args)
115{
116 TCHAR szBuffer[4096];
117
118 if (s_fpWriteLog != NULL)
119 {
120 _vsntprintf(szBuffer, 4096, format, args);
121 s_fpWriteLog(EVENTLOG_DEBUG_TYPE, level, szBuffer);
122 }
123}
124
125/**
126 * Send trap from agent to server
127 */
128void LIBNXAGENT_EXPORTABLE AgentSendTrap(UINT32 dwEvent, const TCHAR *eventName, const char *pszFormat, ...)
129{
130 va_list args;
131
132 if (s_fpSendTrap1 != NULL)
133 {
134 va_start(args, pszFormat);
135 s_fpSendTrap1(dwEvent, eventName, pszFormat, args);
136 va_end(args);
137 }
138}
139
140/**
141 * Send trap from agent to server
142 */
143void LIBNXAGENT_EXPORTABLE AgentSendTrap2(UINT32 dwEvent, const TCHAR *eventName, int nCount, TCHAR **ppszArgList)
144{
145 if (s_fpSendTrap2 != NULL)
146 s_fpSendTrap2(dwEvent, eventName, nCount, ppszArgList);
147}
148
149/**
150 * Goes throught all sessions and executes
151 */
152bool LIBNXAGENT_EXPORTABLE EnumerateSessions(bool (* callback)(AbstractCommSession *, void *), void *data)
153{
154 if (s_fpEnumerateSessions != NULL)
155 {
156 return s_fpEnumerateSessions(callback, data);
157 }
158 else
159 return false;
160}
161
162/**
163 * Send file to server
164 */
165bool LIBNXAGENT_EXPORTABLE AgentSendFileToServer(void *session, UINT32 requestId, const TCHAR *file, long offset)
166{
167 if ((s_fpSendFile == NULL) || (session == NULL) || (file == NULL))
168 return FALSE;
169 return s_fpSendFile(session, requestId, file, offset);
170}
171
172/**
173 * Push parameter's value
174 */
175bool LIBNXAGENT_EXPORTABLE AgentPushParameterData(const TCHAR *parameter, const TCHAR *value)
176{
177 if (s_fpPushData == NULL)
178 return FALSE;
179 return s_fpPushData(parameter, value, 0);
180}
181
182/**
183 * Push parameter's value
184 */
185bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataInt32(const TCHAR *parameter, LONG value)
186{
187 TCHAR buffer[64];
188
189 _sntprintf(buffer, sizeof(buffer), _T("%d"), (int)value);
190 return AgentPushParameterData(parameter, buffer);
191}
192
193/**
194 * Push parameter's value
195 */
196bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataUInt32(const TCHAR *parameter, UINT32 value)
197{
198 TCHAR buffer[64];
199
200 _sntprintf(buffer, sizeof(buffer), _T("%u"), (unsigned int)value);
201 return AgentPushParameterData(parameter, buffer);
202}
203
204/**
205 * Push parameter's value
206 */
207bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataInt64(const TCHAR *parameter, INT64 value)
208{
209 TCHAR buffer[64];
210
211 _sntprintf(buffer, sizeof(buffer), INT64_FMT, value);
212 return AgentPushParameterData(parameter, buffer);
213}
214
215/**
216 * Push parameter's value
217 */
218bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataUInt64(const TCHAR *parameter, QWORD value)
219{
220 TCHAR buffer[64];
221
222 _sntprintf(buffer, sizeof(buffer), UINT64_FMT, value);
223 return AgentPushParameterData(parameter, buffer);
224}
225
226/**
227 * Push parameter's value
228 */
229bool LIBNXAGENT_EXPORTABLE AgentPushParameterDataDouble(const TCHAR *parameter, double value)
230{
231 TCHAR buffer[64];
232
233 _sntprintf(buffer, sizeof(buffer), _T("%f"), value);
234 return AgentPushParameterData(parameter, buffer);
235}
236
237/**
238 * Get shutdown condition
239 */
240CONDITION LIBNXAGENT_EXPORTABLE AgentGetShutdownCondition()
241{
242 return s_agentShutdownCondition;
243}
244
245/**
246 * Sleep and check for agent shutdown
247 */
248bool LIBNXAGENT_EXPORTABLE AgentSleepAndCheckForShutdown(UINT32 sleepTime)
249{
250 return ConditionWait(s_agentShutdownCondition, sleepTime);
251}
252
253/**
254 * Open registry
255 */
256Config LIBNXAGENT_EXPORTABLE *AgentOpenRegistry()
257{
258 s_registry->lock();
259 return s_registry;
260}
261
262/**
263 * Close registry
264 */
265void LIBNXAGENT_EXPORTABLE AgentCloseRegistry(bool modified)
266{
267 if (modified)
268 {
269 if (s_fpSaveRegistry != NULL)
270 s_fpSaveRegistry();
271 }
272 s_registry->unlock();
273}
274
275/**
276 * Get data directory
277 */
278const TCHAR LIBNXAGENT_EXPORTABLE *AgentGetDataDirectory()
279{
280 return s_dataDirectory;
281}