cgroup plugin fixes: disable system.slice, user.slice and systemd cgroups by default, now the plugin allows configuring options to prevent it from decenting into specific sub-cgroups; fixes copy-paste typos; #329
Costa Tsaousis committed
Apr 30, 2016 at 01:05 UTC
3a731643e76772cd2ea8727a44d03dbdaf4e4f94
1 file changed
+52
-24
src/sys_fs_cgroup.c
+52
-24
@@ -36,7 +36,7 @@ static int cgroup_root_max = 50;
36
static int cgroup_max_depth = 0;
37
38
void read_cgroup_plugin_configuration() {
39
- cgroup_check_for_new_every = config_get_number("plugin:cgroups", "check for new plugin every", cgroup_check_for_new_every);
39
+ cgroup_check_for_new_every = config_get_number("plugin:cgroups", "check for new cgroups every", cgroup_check_for_new_every);
40
41
cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat", cgroup_enable_cpuacct_stat);
42
cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage", cgroup_enable_cpuacct_usage);
@@ -155,8 +155,8 @@ struct cpuacct_usage {
155
};
156
157
struct cgroup {
158
- int available;
159
- int enabled;
158
+ int available; // found in the filesystem
159
+ int enabled; // enabled in the config
160
161
char *id;
162
uint32_t hash;
@@ -570,7 +570,7 @@ void read_all_cgroups(struct cgroup *root) {
570
struct cgroup *cg;
571
572
for(cg = root; cg ; cg = cg->next)
573
- if(cg->enabled)
573
+ if(cg->enabled && cg->available)
574
cgroup_read(cg);
575
}
576
@@ -637,7 +637,12 @@ struct cgroup *cgroup_add(const char *id) {
637
638
// disable by default the parent cgroup
639
// for known cgroup managers
640
- if(!strcmp(chart_id, "lxc") || !strcmp(chart_id, "docker")) {
640
+ if(!strcmp(chart_id, "lxc") ||
641
+ !strcmp(chart_id, "docker") ||
642
+ !strcmp(chart_id, "systemd") ||
643
+ !strncmp(chart_id, "user.slice", 10) ||
644
+ !strncmp(chart_id, "system.slice", 12)
645
+ ) {
646
def = 0;
647
debug(D_CGROUP, "cgroup '%s' is container manager (by default %s)", id, (def)?"enabled":"disabled");
648
}
@@ -667,15 +672,16 @@ struct cgroup *cgroup_add(const char *id) {
672
673
cgroup_root_count++;
674
670
- // fprintf(stderr, " > added cgroup No %d, with id '%s' (%u) and name '%s'\n", cgroup_root_count, cg->id, cg->hash, cg->name);
671
-
675
// fix the name by calling the external script
676
cgroup_get_chart_id(cg);
677
678
char option[FILENAME_MAX + 1];
676
- snprintf(option, FILENAME_MAX, "enable cgroup %s", cg->chart_id);
679
+ snprintf(option, FILENAME_MAX, "enable cgroup %s", chart_id);
680
cg->enabled = config_get_boolean("plugin:cgroups", option, def);
678
- debug(D_CGROUP, "finally: new cgroup '%s' is named '%s' and is %s (default was %s)", cg->id, cg->chart_id, (cg->enabled)?"enabled":"disabled", (def)?"enabled":"disabled");
681
+
682
+ // FIXME
683
+ // switch this to debug(D_CGROUP, ...
684
+ info("Added cgroup '%s' with chart id '%s' as %s (default was %s)", cg->id, cg->chart_id, (cg->enabled)?"enabled":"disabled", (def)?"enabled":"disabled");
685
686
return cg;
687
}
@@ -683,6 +689,10 @@ struct cgroup *cgroup_add(const char *id) {
689
void cgroup_remove(struct cgroup *cg) {
690
debug(D_CGROUP, "removing cgroup '%s'", cg->id);
691
692
+ // FIXME
693
+ // switch this to debug(D_CGROUP, ...
694
+ info("Removing cgroup '%s' with chart id '%s' (was %s and %s)", cg->id, cg->chart_id, (cg->enabled)?"enabled":"disabled", (cg->available)?"available":"not available");
695
+
696
if(cg == cgroup_root) {
697
cgroup_root = cg->next;
698
}
@@ -715,6 +725,8 @@ void cgroup_remove(struct cgroup *cg) {
725
free(cg->id);
726
free(cg->chart_id);
727
free(cg);
728
+
729
+ cgroup_root_count--;
730
}
731
732
// find if a given cgroup exists
@@ -723,15 +735,13 @@ struct cgroup *cgroup_find(const char *id) {
735
736
uint32_t hash = simple_hash(id);
737
726
- // fprintf(stderr, " > searching for '%s' (%u)\n", id, hash);
727
-
738
struct cgroup *cg;
739
for(cg = cgroup_root; cg ; cg = cg->next) {
740
if(hash == cg->hash && strcmp(id, cg->id) == 0)
741
break;
742
}
743
734
- debug(D_CGROUP, "cgroup '%s' %s", id, (cg)?"found":"not found");
744
+ debug(D_CGROUP, "cgroup_find('%s') %s", id, (cg)?"found":"not found");
745
return cg;
746
}
747
@@ -765,6 +775,7 @@ void found_dir_in_subdir(const char *dir) {
775
}
776
777
void find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
778
+ int enabled = -1;
779
if(!this) this = base;
780
size_t dirlen = strlen(this), baselen = strlen(base);
781
@@ -785,13 +796,31 @@ void find_dir_in_subdirs(const char *base, const char *this, void (*callback)(co
796
debug(D_CGROUP, "examining '%s/%s'", this, de->d_name);
797
798
if(de->d_type == DT_DIR) {
788
- char *s = malloc(dirlen + strlen(de->d_name) + 2);
789
- if(s) {
790
- strcpy(s, this);
791
- strcat(s, "/");
792
- strcat(s, de->d_name);
793
- find_dir_in_subdirs(base, s, callback);
794
- free(s);
799
+ if(enabled == -1) {
800
+ // we check for this option here
801
+ // so that the config will not have settings
802
+ // for leaf directories
803
+ char option[FILENAME_MAX + 1];
804
+ snprintf(option, FILENAME_MAX, "search for cgroups under %s", (this == base)?"/":this);
805
+
806
+ int def = 1;
807
+ if(!strcmp(this, "system.slice") ||
808
+ !strcmp(this, "user.slice") ||
809
+ !strcmp(this, "systemd"))
810
+ def = 0;
811
+
812
+ enabled = config_get_boolean("plugin:cgroups", option, def);
813
+ }
814
+
815
+ if(enabled) {
816
+ char *s = malloc(dirlen + strlen(de->d_name) + 2);
817
+ if(s) {
818
+ strcpy(s, this);
819
+ strcat(s, "/");
820
+ strcat(s, de->d_name);
821
+ find_dir_in_subdirs(base, s, callback);
822
+ free(s);
823
+ }
824
}
825
}
826
}
@@ -828,9 +857,7 @@ struct cgroup *find_all_cgroups() {
857
// fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
858
859
if(!cg->available) {
831
- // do not remove the cgroup
832
- // it will be added back on the next scan
833
- // cgroup_remove(cg);
860
+ cgroup_remove(cg);
861
continue;
862
}
863
@@ -907,7 +934,8 @@ void update_cgroup_charts(int update_every) {
934
RRDSET *st;
935
936
for(cg = cgroup_root; cg ; cg = cg->next) {
910
- if(!cg->available || !cg->enabled) continue;
937
+ if(!cg->available || !cg->enabled)
938
+ continue;
939
940
if(cg->id[0] == '\0')
941
strcpy(type, "cgroup_host");
@@ -1184,7 +1212,7 @@ void *cgroups_main(void *ptr)
1212
1213
// when ZERO, attempt to do it
1214
int vdo_sys_fs_cgroup = 0;
1187
- int vdo_cpu_netdata = !config_get_boolean("plugin:cgroups", "netdata server resources", 1);
1215
+ int vdo_cpu_netdata = !config_get_boolean("plugin:cgroups", "cgroups plugin resources", 1);
1216
1217
// keep track of the time each module was called
1218
unsigned long long sutime_sys_fs_cgroup = 0ULL;