forked from ganglia/monitor-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
704 lines (572 loc) · 17.6 KB
/
server.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <pthread.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <string.h>
#include "dtd.h"
#include "gmetad.h"
#include "my_inet_ntop.h"
#include "server_priv.h"
extern g_tcp_socket *server_socket;
extern pthread_mutex_t server_socket_mutex;
extern g_tcp_socket *interactive_socket;
extern pthread_mutex_t server_interactive_mutex;
extern Source_t root;
extern gmetad_config_t gmetad_config;
extern char* getfield(char *buf, short int index);
extern struct type_tag* in_type_list (char *, unsigned int);
static inline int CHECK_FMT(2, 3)
xml_print( client_t *client, const char *fmt, ... )
{
int rval, len;
va_list ap;
char buf[4096];
va_start (ap, fmt);
if(! client->valid )
{
va_end(ap);
return 1;
}
vsnprintf (buf, sizeof (buf), fmt, ap);
len = strlen(buf);
SYS_CALL( rval, write( client->fd, buf, len));
if ( rval < 0 && rval != len )
{
va_end(ap);
client->valid = 0;
return 1;
}
va_end(ap);
return 0;
}
static int
metric_summary(datum_t *key, datum_t *val, void *arg)
{
client_t *client = (client_t*) arg;
char *name = (char*) key->data;
char *type;
char sum[256];
Metric_t *metric = (Metric_t*) val->data;
struct type_tag *tt;
int rc,i;
type = getfield(metric->strings, metric->type);
tt = in_type_list(type, strlen(type));
if (!tt) return 0;
/* We sum everything in double to properly combine integer sources
(3.0) with float sources (3.1). This also avoids wraparound
errors: for example memory KB exceeding 4TB. */
switch (tt->type)
{
case INT:
case UINT:
sprintf(sum, "%.f", metric->val.d);
break;
case FLOAT:
sprintf(sum, "%.*f", (int) metric->precision, metric->val.d);
break;
default:
break;
}
rc = xml_print(client, "<METRICS NAME=\"%s\" SUM=\"%s\" NUM=\"%u\" "
"TYPE=\"%s\" UNITS=\"%s\" SLOPE=\"%s\" SOURCE=\"%s\">\n",
name, sum, metric->num,
"double", /* we always report double sums */
getfield(metric->strings, metric->units),
getfield(metric->strings, metric->slope),
getfield(metric->strings, metric->source));
rc = xml_print(client, "<EXTRA_DATA>\n");
for (i=0; !rc && i<metric->ednameslen; i++)
{
rc=xml_print(client, "<EXTRA_ELEMENT NAME=\"%s\" VAL=\"%s\"/>\n",
getfield(metric->strings, metric->ednames[i]),
getfield(metric->strings, metric->edvalues[i]));
}
rc = xml_print(client, "</EXTRA_DATA>\n");
rc=xml_print(client, "</METRICS>\n");
return rc;
}
static int
source_summary(Source_t *source, client_t *client)
{
int rc;
rc=xml_print(client, "<HOSTS UP=\"%u\" DOWN=\"%u\" SOURCE=\"gmetad\"/>\n",
source->hosts_up, source->hosts_down);
if (rc) return 1;
pthread_mutex_lock(source->sum_finished);
rc = hash_foreach(source->metric_summary, metric_summary, (void*) client);
pthread_mutex_unlock(source->sum_finished);
return rc;
}
int
metric_report_start(Generic_t *self, datum_t *key, client_t *client, void *arg)
{
int rc, i;
char *name = (char*) key->data;
Metric_t *metric = (Metric_t*) self;
long tn = 0;
tn = client->now.tv_sec - metric->t0.tv_sec;
if (tn<0) tn = 0;
if (metric->dmax && metric->dmax < tn)
return 0;
rc=xml_print(client, "<METRIC NAME=\"%s\" VAL=\"%s\" TYPE=\"%s\" "
"UNITS=\"%s\" TN=\"%u\" TMAX=\"%u\" DMAX=\"%u\" SLOPE=\"%s\" "
"SOURCE=\"%s\">\n",
name, getfield(metric->strings, metric->valstr),
getfield(metric->strings, metric->type),
getfield(metric->strings, metric->units),
(unsigned int) tn,
metric->tmax, metric->dmax, getfield(metric->strings, metric->slope),
getfield(metric->strings, metric->source));
rc = xml_print(client, "<EXTRA_DATA>\n");
for (i=0; !rc && i<metric->ednameslen; i++)
{
rc=xml_print(client, "<EXTRA_ELEMENT NAME=\"%s\" VAL=\"%s\"/>\n",
getfield(metric->strings, metric->ednames[i]),
getfield(metric->strings, metric->edvalues[i]));
}
rc = xml_print(client, "</EXTRA_DATA>\n");
rc=xml_print(client, "</METRIC>\n");
return rc;
}
int
metric_report_end(Generic_t *self, client_t *client, void *arg)
{
return 0;
}
int
host_report_start(Generic_t *self, datum_t *key, client_t *client, void *arg)
{
int rc;
char *name = (char*) key->data;
Host_t *host = (Host_t*) self;
long tn = 0;
tn = client->now.tv_sec - host->t0.tv_sec;
if (tn<0) tn = 0;
/* Note the hash key is the host's IP address. */
rc = xml_print(client, "<HOST NAME=\"%s\" IP=\"%s\" REPORTED=\"%u\" "
"TN=\"%u\" TMAX=\"%u\" DMAX=\"%u\" LOCATION=\"%s\" GMOND_STARTED=\"%u\" TAGS=\"%s\">\n",
name, getfield(host->strings, host->ip), host->reported,
(unsigned int) tn,
host->tmax, host->dmax, getfield(host->strings, host->location),
host->started, getfield(host->strings, host->tags));
return rc;
}
int
host_report_end(Generic_t *self, client_t *client, void *arg)
{
return xml_print(client, "</HOST>\n");
}
int
source_report_start(Generic_t *self, datum_t *key, client_t *client, void *arg)
{
int rc;
char *name = (char*) key->data;
Source_t *source = (Source_t*) self;
if (self->id == CLUSTER_NODE)
{
rc=xml_print(client, "<CLUSTER NAME=\"%s\" LOCALTIME=\"%u\" OWNER=\"%s\" "
"LATLONG=\"%s\" URL=\"%s\">\n",
name, source->localtime, getfield(source->strings, source->owner),
getfield(source->strings, source->latlong),
getfield(source->strings, source->url));
}
else
{
rc=xml_print(client, "<GRID NAME=\"%s\" AUTHORITY=\"%s\" "
"LOCALTIME=\"%u\">\n",
name, getfield(source->strings, source->authority_ptr), source->localtime);
}
return rc;
}
int
source_report_end(Generic_t *self, client_t *client, void *arg)
{
if (self->id == CLUSTER_NODE)
return xml_print(client, "</CLUSTER>\n");
else
return xml_print(client, "</GRID>\n");
}
/* These are a bit different since they always need to go out. */
int
root_report_start(client_t *client)
{
int rc;
if (client->http)
{
rc = xml_print(client, "HTTP/1.0 200 OK\r\n"
"Server: gmetad/" GANGLIA_VERSION_FULL "\r\n"
"Content-Type: application/xml\r\n"
"Connection: close\r\n"
"\r\n");
if (rc)
return rc;
}
rc = xml_print(client, DTD);
if (rc) return 1;
rc = xml_print(client, "<GANGLIA_XML VERSION=\"%s\" SOURCE=\"gmetad\">\n",
VERSION);
rc = xml_print(client, "<GRID NAME=\"%s\" AUTHORITY=\"%s\" LOCALTIME=\"%u\">\n",
gmetad_config.gridname, getfield(root.strings, root.authority_ptr),
(unsigned int) time(0));
return rc;
}
int
root_report_end(client_t *client)
{
return xml_print(client, "</GRID>\n</GANGLIA_XML>\n");
}
/* Returns true if the filter type can be applied to this node. */
int
applicable(filter_type_t filter, Generic_t *node)
{
switch (filter)
{
case NO_FILTER:
return 1;
case SUMMARY:
return (node->id==ROOT_NODE
|| node->id==CLUSTER_NODE
|| node->id==GRID_NODE);
break;
default:
break;
}
return 0;
}
int
applyfilter(client_t *client, Generic_t *node)
{
filter_type_t f = client->filter;
/* Grids are always summarized. */
if (node->id == GRID_NODE)
f = SUMMARY;
if (!applicable(f, node))
return 1; /* A non-fatal error. */
switch (f)
{
case NO_FILTER:
return 0;
case SUMMARY:
return source_summary((Source_t*) node, client);
default:
break;
}
return 0;
}
/* Processes filter name and sets client->filter.
* Assumes path has already been 'cleaned', and that
* filter points to something like 'filter=[name]'.
*/
int
processfilter(client_t *client, const char *filter)
{
const char *p=filter;
client->filter = NO_FILTER;
p = strchr(p, '=');
if (!p)
return 1;
p++;
/* This could be done with a gperf hash, etc. */
if (!strcmp(p, "summary"))
client->filter = SUMMARY;
else
err_msg("Got unknown filter %s", filter);
return 0;
}
/* sacerdoti: An Object Oriented design in C.
* We use function pointers to approximate virtual method functions.
* A recursive-descent design.
*/
static int
tree_report(datum_t *key, datum_t *val, void *arg)
{
client_t *client = (client_t*) arg;
Generic_t *node = (Generic_t*) val->data;
int rc=0;
if (client->filter && !applicable(client->filter, node)) return 1;
rc = node->report_start(node, key, client, NULL);
if (rc) return 1;
applyfilter(client, node);
if (node->children)
{
/* Allow this to stop early (return code = 1) */
hash_foreach(node->children, tree_report, arg);
}
rc = node->report_end(node, client, NULL);
return rc;
}
/* sacerdoti: This function does a tree walk while respecting the filter path.
* Will return valid XML even if we have chosen a subtree. Since tree depth is
* bounded, this function guarantees O(1) search time.
*/
static int
process_path (client_t *client, char *path, datum_t *myroot, datum_t *key)
{
char *p, *q, *pathend;
char *element;
int rc, len;
datum_t *found;
datum_t findkey;
Generic_t *node;
node = (Generic_t*) myroot->data;
/* Base case */
if (!path)
{
/* Show the subtree. */
applyfilter(client, node);
if (node->children)
{
/* Allow this to stop early (return code = 1) */
hash_foreach(node->children, tree_report, (void*) client);
}
return 0;
}
/* Start tag */
if (node->report_start)
{
rc = node->report_start(node, key, client, NULL);
if (rc) return 1;
}
/* Subtree body */
pathend = path + strlen(path);
p = path+1;
if (!node->children || p >= pathend)
rc = process_path(client, 0, myroot, NULL);
else
{
/* Advance to the next element along path. */
q = strchr(p, '/');
if (!q) q=pathend;
/* len is limited in size by REQUESTLEN through readline() */
len = q-p;
element = malloc(len + 1);
if ( element == NULL )
return 1;
strncpy(element, p, len);
element[len] = '\0';
/* err_msg("Skipping to %s (%d)", element, len); */
/* look for element in hash table. */
findkey.data = element;
findkey.size = len+1;
found = hash_lookup(&findkey, node->children);
if (found)
{
/* err_msg("Found %s", element); */
rc = process_path(client, q, found, &findkey);
datum_free(found);
}
else if (!client->http)
{
/* report this element */
rc = process_path(client, 0, myroot, NULL);
}
else if (!strcmp(element, "*"))
{
/* wildcard detected -> process every child */
struct request_context ctxt;
ctxt.path = q;
ctxt.client = client;
hash_foreach(node->children, process_path_adapter, (void*) &ctxt);
}
else
{
/* element not found */
rc = 0;
}
free(element);
}
if (rc) return 1;
/* End tag */
if (node->report_end)
{
rc = node->report_end(node, client, NULL);
}
return rc;
}
static int
process_path_adapter (datum_t *key, datum_t *val, void *arg)
{
struct request_context *ctxt = (struct request_context*) arg;
return process_path(ctxt->client, ctxt->path, val, key);
}
/* 'Cleans' the request and calls the appropriate handlers.
* This function alters the path to prepare it for processpath().
*/
static int
process_request (client_t *client, char *path)
{
char *p;
int rc, pathlen;
pathlen = strlen(path);
if (!pathlen)
return 1;
if (pathlen >= 12 && memcmp(path, "GET ", 4) == 0)
{
/* looks like a http request, validate it and update path_p */
char *http_p = path + pathlen - 9;
if (memcmp(http_p, " HTTP/1.0", 9) && memcmp(http_p, " HTTP/1.1", 9))
return 1;
*http_p = 0;
pathlen = strlen(path + 4);
memmove(path, path + 4, pathlen + 1);
client->http = 1;
}
if (*path != '/')
return 1;
/* Check for illegal characters in element */
if (strcspn(path, ">!@#$%`;|\"\\'<") < pathlen)
return 1;
p = strchr(path, '?');
if (p)
{
*p = 0;
debug_msg("Found subtree %s and %s", path, p+1);
rc = processfilter(client, p+1);
if (rc) return 1;
}
return 0;
}
/* sacerdoti: A version of Steven's thread-safe readn.
* Only works for reading one line. */
int
readline(int fd, char *buf, int maxlen)
{
int i, justread, nleft, stop;
char c;
char *ptr;
stop = 0;
ptr = buf;
nleft = maxlen;
while (nleft > 0 && !stop)
{
justread = read(fd, ptr, nleft);
if (justread < 0)
{
if (errno == EINTR)
justread = 0; /* and call read() again. */
else
return -1;
}
else if (justread==0)
break; /* EOF */
/* Examine buf for end-of-line indications. */
for (i=0; i<justread; i++)
{
c = ptr[i];
if (c=='\n' || c=='\r')
{
stop = 1;
justread = i; /* Throw out everything after newline. */
break;
}
}
nleft -= justread;
ptr += justread;
}
*ptr = 0;
if ((stop == 0) && (nleft == 0)) /* Overflow */
return -1;
return (maxlen - nleft);
}
#define REQUESTLEN 2048
void *
server_thread (void *arg)
{
int interactive = (arg != NULL);
socklen_t len;
int request_len;
client_t client;
char remote_ip[16];
char request[REQUESTLEN + 1];
llist_entry *le;
datum_t rootdatum;
for (;;)
{
client.valid = 0;
len = sizeof(client.addr);
if (interactive)
{
pthread_mutex_lock(&server_interactive_mutex);
SYS_CALL( client.fd, accept(interactive_socket->sockfd, (struct sockaddr *) &(client.addr), &len));
pthread_mutex_unlock(&server_interactive_mutex);
}
else
{
pthread_mutex_lock ( &server_socket_mutex );
SYS_CALL( client.fd, accept(server_socket->sockfd, (struct sockaddr *) &(client.addr), &len));
pthread_mutex_unlock( &server_socket_mutex );
}
if ( client.fd < 0 )
{
err_ret("server_thread() error");
debug_msg("server_thread() %lx clientfd = %d errno=%d\n",
(unsigned long) pthread_self(), client.fd, errno);
continue;
}
my_inet_ntop( AF_INET, (void *)&(client.addr.sin_addr), remote_ip, 16 );
if ( !strcmp(remote_ip, "127.0.0.1")
|| gmetad_config.all_trusted
|| (llist_search(&(gmetad_config.trusted_hosts), (void *)remote_ip, strcmp, &le) == 0) )
{
client.valid = 1;
}
if(! client.valid )
{
debug_msg("server_thread() %s tried to connect and is not a trusted host",
remote_ip);
close( client.fd );
continue;
}
client.filter=0;
client.http=0;
gettimeofday(&client.now, NULL);
if (interactive)
{
request_len = readline(client.fd, request, REQUESTLEN);
if (request_len < 0)
{
err_msg("server_thread() could not read request from %s", remote_ip);
close(client.fd);
continue;
}
debug_msg("server_thread() received request \"%s\" from %s", request, remote_ip);
if (process_request(&client, request))
{
err_msg("Got a malformed path request from %s", remote_ip);
close(client.fd);
continue;
}
}
else
strcpy(request, "/");
if(root_report_start(&client))
{
err_msg("server_thread() %lx unable to write root preamble (DTD, etc)",
(unsigned long) pthread_self() );
close(client.fd);
continue;
}
/* Start search at the root node. */
rootdatum.data = &root;
rootdatum.size = sizeof(root);
if (process_path(&client, request, &rootdatum, NULL))
{
err_msg("server_thread() %lx unable to write XML tree info",
(unsigned long) pthread_self() );
close(client.fd);
continue;
}
if(root_report_end(&client))
{
err_msg("server_thread() %lx unable to write root epilog",
(unsigned long) pthread_self() );
}
close(client.fd);
}
}