static void sensors_submit (const char *plugin_instance, const char *type, const char *type_instance, double value) { char match_key[1024]; int status; value_list_t vl = VALUE_LIST_INIT; status = ssnprintf (match_key, sizeof (match_key), "%s/%s-%s", plugin_instance, type, type_instance); if (status < 1) return; if (sensor_list != NULL) { DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key); if (ignorelist_match (sensor_list, match_key)) return; } vl.values = &(value_t) { .gauge = value }; vl.values_len = 1; sstrncpy (vl.plugin, "sensors", sizeof (vl.plugin)); sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); sstrncpy (vl.type, type, sizeof (vl.type)); sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); plugin_dispatch_values (&vl); } /* void sensors_submit */
static int thermal_procfs_device_read (const char *dir, const char *name, void *user_data) { const char str_temp[] = "temperature:"; char filename[256]; char data[1024]; int len; if (device_list && ignorelist_match (device_list, name)) return -1; /** * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature * temperature: 55 C */ len = snprintf (filename, sizeof (filename), "%s/%s/temperature", dirname_procfs, name); if ((len < 0) || ((unsigned int)len >= sizeof (filename))) return -1; len = read_file_contents (filename, data, sizeof(data)); if (len > sizeof(str_temp) && data[--len] == '\n' && !strncmp(data, str_temp, sizeof(str_temp)-1)) { char *endptr = NULL; double temp; double celsius, add; if (data[--len] == 'C') { add = 0; celsius = 1; } else if (data[len] == 'F') { add = -32; celsius = 5/9; } else if (data[len] == 'K') { add = -273.15; celsius = 1; } else return -1; while (len > 0 && data[--len] == ' ') ; data[len + 1] = 0; while (len > 0 && data[--len] != ' ') ; ++len; errno = 0; temp = (strtod (data + len, &endptr) + add) * celsius; if (endptr != data + len && errno == 0) { thermal_submit(name, TEMP, temp); return 0; } } return -1; }
static int procfs_iterate(int sk) { char buffer[1024]; char *device, *dummy; FILE *fh; int status; int num_success; int num_fail; if ((fh = fopen ("/host_proc/net/dev", "r")) == NULL) { WARNING ("madwifi plugin: opening /host_proc/net/dev failed"); return (-1); } num_success = 0; num_fail = 0; while (fgets (buffer, sizeof (buffer), fh) != NULL) { dummy = strchr(buffer, ':'); if (dummy == NULL) continue; dummy[0] = 0; device = buffer; while (device[0] == ' ') device++; if (device[0] == 0) continue; if (ignorelist_match (ignorelist, device) != 0) continue; status = process_device (sk, device); if (status != 0) { ERROR ("madwifi plugin: Processing interface " "%s failed.", device); num_fail++; } else { num_success++; } } /* while (fgets) */ fclose(fh); if ((num_success == 0) && (num_fail != 0)) return (-1); return 0; }
static int md_read (void) { FILE *fh; char buffer[1024]; fh = fopen (PROC_DISKSTATS, "r"); if (fh == NULL) { char errbuf[1024]; WARNING ("md: Unable to open %s: %s", PROC_DISKSTATS , sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } /* Iterate md devices */ while (fgets (buffer, sizeof (buffer), fh) != NULL) { char path[PATH_MAX]; char *fields[4]; char *name; int major, minor; /* Extract interesting fields */ if (strsplit (buffer, fields, STATIC_ARRAY_SIZE(fields)) < 3) continue; major = atoi (fields[0]); if (major != MD_MAJOR) continue; minor = atoi (fields[1]); name = fields[2]; if (ignorelist_match (ignorelist, name)) continue; /* FIXME: Don't hardcode path. Walk /dev collecting major, * minor and name, then use lookup table to find device. * Alternatively create a temporary device file with correct * major/minor, but that again can be tricky if the filesystem * with the device file is mounted using the "nodev" option. */ ssnprintf (path, sizeof (path), "%s/%s", DEV_DIR, name); md_process (minor, path); } fclose (fh); return (0); } /* int md_read */
static int thermal_sysfs_device_read (const char *dir, const char *name, void *user_data) { char filename[256]; char data[1024]; int len; int ok = 0; if (device_list && ignorelist_match (device_list, name)) return -1; len = snprintf (filename, sizeof (filename), "%s/%s/temp", dirname_sysfs, name); if ((len < 0) || ((unsigned int)len >= sizeof (filename))) return -1; len = read_file_contents (filename, data, sizeof(data)); if (len > 1 && data[--len] == '\n') { char *endptr = NULL; double temp; data[len] = 0; errno = 0; temp = strtod (data, &endptr) / 1000.0; if (endptr == data + len && errno == 0) { thermal_submit(name, TEMP, temp); ++ok; } } len = snprintf (filename, sizeof (filename), "%s/%s/cur_state", dirname_sysfs, name); if ((len < 0) || ((unsigned int)len >= sizeof (filename))) return -1; len = read_file_contents (filename, data, sizeof(data)); if (len > 1 && data[--len] == '\n') { char *endptr = NULL; double state; data[len] = 0; errno = 0; state = strtod (data, &endptr); if (endptr == data + len && errno == 0) { thermal_submit(name, COOLING_DEV, state); ++ok; } } return ok ? 0 : -1; }
static int ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath) { char *name; int n, r; n = sizeof (char) * (strlen (domname) + strlen (devpath) + 2); name = malloc (n); if (name == NULL) { ERROR ("libvirt plugin: malloc failed."); return 0; } ssnprintf (name, n, "%s:%s", domname, devpath); r = ignorelist_match (il, name); sfree (name); return r; }
static int sysfs_iterate(int sk) { struct dirent *de; DIR *nets; int status; int num_success; int num_fail; nets = opendir ("/sys/class/net/"); if (nets == NULL) { WARNING ("madwifi plugin: opening /sys/class/net failed"); return (-1); } num_success = 0; num_fail = 0; while ((de = readdir (nets))) { if (check_devname (de->d_name) == 0) continue; if (ignorelist_match (ignorelist, de->d_name) != 0) continue; status = process_device (sk, de->d_name); if (status != 0) { ERROR ("madwifi plugin: Processing interface " "%s failed.", de->d_name); num_fail++; } else { num_success++; } } /* while (readdir) */ closedir(nets); if ((num_success == 0) && (num_fail != 0)) return (-1); return (0); }
static void submit_in_progress (char const *disk_name, gauge_t in_progress) { value_t v; value_list_t vl = VALUE_LIST_INIT; if (ignorelist_match (ignorelist, disk_name) != 0) return; v.gauge = in_progress; vl.values = &v; vl.values_len = 1; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "disk", sizeof (vl.plugin)); sstrncpy (vl.plugin_instance, disk_name, sizeof (vl.plugin_instance)); sstrncpy (vl.type, "pending_operations", sizeof (vl.type)); plugin_dispatch_values (&vl); }
static void irq_submit (const char *irq_name, derive_t value) { value_t values[1]; value_list_t vl = VALUE_LIST_INIT; if (ignorelist_match (ignorelist, irq_name) != 0) return; values[0].derive = value; vl.values = values; vl.values_len = 1; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "irq", sizeof (vl.plugin)); sstrncpy (vl.type, "irq", sizeof (vl.type)); sstrncpy (vl.type_instance, irq_name, sizeof (vl.type_instance)); plugin_dispatch_values (&vl); } /* void irq_submit */
static void if_submit_rate(const char *dev, const char *type, derive_t rate) { value_t values[1]; value_list_t vl = VALUE_LIST_INIT; if (ignorelist_match (ignorelist, dev) != 0) return; values[0].derive = rate; vl.values = values; vl.values_len = 1; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "interface", sizeof (vl.plugin)); sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance)); sstrncpy (vl.type, type, sizeof (vl.type)); plugin_dispatch_values (&vl); }
static void submit_io_time (char const *plugin_instance, derive_t io_time, derive_t weighted_time) { value_t values[2]; value_list_t vl = VALUE_LIST_INIT; if (ignorelist_match (ignorelist, plugin_instance) != 0) return; values[0].derive = io_time; values[1].derive = weighted_time; vl.values = values; vl.values_len = 2; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "disk", sizeof (vl.plugin)); sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); sstrncpy (vl.type, "disk_io_time", sizeof (vl.type)); plugin_dispatch_values (&vl); }
static void if_submit (const char *dev, const char *type, unsigned long long rx, unsigned long long tx) { value_t values[2]; value_list_t vl = VALUE_LIST_INIT; if (ignorelist_match (ignorelist, dev) != 0) return; values[0].counter = rx; values[1].counter = tx; vl.values = values; vl.values_len = 2; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "interface", sizeof (vl.plugin)); sstrncpy (vl.type, type, sizeof (vl.type)); sstrncpy (vl.type_instance, dev, sizeof (vl.type_instance)); plugin_dispatch_values (&vl); } /* void if_submit */
static void disk_submit (const char *plugin_instance, const char *type, derive_t read, derive_t write) { value_t values[2]; value_list_t vl = VALUE_LIST_INIT; /* Both `ignorelist' and `plugin_instance' may be NULL. */ if (ignorelist_match (ignorelist, plugin_instance) != 0) return; values[0].derive = read; values[1].derive = write; vl.values = values; vl.values_len = 2; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "disk", sizeof (vl.plugin)); sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); sstrncpy (vl.type, type, sizeof (vl.type)); plugin_dispatch_values (&vl); } /* void disk_submit */
static int refresh_lists (void) { int n; n = virConnectNumOfDomains (conn); if (n < 0) { VIRT_ERROR (conn, "reading number of domains"); return -1; } if (n > 0) { int i; int *domids; /* Get list of domains. */ domids = malloc (sizeof (*domids) * n); if (domids == NULL) { ERROR (PLUGIN_NAME " plugin: malloc failed."); return -1; } n = virConnectListDomains (conn, domids, n); if (n < 0) { VIRT_ERROR (conn, "reading list of domains"); sfree (domids); return -1; } free_block_devices (); free_interface_devices (); free_domains (); /* Fetch each domain and add it to the list, unless ignore. */ for (i = 0; i < n; ++i) { virDomainPtr dom = NULL; const char *name; char *xml = NULL; xmlDocPtr xml_doc = NULL; xmlXPathContextPtr xpath_ctx = NULL; xmlXPathObjectPtr xpath_obj = NULL; int j; dom = virDomainLookupByID (conn, domids[i]); if (dom == NULL) { VIRT_ERROR (conn, "virDomainLookupByID"); /* Could be that the domain went away -- ignore it anyway. */ continue; } name = virDomainGetName (dom); if (name == NULL) { VIRT_ERROR (conn, "virDomainGetName"); goto cont; } if (il_domains && ignorelist_match (il_domains, name) != 0) goto cont; if (add_domain (dom) < 0) { ERROR (PLUGIN_NAME " plugin: malloc failed."); goto cont; } /* Get a list of devices for this domain. */ xml = virDomainGetXMLDesc (dom, 0); if (!xml) { VIRT_ERROR (conn, "virDomainGetXMLDesc"); goto cont; } /* Yuck, XML. Parse out the devices. */ xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET); if (xml_doc == NULL) { VIRT_ERROR (conn, "xmlReadDoc"); goto cont; } xpath_ctx = xmlXPathNewContext (xml_doc); /* Block devices. */ xpath_obj = xmlXPathEval ((xmlChar *) "/domain/devices/disk/target[@dev]", xpath_ctx); if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET || xpath_obj->nodesetval == NULL) goto cont; for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) { xmlNodePtr node; char *path = NULL; node = xpath_obj->nodesetval->nodeTab[j]; if (!node) continue; path = (char *) xmlGetProp (node, (xmlChar *) "dev"); if (!path) continue; if (il_block_devices && ignore_device_match (il_block_devices, name, path) != 0) goto cont2; add_block_device (dom, path); cont2: if (path) xmlFree (path); } xmlXPathFreeObject (xpath_obj); /* Network interfaces. */ xpath_obj = xmlXPathEval ((xmlChar *) "/domain/devices/interface[target[@dev]]", xpath_ctx); if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET || xpath_obj->nodesetval == NULL) goto cont; xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval; for (j = 0; j < xml_interfaces->nodeNr; ++j) { char *path = NULL; char *address = NULL; xmlNodePtr xml_interface; xml_interface = xml_interfaces->nodeTab[j]; if (!xml_interface) continue; xmlNodePtr child = NULL; for (child = xml_interface->children; child; child = child->next) { if (child->type != XML_ELEMENT_NODE) continue; if (xmlStrEqual(child->name, (const xmlChar *) "target")) { path = (char *) xmlGetProp (child, (const xmlChar *) "dev"); if (!path) continue; } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) { address = (char *) xmlGetProp (child, (const xmlChar *) "address"); if (!address) continue; } } if (il_interface_devices && (ignore_device_match (il_interface_devices, name, path) != 0 || ignore_device_match (il_interface_devices, name, address) != 0)) goto cont3; add_interface_device (dom, path, address, j+1); cont3: if (path) xmlFree (path); if (address) xmlFree (address); } cont: if (xpath_obj) xmlXPathFreeObject (xpath_obj); if (xpath_ctx) xmlXPathFreeContext (xpath_ctx); if (xml_doc) xmlFreeDoc (xml_doc); sfree (xml); } sfree (domids); } return 0; }
static int df_read (void) { #if HAVE_STATVFS struct statvfs statbuf; #elif HAVE_STATFS struct statfs statbuf; #endif /* struct STATANYFS statbuf; */ cu_mount_t *mnt_list; cu_mount_t *mnt_ptr; unsigned long long blocksize; gauge_t df_free; gauge_t df_used; char mnt_name[256]; mnt_list = NULL; if (cu_mount_getlist (&mnt_list) == NULL) return (-1); for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next) { if (STATANYFS (mnt_ptr->dir, &statbuf) < 0) { char errbuf[1024]; ERROR ("statv?fs failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); continue; } if (!statbuf.f_blocks) continue; blocksize = BLOCKSIZE(statbuf); df_free = statbuf.f_bfree * blocksize; df_used = (statbuf.f_blocks - statbuf.f_bfree) * blocksize; if (strcmp (mnt_ptr->dir, "/") == 0) { sstrncpy (mnt_name, "root", sizeof (mnt_name)); } else { int i, len; sstrncpy (mnt_name, mnt_ptr->dir + 1, sizeof (mnt_name)); len = strlen (mnt_name); for (i = 0; i < len; i++) if (mnt_name[i] == '/') mnt_name[i] = '-'; } if (ignorelist_match (il_device, (mnt_ptr->spec_device != NULL) ? mnt_ptr->spec_device : mnt_ptr->device)) continue; if (ignorelist_match (il_mountpoint, mnt_ptr->dir)) continue; if (ignorelist_match (il_fstype, mnt_ptr->type)) continue; df_submit (mnt_name, df_used, df_free); } cu_mount_freelist (mnt_list); return (0); } /* int df_read */
static void smart_handle_disk (const char *dev) { SkDisk *d = NULL; SkBool awake = FALSE; SkBool available = FALSE; const char *shortname; const SkSmartParsedData *spd; uint64_t poweron, powercycles, badsectors, temperature; shortname = strrchr(dev, '/'); if (!shortname) return; shortname++; if (ignorelist_match (ignorelist, shortname) != 0) { INFO ("smart plugin: ignoring %s.", dev); return; } INFO ("smart plugin: checking SMART status of %s.", dev); if (sk_disk_open (dev, &d) < 0) { ERROR ("smart plugin: unable to open %s.", dev); return; } if (sk_disk_identify_is_available (d, &available) < 0 || !available) { INFO ("smart plugin: disk %s cannot be identified.", dev); /* goto end;*/ } if (sk_disk_smart_is_available (d, &available) < 0 || !available) { INFO ("smart plugin: disk %s has no SMART support.", dev); goto end; } if (sk_disk_check_sleep_mode (d, &awake) < 0 || !awake) { INFO ("smart plugin: disk %s is sleeping.", dev); goto end; } if (sk_disk_smart_read_data (d) < 0) { ERROR ("smart plugin: unable to get SMART data for disk %s.", dev); goto end; } if (sk_disk_smart_parse (d, &spd) < 0) { ERROR ("smart plugin: unable to parse SMART data for disk %s.", dev); goto end; } /* Get some specific values */ if (sk_disk_smart_get_power_on (d, &poweron) < 0) { WARNING ("smart plugin: unable to get milliseconds since power on for %s.", dev); } else smart_submit (shortname, "smart_poweron", "", poweron / 1000.); if (sk_disk_smart_get_power_cycle (d, &powercycles) < 0) { WARNING ("smart plugin: unable to get number of power cycles for %s.", dev); } else smart_submit (shortname, "smart_powercycles", "", powercycles); if (sk_disk_smart_get_bad (d, &badsectors) < 0) { WARNING ("smart plugin: unable to get number of bad sectors for %s.", dev); } else smart_submit (shortname, "smart_badsectors", "", badsectors); if (sk_disk_smart_get_temperature (d, &temperature) < 0) { WARNING ("smart plugin: unable to get temperature for %s.", dev); } else smart_submit (shortname, "smart_temperature", "", temperature / 1000. - 273.15); /* Grab all attributes */ if (sk_disk_smart_parse_attributes(d, smart_handle_disk_attribute, (char *)shortname) < 0) { ERROR ("smart plugin: unable to handle SMART attributes for %s.", dev); } end: sk_disk_free(d); }
static int df_read (void) { #if HAVE_STATVFS struct statvfs statbuf; #elif HAVE_STATFS struct statfs statbuf; #endif /* struct STATANYFS statbuf; */ cu_mount_t *mnt_list; cu_mount_t *mnt_ptr; mnt_list = NULL; if (cu_mount_getlist (&mnt_list) == NULL) { ERROR ("df plugin: cu_mount_getlist failed."); return (-1); } for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next) { unsigned long long blocksize; char disk_name[256]; uint64_t blk_free; uint64_t blk_reserved; uint64_t blk_used; char const *dev = (mnt_ptr->spec_device != NULL) ? mnt_ptr->spec_device : mnt_ptr->device; if (ignorelist_match (il_device, dev)) continue; if (ignorelist_match (il_mountpoint, mnt_ptr->dir)) continue; if (ignorelist_match (il_fstype, mnt_ptr->type)) continue; if (STATANYFS (mnt_ptr->dir, &statbuf) < 0) { char errbuf[1024]; ERROR (STATANYFS_STR"(%s) failed: %s", mnt_ptr->dir, sstrerror (errno, errbuf, sizeof (errbuf))); continue; } if (!statbuf.f_blocks) continue; if (by_device) { /* eg, /dev/hda1 -- strip off the "/dev/" */ if (strncmp (dev, "/dev/", strlen ("/dev/")) == 0) sstrncpy (disk_name, dev + strlen ("/dev/"), sizeof (disk_name)); else sstrncpy (disk_name, dev, sizeof (disk_name)); if (strlen(disk_name) < 1) { DEBUG("df: no device name for mountpoint %s, skipping", mnt_ptr->dir); continue; } } else { if (strcmp (mnt_ptr->dir, "/") == 0) { if (strcmp (mnt_ptr->type, "rootfs") == 0) continue; sstrncpy (disk_name, "root", sizeof (disk_name)); } else { int i, len; sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name)); len = strlen (disk_name); for (i = 0; i < len; i++) if (disk_name[i] == '/') disk_name[i] = '-'; } } blocksize = BLOCKSIZE(statbuf); /* * Sanity-check for the values in the struct */ /* Check for negative "available" byes. For example UFS can * report negative free space for user. Notice. blk_reserved * will start to diminish after this. */ #if HAVE_STATVFS /* Cast and temporary variable are needed to avoid * compiler warnings. * ((struct statvfs).f_bavail is unsigned (POSIX)) */ int64_t signed_bavail = (int64_t) statbuf.f_bavail; if (signed_bavail < 0) statbuf.f_bavail = 0; #elif HAVE_STATFS if (statbuf.f_bavail < 0) statbuf.f_bavail = 0; #endif /* Make sure that f_blocks >= f_bfree >= f_bavail */ if (statbuf.f_bfree < statbuf.f_bavail) statbuf.f_bfree = statbuf.f_bavail; if (statbuf.f_blocks < statbuf.f_bfree) statbuf.f_blocks = statbuf.f_bfree; blk_free = (uint64_t) statbuf.f_bavail; blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail); blk_used = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree); if (values_absolute) { df_submit_one (disk_name, "df_complex", "free", (gauge_t) (blk_free * blocksize)); df_submit_one (disk_name, "df_complex", "reserved", (gauge_t) (blk_reserved * blocksize)); df_submit_one (disk_name, "df_complex", "used", (gauge_t) (blk_used * blocksize)); } if (values_percentage) { if (statbuf.f_blocks > 0) { df_submit_one (disk_name, "percent_bytes", "free", (gauge_t) ((float_t)(blk_free) / statbuf.f_blocks * 100)); df_submit_one (disk_name, "percent_bytes", "reserved", (gauge_t) ((float_t)(blk_reserved) / statbuf.f_blocks * 100)); df_submit_one (disk_name, "percent_bytes", "used", (gauge_t) ((float_t)(blk_used) / statbuf.f_blocks * 100)); } else return (-1); } /* inode handling */ if (report_inodes && statbuf.f_files != 0 && statbuf.f_ffree != 0) { uint64_t inode_free; uint64_t inode_reserved; uint64_t inode_used; /* Sanity-check for the values in the struct */ if (statbuf.f_ffree < statbuf.f_favail) statbuf.f_ffree = statbuf.f_favail; if (statbuf.f_files < statbuf.f_ffree) statbuf.f_files = statbuf.f_ffree; inode_free = (uint64_t) statbuf.f_favail; inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail); inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree); if (values_percentage) { if (statbuf.f_files > 0) { df_submit_one (disk_name, "percent_inodes", "free", (gauge_t) ((float_t)(inode_free) / statbuf.f_files * 100)); df_submit_one (disk_name, "percent_inodes", "reserved", (gauge_t) ((float_t)(inode_reserved) / statbuf.f_files * 100)); df_submit_one (disk_name, "percent_inodes", "used", (gauge_t) ((float_t)(inode_used) / statbuf.f_files * 100)); } else return (-1); } if (values_absolute) { df_submit_one (disk_name, "df_inodes", "free", (gauge_t) inode_free); df_submit_one (disk_name, "df_inodes", "reserved", (gauge_t) inode_reserved); df_submit_one (disk_name, "df_inodes", "used", (gauge_t) inode_used); } } } cu_mount_freelist (mnt_list); return (0); } /* int df_read */
/* * This callback reads the user/system CPU time for each cgroup. */ static int read_cpuacct_procs(const char *dirname, char const *cgroup_name, void *user_data) { char abs_path[PATH_MAX]; struct stat statbuf; char buf[1024]; int status; FILE *fh; if (ignorelist_match(il_cgroup, cgroup_name)) return (0); ssnprintf(abs_path, sizeof(abs_path), "%s/%s", dirname, cgroup_name); status = lstat(abs_path, &statbuf); if (status != 0) { ERROR("cgroups plugin: stat (\"%s\") failed.", abs_path); return (-1); } /* We are only interested in directories, so skip everything else. */ if (!S_ISDIR(statbuf.st_mode)) return (0); ssnprintf(abs_path, sizeof(abs_path), "%s/%s/cpuacct.stat", dirname, cgroup_name); fh = fopen(abs_path, "r"); if (fh == NULL) { char errbuf[1024]; ERROR("cgroups plugin: fopen (\"%s\") failed: %s", abs_path, sstrerror(errno, errbuf, sizeof(errbuf))); return (-1); } while (fgets(buf, sizeof(buf), fh) != NULL) { char *fields[8]; int numfields = 0; char *key; size_t key_len; value_t value; /* Expected format: * * user: 12345 * system: 23456 * * Or: * * user 12345 * system 23456 */ strstripnewline(buf); numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE(fields)); if (numfields != 2) continue; key = fields[0]; key_len = strlen(key); if (key_len < 2) continue; /* Strip colon off the first column, if found */ if (key[key_len - 1] == ':') key[key_len - 1] = 0; status = parse_value(fields[1], &value, DS_TYPE_DERIVE); if (status != 0) continue; cgroups_submit_one(cgroup_name, key, value); } fclose(fh); return (0); } /* int read_cpuacct_procs */
static int df_read (void) { #if HAVE_STATVFS struct statvfs statbuf; #elif HAVE_STATFS struct statfs statbuf; #endif /* struct STATANYFS statbuf; */ cu_mount_t *mnt_list; cu_mount_t *mnt_ptr; mnt_list = NULL; if (cu_mount_getlist (&mnt_list) == NULL) { ERROR ("df plugin: cu_mount_getlist failed."); return (-1); } for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next) { unsigned long long blocksize; char disk_name[256]; uint64_t blk_free; uint64_t blk_reserved; uint64_t blk_used; if (ignorelist_match (il_device, (mnt_ptr->spec_device != NULL) ? mnt_ptr->spec_device : mnt_ptr->device)) continue; if (ignorelist_match (il_mountpoint, mnt_ptr->dir)) continue; if (ignorelist_match (il_fstype, mnt_ptr->type)) continue; if (STATANYFS (mnt_ptr->dir, &statbuf) < 0) { char errbuf[1024]; ERROR ("statv?fs failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); continue; } if (!statbuf.f_blocks) continue; if (by_device) { /* eg, /dev/hda1 -- strip off the "/dev/" */ if (strncmp (mnt_ptr->spec_device, "/dev/", strlen ("/dev/")) == 0) sstrncpy (disk_name, mnt_ptr->spec_device + strlen ("/dev/"), sizeof (disk_name)); else sstrncpy (disk_name, mnt_ptr->spec_device, sizeof (disk_name)); if (strlen(disk_name) < 1) { DEBUG("df: no device name name for mountpoint %s, skipping", mnt_ptr->dir); continue; } } else { if (strcmp (mnt_ptr->dir, "/") == 0) { sstrncpy (disk_name, "root", sizeof (disk_name)); } else { int i, len; sstrncpy (disk_name, mnt_ptr->dir + 1, sizeof (disk_name)); len = strlen (disk_name); for (i = 0; i < len; i++) if (disk_name[i] == '/') disk_name[i] = '-'; } } blocksize = BLOCKSIZE(statbuf); /* Sanity-check for the values in the struct */ if (statbuf.f_bfree < statbuf.f_bavail) statbuf.f_bfree = statbuf.f_bavail; if (statbuf.f_blocks < statbuf.f_bfree) statbuf.f_blocks = statbuf.f_bfree; blk_free = (uint64_t) statbuf.f_bavail; blk_reserved = (uint64_t) (statbuf.f_bfree - statbuf.f_bavail); blk_used = (uint64_t) (statbuf.f_blocks - statbuf.f_bfree); df_submit_one (disk_name, "df_complex", "free", (gauge_t) (blk_free * blocksize)); df_submit_one (disk_name, "df_complex", "reserved", (gauge_t) (blk_reserved * blocksize)); df_submit_one (disk_name, "df_complex", "used", (gauge_t) (blk_used * blocksize)); /* inode handling */ if (report_inodes) { uint64_t inode_free; uint64_t inode_reserved; uint64_t inode_used; /* Sanity-check for the values in the struct */ if (statbuf.f_ffree < statbuf.f_favail) statbuf.f_ffree = statbuf.f_favail; if (statbuf.f_files < statbuf.f_ffree) statbuf.f_files = statbuf.f_ffree; inode_free = (uint64_t) statbuf.f_favail; inode_reserved = (uint64_t) (statbuf.f_ffree - statbuf.f_favail); inode_used = (uint64_t) (statbuf.f_files - statbuf.f_ffree); df_submit_one (disk_name, "df_inodes", "free", (gauge_t) inode_free); df_submit_one (disk_name, "df_inodes", "reserved", (gauge_t) inode_reserved); df_submit_one (disk_name, "df_inodes", "used", (gauge_t) inode_used); } } cu_mount_freelist (mnt_list); return (0); } /* int df_read */
static int read_file(const char *path) { FILE *fh; char key_buffer[4096]; char value_buffer[4096]; char *key_ptr; char *value_ptr; char *key_fields[256]; char *value_fields[256]; int key_fields_num; int value_fields_num; int status; int i; fh = fopen(path, "r"); if (fh == NULL) { ERROR("protocols plugin: fopen (%s) failed: %s.", path, sstrerror(errno, key_buffer, sizeof(key_buffer))); return (-1); } status = -1; while (42) { clearerr(fh); key_ptr = fgets(key_buffer, sizeof(key_buffer), fh); if (key_ptr == NULL) { if (feof(fh) != 0) { status = 0; break; } else if (ferror(fh) != 0) { ERROR("protocols plugin: Reading from %s failed.", path); break; } else { ERROR("protocols plugin: fgets failed for an unknown reason."); break; } } /* if (key_ptr == NULL) */ value_ptr = fgets(value_buffer, sizeof(value_buffer), fh); if (value_ptr == NULL) { ERROR("protocols plugin: read_file (%s): Could not read values line.", path); break; } key_ptr = strchr(key_buffer, ':'); if (key_ptr == NULL) { ERROR("protocols plugin: Could not find protocol name in keys line."); break; } *key_ptr = 0; key_ptr++; value_ptr = strchr(value_buffer, ':'); if (value_ptr == NULL) { ERROR("protocols plugin: Could not find protocol name " "in values line."); break; } *value_ptr = 0; value_ptr++; if (strcmp(key_buffer, value_buffer) != 0) { ERROR("protocols plugin: Protocol names in keys and values lines " "don't match: `%s' vs. `%s'.", key_buffer, value_buffer); break; } key_fields_num = strsplit(key_ptr, key_fields, STATIC_ARRAY_SIZE(key_fields)); value_fields_num = strsplit(value_ptr, value_fields, STATIC_ARRAY_SIZE(value_fields)); if (key_fields_num != value_fields_num) { ERROR("protocols plugin: Number of fields in keys and values lines " "don't match: %i vs %i.", key_fields_num, value_fields_num); break; } for (i = 0; i < key_fields_num; i++) { if (values_list != NULL) { char match_name[2 * DATA_MAX_NAME_LEN]; ssnprintf(match_name, sizeof(match_name), "%s:%s", key_buffer, key_fields[i]); if (ignorelist_match(values_list, match_name)) continue; } /* if (values_list != NULL) */ submit(key_buffer, key_fields[i], value_fields[i]); } /* for (i = 0; i < key_fields_num; i++) */ } /* while (42) */ fclose(fh); return (status); } /* int read_file */
static int cow_read_values (const char *path, const char *name, const ow_family_features_t *family_info) { value_t values[1]; value_list_t vl = VALUE_LIST_INIT; int success = 0; if (sensor_list != NULL) { DEBUG ("onewire plugin: Checking ignorelist for `%s'", name); if (ignorelist_match (sensor_list, name) != 0) return 0; } vl.values = values; vl.values_len = 1; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "onewire", sizeof (vl.plugin)); sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance)); for (size_t i = 0; i < family_info->features_num; i++) { char *buffer; size_t buffer_size; int status; char file[4096]; char *endptr; snprintf (file, sizeof (file), "%s/%s", path, family_info->features[i].filename); file[sizeof (file) - 1] = 0; buffer = NULL; buffer_size = 0; DEBUG ("Start reading onewire device %s", file); status = OW_get (file, &buffer, &buffer_size); if (status < 0) { ERROR ("onewire plugin: OW_get (%s/%s) failed. status = %#x;", path, family_info->features[i].filename, status); return (-1); } DEBUG ("Read onewire device %s as %s", file, buffer); endptr = NULL; values[0].gauge = strtod (buffer, &endptr); if (endptr == NULL) { ERROR ("onewire plugin: Buffer is not a number: %s", buffer); continue; } sstrncpy (vl.type, family_info->features[i].type, sizeof (vl.type)); sstrncpy (vl.type_instance, family_info->features[i].type_instance, sizeof (vl.type_instance)); plugin_dispatch_values (&vl); success++; free (buffer); } /* for (i = 0; i < features_num; i++) */ return ((success > 0) ? 0 : -1); } /* int cow_read_values */