void read_cgroup_plugin_configuration() { cgroup_check_for_new_every = config_get_number("plugin:cgroups", "check for new cgroups every", cgroup_check_for_new_every); cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat", cgroup_enable_cpuacct_stat); cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage", cgroup_enable_cpuacct_usage); cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory", cgroup_enable_memory); cgroup_enable_blkio = config_get_boolean_ondemand("plugin:cgroups", "enable blkio", cgroup_enable_blkio); char filename[FILENAME_MAX + 1], *s; struct mountinfo *mi, *root = mountinfo_read(); mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct"); if(!mi) mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct"); if(!mi) { error("Cannot find cgroup cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct"); s = "/sys/fs/cgroup/cpuacct"; } else s = mi->mount_point; snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s); cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename); mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio"); if(!mi) mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio"); if(!mi) { error("Cannot find cgroup blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio"); s = "/sys/fs/cgroup/blkio"; } else s = mi->mount_point; snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s); cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename); mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory"); if(!mi) mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory"); if(!mi) { error("Cannot find cgroup memory mountinfo. Assuming default: /sys/fs/cgroup/memory"); s = "/sys/fs/cgroup/memory"; } else s = mi->mount_point; snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s); cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename); mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices"); if(!mi) mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices"); if(!mi) { error("Cannot find cgroup devices mountinfo. Assuming default: /sys/fs/cgroup/devices"); s = "/sys/fs/cgroup/devices"; } else s = mi->mount_point; snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s); cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename); cgroup_root_max = config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max); cgroup_max_depth = config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth); cgroup_enable_new_cgroups_detected_at_runtime = config_get_boolean("plugin:cgroups", "enable new cgroups detected at run time", cgroup_enable_new_cgroups_detected_at_runtime); mountinfo_free(root); }
static inline void mountinfo_reload(int force) { static time_t last_loaded = 0; time_t now = now_realtime_sec(); if(force || now - last_loaded >= check_for_new_mountpoints_every) { // mountinfo_free() can be called with NULL disk_mountinfo_root mountinfo_free(disk_mountinfo_root); // re-read mountinfo in case something changed disk_mountinfo_root = mountinfo_read(0); last_loaded = now; } }
struct disk *get_disk(unsigned long major, unsigned long minor) { static char path_find_block_device_partition[FILENAME_MAX + 1] = ""; static struct mountinfo *mountinfo_root = NULL; struct disk *d; // search for it in our RAM list. // this is sequential, but since we just walk through // and the number of disks / partitions in a system // should not be that many, it should be acceptable for(d = disk_root; d ; d = d->next) if(unlikely(d->major == major && d->minor == minor)) break; // if we found it, return it if(likely(d)) return d; if(unlikely(!path_find_block_device_partition[0])) { char filename[FILENAME_MAX + 1]; snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/dev/block/%lu:%lu/partition"); snprintfz(path_find_block_device_partition, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get block device partition", filename)); } // not found // create a new disk structure d = (struct disk *)malloc(sizeof(struct disk)); if(!d) fatal("Cannot allocate memory for struct disk in proc_diskstats."); d->major = major; d->minor = minor; d->partition_id = -1; d->next = NULL; // append it to the list if(!disk_root) disk_root = d; else { struct disk *last; for(last = disk_root; last->next ;last = last->next); last->next = d; } // find if it is a partition // by reading /sys/dev/block/MAJOR:MINOR/partition char buffer[FILENAME_MAX + 1]; snprintfz(buffer, FILENAME_MAX, path_find_block_device_partition, major, minor); int fd = open(buffer, O_RDONLY, 0666); if(likely(fd != -1)) { // we opened it int bytes = read(fd, buffer, FILENAME_MAX); close(fd); if(bytes > 0) d->partition_id = strtoul(buffer, NULL, 10); } // if the /partition file does not exist, it is a disk, not a partition // ------------------------------------------------------------------------ // check if we can find its mount point // mountinfo_find() can be called with NULL mountinfo_root struct mountinfo *mi = mountinfo_find(mountinfo_root, d->major, d->minor); if(unlikely(!mi)) { // mountinfo_free() can be called with NULL mountinfo_root mountinfo_free(mountinfo_root); // re-read mountinfo in case something changed mountinfo_root = mountinfo_read(); // search again for this disk mi = mountinfo_find(mountinfo_root, d->major, d->minor); } if(mi) d->family = strdup(mi->mount_point); // no need to check for NULL else d->family = NULL; return d; }