-
Notifications
You must be signed in to change notification settings - Fork 0
/
dalma.c
372 lines (316 loc) · 9.69 KB
/
dalma.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*----------------------------------------------------------------------------*
* Copyright (c) 2020 Southeastern Universities Research Association, *
* Thomas Jefferson National Accelerator Facility *
* *
* This software was developed under a United States Government license *
* described in the NOTICE file included as part of this distribution. *
* *
* Author: Bryan Moffit *
* [email protected] Jefferson Lab, MS-12B3 *
* Phone: (757) 269-5660 12000 Jefferson Ave. *
* Fax: (757) 269-5800 Newport News, VA 23606 *
* *
*----------------------------------------------------------------------------*
*
* Description:
* Program to capture dalogMsg's from specified components
*
*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>
#include "cMsg.h"
void *domainId;
void *unSubHandle;
static int Verbose = 0;
char *userSession = NULL;
FILE *runlog;
void sig_handler(int signo);
/******************************************************************/
static void callback(void *msg, void *arg) {
char *logText, *name, *severity, *session;
int writeOutput = 1;
struct timespec ts_msgtime;
char senderTimeBuf[32];
struct tm tBuf;
if(userSession != NULL)
{
if(cMsgGetString(msg, "session", (const char **)&session) == CMSG_OK)
{
if(Verbose)
printf("-- Checking SESSION (%s ?= %s)\n", userSession, session);
if(strcmp(userSession,session) != 0)
{
/* Session doesn't match... skip it */
writeOutput = 0;
}
if(Verbose)
printf("-- writeOutput = %d\n", writeOutput);
}
if(Verbose)
printf("-- no session\n");
}
else
{
if(Verbose)
printf("-- userSession NULL\n");
}
if(writeOutput)
{
cMsgGetSenderTime(msg, &ts_msgtime);
/* convert struct timespect to struct tm */
localtime_r(&ts_msgtime.tv_sec, &tBuf);
/* convert struct tm to readable time */
strftime(senderTimeBuf, 32, "%d%b%Y %T", &tBuf);
/* Null to end the time string */
senderTimeBuf[strlen(senderTimeBuf)]='\0';
fprintf(runlog, "%s: ", senderTimeBuf);
if(Verbose)
printf(" senderTimebuf = %s\n", senderTimeBuf);
if(cMsgGetSender(msg, (const char **)&name) == CMSG_OK)
{
fprintf(runlog, "%s ", name);
if(Verbose)
printf(" name = %s\n", name);
}
if(cMsgGetString(msg, "severity", (const char **)&severity) == CMSG_OK)
{
fprintf(runlog, "%s: ", severity);
if(Verbose)
printf(" severity = %s\n", severity);
}
if(cMsgGetString(msg, "dalogText", (const char **)&logText) == CMSG_OK)
{
fprintf(runlog, "%s\n", logText);
if(Verbose)
printf(" dalogText = %s\n", logText);
}
fflush(runlog);
}
// in case we need to respond back... here is the canned response
int response;
void *rmsg;
if(cMsgGetGetRequest(msg,&response)==CMSG_OK)
{
if(response==1)
{
printf("%s: Need to respond to this one\n",__FUNCTION__);
rmsg = cMsgCreateResponseMessage(msg);
cMsgAddString(rmsg, "payloadItem", "any string you want");
cMsgSetSubject(rmsg, "RESPONDING");
cMsgSetType(rmsg, "TO MESSAGE");
cMsgSetText(rmsg, "responder's text");
cMsgSend(domainId, rmsg);
cMsgFreeMessage(&rmsg);
}
}
else
printf("%s: Something wrong here\n",__FUNCTION__);
/* user MUST free messages passed to the callback */
cMsgFreeMessage(&msg);
}
/******************************************************************/
static void usage() {
printf("Usage: dalma [OPTION]...\n");
printf(" \n");
printf("Archive dalogMsgs to file (default = out.log)\n");
printf("Use multicast, environment EXPID, and all component names if none of --host, \n");
printf(" --expid, --name are specified\n");
printf("\n");
printf("Mandatory arguments to long options are mandatory for short options too.\n");
printf(" -h, --host HOSTNAME Platform hostname\n");
printf(" -p, --port PORT Platform port\n");
printf(" -n, --name NAME CODA3 Component Name\n");
printf(" regex: ? and * are supported\n");
printf(" -s, --session SESSION SESSION\n");
printf(" -e, --expid EXPID EXPID\n");
printf(" -f, --filename FILENAME Output filename\n");
printf(" -v, --verbose Increase verbosity\n");
printf("\n");
}
/******************************************************************/
int main(int argc,char **argv) {
/* Parameters configurable from the command line */
int opt_param, option_index = 0;
static char *pHost = NULL;
static char *pEXPID = NULL;
static char *cName = "*";
static char *pPort = "45000";
static char *oFilename = "out.log";
time_t utime = time(NULL);
/* cMsg Connection and Subscription information */
char *myName;
myName = (char *)malloc(256*sizeof(char));
sprintf(myName,"dalma%lx",(unsigned long) utime);
char *myDescription = "dalogMsg Archiver";
char UDL[64];
char hostinfo[32];
int err, debug = 1;
cMsgSubscribeConfig *config;
/* structure for getopt_long */
static struct option long_options[] =
{
/* {const char *name, int has_arg, int *flag, int val} */
{"host", 1, NULL, 'h'},
{"port", 1, NULL, 'p'},
{"name", 1, NULL, 'n'},
{"session", 1, NULL, 's'},
{"expid", 1, NULL, 'e'},
{"filename", 1, NULL, 'f'},
{"verbose", 0, &Verbose, 1},
{0, 0, 0, 0}
};
/* Parse the commandline parameters */
while(1)
{
option_index = 0;
opt_param = getopt_long (argc, argv, "h:p:n:s:e:f:v",
long_options, &option_index);
if (opt_param == -1) /* No more option parameters left */
break;
switch (opt_param)
{
case 0:
break;
case 'h': /* Platform Host */
pHost = optarg;
if(Verbose) printf("-- Platform Host (%s)\n",pHost);
break;
case 'p': /* Platform Port */
pPort = optarg;
if(Verbose) printf("-- Platform Port (%s)\n",pPort);
break;
case 'n': /* Component Name */
cName = optarg;
if(Verbose) printf("-- Component Name (%s)\n",cName);
break;
case 's': /* SESSION */
userSession = optarg;
if(Verbose) printf("-- SESSION (%s)\n",userSession);
break;
case 'e': /* EXPID */
pEXPID = optarg;
if(Verbose) printf("-- EXPID (%s)\n",pEXPID);
break;
case 'f': /* Output Filename */
oFilename = optarg;
if(Verbose) printf("-- Output Filename (%s)\n",oFilename);
break;
case 'v': /* Verbose */
Verbose = 1;
if(Verbose) printf("-- Verbose\n");
break;
case '?': /* Invalid Option */
default:
usage();
exit(1);
}
}
/* Construct the cMsg hostname:port for the UDL */
if(pHost)
{
sprintf(hostinfo,
"%s:%s",
pHost, pPort);
}
else
{
/* If the host is not specified on the commandline, use multicast */
sprintf(hostinfo,"multicast");
}
/* if the EXPID is not specified, try to get it from the environment */
if(pEXPID == NULL)
{
pEXPID = getenv("EXPID");
if(pEXPID == NULL)
{
usage();
exit(-1);
}
if(Verbose) printf("-- env: EXPID (%s)\n",pEXPID);
}
/* Construct the cMsg UDL */
sprintf(UDL,
"cMsg://%s/cMsg/%s?regime=low&multicastTO=5&cmsgpassword=%s",
hostinfo, pEXPID, pEXPID);
/* Set up signal handler */
signal(SIGINT, sig_handler);
signal(SIGUSR1, sig_handler);
signal(SIGUSR2, sig_handler);
/* Open the output file */
runlog = fopen(oFilename, "w");
if(runlog)
{
if (Verbose)
{
printf(" Output file: %s\n", oFilename);
}
}
else
{
perror("fopen");
}
if (Verbose) {
printf("Connecting to the platform as, \"%s\"\n", myName);
printf(" UDL = %s\n", UDL);
}
/* connect to cMsg server */
err = cMsgConnect(UDL, myName, myDescription, &domainId);
if (err != CMSG_OK) {
if (debug) {
printf("cMsgConnect: %s\n",cMsgPerror(err));
}
exit(1);
}
/* start receiving messages */
if(cMsgReceiveStart(domainId) != CMSG_OK)
printf("cMsgReceiveStart Failed\n");
/* set the subscribe configuration */
config = cMsgSubscribeConfigCreate();
cMsgSubscribeSetMaxCueSize(config, 10);
/* subscribe */
if (cMsgSubscribe(domainId, cName, "rc/report/dalog",
callback, NULL, config, &unSubHandle) != CMSG_OK)
printf("cMsgSubscribe Failed\n");
cMsgSubscribeConfigDestroy(config);
printf("dalma daemon running\nPress <Enter> to end\n");
getchar();
printf("ending...\n");
fclose(runlog);
if(Verbose) printf("Unsubscribing\n");
cMsgUnSubscribe(domainId, unSubHandle);
sleep(4);
cMsgDisconnect(&domainId);
pthread_exit(NULL);
return(0);
}
void
sig_handler(int signo)
{
switch (signo)
{
case SIGINT:
printf("Got SIGINT (%d)\n", signo);
printf("Unsubscribing\n");
cMsgUnSubscribe(domainId, unSubHandle);
sleep(4);
cMsgDisconnect(&domainId);
pthread_exit(NULL);
exit(1); /* exit if CRTL/C is issued */
break;
case SIGUSR1:
printf("Got SIGUSR1 (%d)\n", signo);
break;
case SIGUSR2:
printf("Got SIGUSR2 (%d)\n", signo);
break;
default:
printf("Got %d\n", signo);
}
return;
}