コード例 #1
0
ファイル: uuid.c プロジェクト: dago/collectd
static char *uuid_parse_dmidecode(FILE *file) {
  char line[1024];

  while (fgets(line, sizeof(line), file) != NULL) {
    char *fields[4];
    int fields_num;

    strstripnewline(line);

    /* Look for a line reading:
     *   UUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
     */
    fields_num = strsplit(line, fields, STATIC_ARRAY_SIZE(fields));
    if (fields_num != 2)
      continue;

    if (strcmp("UUID:", fields[0]) != 0)
      continue;

    if (!looks_like_a_uuid(fields[1]))
      continue;

    return strdup(fields[1]);
  }
  return NULL;
}
コード例 #2
0
ファイル: battery.c プロジェクト: mleinart/collectd
/* Reads a file which contains only a number (and optionally a trailing
 * newline) and parses that number. */
static int sysfs_file_to_buffer(char const *dir, /* {{{ */
		char const *power_supply,
		char const *basename,
		char *buffer, size_t buffer_size)
{
	int status;
	FILE *fp;
	char filename[PATH_MAX];

	ssnprintf (filename, sizeof (filename), "%s/%s/%s",
			dir, power_supply, basename);

	/* No file isn't the end of the world -- not every system will be
	 * reporting the same set of statistics */
	if (access (filename, R_OK) != 0)
		return ENOENT;

	fp = fopen (filename, "r");
	if (fp == NULL)
	{
		status = errno;
		if (status != ENOENT)
		{
			char errbuf[1024];
			WARNING ("battery plugin: fopen (%s) failed: %s", filename,
					sstrerror (status, errbuf, sizeof (errbuf)));
		}
		return status;
	}

	if (fgets (buffer, buffer_size, fp) == NULL)
	{
		status = errno;
		if (status != ENODEV)
		{
			char errbuf[1024];
			WARNING ("battery plugin: fgets (%s) failed: %s", filename,
					sstrerror (status, errbuf, sizeof (errbuf)));
		}
		fclose (fp);
		return status;
	}

	strstripnewline (buffer);

	fclose (fp);
	return 0;
} /* }}} int sysfs_file_to_buffer */
コード例 #3
0
ファイル: uuid.c プロジェクト: dago/collectd
static char *uuid_get_from_file(const char *path) {
  FILE *file;
  char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1] = "";

  file = fopen(path, "r");
  if (file == NULL)
    return NULL;

  if (!fgets(uuid, sizeof(uuid), file)) {
    fclose(file);
    return NULL;
  }
  fclose(file);
  strstripnewline(uuid);

  return strdup(uuid);
}
コード例 #4
0
ファイル: battery.c プロジェクト: ajdiaz/collectd
/* Reads a file which contains only a number (and optionally a trailing
 * newline) and parses that number. */
static int sysfs_file_to_buffer(char const *dir, /* {{{ */
                                char const *power_supply, char const *basename,
                                char *buffer, size_t buffer_size) {
  char filename[PATH_MAX];
  int status;

  ssnprintf(filename, sizeof(filename), "%s/%s/%s", dir, power_supply,
            basename);

  status = (int)read_file_contents(filename, buffer, buffer_size - 1);
  if (status < 0)
    return status;

  buffer[status] = '\0';

  strstripnewline(buffer);
  return 0;
} /* }}} int sysfs_file_to_buffer */
コード例 #5
0
ファイル: cgroups.c プロジェクト: ajdiaz/collectd
/*
 * 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 */