Ejemplo n.º 1
0
/*
 * _metric_list
 *
 * Output list of all available metrics
 */
static void
_metric_list(void)
{
  const char *func = __FUNCTION__;
  cerebro_namelist_t m = NULL;
  cerebro_namelist_iterator_t mitr = NULL;
  List l = NULL;
  ListIterator litr = NULL;
  char *str;

  if (!(m = cerebro_get_metric_names(handle)))
    {
      char *msg = cerebro_strerror(cerebro_errnum(handle));

      _clean_err_exit(cerebro_errnum(handle));
      err_exit("%s: cerebro_get_metric_names: %s", func, msg);
    }
  
  if (!(mitr = cerebro_namelist_iterator_create(m)))
    {
      char *msg = cerebro_strerror(cerebro_namelist_errnum(m));
      err_exit("%s: cerebro_namelist_iterator_create: %s", func, msg);
    }

  l = List_create(NULL);

  while (!cerebro_namelist_iterator_at_end(mitr))
    {
      if (cerebro_namelist_iterator_name(mitr, &str) < 0)
        {
          char *msg = cerebro_strerror(cerebro_namelist_iterator_errnum(mitr));
          err_exit("%s: cerebro_namelist_iterator_name: %s", func, msg);
        }

      List_append(l, str);

      if (cerebro_namelist_iterator_next(mitr) < 0)
        {
          char *msg = cerebro_strerror(cerebro_namelist_iterator_errnum(mitr));
          err_exit("%s: cerebro_namelist_iterator_next: %s", func, msg);
        }
    }
  
  litr = List_iterator_create(l);

  while ((str = list_next(litr)))
    fprintf(stdout, "%s\n", str);

  /* List_destroy() and cerebro_namelist_destory() destroy iterators too */
  List_destroy(l);
  (void)cerebro_namelist_destroy(m);
}
Ejemplo n.º 2
0
static int
_get_metric_data (cerebro_t ch, char *name, List rl)
{
    int retval; 
    cerebro_nodelist_t n = NULL;
    cerebro_nodelist_iterator_t nitr;
    cmetric_t c;
    unsigned int time, type, size;
    void *value;
    char *nodename;

    if (!(n = cerebro_get_metric_data (ch, name))) {
        if (lmt_conf_get_cbr_debug ())
            msg ("error getting metric data for %s: %s",
                 name, cerebro_strerror (cerebro_errnum (ch)));
        goto done;
    }
    if (!(nitr = cerebro_nodelist_iterator_create (n))) {
        if (lmt_conf_get_cbr_debug ())
            msg ("error creating nodelist iterator for %s metric data: %s",
                 name, cerebro_strerror (cerebro_errnum (ch)));
        goto done;
    }
    while (!cerebro_nodelist_iterator_at_end (nitr)) {
        if (cerebro_nodelist_iterator_nodename (nitr, &nodename) < 0 ||
            cerebro_nodelist_iterator_metric_value (nitr,
                                       &time, &type, &size, &value) < 0) {
            if (lmt_conf_get_cbr_debug ())
                msg ("error retrieving metric value for %s: %s", name,
                   cerebro_strerror (cerebro_nodelist_iterator_errnum (nitr)));
            goto done;
        }
        c = _create_cmetric (name, nodename, time, type, size, value);
        if (!(list_append (rl, c))) {
            _destroy_cmetric (c);
            goto done;
        }
        if (cerebro_nodelist_iterator_next (nitr) < 0) {
            if (lmt_conf_get_cbr_debug ())
                msg ("error iterating on metric %s: %s", name,
                   cerebro_strerror (cerebro_nodelist_iterator_errnum (nitr)));
            goto done;
        }
    }
    retval = 0;
done:
    if (n)
        cerebro_nodelist_destroy (n); /* side effect: destroys nitr */
    return retval;
}
Ejemplo n.º 3
0
int 
main(int argc, char *argv[]) 
{
  const char *func = __FUNCTION__;
  int rv = 0;

  err_init(argv[0]);
  err_set_flags(ERROR_STDERR);
#if CEREBRO_DEBUG
  cerebro_err_init(argv[0]);
#endif /* CEREBRO_DEBUG */

  _init_cerebro_admin();

  _cmdline_parse(argc, argv);
  
  if (operation == CEREBRO_ADMIN_REGISTER)
    rv = cerebro_register_metric(handle, metric_name);
  else if (operation == CEREBRO_ADMIN_UNREGISTER)
    rv = cerebro_unregister_metric(handle, metric_name);
  else if (operation == CEREBRO_ADMIN_UPDATE)
    rv = cerebro_update_metric_value(handle, 
                                     metric_name,
                                     metric_value_type,
                                     metric_value_len,
                                     metric_value_ptr);
  else if (operation == CEREBRO_ADMIN_RESEND)
    rv = cerebro_resend_metric(handle, metric_name);
  else if (operation == CEREBRO_ADMIN_FLUSH)
    rv = cerebro_flush_metric(handle, metric_name);
  
  if (rv < 0)
    {
      char *msg = cerebro_strerror(cerebro_errnum(handle));
      
      _clean_err_exit(cerebro_errnum(handle));
      err_exit("%s: %s", func, msg);
    }
  
  _cleanup_cerebro_stat();
  exit(0);
}
Ejemplo n.º 4
0
/* 
 * _cerebro_set_flags
 *
 * Set a cerebro flag
 */
static void
_cerebro_set_flags(unsigned int new_flag)
{
  const char *func = __FUNCTION__;
  int flags;

  if ((flags = cerebro_get_flags(handle)) < 0)
    {
      char *msg = cerebro_strerror(cerebro_errnum(handle));
      err_exit("%s: cerebro_get_flags: %s", func, msg);
    }

  flags |= new_flag;

  if (cerebro_set_flags(handle, flags) < 0)
    {
      char *msg = cerebro_strerror(cerebro_errnum(handle));
      err_exit("%s: cerebro_set_flags: %s", func, msg);
    }
}
Ejemplo n.º 5
0
/*
 * _metric_data
 *
 * Output list of all available metrics
 */
static void
_metric_data(void)
{
  const char *func = __FUNCTION__;
  cerebro_nodelist_t n = NULL;
  cerebro_nodelist_iterator_t nitr = NULL;
  List l = NULL;

  if (!(n = cerebro_get_metric_data(handle, metric_name)))
    {
      char *msg = cerebro_strerror(cerebro_errnum(handle));

      _clean_err_exit(cerebro_errnum(handle));
      err_exit("%s: cerebro_get_metric_data: %s", func, msg);
    }
  
  if (!(nitr = cerebro_nodelist_iterator_create(n)))
    {
      char *msg = cerebro_strerror(cerebro_nodelist_errnum(n));
      err_exit("%s: cerebro_nodelist_iterator_create: %s", func, msg);
    }

  l = List_create((ListDelF)_Free);

  while (!cerebro_nodelist_iterator_at_end(nitr))
    {
      struct node_metric_data *data = NULL;

      data = Malloc(sizeof(struct node_metric_data));
      memset(data, '\0', sizeof(struct node_metric_data));

      if (cerebro_nodelist_iterator_nodename(nitr, &(data->nodename)) < 0)
        {
          char *msg = cerebro_strerror(cerebro_nodelist_iterator_errnum(nitr));
          err_exit("%s: cerebro_nodelist_iterator_nodename: %s", func, msg);
        }

      if (cerebro_nodelist_iterator_metric_value(nitr, 
                                                 &(data->metric_value_received_time),
                                                 &(data->metric_value_type),
                                                 &(data->metric_value_len),
                                                 &(data->metric_value)) < 0)
        {
          char *msg = cerebro_strerror(cerebro_nodelist_iterator_errnum(nitr));
          err_exit("%s: cerebro_nodelist_iterator_metric_value: %s", func, msg);
        }

      List_append(l, data);
      
      if (cerebro_nodelist_iterator_next(nitr) < 0)
        {
          char *msg = cerebro_strerror(cerebro_nodelist_iterator_errnum(nitr));
          err_exit("%s: cerebro_nodelist_iterator_next: %s", func, msg);
        }
    }

  if (!strcmp(metric_name, CEREBRO_METRIC_CLUSTER_NODES))
    _cluster_nodes_output(l);
  else if (output_type == CEREBRO_STAT_NEWLINE)
    _newline_output(l);
  else if (output_type == CEREBRO_STAT_HOSTRANGE)
    _hostrange_output(l);

  /* List_destroy() and cerebro_nodelist_destory() destroy iterators too */
  List_destroy(l);
  (void)cerebro_nodelist_destroy(n);
}
Ejemplo n.º 6
0
/*
 * _event_data
 *
 * Monitor event data
 */
static void
_event_data(void)
{
  const char *func = __FUNCTION__;
  int fd;

  if ((fd = cerebro_event_register(handle, event_name)) < 0)
    {
      char *msg = cerebro_strerror(cerebro_errnum(handle));

      _clean_err_exit(cerebro_errnum(handle));
      err_exit("%s: cerebro_event_register: %s", func, msg);
    }
  
  while (1)
    {
      struct pollfd pfd;
      int n;

      pfd.fd = fd;
      pfd.events = POLLIN;
      pfd.revents = 0;
  
      n = Poll(&pfd, 1, -1);

      if (n && pfd.revents & POLLIN)
        {
          char vbuf[CEREBRO_STAT_BUFLEN];
          char tbuf[CEREBRO_STAT_BUFLEN];
          char *nodename;
          unsigned int event_value_type;
          unsigned int event_value_len;
          void *event_value;
          time_t t;
          struct tm *tm;

          if (cerebro_event_parse(handle,
                                  fd,
                                  &nodename,
                                  &event_value_type,
                                  &event_value_len,
                                  &event_value) < 0)
            {
              char *msg = cerebro_strerror(cerebro_errnum(handle));
              
              _clean_err_exit(cerebro_errnum(handle));
              err_exit("%s: cerebro_event_parse: %s", func, msg);
            }

          
          memset(vbuf, '\0', CEREBRO_STAT_BUFLEN);
          _metric_value_str(event_value_type,
                            event_value_len,
                            event_value,
                            vbuf,
                            CEREBRO_STAT_BUFLEN);
          
          memset(tbuf, '\0', CEREBRO_STAT_BUFLEN);
          t = time(NULL);
          tm = Localtime(&t);
          strftime(tbuf, CEREBRO_STAT_BUFLEN, "%F %I:%M:%S%P", tm);
          
          fprintf(stdout, "%s(%s): %s\n", nodename, tbuf, vbuf);
          
          free(nodename);
          free(event_value);
        }     
    }

  (void)cerebro_event_unregister(handle, fd);
}
Ejemplo n.º 7
0
/* 
 * _cmdline_parse
 *
 * parse all cmdline input
 */
static void
_cmdline_parse(int argc, char **argv) 
{
  const char *func = __FUNCTION__;
  char options[1024];
  char *ptr;
  int c;

#if HAVE_GETOPT_LONG
  struct option loptions[] = 
    {
      {"help",                  0, NULL, 'h'},
      {"version",               0, NULL, 'v'},
      {"hostname",              1, NULL, 'o'},
      {"port",                  1, NULL, 'p'},
      {"metric",                1, NULL, 'm'},
      {"metric-list",           0, NULL, 'l'},
      {"up-only",               0, NULL, 'U'},
      {"none-if-down",          0, NULL, 'D'},
      {"none-if-not-monitored", 0, NULL, 'N'},
      {"newline",               0, NULL, 'n'},
      {"hostrange",             0, NULL, 'q'},
      {"metric-received-time",  0, NULL, 't'},
      {"event",                 1, NULL, 'e'},
      {"event-list",            0, NULL, 'z'},
#if CEREBRO_DEBUG
      {"debug",                 0, NULL, 'd'},
#endif /* CEREBRO_DEBUG */
      {0, 0, 0, 0},
    };
#endif /* HAVE_GETOPT_LONG */

  assert(argv);

  strcpy(options, "hvo:p:ze:lm:UDNnqt");
#if CEREBRO_DEBUG
  strcat(options, "d");
#endif /* CEREBRO_DEBUG */
  
  /* turn off output messages printed by getopt_long */
  opterr = 0;
  
#if HAVE_GETOPT_LONG
  while ((c = getopt_long(argc, argv, options, loptions, NULL)) != -1)
#else
  while ((c = getopt(argc, argv, options)) != -1)
#endif
    {
    switch(c) 
      {
      case 'h':
        _usage();
      case 'v':
        _version();
      case 'o':
        hostname = optarg;
        if (cerebro_set_hostname(handle, hostname) < 0)
          {
            char *msg = cerebro_strerror(cerebro_errnum(handle));
            err_exit("%s: cerebro_set_hostname: %s", func, msg);
          }
        break;
      case 'p':
        port = strtol(optarg, &ptr, 10);
        if (ptr != (optarg + strlen(optarg)))
          err_exit("invalid port specified");
        if (cerebro_set_port(handle, port) < 0)
          {
            char *msg = cerebro_strerror(cerebro_errnum(handle));
            err_exit("%s: cerebro_set_port: %s", func, msg);
          }
        break;
      case 'm':
        metric_name = optarg;
        break;
      case 'l':
        metric_list_flag++;
        break;
      case 'U':
        _cerebro_set_flags(CEREBRO_METRIC_DATA_FLAGS_UP_ONLY);
        break;
      case 'D':
        _cerebro_set_flags(CEREBRO_METRIC_DATA_FLAGS_NONE_IF_DOWN);
        break;
      case 'N':
        _cerebro_set_flags(CEREBRO_METRIC_DATA_FLAGS_NONE_IF_NOT_MONITORED);
        break;
      case 'n':
        output_type = CEREBRO_STAT_NEWLINE;
        break;
      case 'q':
        output_type = CEREBRO_STAT_HOSTRANGE;
        break;
      case 't':
        metric_received_time_flag++;
        break;
      case 'e':
        event_name = optarg;
        break;
      case 'z':
        event_list_flag++;
        break;
#if CEREBRO_DEBUG
      case 'd':
        cerebro_err_set_flags(CEREBRO_ERROR_STDERR);
        break;
#endif /* CEREBRO_DEBUG */
      default:
      case '?':
        fprintf(stderr, "command line option error\n");
        _usage();
      }
    }

  if (((metric_list_flag ? 1 : 0)
       + (metric_name ? 1 : 0)
       + (event_name ? 1 : 0)
       + (event_list_flag ? 1 : 0)) > 1)
    err_exit("Specify one of --event-list, --event, --metric-list, and --metric options");
  
  if (!event_name && !event_list_flag && !metric_list_flag && !metric_name)
    _usage();
}