implemented single housekeeping thread for all message wait queues
[public/netxms.git] / src / client / nxevent / nxevent.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
3** Command line event sender
43a6f3ca 4** Copyright (C) 2003-2015 Victor Kirhenshtein
5039dede
AK
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: nxevent.cpp
21**
22**/
23
24#include "nxevent.h"
25
2564612e
VK
26/**
27 * Static data
28 */
5039dede
AK
29static BOOL m_bDebug = FALSE;
30static TCHAR m_szServer[MAX_DB_STRING] = _T("127.0.0.1");
31static TCHAR m_szLogin[MAX_DB_STRING] = _T("guest");
32static TCHAR m_szPassword[MAX_DB_STRING] = _T("");
33static TCHAR m_szUserTag[MAX_USERTAG_LENGTH] = _T("");
34static DWORD m_dwEventCode = 0;
d9a4c9a2 35static TCHAR m_eventName[MAX_EVENT_NAME] = _T("");
5039dede
AK
36static DWORD m_dwObjectId = 0;
37static DWORD m_dwTimeOut = 3;
38
d9a4c9a2
VK
39/**
40 * Callback function for debug printing
41 */
14149881 42static void DebugCallback(const TCHAR *pMsg)
5039dede 43{
7b1daa2e 44 _tprintf(_T("*debug* %s\n"), pMsg);
5039dede
AK
45}
46
d9a4c9a2
VK
47/**
48 * Send event to server
49 */
50static DWORD SendEvent(int iNumArgs, char **pArgList, BOOL bEncrypt)
5039dede
AK
51{
52 DWORD dwResult;
5039dede
AK
53
54 if (!NXCInitialize())
55 {
56 _tprintf(_T("Failed to initialize NetXMS client library\n"));
57 }
58 else
59 {
60 if (m_bDebug)
61 NXCSetDebugCallback(DebugCallback);
62
14149881 63 NXCSession *session = new NXCSession();
e5390fb5
VK
64 static UINT32 protocolVersions[] = { CPV_INDEX_TRAP };
65 dwResult = session->connect(m_szServer, m_szLogin, m_szPassword, bEncrypt ? NXCF_ENCRYPT : 0, _T("nxevent/") NETXMS_VERSION_STRING,
66 protocolVersions, sizeof(protocolVersions) / sizeof(UINT32));
5039dede
AK
67 if (dwResult != RCC_SUCCESS)
68 {
69 _tprintf(_T("Unable to connect to server: %s\n"), NXCGetErrorText(dwResult));
70 }
71 else
72 {
14149881
VK
73 session->setCommandTimeout(m_dwTimeOut * 1000);
74 EventController *ctrl = (EventController *)session->getController(CONTROLLER_EVENTS);
7b1daa2e
VK
75#ifdef UNICODE
76 WCHAR **argList = (WCHAR **)malloc(sizeof(WCHAR *) * iNumArgs);
77 for(int i = 0; i < iNumArgs; i++)
78 argList[i] = WideStringFromMBString(pArgList[i]);
14149881 79 dwResult = ctrl->sendEvent(m_dwEventCode, m_eventName, m_dwObjectId, iNumArgs, argList, m_szUserTag);
7b1daa2e
VK
80 for(int i = 0; i < iNumArgs; i++)
81 free(argList[i]);
82 free(argList);
83#else
14149881 84 dwResult = ctrl->sendEvent(m_dwEventCode, m_eventName, m_dwObjectId, iNumArgs, pArgList, m_szUserTag);
7b1daa2e 85#endif
5039dede
AK
86 if (dwResult != RCC_SUCCESS)
87 _tprintf(_T("Unable to send event: %s\n"), NXCGetErrorText(dwResult));
5039dede 88 }
14149881 89 delete session;
d87ddcc2 90 NXCShutdown();
5039dede 91 }
d9a4c9a2 92 return dwResult;
5039dede
AK
93}
94
2564612e
VK
95#ifdef _WIN32
96#define CMDLINE_OPTIONS "deho:P:T:u:vw:"
97#else
98#define CMDLINE_OPTIONS "c:deho:P:T:u:vw:"
99#endif
100
d9a4c9a2
VK
101/**
102 * Entry point
103 */
5039dede
AK
104int main(int argc, char *argv[])
105{
d9a4c9a2 106 int ch, rcc = RCC_INVALID_ARGUMENT;
5039dede
AK
107 BOOL bStart = TRUE, bEncrypt = FALSE;
108
109 // Parse command line
110 opterr = 1;
2564612e 111 while((ch = getopt(argc, argv, CMDLINE_OPTIONS)) != -1)
5039dede
AK
112 {
113 switch(ch)
114 {
115 case 'h': // Display help and exit
d9a4c9a2 116 printf("Usage: nxevent [<options>] <server> <event> [<param_1> [... <param_N>]]\n"
5039dede 117 "Valid options are:\n"
2564612e
VK
118#ifndef _WIN32
119 " -c : Codepage (default is " ICONV_DEFAULT_CODEPAGE ")\n"
120#endif
5039dede
AK
121 " -d : Turn on debug mode.\n"
122 " -e : Encrypt session.\n"
123 " -h : Display help and exit.\n"
124 " -o <id> : Specify source object ID.\n"
125 " -P <password> : Specify user's password. Default is empty password.\n"
126 " -T <tag> : User tag to be associated with the message. Default is empty.\n"
127 " -u <user> : Login to server as <user>. Default is \"guest\".\n"
128 " -v : Display version and exit.\n"
129 " -w <seconds> : Specify command timeout (default is 3 seconds).\n"
d9a4c9a2 130 "Event could be specified either by code or by name\n"
5039dede
AK
131 "\n");
132 bStart = FALSE;
133 break;
2564612e
VK
134#ifndef _WIN32
135 case 'c':
c3686952 136 SetDefaultCodepage(optarg);
2564612e
VK
137 break;
138#endif
5039dede
AK
139 case 'd':
140 m_bDebug = TRUE;
141 break;
142 case 'e':
143 bEncrypt = TRUE;
144 break;
145 case 'o':
7ee7f8bc 146 m_dwObjectId = strtoul(optarg, NULL, 0);
5039dede
AK
147 break;
148 case 'u':
7b1daa2e
VK
149#ifdef UNICODE
150 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, m_szLogin, MAX_DB_STRING);
151 m_szLogin[MAX_DB_STRING - 1] = 0;
152#else
5039dede 153 nx_strncpy(m_szLogin, optarg, MAX_DB_STRING);
7b1daa2e 154#endif
5039dede
AK
155 break;
156 case 'P':
7ee7f8bc
VK
157#ifdef UNICODE
158 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, m_szPassword, MAX_DB_STRING);
7b1daa2e 159 m_szPassword[MAX_DB_STRING - 1] = 0;
7ee7f8bc 160#else
5039dede 161 nx_strncpy(m_szPassword, optarg, MAX_DB_STRING);
7ee7f8bc 162#endif
5039dede
AK
163 break;
164 case 'T':
7b1daa2e
VK
165#ifdef UNICODE
166 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, m_szUserTag, MAX_USERTAG_LENGTH);
167 m_szUserTag[MAX_USERTAG_LENGTH - 1] = 0;
168#else
5039dede 169 nx_strncpy(m_szUserTag, optarg, MAX_USERTAG_LENGTH);
7b1daa2e 170#endif
5039dede
AK
171 break;
172 case 'w':
7b1daa2e 173 m_dwTimeOut = strtoul(optarg, NULL, 0);
5039dede
AK
174 if ((m_dwTimeOut < 1) || (m_dwTimeOut > 120))
175 {
7b1daa2e 176 _tprintf(_T("Invalid timeout %hs\n"), optarg);
5039dede
AK
177 bStart = FALSE;
178 }
179 break;
180 case 'v':
7b1daa2e 181 _tprintf(_T("NetXMS Event Sender Version ") NETXMS_VERSION_STRING _T("\n"));
5039dede
AK
182 bStart = FALSE;
183 break;
184 case '?':
185 bStart = FALSE;
186 break;
187 default:
188 break;
189 }
190 }
191
192 // Do requested action if everything is OK
193 if (bStart)
194 {
195 if (argc - optind < 2)
196 {
197 _tprintf(_T("Required arguments missing. Use nxevent -h for help.\n"));
198 }
199 else
200 {
201#ifdef _WIN32
202 WSADATA wsaData;
203
204 if (WSAStartup(2, &wsaData) != 0)
205 {
206 _tprintf(_T("Unable to initialize Windows sockets\n"));
d9a4c9a2 207 return RCC_COMM_FAILURE;
5039dede
AK
208 }
209#endif
7b1daa2e
VK
210#ifdef UNICODE
211 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, argv[optind], -1, m_szServer, 256);
212 m_szServer[255] = 0;
213#else
5039dede 214 nx_strncpy(m_szServer, argv[optind], 256);
7b1daa2e 215#endif
d9a4c9a2
VK
216
217 char *eptr;
218 m_dwEventCode = strtoul(argv[optind + 1], &eptr, 0);
219 if (*eptr != 0)
220 {
221 // assume that event is specified by name
222 m_dwEventCode = 0;
b860c14b 223#ifdef UNICODE
d9a4c9a2 224 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, argv[optind + 1], -1, m_eventName, MAX_EVENT_NAME);
b860c14b
VK
225 m_eventName[MAX_EVENT_NAME - 1] = 0;
226#else
227 nx_strncpy(m_eventName, argv[optind + 1], MAX_EVENT_NAME);
228#endif
d9a4c9a2
VK
229 }
230
231 rcc = (int)SendEvent(argc - optind - 2, &argv[optind + 2], bEncrypt);
5039dede
AK
232 }
233 }
234
d9a4c9a2 235 return rcc;
5039dede 236}