2 ** nxapush - command line tool used to push DCI values to NetXMS server
3 ** via local NetXMS agent
4 ** Copyright (C) 2006-2016 Raden Solutions
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.
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.
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.
22 #include <nms_common.h>
23 #include <nms_agent.h>
35 static NamedPipe
*s_pipe
= NULL
;
40 static StringMap
*s_data
= new StringMap
;
45 static int s_optVerbose
= 1;
46 static UINT32 s_optObjectId
= 0;
47 static time_t s_timestamp
= 0;
50 * Values parser - clean string and split by '='
52 static BOOL
AddValue(TCHAR
*pair
)
58 for (p
= pair
; *p
!= 0; p
++)
60 if (*p
== _T('=') && value
== NULL
)
64 if (*p
== 0x0D || *p
== 0x0A)
74 s_data
->set(pair
, value
);
82 * Initialize and connect to the agent
86 s_pipe
= NamedPipe
::connect(_T("nxagentd.push"));
91 _tprintf(_T("Connected to NetXMS agent\n"));
102 msg
.setCode(CMD_PUSH_DCI_DATA
);
103 msg
.setField(VID_OBJECT_ID
, s_optObjectId
);
104 msg
.setFieldFromTime(VID_TIMESTAMP
, s_timestamp
);
105 s_data
->fillMessage(&msg
, VID_NUM_ITEMS
, VID_PUSH_DCI_DATA_BASE
);
107 // Send response to pipe
108 NXCP_MESSAGE
*rawMsg
= msg
.createMessage();
109 bool success
= s_pipe
->write(rawMsg
, ntohl(rawMsg
->size
));
116 * Disconnect and cleanup
118 static BOOL
Teardown()
126 * Command line options
128 #if HAVE_DECL_GETOPT_LONG
129 static struct option longOptions
[] =
131 { (char *)"help", no_argument
, NULL
, 'h'},
132 { (char *)"object", required_argument
, NULL
, 'o'},
133 { (char *)"quiet", no_argument
, NULL
, 'q'},
134 { (char *)"timestamp-unix", required_argument
, NULL
, 't'},
135 { (char *)"timestamp-text", required_argument
, NULL
, 'T'},
136 { (char *)"verbose", no_argument
, NULL
, 'v'},
137 { (char *)"version", no_argument
, NULL
, 'V'},
142 #define SHORT_OPTIONS "ho:qt:T:vV"
147 static void usage(char *argv0
)
150 _T("NetXMS Agent PUSH Version ") NETXMS_VERSION_STRING
_T("\n")
151 _T("Copyright (c) 2006-2013 Raden Solutions\n\n")
152 _T("Usage: %hs [OPTIONS] [@batch_file] [values]\n")
156 _T(" -h, --help Display this help message.\n")
157 _T(" -o, --object <id> Push data on behalf of object with given id.\n")
158 _T(" -q, --quiet Suppress all messages.\n")
159 _T(" -t, --timestamp-unix <time> Specify timestamp for data as UNIX timestamp.\n")
160 _T(" -T, --timestamp-text <time> Specify timestamp for data as YYYYMMDDhhmmss.\n")
161 _T(" -v, --verbose Enable verbose messages. Add twice for debug\n")
162 _T(" -V, --version Display version information.\n\n")
164 _T(" -h Display this help message.\n")
165 _T(" -o <id> Push data on behalf of object with given id.\n")
166 _T(" -q Suppress all messages.\n")
167 _T(" -t <time> Specify timestamp for data as UNIX timestamp.\n")
168 _T(" -T <time> Specify timestamp for data as YYYYMMDDhhmmss.\n")
169 _T(" -v Enable verbose messages. Add twice for debug\n")
170 _T(" -V Display version information.\n\n")
173 _T(" * Values should be given in the following format:\n")
175 _T(" where dci can be specified by it's name\n")
176 _T(" * Name of batch file cannot contain character = (equality sign)\n")
179 _T(" Push two values:\n")
180 _T(" nxapush PushParam1=1 PushParam2=4\n\n")
181 _T(" Push values from file:\n")
182 _T(" nxapush @file\n")
189 int main(int argc
, char *argv
[])
194 InitNetXMSProcess(true);
197 #if HAVE_DECL_GETOPT_LONG
198 while ((c
= getopt_long(argc
, argv
, SHORT_OPTIONS
, longOptions
, NULL
)) != -1)
200 while ((c
= getopt(argc
, argv
, SHORT_OPTIONS
)) != -1)
209 case 'o': // object ID
210 s_optObjectId
= strtoul(optarg
, NULL
, 0);
215 case 't': // timestamp as UNIX time
216 s_timestamp
= (time_t)strtoull(optarg
, NULL
, 0);
218 case 'T': // timestamp as YYYYMMDDhhmmss
219 s_timestamp
= ParseDateTimeA(optarg
, 0);
225 _tprintf(_T("nxapush (") NETXMS_VERSION_STRING
_T(")\n"));
236 if (s_optVerbose
> 0)
238 printf("Not enough arguments\n\n");
240 printf("Try `%s --help' for more information.\n", argv
[0]);
242 printf("Try `%s -h' for more information.\n", argv
[0]);
251 while (optind
< argc
)
253 char *p
= argv
[optind
];
255 if (((*p
== '@') && (strchr(p
, '=') == NULL
)) || (*p
== '-'))
257 FILE *fileHandle
= stdin
;
261 fileHandle
= fopen(p
+ 1, "r");
264 if (fileHandle
!= NULL
)
268 while (fgets(buffer
, sizeof(buffer
), fileHandle
) != NULL
)
271 WCHAR
*wvalue
= WideStringFromMBString(buffer
);
279 if (fileHandle
!= stdin
)
286 if (s_optVerbose
> 0)
288 printf("Cannot open \"%s\": %s\n", p
+ 1, strerror(errno
));
295 WCHAR
*wvalue
= WideStringFromMBString(argv
[optind
]);
299 AddValue(argv
[optind
]);
307 if (s_data
->size() > 0)
319 if (s_optVerbose
> 0)
321 printf("No valid pairs found; nothing to send\n");