static int
noit_rabbimq_submit(iep_thread_driver_t *dr,
                    const char *payload, size_t payloadlen) {
  int rv;
  amqp_bytes_t body;
  struct amqp_driver *driver = (struct amqp_driver *)dr;
  const char *routingkey = driver->routingkey;

  body.len = payloadlen;
  body.bytes = (char *)payload;
  if(*payload == 'M' ||
     *payload == 'S' ||
     *payload == 'C' ||
     (*payload == 'H' && payload[1] == '1') ||
     (*payload == 'F' && payload[1] == '1') ||
     (*payload == 'B' && (payload[1] == '1' || payload[1] == '2'))) {
    char uuid_str[32 * 2 + 1];
    int account_id, check_id;
    if(extract_uuid_from_jlog(payload, payloadlen,
                              &account_id, &check_id, uuid_str)) {
      if(*routingkey) {
        char *replace;
        int newlen = strlen(driver->routingkey) + 1 + sizeof(uuid_str) + 2 * 32;
        replace = alloca(newlen);
        snprintf(replace, newlen, "%s.%x.%x.%d.%d%s", driver->routingkey,
                 account_id%16, (account_id/16)%16, account_id,
                 check_id, uuid_str);
        routingkey = replace;
      }
    }
  }
  rv = amqp_basic_publish(driver->connection, 1,
                          amqp_cstring_bytes(driver->exchange),
                          amqp_cstring_bytes(routingkey),
                          1, 0, NULL, body);
  if(rv < 0) {
    mtevL(mtev_error, "AMQP publish failed, disconnecting\n");
    amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
    amqp_destroy_connection(driver->connection);
    if(driver->sockfd >= 0) close(driver->sockfd);
    driver->sockfd = -1;
    driver->connection = NULL;
    return -1;
  }
  BUMPSTAT(publications);
  noit_rabbitmq_heartbeat(driver);
  noit_rabbitmq_read_frame(driver);
  amqp_maybe_release_buffers(driver->connection);
  if(driver->has_error) {
    amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
    amqp_destroy_connection(driver->connection);
    if(driver->sockfd >= 0) close(driver->sockfd);
    driver->sockfd = -1;
    driver->connection = NULL;
    return -1;
  }
  return 0;
}
Exemple #2
0
static int
noit_fq_submit(iep_thread_driver_t *dr,
               const char *payload, size_t payloadlen) {
  int i;
  struct fq_driver *driver = (struct fq_driver *)dr;
  const char *routingkey = driver->routingkey;
  fq_msg *msg;

  if(*payload == 'M' ||
     *payload == 'S' ||
     *payload == 'C' ||
     (*payload == 'B' && (payload[1] == '1' || payload[1] == '2'))) {
    char uuid_str[32 * 2 + 1];
    int account_id, check_id;
    if(extract_uuid_from_jlog(payload, payloadlen,
                              &account_id, &check_id, uuid_str)) {
      if(*routingkey) {
        char *replace;
        int newlen = strlen(driver->routingkey) + 1 + sizeof(uuid_str) + 2 * 32;
        replace = alloca(newlen);
        snprintf(replace, newlen, "%s.%x.%x.%d.%d%s", driver->routingkey,
                 account_id%16, (account_id/16)%16, account_id,
                 check_id, uuid_str);
        routingkey = replace;
      }
    }
  }

  /* Setup our message */
  msg = fq_msg_alloc(payload, payloadlen);
  if(msg == NULL) {
    driver->allocation_failures++;
    return -1;
  }
  driver->msg_cnt++;
  fq_msg_exchange(msg, driver->exchange, strlen(driver->exchange));
  noitL(noit_debug, "route[%s] -> %s\n", driver->exchange, routingkey);
  fq_msg_route(msg, routingkey, strlen(routingkey));
  fq_msg_id(msg, NULL);

  for(i=0; i<driver->nhosts; i++) {
    if(fq_client_publish(driver->client[i], msg) == 1)
      BUMPSTAT(i, publications);
    else
      BUMPSTAT(i, client_tx_drop);
  }
  fq_msg_deref(msg);
  return 0;
}
Exemple #3
0
static int
noit_fq_submit(iep_thread_driver_t *dr,
               const char *payload, size_t payloadlen) {
  int i;
  struct fq_driver *driver = (struct fq_driver *)dr;
  const char *routingkey = driver->routingkey;
  mtev_hash_table *filtered_metrics;
  char uuid_formatted_str[UUID_STR_LEN+1];
  bool is_bundle = false, is_metric = false, send = false;
  fq_msg *msg;
  char *metric = NULL;

  if(*payload == 'M' ||
     *payload == 'S' ||
     *payload == 'C' ||
     (*payload == 'H' && payload[1] == '1') ||
     (*payload == 'F' && payload[1] == '1') ||
     (*payload == 'B' && (payload[1] == '1' || payload[1] == '2'))) {
    char uuid_str[32 * 2 + 1];
    int account_id, check_id;

    if (*payload == 'B') is_bundle = true;
    else if ((*payload == 'H') || (*payload == 'M')) {
      is_metric = true;
    }

    if(extract_uuid_from_jlog(payload, payloadlen,
                              &account_id, &check_id, uuid_str,
                              uuid_formatted_str, &metric)) {
      if(*routingkey) {
        char *replace;
        int newlen = strlen(driver->routingkey) + 1 + sizeof(uuid_str) + 2 * 32;
        replace = alloca(newlen);
        snprintf(replace, newlen, "%s.%x.%x.%d.%d%s", driver->routingkey,
                 account_id%16, (account_id/16)%16, account_id,
                 check_id, uuid_str);
        routingkey = replace;
      }
    }
  }

  /* Let through any messages that aren't metrics or bundles */
  if (!is_bundle && !is_metric) {
    send = true;
  }

  /* Setup our message */
  msg = fq_msg_alloc(payload, payloadlen);
  if(msg == NULL) {
    driver->allocation_failures++;
    if (metric) free(metric);
    return -1;
  }
  driver->msg_cnt++;
  fq_msg_exchange(msg, driver->exchange, strlen(driver->exchange));
  mtevL(mtev_debug, "route[%s] -> %s\n", driver->exchange, routingkey);
  fq_msg_route(msg, routingkey, strlen(routingkey));
  fq_msg_id(msg, NULL);

  if (global_fq_ctx.round_robin) {
    int checked = 0, good = 0;
    time_t cur_time;
    while (1) {
      if (!global_fq_ctx.down_host[global_fq_ctx.round_robin_target]) {
        good = 1;
        break;
      }
      cur_time = time(NULL);
      if (cur_time - global_fq_ctx.last_error[global_fq_ctx.round_robin_target] >= 10) {
        global_fq_ctx.down_host[global_fq_ctx.round_robin_target] = false;
        good = 1;
        break;
      } 
      global_fq_ctx.round_robin_target = (global_fq_ctx.round_robin_target+1) % driver->nhosts;
      checked++;
      if (checked == driver->nhosts) {
        /* This means everybody is down.... just try to send to whatever fq
           we're pointing at */
        break;
      }
    }
    if (good) {
      if(fq_client_publish(driver->client[global_fq_ctx.round_robin_target], msg) == 1) {
        BUMPSTAT(global_fq_ctx.round_robin_target, publications);
      }
      else {
        BUMPSTAT(global_fq_ctx.round_robin_target, client_tx_drop);
      }
    }
    /* Go ahead and try to publish to the hosts that are down, just in
       case they've come back up. This should help minimize lost messages */
    for (i=0; i<driver->nhosts; i++) {
      if (global_fq_ctx.down_host[i]) {
        if(fq_client_publish(driver->client[i], msg) == 1) {
          BUMPSTAT(i, publications);
        }
        else {
          BUMPSTAT(i, client_tx_drop);
        }
      }
    }
    global_fq_ctx.round_robin_target = (global_fq_ctx.round_robin_target+1) % driver->nhosts;
  }
  else {
    for(i=0; i<driver->nhosts; i++) {
      if(fq_client_publish(driver->client[i], msg) == 1) {
        BUMPSTAT(i, publications);
      }
      else {
        BUMPSTAT(i, client_tx_drop);
      }
    }
  }

  fq_msg_deref(msg);
  if (!send) {
    if(mtev_hash_retrieve(&filtered_checks_hash, uuid_formatted_str, strlen(uuid_formatted_str), (void**)&filtered_metrics)) {
      if (is_bundle || (is_metric && mtev_hash_size(filtered_metrics) == 0)) {
        send = true;
      }
      else if (is_metric) {
        void *tmp;
        if (mtev_hash_retrieve(filtered_metrics, metric, strlen(metric), &tmp)) {
          send = true;
        }
      }
    }
  }

  if (global_fq_ctx.filtered_exchange[0] && send) {
    fq_msg *msg2;
    msg2 = fq_msg_alloc(payload, payloadlen);
    fq_msg_exchange(msg2, driver->filtered_exchange, strlen(driver->filtered_exchange));
    mtevL(mtev_debug, "route[%s] -> %s\n", driver->filtered_exchange, routingkey);
    fq_msg_route(msg2, routingkey, strlen(routingkey));
    fq_msg_id(msg2, NULL);
    for(i=0; i<driver->nhosts; i++) {
      if(fq_client_publish(driver->client[i], msg2) == 1) {
        BUMPSTAT(i, publications);
      }
      else {
        BUMPSTAT(i, client_tx_drop);
      }
    }
    fq_msg_deref(msg2);
  }
  if (metric) free(metric);
  return 0;
}