static int parse_identifier(lcc_connection_t *c, const char *value,
                            lcc_identifier_t *ident) {
    char hostname[1024];
    char ident_str[1024] = "";
    int n_slashes;

    int status;

    n_slashes = count_chars(value, '/');
    if (n_slashes == 1) {
        /* The user has omitted the hostname part of the identifier
         * (there is only one '/' in the identifier)
         * Let's add the local hostname */
        if (gethostname(hostname, sizeof(hostname)) != 0) {
            fprintf(stderr, "ERROR: Failed to get local hostname: %s",
                    strerror(errno));
            return (-1);
        }
        hostname[sizeof(hostname) - 1] = '\0';

        snprintf(ident_str, sizeof(ident_str), "%s/%s", hostname, value);
        ident_str[sizeof(ident_str) - 1] = '\0';
    } else {
        strncpy(ident_str, value, sizeof(ident_str));
        ident_str[sizeof(ident_str) - 1] = '\0';
    }

    status = lcc_string_to_identifier(c, ident, ident_str);
    if (status != 0) {
        fprintf(stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
                ident_str, lcc_strerror(c));
        return (-1);
    }
    return (0);
} /* parse_identifier */
Example #2
0
static int do_check (lcc_connection_t *connection)
{
	gauge_t *values;
	char   **values_names;
	size_t   values_num;
	char ident_str[1024];
	lcc_identifier_t ident;
	size_t i;
	int status;

	snprintf (ident_str, sizeof (ident_str), "%s/%s",
			hostname_g, value_string_g);
	ident_str[sizeof (ident_str) - 1] = 0;

	memset (&ident, 0, sizeof (ident));
	status = lcc_string_to_identifier (connection, &ident, ident_str);
	if (status != 0)
	{
		printf ("ERROR: Creating an identifier failed: %s.\n",
				lcc_strerror (connection));
		LCC_DESTROY (connection);
		return (RET_CRITICAL);
	}

	status = lcc_getval (connection, &ident,
			&values_num, &values, &values_names);
	if (status != 0)
	{
		printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
				lcc_strerror (connection));
		LCC_DESTROY (connection);
		return (RET_CRITICAL);
	}

	LCC_DESTROY (connection);

	status = filter_ds (&values_num, &values, &values_names);
	if (status != RET_OKAY)
		return (status);

	status = RET_UNKNOWN;
	if (consolitation_g == CON_NONE)
		status =  do_check_con_none (values_num, values, values_names);
	else if (consolitation_g == CON_AVERAGE)
		status =  do_check_con_average (values_num, values, values_names);
	else if (consolitation_g == CON_SUM)
		status = do_check_con_sum (values_num, values, values_names);
	else if (consolitation_g == CON_PERCENTAGE)
		status = do_check_con_percentage (values_num, values, values_names);

	free (values);
	if (values_names != NULL)
		for (i = 0; i < values_num; i++)
			free (values_names[i]);
	free (values_names);

	return (status);
} /* int do_check */
Example #3
0
static int flush (
    const char *address,
    const char *plugin,
    const char *ident_str,
    int timeout)
{
  lcc_connection_t *connection;
  lcc_identifier_t ident;

  /* Pointer which is passed to lcc_flush.
   * Either a null pointer or it points to ident */
  lcc_identifier_t *identp;
  int status;

  connection = NULL;
  status = lcc_connect(address, &connection);
  if (status != 0) {
    fprintf (stderr, "ERROR: Connecting to daemon at %s failed: %s.\n",
        address, strerror (errno));
    return 1;
  }

  identp = NULL;
  if (ident_str != NULL && *ident_str != '\0') {
    status = lcc_string_to_identifier (connection, &ident, ident_str);
    if (status != 0) {
      fprintf (stderr, "ERROR: Creating and identifier failed: %s.\n",
          lcc_strerror(connection));
      LCC_DESTROY (connection);

      return 1;
    }
    identp = &ident;
  }

  status = lcc_flush (connection, plugin, identp, timeout);
  if (status != 0) {
    fprintf (stderr, "ERROR: Flushing failed: %s.\n",
        lcc_strerror (connection));
    LCC_DESTROY (connection);

    return 1;
  }

  LCC_DESTROY (connection);

  return 0;
}
Example #4
0
int lcc_listval (lcc_connection_t *c, /* {{{ */
    lcc_identifier_t **ret_ident, size_t *ret_ident_num)
{
  lcc_response_t res;
  size_t i;
  int status;

  lcc_identifier_t *ident;
  size_t ident_num;

  if (c == NULL)
    return (-1);

  if ((ret_ident == NULL) || (ret_ident_num == NULL))
  {
    lcc_set_errno (c, EINVAL);
    return (-1);
  }

  status = lcc_sendreceive (c, "LISTVAL", &res);
  if (status != 0)
    return (status);

  if (res.status != 0)
  {
    LCC_SET_ERRSTR (c, "Server error: %s", res.message);
    lcc_response_free (&res);
    return (-1);
  }

  ident_num = res.lines_num;
  ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
  if (ident == NULL)
  {
    lcc_response_free (&res);
    lcc_set_errno (c, ENOMEM);
    return (-1);
  }

  for (i = 0; i < res.lines_num; i++)
  {
    char *time_str;
    char *ident_str;

    /* First field is the time. */
    time_str = res.lines[i];

    /* Set `ident_str' to the beginning of the second field. */
    ident_str = time_str;
    while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
      ident_str++;
    while ((*ident_str == ' ') || (*ident_str == '\t'))
    {
      *ident_str = 0;
      ident_str++;
    }

    if (*ident_str == 0)
    {
      lcc_set_errno (c, EILSEQ);
      status = -1;
      break;
    }

    status = lcc_string_to_identifier (c, ident + i, ident_str);
    if (status != 0)
      break;
  }

  lcc_response_free (&res);

  if (status != 0)
  {
    free (ident);
    return (-1);
  }

  *ret_ident = ident;
  *ret_ident_num = ident_num;

  return (0);
} /* }}} int lcc_listval */