-
Notifications
You must be signed in to change notification settings - Fork 246
/
gmond.c
3538 lines (3095 loc) · 113 KB
/
gmond.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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ganglia.h> /* for the libgmond messaging */
#include <gm_metric.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <math.h>
#ifdef SOLARIS
#define fabsf(f) ((float)fabs(f))
#endif
#ifdef _AIX
#ifndef _AIX52
/* _AIX52 is defined on all versions of AIX >= 5.2
fabsf doesn't exist on versions prior to 5.2 */
#define fabsf(f) ((float)fabs(f))
#endif
#endif
#ifdef LINUX
#include <sys/utsname.h>
#endif
#include <zlib.h>
#include <apr.h>
#include <apr_strings.h>
#include <apr_hash.h>
#include <apr_time.h>
#include <apr_pools.h>
#include <apr_poll.h>
#include <apr_network_io.h>
#include <apr_signal.h>
#include <apr_thread_proc.h>
#include <apr_tables.h>
#include <apr_dso.h>
#include <apr_version.h>
#ifdef HAVE_LIBPCRE
#if defined (HAVE_PCRE_PCRE_H)
#include <pcre/pcre.h>
#else
#include <pcre.h>
#endif
#endif
#include "cmdline.h" /* generated by cmdline.sh which runs gengetopt */
#include "become_a_nobody.h"
#include "apr_net.h" /* our private network functions based on apr */
#include "dtd.h" /* the DTD definition for our XML */
#include "g25_config.h" /* for converting old file formats to new */
#include "update_pidfile.h"
#include "gm_scoreboard.h"
#include "ganglia_priv.h"
/* Specifies a single value metric callback */
#define CB_NOINDEX -1
/* If a bind fails, and retry_bind is true, this is the interval to sleep
before retry. Specified in seconds */
#define RETRY_BIND_DELAY 60
/* The key in the apr_socket_t struct where our gzipped data is stored */
#define GZIP_KEY "gzip"
/* When this gmond was started */
apr_time_t started;
/* My name */
char myname[APRMAXHOSTLEN+1];
/* The commandline options */
struct gengetopt_args_info args_info;
/* The configuration file */
cfg_t *config_file;
/* The debug level (in debug_msg.c) */
static int debug_level;
/* The global context */
apr_pool_t *global_context = NULL;
/* Deaf mode boolean */
int deaf;
/* Mute mode boolean */
int mute;
/* Allow extra data boolean */
int allow_extra_data;
/* last time we received any data */
apr_time_t udp_last_heard;
/* Cluster tag boolean */
int cluster_tag = 0;
/* This host's location */
char *host_location = NULL;
/* This host name, spoofed */
char *override_hostname = NULL;
/* This host ip, spoofed */
char *override_ip = NULL;
/* Tags */
char *tags = NULL;
/* Boolean. Will this host received gexec requests? */
int gexec_on = 0;
/* This is tweakable by globals{max_udp_msg_len=...} */
int max_udp_message_len = 1472;
/* The default configuration for gmond. Found in conf.c. */
extern char *default_gmond_configuration;
/* The number of seconds to hold "dead" hosts in the hosts hash */
int host_dmax = 0;
/* The number of seconds to wait for a message before considering it down */
int host_tmax = 20;
/* The amount of time between cleanups */
int cleanup_threshold = 300;
/* Time interval before send another metadata packet */
int send_metadata_interval = 0;
/* The directory where DSO modules are located */
char *module_dir = NULL;
/* The array for outgoing UDP message channels */
Ganglia_udp_send_channels udp_send_channels = NULL;
/* TODO: The array for outgoing TCP message channels (later) */
apr_array_header_t *tcp_send_array = NULL;
enum Ganglia_action_types {
GANGLIA_ACCESS_DENY = 0,
GANGLIA_ACCESS_ALLOW = 1
};
typedef enum Ganglia_action_types Ganglia_action_types;
/* This is the structure used for the access control lists */
struct Ganglia_access {
apr_ipsubnet_t *ipsub;
Ganglia_action_types action;
};
typedef struct Ganglia_access Ganglia_access;
struct Ganglia_acl {
apr_array_header_t *access_array;
Ganglia_action_types default_action;
};
typedef struct Ganglia_acl Ganglia_acl;
/* This is the channel definitions */
enum Ganglia_channel_types {
TCP_ACCEPT_CHANNEL,
UDP_RECV_CHANNEL
};
typedef enum Ganglia_channel_types Ganglia_channel_types;
struct Ganglia_channel {
Ganglia_channel_types type;
Ganglia_acl *acl;
int timeout;
int gzip_output;
};
typedef struct Ganglia_channel Ganglia_channel;
/* Two separate pollsets hold the tcp_accept and udp_recv channels */
apr_pollset_t *udp_listen_channels = NULL;
apr_pollset_t *tcp_listen_channels = NULL;
/* These are the TCP listen channels */
apr_socket_t **tcp_sockets = NULL;
/* These are the UDP sockets */
apr_socket_t **udp_recv_sockets = NULL;
/* The hash to hold the hosts (key = host IP) */
apr_hash_t *hosts = NULL;
apr_thread_mutex_t *hosts_mutex = NULL;
/* The "hosts" hash contains values of type "hostdata" */
#ifdef SFLOW
#include "sflow.h"
uint16_t sflow_udp_port = SFLOW_IANA_REGISTERED_PORT;
#endif
#include "gmond_internal.h"
/* This is the structure of the data save to each host->metric hash */
struct Ganglia_metadata {
/* The pool used for allocating memory */
apr_pool_t *pool;
/* The name of the metric */
char *name;
union {
/* The ganglia message */
Ganglia_metadata_msg f_message;
Ganglia_value_msg v_message;
} message_u;
/* Last heard from */
apr_time_t last_heard_from;
};
typedef struct Ganglia_metadata Ganglia_metadata;
/* The hash to hold the metrics available on this platform */
apr_hash_t *metric_callbacks = NULL;
/* The "metrics" hash contains values of type "Ganglia_metric_callback" */
/* This is where libmetrics meets gmond */
struct Ganglia_metric_callback {
char *name; /* metric name */
float value_threshold;/* the value threshold */
char *title; /* Altername metric name or short description */
Ganglia_25metric *info;/* the information about this metric */
metric_func_void cb; /* callback function (deprecated) */
metric_func cbindexed; /* multi-metric callback function */
g_val_t now; /* the current value */
g_val_t last; /* the last value */
Ganglia_value_msg msg; /* the message to send */
mmodule *modp; /* dynamic module info struct */
int multi_metric_index; /* index identifying which metric is wanted */
apr_time_t metadata_last_sent; /* when the metadata was last sent */
};
typedef struct Ganglia_metric_callback Ganglia_metric_callback;
/* This is the structure of a collection group */
struct Ganglia_collection_group {
apr_time_t next_collect; /* When to collect next */
apr_time_t next_send; /* When to send next (tmax) */
int once;
int collect_every;
int time_threshold;
apr_array_header_t *metric_array;
};
typedef struct Ganglia_collection_group Ganglia_collection_group;
/* This is the array of collection groups that we are processing... */
apr_array_header_t *collection_groups = NULL;
mmodule *metric_modules = NULL;
extern int daemon_proc; /* defined in error.c */
char **gmond_argv;
extern char **environ;
/* apr_socket_send can't assure all characters in buf been sent. */
static apr_status_t
socket_send_raw(apr_socket_t *sock, const char *buf, apr_size_t *len)
{
apr_size_t total = *len;
apr_size_t thisTime = total;
const char* p = buf;
apr_status_t ret;
for(ret=apr_socket_send(sock, p, &thisTime); ret == APR_SUCCESS;
ret=apr_socket_send(sock, p, &thisTime))
{
if(thisTime < total)
{
total -= thisTime;
p += thisTime;
thisTime = total;
}
else
break;
}
return ret;
}
/* wrap socket_send_raw with gzip deflate if enabled. */
static apr_status_t
socket_send(apr_socket_t *sock, const char *buf, apr_size_t *len)
{
char outputbuffer[2048];
const int outputlen = sizeof(outputbuffer);
apr_size_t wlen;
apr_status_t ret;
z_stream *strm;
int z_ret;
ret = apr_socket_data_get((void**)&strm, GZIP_KEY, sock);
if (ret != APR_SUCCESS)
{
return ret;
}
if (!strm)
{
ret = socket_send_raw( sock, buf, len );
}
else
{
strm->next_in = (Bytef *)buf;
strm->avail_in = *len;
while( strm->avail_in )
{
strm->next_out = (Bytef *)outputbuffer;
strm->avail_out = outputlen;
z_ret = deflate( strm, 0 );
if (z_ret != Z_OK)
{
return APR_ENOMEM;
}
wlen = outputlen - strm->avail_out;
if( wlen )
{
ret = socket_send_raw( sock, outputbuffer, &wlen );
if(ret != APR_SUCCESS)
{
return ret;
}
}
}
}
return ret;
}
/* Reload the Ganglia configuration */
void
reload_ganglia_configuration(void)
{
int i = 0;
char *gmond_bin = gmond_argv[0];
if(udp_listen_channels != NULL)
apr_pollset_destroy(udp_listen_channels);
if(tcp_listen_channels != NULL)
apr_pollset_destroy(tcp_listen_channels);
if(tcp_sockets != NULL)
for(i = 0; tcp_sockets[i] != 0; i++)
apr_socket_close(tcp_sockets[i]);
if(udp_recv_sockets != NULL)
for(i = 0; udp_recv_sockets[i] != 0; i++)
apr_socket_close(udp_recv_sockets[i]);
debug_msg("reloading %s", gmond_bin);
#ifndef CYGWIN
/* To do: over-ride some config opts:
- tell new process not to re-daemonize
- tell new process not to setuid
Any paths in gmond_argv must be absolute or relative to / */
execve(gmond_bin, gmond_argv, environ);
#else
/* Exit and let Windows service manager restart the process, as
neither Cygwin nor apr provide a perfect equivalent to execve */
exit(EXIT_SUCCESS);
#endif
err_msg("execve failed to reload %s: %s", gmond_bin, strerror(errno));
exit(EXIT_FAILURE);
}
/* this is just a temporary function */
void
process_configuration_file(void)
{
cfg_t *tmp;
/* this is a global for now */
config_file = (cfg_t*)Ganglia_gmond_config_create( args_info.conf_arg, !args_info.conf_given );
/* Initialize a few variables */
tmp = cfg_getsec( config_file, "globals");
/* Get the maximum UDP message size */
max_udp_message_len = cfg_getint( tmp, "max_udp_msg_len");
/* Get the gexec status requested */
gexec_on = cfg_getbool(tmp, "gexec");
/* Get the host dmax ... */
host_dmax = cfg_getint( tmp, "host_dmax");
/* Get the host tmax ... */
host_tmax = cfg_getint( tmp, "host_tmax");
/* Get the cleanup threshold */
cleanup_threshold = cfg_getint( tmp, "cleanup_threshold");
/* Get the send meta data packet interval */
send_metadata_interval = cfg_getint( tmp, "send_metadata_interval");
/* Get the DSO module dir */
module_dir = cfg_getstr(tmp, "module_dir");
/* Acquire spoof name/ip, if they are specified */
override_hostname = cfg_getstr(tmp, "override_hostname");
override_ip = cfg_getstr(tmp, "override_ip");
/* Any tags for this host */
tags = cfg_getstr(tmp, "tags");
/* Commandline for debug_level trumps configuration file behaviour ... */
if (args_info.debug_given)
{
debug_level = args_info.debug_arg;
}
else
{
debug_level = cfg_getint ( tmp, "debug_level");
}
set_debug_msg_level(debug_level);
}
static void
daemonize_if_necessary( char *argv[] )
{
int should_daemonize;
cfg_t *tmp;
tmp = cfg_getsec( config_file, "globals");
should_daemonize = cfg_getbool( tmp, "daemonize");
/* Daemonize if needed */
if(!args_info.foreground_flag && should_daemonize && !debug_level)
{
char *cwd;
apr_filepath_get(&cwd, 0, global_context);
apr_proc_detach(1);
apr_filepath_set(cwd, global_context);
/* enable errmsg logging to syslog */
daemon_proc = 1;
openlog (argv[0], LOG_PID, LOG_DAEMON);
}
}
static void
setuid_if_necessary( void )
{
cfg_t *tmp;
int setuid;
#ifndef CYGWIN
char *user;
#endif
tmp = cfg_getsec( config_file, "globals");
setuid = cfg_getbool( tmp, "setuid" );
if(setuid)
{
#ifdef CYGWIN
fprintf(stderr,"Windows does not support setuid.\n");
#else
user = cfg_getstr(tmp, "user" );
become_a_nobody(user);
#endif
}
}
static void
process_deaf_mute_mode( void )
{
cfg_t *tmp = cfg_getsec( config_file, "globals");
deaf = cfg_getbool( tmp, "deaf");
mute = cfg_getbool( tmp, "mute");
if(deaf && mute)
{
err_msg("Configured to run both deaf and mute. Nothing to do. Exiting.\n");
exit(EXIT_FAILURE);
}
}
static void
process_allow_extra_data_mode( void )
{
cfg_t *tmp = cfg_getsec( config_file, "globals");
allow_extra_data = cfg_getbool( tmp, "allow_extra_data");
}
static Ganglia_acl *
Ganglia_acl_create ( cfg_t *channel, apr_pool_t *pool )
{
apr_status_t status;
Ganglia_acl *acl = NULL;
cfg_t *acl_config;
char *default_action;
int num_access = 0;
int i;
if(!channel || !pool)
{
return acl;
}
acl_config = cfg_getsec(channel, "acl");
if(!acl_config)
{
return acl;
}
/* Find out the number of access entries */
num_access = cfg_size( acl_config, "access" );
if(!num_access)
{
return acl;
}
/* Create a new ACL from the pool */
acl = apr_pcalloc( pool, sizeof(Ganglia_acl));
if(!acl)
{
err_msg("Unable to allocate memory for ACL. Exiting.\n");
exit(EXIT_FAILURE);
}
default_action = cfg_getstr( acl_config, "default");
if(!apr_strnatcasecmp( default_action, "deny"))
{
acl->default_action = GANGLIA_ACCESS_DENY;
}
else if(!apr_strnatcasecmp( default_action, "allow"))
{
acl->default_action = GANGLIA_ACCESS_ALLOW;
}
else
{
err_msg("Invalid default ACL '%s'. Exiting.\n", default_action);
exit(EXIT_FAILURE);
}
/* Create an array to hold each of the access instructions */
acl->access_array = apr_array_make( pool, num_access, sizeof(Ganglia_acl *));
if(!acl->access_array)
{
err_msg("Unable to malloc access array. Exiting.\n");
exit(EXIT_FAILURE);
}
for(i=0; i< num_access; i++)
{
cfg_t *access_config = cfg_getnsec( acl_config, "access", i);
Ganglia_access *access = apr_pcalloc( pool, sizeof(Ganglia_access));
char *ip, *mask, *action;
if(!access_config)
{
/* This shouldn't happen unless maybe acl is empty and
* the safest thing to do it exit */
err_msg("Unable to process ACLs. Exiting.\n");
exit(EXIT_FAILURE);
}
ip = cfg_getstr( access_config, "ip");
mask = cfg_getstr( access_config, "mask");
action = cfg_getstr( access_config, "action");
if(!ip && !mask && !action)
{
err_msg("An access record requires an ip, mask and action. Exiting.\n");
exit(EXIT_FAILURE);
}
/* Process the action first */
if(!apr_strnatcasecmp( action, "deny" ))
{
access->action = GANGLIA_ACCESS_DENY;
}
else if(!apr_strnatcasecmp( action, "allow"))
{
access->action = GANGLIA_ACCESS_ALLOW;
}
else
{
err_msg("ACL access entry has action '%s'. Must be deny|allow. Exiting.\n", action);
exit(EXIT_FAILURE);
}
/* Create the subnet */
access->ipsub = NULL;
status = apr_ipsubnet_create( &(access->ipsub), ip, mask, pool);
if(status != APR_SUCCESS)
{
err_msg("ACL access entry has invalid ip('%s')/mask('%s'). Exiting.\n", ip, mask);
exit(EXIT_FAILURE);
}
/* Save this access entry to the acl */
*(Ganglia_access **)apr_array_push( acl->access_array ) = access;
}
return acl;
}
static int
Ganglia_acl_action( Ganglia_acl *acl, apr_sockaddr_t *addr )
{
int i;
if(!acl)
{
/* If no ACL is specified, we assume there is no access control */
return GANGLIA_ACCESS_ALLOW;
}
for(i=0; i< acl->access_array->nelts; i++)
{
Ganglia_access *access = ((Ganglia_access **)(acl->access_array->elts))[i];
if(!apr_ipsubnet_test( access->ipsub, addr ))
{
/* no action will occur because addr is not in this subnet */
continue;
}
else
{
return access->action;
}
}
/* No matches in the access list so we return the default */
return acl->default_action;
}
static int32_t
get_sock_family( char *family )
{
if( strchr( family, '4' ))
{
return APR_INET;
}
else if( strchr( family, '6'))
{
#if APR_INET6
return APR_INET6;
#else
err_msg("IPv6 is not supported on this host. Exiting.\n");
exit(EXIT_FAILURE);
#endif
}
err_msg("Unknown family '%s'. Try inet4|inet6. Exiting.\n", family);
exit(EXIT_FAILURE);
/* shouldn't get here */
return APR_UNSPEC;
}
static void
reset_mcast_channels( void )
{
int i;
int num_udp_recv_channels = cfg_size( config_file, "udp_recv_channel");
for(i = 0; i< num_udp_recv_channels; i++)
{
cfg_t *udp_recv_channel;
char *mcast_join, *mcast_if;
int port;
apr_socket_t *socket = NULL;
udp_recv_channel = cfg_getnsec( config_file, "udp_recv_channel", i);
mcast_join = cfg_getstr( udp_recv_channel, "mcast_join" );
mcast_if = cfg_getstr( udp_recv_channel, "mcast_if" );
port = cfg_getint( udp_recv_channel, "port");
if ( mcast_join )
{
socket = udp_recv_sockets[i];
join_mcast(global_context, socket, mcast_join, port, mcast_if);
}
}
}
static void
setup_listen_channels_pollset( void )
{
apr_status_t status;
int i;
int num_udp_recv_channels = cfg_size( config_file, "udp_recv_channel");
int num_tcp_accept_channels = cfg_size( config_file, "tcp_accept_channel");
int total_listen_channels = num_udp_recv_channels + num_tcp_accept_channels;
Ganglia_channel *channel;
int pollset_opts = 0;
/* check if gmond was really meant to be deaf */
if (total_listen_channels == 0)
{
deaf = 1;
return;
}
/* Create my incoming pollset */
#ifdef LINUX
struct utsname _name;
if(uname(&_name) >= 0) {
if(strcmp(_name.release, "2.6") >= 0)
pollset_opts = APR_POLLSET_THREADSAFE;
}
#endif
if (num_udp_recv_channels > 0) {
if((status = apr_pollset_create(&udp_listen_channels, num_udp_recv_channels, global_context, pollset_opts)) != APR_SUCCESS)
{
char apr_err[512];
apr_strerror(status, apr_err, 511);
err_msg("apr_pollset_create failed: %s", apr_err);
exit(EXIT_FAILURE);
}
}
if (num_tcp_accept_channels > 0) {
if((status = apr_pollset_create(&tcp_listen_channels, num_tcp_accept_channels, global_context, pollset_opts)) != APR_SUCCESS)
{
char apr_err[512];
apr_strerror(status, apr_err, 511);
err_msg("apr_pollset_create failed: %s", apr_err);
exit(EXIT_FAILURE);
}
}
if((udp_recv_sockets = (apr_socket_t **)apr_pcalloc(global_context, sizeof(apr_socket_t *) * (num_udp_recv_channels + 1))) == NULL)
err_quit("unable to allocate UDP listening sockets");
/* Process all the udp_recv_channels */
for(i = 0; i< num_udp_recv_channels; i++)
{
cfg_t *udp_recv_channel;
char *mcast_join, *mcast_if, *bindaddr, *family;
int port, retry_bind, buffer;
apr_socket_t *socket = NULL;
apr_pollfd_t socket_pollfd;
apr_pool_t *pool = NULL;
int32_t sock_family = APR_INET;
apr_int32_t rx_buf_sz;
socklen_t _optlen;
udp_recv_channel = cfg_getnsec( config_file, "udp_recv_channel", i);
mcast_join = cfg_getstr( udp_recv_channel, "mcast_join" );
mcast_if = cfg_getstr( udp_recv_channel, "mcast_if" );
port = cfg_getint( udp_recv_channel, "port");
bindaddr = cfg_getstr( udp_recv_channel, "bind");
family = cfg_getstr( udp_recv_channel, "family");
retry_bind = cfg_getbool( udp_recv_channel, "retry_bind");
buffer = cfg_getint( udp_recv_channel, "buffer");
debug_msg("udp_recv_channel mcast_join=%s mcast_if=%s port=%d bind=%s buffer=%d",
mcast_join? mcast_join:"NULL",
mcast_if? mcast_if:"NULL", port,
bindaddr? bindaddr: "NULL", buffer);
/* Create a sub-pool for this channel */
apr_pool_create(&pool, global_context);
sock_family = get_sock_family(family);
if( mcast_join )
{
/* Listen on the specified multicast channel */
socket = create_mcast_server(pool, sock_family, mcast_join, port, bindaddr, mcast_if );
while(!socket)
{
if(retry_bind == cfg_false)
{
err_msg("Error creating multicast server mcast_join=%s port=%d mcast_if=%s family='%s'. Try setting retry_bind. Exiting.\n",
mcast_join? mcast_join: "NULL", port, mcast_if? mcast_if:"NULL",family);
exit(EXIT_FAILURE);
}
err_msg("Error creating multicast server mcast_join=%s port=%d mcast_if=%s family='%s'. Will try again...\n",
mcast_join? mcast_join: "NULL", port, mcast_if? mcast_if:"NULL",family);
apr_sleep(APR_USEC_PER_SEC * RETRY_BIND_DELAY);
socket = create_mcast_server(pool, sock_family, mcast_join, port, bindaddr, mcast_if );
}
}
else
{
/* Create a UDP server */
socket = create_udp_server( pool, sock_family, port, bindaddr );
while(!socket)
{
if(retry_bind == cfg_false)
{
err_msg("Error creating UDP server on port %d bind=%s. Try setting retry_bind. Exiting.\n",
port, bindaddr? bindaddr: "unspecified");
exit(EXIT_FAILURE);
}
err_msg("Error creating UDP server on port %d bind=%s. Will try again...\n",
port, bindaddr? bindaddr: "unspecified");
apr_sleep(APR_USEC_PER_SEC * RETRY_BIND_DELAY);
socket = create_udp_server( pool, sock_family, port, bindaddr );
}
}
if(buffer)
{
debug_msg("setting UDP socket receive buffer to: %d\n", (apr_int32_t) buffer);
if(apr_socket_opt_set(socket, APR_SO_RCVBUF, (apr_int32_t) buffer) == APR_SUCCESS)
{
debug_msg("APR reports success setting APR_SO_RCVBUF to: %d\n", (apr_int32_t)buffer );
/* RB: Let's check if it actually worked to be sure */
_optlen = sizeof(rx_buf_sz);
if(getsockopt(get_apr_os_socket(socket), SOL_SOCKET, SO_RCVBUF,
&rx_buf_sz, &_optlen) == 0)
{
debug_msg("socket created, SO_RCVBUF = %d\n", rx_buf_sz);
if(buffer)
{
/* NOTE: getsockopt() returns double SO_RCVBUF on Linux for reserved overhead space */
if(rx_buf_sz!=(buffer*2))
{
err_msg("WARNING When setting UDP receive buffer for port %d bind=%s to size: %d.\n",
port, bindaddr? bindaddr: "unspecified", (apr_int32_t) buffer);
err_msg("Reported raw buffer size by OS: %d : config setting %d. Unable to verify\n",
(int) rx_buf_sz, (int) buffer);
err_msg("NOTE: Linux will report twice the configured value. See socket(7).\n");
err_msg("Check Operating System (kernel) limits, change or disable buffer size.\n");
}
else
{ /* RB: Eureka */
debug_msg("Actual receive buffer size reported by OS matches config setting. Success.");
}
}
}
else
{
err_msg("Unable to verify UDP receive buffer for port %d bind=%s to size: %d. Check Operating System (limits) or change buffer size. Exiting.\n",
port, bindaddr? bindaddr: "unspecified", buffer);
exit(EXIT_FAILURE);
}
}
else
{
err_msg("Error setting UDP receive buffer for port %d bind=%s to size: %d. Check Operating System limits or change buffer size. Exiting.\n",
port, bindaddr? bindaddr: "unspecified", (apr_int32_t) buffer);
err_msg("This is currently only supported on systems that have Apache Portable Runtime library version 0.9.4 or higher.\n");
err_msg("Check Operating System (kernel) limits, change or disable buffer size. Exiting.\n");
exit(EXIT_FAILURE);
}
}
/* Find out about the RX socket buffer
This is logged to help people troubleshoot
Some users have observed messages about errors when sending
or receiving metric packets, and a small buffer size
could be an issue */
/* RB: Just log this for debugging purposes now */
_optlen = sizeof(rx_buf_sz);
if(getsockopt(get_apr_os_socket(socket), SOL_SOCKET, SO_RCVBUF,
&rx_buf_sz, &_optlen) == 0)
{
debug_msg("socket created, SO_RCVBUF = %d\n", rx_buf_sz);
}
else
{
debug_msg("getsockopt SO_RCVBUF failed\n");
}
/* Build the socket poll file descriptor structure */
socket_pollfd.desc_type = APR_POLL_SOCKET;
socket_pollfd.reqevents = APR_POLLIN;
socket_pollfd.desc.s = socket;
udp_recv_sockets[i] = socket;
channel = apr_pcalloc( pool, sizeof(Ganglia_channel));
if(!channel)
{
err_msg("Unable to malloc memory for channel. Exiting. \n");
exit(EXIT_FAILURE);
}
/* Mark this channel as a udp_recv_channel */
channel->type = UDP_RECV_CHANNEL;
/* Make sure this socket never blocks */
channel->timeout = 0;
apr_socket_timeout_set( socket, channel->timeout);
/* Save the ACL information */
channel->acl = Ganglia_acl_create ( udp_recv_channel, pool );
/* Save the pointer to this socket specific data */
socket_pollfd.client_data = channel;
/* Add the socket to the pollset */
status = apr_pollset_add(udp_listen_channels, &socket_pollfd);
if(status != APR_SUCCESS)
{
err_msg("Failed to add socket to pollset. Exiting.\n");
exit(EXIT_FAILURE);
}
}
if ((tcp_sockets = (apr_socket_t **)apr_pcalloc(global_context, sizeof(apr_socket_t *) * (num_tcp_accept_channels + 1))) == NULL)
err_quit("Unable to allocate TCP listening sockets");
/* Process all the tcp_accept_channels */
for(i=0; i< num_tcp_accept_channels; i++)
{
cfg_t *tcp_accept_channel = cfg_getnsec( config_file, "tcp_accept_channel", i);
char *bindaddr, *interface, *family;
int port, timeout, gzip_output;
apr_socket_t *socket = NULL;
apr_pollfd_t socket_pollfd;
apr_pool_t *pool = NULL;
int32_t sock_family;
port = cfg_getint( tcp_accept_channel, "port");
bindaddr = cfg_getstr( tcp_accept_channel, "bind");
interface = cfg_getstr( tcp_accept_channel, "interface");
timeout = cfg_getint( tcp_accept_channel, "timeout");
family = cfg_getstr( tcp_accept_channel, "family");
gzip_output = cfg_getbool( tcp_accept_channel, "gzip_output");
debug_msg("tcp_accept_channel bind=%s port=%d gzip_output=%d",
bindaddr? bindaddr: "NULL", port, gzip_output);
/* Create a subpool context */
apr_pool_create(&pool, global_context);
sock_family = get_sock_family(family);
/* Create the socket for the channel, blocking w/timeout */
socket = create_tcp_server(pool, sock_family, port, bindaddr,
interface, 1, gzip_output);
if(!socket)
{
err_msg("Unable to create tcp_accept_channel. Exiting.\n");
exit(EXIT_FAILURE);
}
tcp_sockets[i] = socket;
/* Build the socket poll file descriptor structure */
socket_pollfd.desc_type = APR_POLL_SOCKET;
socket_pollfd.reqevents = APR_POLLIN;
socket_pollfd.desc.s = socket;
channel = apr_pcalloc( pool, sizeof(Ganglia_channel));
if(!channel)
{
err_msg("Unable to malloc data for channel. Exiting.\n");
exit(EXIT_FAILURE);
}
channel->type = TCP_ACCEPT_CHANNEL;
/* Save the timeout for this socket */
channel->timeout = timeout;
// Does channel support gzip
channel->gzip_output = gzip_output;
/* Save the ACL information */
channel->acl = Ganglia_acl_create( tcp_accept_channel, pool );
/* Save the pointer to this channel data */
socket_pollfd.client_data = channel;
/* Add the socket to the pollset */
status = apr_pollset_add(tcp_listen_channels, &socket_pollfd);
if(status != APR_SUCCESS)
{
err_msg("Failed to add socket to pollset. Exiting.\n");
exit(EXIT_FAILURE);
}
}
}
void sanitize_metric_name(char *metric_name, int is_spoof_msg)
{
if (metric_name == NULL) return;
if (strlen(metric_name) < 1) return;
char *p = metric_name;
while (p < (metric_name + strlen(metric_name))) {
if (
!(*p >= 'A' && *p <= 'Z')
&& !(*p >= 'a' && *p <= 'z')
&& !(*p >= '0' && *p <= '9')
&& (*p != '_')
&& (*p != '-')
&& (*p != '.')
&& (*p == ':' && !is_spoof_msg)
&& (*p != '\0')
) {
*p = '_';
}
p++;
}
}
static void
get_metric_names (Ganglia_metric_id *metric_id, char **metricName, char **realName)
{
char *firstName=NULL, *secondName=NULL, *buff=NULL;
int name_len;
*metricName = *realName = NULL;
firstName = metric_id->name;
if (metric_id->spoof)
{
name_len = strlen(firstName);
buff = malloc(name_len + 1);
strncpy(buff, firstName, name_len + 1);
firstName = buff;
secondName = strchr(buff + 1, ':');
if(secondName)
{
*secondName = 0;
secondName++;
}
}
if (firstName) {
*metricName = strdup(firstName);
if (secondName) {
*realName = strdup(secondName);
}
}
if (buff)
free(buff);
return;
}
Ganglia_host *
Ganglia_host_get( char *remIP, apr_sockaddr_t *sa, Ganglia_metric_id *metric_id)
{