Esempio n. 1
0
END_TEST

START_TEST (test_riemann_simple_communicate)
{
  riemann_client_t *client, *dummy_client;
  riemann_message_t *message, *response;

  client = riemann_client_create (RIEMANN_CLIENT_TCP, "127.0.0.1", 5555);
  message = riemann_message_create_with_events
    (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost",
                           RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate",
                           RIEMANN_EVENT_FIELD_STATE, "ok",
                           RIEMANN_EVENT_FIELD_NONE),
     NULL);

  ck_assert (riemann_communicate (NULL, NULL) == NULL);
  ck_assert_errno (-errno, ENOTCONN);

  ck_assert (riemann_communicate (client, NULL) == NULL);
  ck_assert_errno (-errno, EINVAL);

  ck_assert (riemann_communicate (NULL, message) == NULL);
  ck_assert_errno (-errno, ENOTCONN);

  message = riemann_message_create_with_events
    (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost",
                           RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate",
                           RIEMANN_EVENT_FIELD_STATE, "ok",
                           RIEMANN_EVENT_FIELD_NONE),
     NULL);
  dummy_client = riemann_client_new ();
  ck_assert (riemann_communicate (dummy_client, message) == NULL);
  ck_assert_errno (-errno, ENOTCONN);
  riemann_client_free (dummy_client);

  message = riemann_message_create_with_events
    (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost",
                           RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate",
                           RIEMANN_EVENT_FIELD_STATE, "ok",
                           RIEMANN_EVENT_FIELD_NONE),
     NULL);
  response = riemann_communicate (client, message);
  ck_assert (response != NULL);
  ck_assert_int_eq (response->ok, 1);
  riemann_message_free (response);

  response = riemann_communicate
    (client,
     riemann_message_create_with_query
     (riemann_query_new ("true")));
  ck_assert (response != NULL);
  ck_assert_int_eq (response->ok, 1);
  ck_assert (response->n_events > 0);
  riemann_message_free (response);

  riemann_client_disconnect (client);
  riemann_client_connect (client, RIEMANN_CLIENT_UDP, "127.0.0.1", 5555);
  message = riemann_message_create_with_events
    (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost",
                           RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate",
                           RIEMANN_EVENT_FIELD_STATE, "ok",
                           RIEMANN_EVENT_FIELD_NONE),
     NULL);
  response = riemann_communicate (client, message);
  ck_assert (response != NULL);
  ck_assert_int_eq (response->ok, 1);
  riemann_message_free (response);
  riemann_client_disconnect (client);

  riemann_client_connect (client, RIEMANN_CLIENT_TCP, "127.0.0.1", 5555);
  message = riemann_message_create_with_events
    (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost",
                           RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate #2",
                           RIEMANN_EVENT_FIELD_STATE, "ok",
                           RIEMANN_EVENT_FIELD_NONE),
     NULL);
  riemann_message_set_query (message,
                             riemann_query_new ("true"));

  response = riemann_communicate (client, message);
  ck_assert (response != NULL);
  ck_assert_int_eq (response->ok, 1);
  ck_assert (response->n_events > 0);
  riemann_message_free (response);
  riemann_client_disconnect (client);

  riemann_client_connect (client, RIEMANN_CLIENT_UDP, "127.0.0.1", 5555);
  message = riemann_message_create_with_events
    (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost",
                           RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate #2",
                           RIEMANN_EVENT_FIELD_STATE, "ok",
                           RIEMANN_EVENT_FIELD_NONE),
     NULL);
  riemann_message_set_query (message,
                             riemann_query_new ("true"));

  response = riemann_communicate (client, message);
  ck_assert (response != NULL);
  ck_assert_int_eq (response->ok, 1);
  ck_assert (response->n_events == 0);
  riemann_message_free (response);
  riemann_client_disconnect (client);

  riemann_client_free (client);
}
Esempio n. 2
0
int main(int argc, char** argv){
  int r;
  char* end;
  sd_journal *j;
  riemann_client_t *c;
  riemann_message_t *req, *res;
  riemann_event_t *event;
  int after_wait = 0;

  r = sd_journal_open(&j, 0);
  assert(r == 0);
  
  if(argc != 3 && argc != 6){
    fprintf(stderr,"usage: journal2riemann <host> <port> [<ca-file> <cert-file> <key-file>]\n"); 
    fprintf(stderr,"example: journal2riemann 127.0.0.1 5555\n"); 
    exit(1);
  }
  long port = strtol(argv[2],&end,10);
  if(argc >= 6){
    c = riemann_client_create (
        RIEMANN_CLIENT_TLS, argv[1], port,
        RIEMANN_CLIENT_OPTION_TLS_CA_FILE, argv[3],
        RIEMANN_CLIENT_OPTION_TLS_CERT_FILE, argv[4],
        RIEMANN_CLIENT_OPTION_TLS_KEY_FILE, argv[5],
        RIEMANN_CLIENT_OPTION_TLS_HANDSHAKE_TIMEOUT, 10000,
        RIEMANN_CLIENT_OPTION_NONE);
  }else{
    c = riemann_client_create (RIEMANN_CLIENT_TCP, argv[1], port);
  }

  r = sd_journal_seek_tail(j);
  assert(r == 0);
  for (;;)  {
    const void *field; size_t len;
    r = sd_journal_next(j); assert (r >= 0);
    if (r == 0) { // if no more log available, wait for new ones
      r = sd_journal_wait(j, (uint64_t) -1); 
      assert (r >= 0); 
      after_wait = 1;
      continue; 
    }
    else if (!after_wait){ // skip all logs before the first wait
      continue; 
    }
    
    event = riemann_event_create(RIEMANN_EVENT_FIELD_NONE);
    strncpy(servicename,"journal",8);

    SD_JOURNAL_FOREACH_DATA(j, field, len){
      char* attr = ""; enum RiemannType type; void* val;

      int matched = NOMATCH;
      MATCH(F,RIEMANN_EVENT_FIELD_DESCRIPTION      , STRING             , "MESSAGE=")
      MATCH(A,"message_id"                         , STRING             , "MESSAGE_ID=")
      MATCH(F,RIEMANN_EVENT_FIELD_STATE            , STATE              , "PRIORITY=")
      MATCH(F,MATCHNOTHING                         , STRING             , "CODE_FILE=")
      MATCH(F,MATCHNOTHING                         , STRING             , "CODE_LINE=")
      MATCH(F,MATCHNOTHING                         , STRING             , "CODE_FUNC=")
      MATCH(F,MATCHNOTHING                         , STRING             , "ERRNO=")
      MATCH(F,MATCHNOTHING                         , STRING             , "SYSLOG_FACILITY=")
      MATCH(F,MATCHNOTHING                         , STRING             , "SYSLOG_IDENTIFIER=")
      MATCH(F,MATCHNOTHING                         , STRING             , "SYSLOG_PID=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_PID=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_UID=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_GID=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_COMM=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_EXE=")
      MATCH(A,"command_line"                       , STRING             , "_CMDLINE=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_CAP_EFFECTIVE=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_AUDIT_SESSION=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_AUDIT_LOGINUID=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_SYSTEMD_CGROUP=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_SYSTEMD_SESSION=")
      MATCH(F,SERVICEPART                          , STRING             , "_SYSTEMD_UNIT=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_SYSTEMD_USER_UNIT=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_SYSTEMD_OWNER_UID=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_SYSTEMD_SLICE=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_SELINUX_CONTEXT=")
      MATCH(F,RIEMANN_EVENT_FIELD_TIME             , TIME               , "_SOURCE_REALTIME_TIMESTAMP=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_BOOT_ID=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_MACHINE_ID=")
      MATCH(F,RIEMANN_EVENT_FIELD_HOST             , STRING             , "_HOSTNAME=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_TRANSPORT=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_KERNEL_DEVICE=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_KERNEL_SUBSYSTEM=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_UDEV_SYSNAME=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_UDEV_DEVNODE=")
      MATCH(F,MATCHNOTHING                         , STRING             , "_UDEV_DEVLINK=")
      MATCH(F,MATCHNOTHING                         , STRING             , "COREDUMP_UNIT=")
      MATCH(F,MATCHNOTHING                         , STRING             , "COREDUMP_USER_UNIT=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_UID=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_GID=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_COMM=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_EXE=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_CMDLINE=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_AUDIT_SESSION=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_AUDIT_LOGINUID=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_SYSTEMD_CGROUP=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_SYSTEMD_SESSION=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_SYSTEMD_OWNER_UID=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_SYSTEMD_UNIT=")
      MATCH(F,MATCHNOTHING                         , STRING             , "OBJECT_SYSTEMD_USER_UNIT=")
      MATCH(F,MATCHNOTHING                         , STRING             , "__CURSOR=")
      MATCH(F,MATCHNOTHING                         , STRING             , "__REALTIME_TIMESTAMP=")
      MATCH(F,MATCHNOTHING                         , STRING             , "__MONOTONIC_TIMESTAMP=") 
      
      if(matched != MATCHNOTHING && matched != NOMATCH){
        memcpy(valuebuf,field,len); *(valuebuf+len) = 0;
        switch(type){
          case STATE:
            val = (void*) states[atoi(valuebuf)];
            break;
          case TIME:
            *(valuebuf+len-6) = 0;
            long t = strtol(valuebuf,&end,10);
            val = (int64_t) t;
            break;
          default:
            val = (void*) valuebuf;      
        }
        
        switch(matched){
          case ATTRIBUTE:
            riemann_event_string_attribute_add(event,attr,valuebuf); break;
          case TAG:
            riemann_event_tag_add(event,valuebuf); break;
          case SERVICEPART: 
            if(len > 0){
              strncat(servicename," ",2);
              strncat(servicename,valuebuf,len+1);
            }
            break;
          default:
            riemann_event_set(event,matched,val,RIEMANN_EVENT_FIELD_NONE);
        }
      }
    } 
    riemann_event_set(event,RIEMANN_EVENT_FIELD_SERVICE,servicename,RIEMANN_EVENT_FIELD_NONE);
    req = riemann_message_create_with_events(event, NULL);
    res = riemann_communicate(c, req);
    if (!res)
    {
      fprintf (stderr, "Error communicating with Riemann: %s\n",
               strerror (errno));
      exit (1);
    }

    if (res->error)
    {
      fprintf (stderr, "Error communicating with Riemann: %s\n",
               res->error);
      exit (1);
    }

    if (res->has_ok && !res->ok)
    {
      fprintf (stderr, "Error communicating with Riemann: %s\n",
               strerror (errno));
      exit (1);
    }
    riemann_message_free(res); 
  }