Пример #1
0
      /* Ready to send data to the server. */
      debug_msg("carbon proxy:: %s is ready to receive",gmetad_config.carbon_server);
      nbytes = write (carbon_socket, graphite_msg, strlen(graphite_msg) + 1);
      if (nbytes < 0) {
        err_msg("write: %s", strerror(errno));
        close(carbon_socket);
        return EXIT_FAILURE;
      }
    } else if ( carbon_struct_poll.revents & POLLHUP ) {
      debug_msg("carbon proxy:: Recvd an RST from %s during transmission",gmetad_config.carbon_server);
     close(carbon_socket);
     return EXIT_FAILURE;
    } else if ( carbon_struct_poll.revents & POLLERR ) {
      debug_msg("carbon proxy:: Recvd an POLLERR from %s during transmission",gmetad_config.carbon_server);
      close(carbon_socket);
      return EXIT_FAILURE;
    }
  }
  close (carbon_socket);
  return EXIT_SUCCESS;
}

#ifdef WITH_MEMCACHED
#define MEMCACHED_MAX_KEY_LENGTH 250 /* Maximum allowed by memcached */
int
write_data_to_memcached ( const char *cluster, const char *host, const char *metric, 
                    const char *sum, unsigned int process_time, unsigned int expiry )
{
   time_t expiry_time;
   char s_path[MEMCACHED_MAX_KEY_LENGTH];
   if (strlen(cluster) + strlen(host) + strlen(metric) + 3 > MEMCACHED_MAX_KEY_LENGTH) {
      debug_msg("Cluster + host + metric + 3 > %d", MEMCACHED_MAX_KEY_LENGTH);
      return EXIT_FAILURE;
   }
   sprintf(s_path, "%s/%s/%s", cluster, host, metric);

   if (expiry != 0) {
      expiry_time = expiry;
   } else {
      expiry_time = (time_t) 0;
   }

   memcached_return_t rc;
   memcached_st *memc = memcached_pool_pop(memcached_connection_pool, false, &rc);
   if (rc != MEMCACHED_SUCCESS) {
      debug_msg("Unable to retrieve a memcached connection from the pool");
      return EXIT_FAILURE;
   }
   rc = memcached_set(memc, s_path, strlen(s_path), sum, strlen(sum), expiry_time, (uint32_t)0);
   if (rc != MEMCACHED_SUCCESS) {
      debug_msg("Unable to push %s value %s to the memcached server(s) - %s", s_path, sum, memcached_strerror(memc, rc));
      memcached_pool_push(memcached_connection_pool, memc);
      return EXIT_FAILURE;
   } else {
      debug_msg("Pushed %s value %s to the memcached server(s)", s_path, sum);
      memcached_pool_push(memcached_connection_pool, memc);
      return EXIT_SUCCESS;
   }
}
Пример #2
0
          /* Ready to send data to the server. */
          debug_msg("carbon proxy:: %s is ready to receive",gmetad_config.carbon_server);
          nbytes = write (carbon_socket, graphite_msg, strlen(graphite_msg) + 1);
          if (nbytes < 0) {
            err_msg("write: %s", strerror(errno));
            close(carbon_socket);
            return EXIT_FAILURE;
          }
        } else if ( carbon_struct_poll.revents & POLLHUP ) {
          debug_msg("carbon proxy:: Recvd an RST from %s during transmission",gmetad_config.carbon_server);
         close(carbon_socket);
         return EXIT_FAILURE;
        } else if ( carbon_struct_poll.revents & POLLERR ) {
          debug_msg("carbon proxy:: Recvd an POLLERR from %s during transmission",gmetad_config.carbon_server);
          close(carbon_socket);
          return EXIT_FAILURE;
        }
      }
      close (carbon_socket);
      ganglia_scoreboard_inc(METS_SENT_GRAPHITE);
      ganglia_scoreboard_inc(METS_SENT_ALL);
      return EXIT_SUCCESS;

  } else {

      int nbytes;

      pthread_mutex_lock( &carbon_mutex );
      nbytes = sendto (carbon_udp_socket->sockfd, graphite_msg, strlen(graphite_msg), 0,
                         (struct sockaddr_in*)&carbon_udp_socket->sa, sizeof (struct sockaddr_in));
      pthread_mutex_unlock( &carbon_mutex );

      if (nbytes != strlen(graphite_msg))
      {
             err_msg("sendto socket (client): %s", strerror(errno));
             return EXIT_FAILURE;
      }
      ganglia_scoreboard_inc(METS_SENT_GRAPHITE);
      ganglia_scoreboard_inc(METS_SENT_ALL);
      return EXIT_SUCCESS;
  }
}

#ifdef WITH_MEMCACHED
#define MEMCACHED_MAX_KEY_LENGTH 250 /* Maximum allowed by memcached */
int
write_data_to_memcached ( const char *cluster, const char *host, const char *metric, 
                    const char *sum, unsigned int process_time, unsigned int expiry )
{
   apr_time_t start = apr_time_now(), now;
   time_t expiry_time;
   char s_path[MEMCACHED_MAX_KEY_LENGTH];
   
   // Check whether we are including cluster in the memcached key
   if ( gmetad_config.memcached_include_cluster_in_key ) {
      if (strlen(cluster) + strlen(host) + strlen(metric) + 3 > MEMCACHED_MAX_KEY_LENGTH) {
	  debug_msg("Cluster + host + metric + 3 > %d", MEMCACHED_MAX_KEY_LENGTH);
	  return EXIT_FAILURE;
      }
      sprintf(s_path, "%s/%s/%s", cluster, host, metric);
    } else {
      if ( strlen(host) + strlen(metric) + 3 > MEMCACHED_MAX_KEY_LENGTH) {
	  debug_msg("host + metric + 3 > %d", MEMCACHED_MAX_KEY_LENGTH);
	  return EXIT_FAILURE;
      }
      sprintf(s_path, "%s/%s", host, metric);     
   }
    
    
   if (expiry != 0) {
      expiry_time = expiry;
   } else {
      expiry_time = (time_t) 0;
   }

   memcached_return_t rc;
   memcached_st *memc = memcached_pool_pop(memcached_connection_pool, false, &rc);
   if (rc != MEMCACHED_SUCCESS) {
      debug_msg("Unable to retrieve a memcached connection from the pool");
      return EXIT_FAILURE;
   }
   rc = memcached_set(memc, s_path, strlen(s_path), sum, strlen(sum), expiry_time, (uint32_t)0);
   if (rc != MEMCACHED_SUCCESS) {
      debug_msg("Unable to push %s value %s to the memcached server(s) - %s", s_path, sum, memcached_strerror(memc, rc));
      memcached_pool_push(memcached_connection_pool, memc);
      return EXIT_FAILURE;
   } else {
      debug_msg("Pushed %s value %s to the memcached server(s)", s_path, sum);
      memcached_pool_push(memcached_connection_pool, memc);
      now = apr_time_now();
      last_memcached = now;  //Updating global variable
      ganglia_scoreboard_inc(METS_SENT_MEMCACHED);
      ganglia_scoreboard_inc(METS_SENT_ALL);
      ganglia_scoreboard_incby(METS_ALL_DURATION, now - start);
      ganglia_scoreboard_incby(METS_MEMCACHED_DURATION, now - start);
      return EXIT_SUCCESS;
   }
}
Пример #3
0
void ESPMemCached::connect()
{
    assertPool();
#if (LIBMEMCACHED_VERSION_HEX<0x53000)
    if (connection)
        memcached_pool_push(pool, connection);
    memcached_return_t rc;
    connection = memcached_pool_pop(pool, (struct timespec *)0 , &rc);
#else
    if (connection)
        memcached_pool_release(pool, connection);
    memcached_return_t rc;
    connection = memcached_pool_fetch(pool, (struct timespec *)0 , &rc);
#endif
    assertOnError(rc, "memcached_pool_pop failed - ");
}