예제 #1
0
int main(int argc, char **argv)
{
	struct lnstat_file *lnstat_files;
	const char *basename;
	int i, c;
	int interval = DEFAULT_INTERVAL;
	int hdr = 2;
	enum {
		MODE_DUMP,
		MODE_JSON,
		MODE_NORMAL,
	} mode = MODE_NORMAL;
	unsigned long count = 1;
	struct table_hdr *header;
	static struct field_params fp;
	int num_req_files = 0;
	char *req_files[LNSTAT_MAX_FILES];

	/* backwards compatibility mode for old tools */
	basename = strrchr(argv[0], '/');
	if (basename)
		basename += 1;	  /* name after slash */
	else
		basename = argv[0]; /* no slash */

	if (!strcmp(basename, "rtstat")) {
		/* rtstat compatibility mode */
		req_files[0] = "rt_cache";
		num_req_files = 1;
	} else if (!strcmp(basename, "ctstat")) {
		/* ctstat compatibility mode */
		req_files[0] = "ip_conntrack";
		num_req_files = 1;
	}

	while ((c = getopt_long(argc, argv,"Vc:djf:h?i:k:s:w:",
				opts, NULL)) != -1) {
		int len = 0;
		char *tmp, *tok;

		switch (c) {
		case 'c':
			count = strtoul(optarg, NULL, 0);
			break;
		case 'd':
			mode = MODE_DUMP;
			break;
		case 'j':
			mode = MODE_JSON;
			break;
		case 'f':
			req_files[num_req_files++] = strdup(optarg);
			break;
		case '?':
		case 'h':
			usage(argv[0], 0);
			break;
		case 'i':
			sscanf(optarg, "%u", &interval);
			break;
		case 'k':
			tmp = strdup(optarg);
			if (!tmp)
				break;
			for (tok = strtok(tmp, ",");
			     tok;
			     tok = strtok(NULL, ",")) {
				if (fp.num >= MAX_FIELDS) {
					fprintf(stderr, 
						"WARN: too many keys"
						" requested: (%d max)\n",
						MAX_FIELDS);
					break;
				}
				fp.params[fp.num++].name = tok;
			}
			break;
		case 's':
			sscanf(optarg, "%u", &hdr);
			break;
		case 'w':
			tmp = strdup(optarg);
			if (!tmp)
				break;
			i = 0;
			for (tok = strtok(tmp, ",");
			     tok;
			     tok = strtok(NULL, ",")) {
				len  = strtoul(tok, NULL, 0);
				if (len > FIELD_WIDTH_MAX)
					len = FIELD_WIDTH_MAX;
				fp.params[i].print.width = len;
				i++;
			}
			if (i == 1) {
				for (i = 0; i < MAX_FIELDS; i++)
					fp.params[i].print.width = len;
			}
			break;
		default:
			usage(argv[0], 1);
			break;
		}
	}

	lnstat_files = lnstat_scan_dir(PROC_NET_STAT, num_req_files,
				       (const char **) req_files);

	switch (mode) {
	case MODE_DUMP:
		lnstat_dump(stderr, lnstat_files);
		break;

	case MODE_NORMAL:
	case MODE_JSON:
		if (!map_field_params(lnstat_files, &fp, interval))
			exit(1);

		header = build_hdr_string(lnstat_files, &fp, 80);
		if (!header)
			exit(1);

		if (interval < 1 )
			interval = 1;

		for (i = 0; i < count; i++) {
			lnstat_update(lnstat_files);
			if (mode == MODE_JSON)
				print_json(stdout, lnstat_files, &fp);
			else {
				if  ((hdr > 1 &&
				      (! (i % 20))) || (hdr == 1 && i == 0))
					print_hdr(stdout, header);
				print_line(stdout, lnstat_files, &fp);
			}
			fflush(stdout);
			if (i < count - 1)
				sleep(interval);
		}
		break;
	}

	return 1;
}
예제 #2
0
파일: json-list.c 프로젝트: irods/baton
int do_list_paths(FILE *input, print_flags pflags) {
    int path_count = 0;
    int error_count = 0;

    rodsEnv env;
    rcComm_t *conn = rods_login(&env);
    if (!conn) goto error;

    size_t jflags = JSON_DISABLE_EOF_CHECK | JSON_REJECT_DUPLICATES;

    while (!feof(input)) {
        json_error_t load_error;
        json_t *target = json_loadf(input, jflags, &load_error);
        if (!target) {
            if (!feof(input)) {
                log(ERROR, "JSON error at line %d, column %d: %s",
                    load_error.line, load_error.column, load_error.text);
            }

            continue;
        }

        baton_error_t path_error;
        char *path = json_to_path(target, &path_error);
        path_count++;

        if (path_error.code != 0) {
            error_count++;
            add_error_value(target, &path_error);
            print_json(target);
        }
        else {
            rodsPath_t rods_path;
            int status = resolve_rods_path(conn, &env, &rods_path, path);
            if (status < 0) {
                error_count++;
                set_baton_error(&path_error, status,
                                "Failed to resolve path '%s'", path);
                add_error_value(target, &path_error);
                print_json(target);
            }
            else {
                baton_error_t error;
                json_t *results = list_path(conn, &rods_path, pflags, &error);

                if (error.code != 0) {
                    error_count++;
                    add_error_value(target, &error);
                    print_json(target);
                }
                else {
                    print_json(results);
                    json_decref(results);
                }
            }

            if (rods_path.rodsObjStat) free(rods_path.rodsObjStat);
        }

        if (unbuffered_flag) fflush(stdout);

        json_decref(target);
        free(path);
    } // while

    rcDisconnect(conn);

    log(DEBUG, "Processed %d paths with %d errors", path_count, error_count);

    return error_count;

error:
    if (conn) rcDisconnect(conn);

    log(ERROR, "Processed %d paths with %d errors", path_count, error_count);

    return 1;
}
예제 #3
0
파일: test.c 프로젝트: willisj/varsync
int main(int argv, char * argc[]){
	uint8_t role 			 = 0;
	uint32_t example_variable	 = 0;
	struct sync_list * list		 = NULL;
	char  example_buffer[] = "this is some sample data for the buffer";
	struct variable_handler * variable_handle = NULL;
	struct variable_handler * buffer_handle = NULL;
	pthread_t thread_handler;

	if( argv == 3 ){
		if( !strcmp(argc[1], "server"))
			role =  setbit( role, VSROLE_SERVER);
		else if( !strcmp(argc[1], "client"))
			role =  setbit( role, VSROLE_CLIENT);
		else{
			fprintf( stderr, USAGE);
			exit(1);
		}
			
		if( !strcmp(argc[2], "send"))
			role =  setbit( role, VSROLE_SENDER);
		else if( !strcmp(argc[2], "recv"))
			role =  setbit( role, VSROLE_RECEIVER);
		else{
			fprintf( stderr, USAGE);
			exit(1);
		}
	}
	else{
		fprintf( stderr, USAGE);
		exit(1);
	}

	fprintf( stderr, "Initializing list\n");
	list = init_list( role, "127.0.0.1",  5340);
	if(!list){
		fprintf( stderr, "\t\t[Failed]\n");
		exit(1);
	}
	fprintf( stderr, "\t\t[Done]\n");

	
	fprintf( stderr, "Initializing variable\n");
	variable_handle = init_variable_handler( EXAMPLE_ID, &example_variable, sizeof(uint32_t), NULL);
	if(!variable_handle){
		fprintf( stderr, "\t\t[Failed]\n");
		exit(1);
	}
	buffer_handle = init_variable_handler( EXAMPLE_ID + 1, example_buffer, strlen(example_buffer), NULL);
	if(!buffer_handle){
		fprintf( stderr, "\t\t[Failed]\n");
		exit(1);

	}
	fprintf( stderr, "\t\t[Done]\n");

	fprintf( stderr, "Adding variable to list\n");
	if(!add_var_handler( list, variable_handle) ){
		fprintf( stderr, "\t\t[Failed]\n");
		fprintf( stderr, "Unable to add handler to  list\n");
		return 1;
	}
	if(!add_var_handler( list, buffer_handle) ){
		fprintf( stderr, "\t\t[Failed]\n");
		fprintf( stderr, "Unable to add handler to  list\n");
		return 1;
	}
	fprintf( stderr, "\t\t[Done]\n");

	fprintf( stderr, "Creating thread\n");
	if(pthread_create(&thread_handler, NULL, update_thread, list)){
		fprintf( stderr, "\t\t[Failed]\n");
		perror("ERROR creating thread.");
	}

	fprintf( stderr, "\t\t[Done]\n");
	while(1){
		if( getbit( role, VSROLE_SENDER))
			printf("Var: %d\n", example_variable++);
		else
			print_json(   list);
		sleep(1);
	}

	deinit_list(list);

	return 0;
}
예제 #4
0
파일: ovsdb-client.c 프로젝트: ALutzG/ovs
static void
print_and_free_json(struct json *json)
{
    print_json(json);
    json_destroy(json);
}
예제 #5
0
파일: table.cpp 프로젝트: draios/sysdig
vector<sinsp_sample_row>* sinsp_table::get_sample(uint64_t time_delta)
{
	//
	// No sample generation happens when the table is paused
	//
	if(!m_paused)
	{
		//
		// If we have a freetext filter, we start by filtering the sample
		//
		if(m_freetext_filter != "")
		{
			filter_sample();
			m_sample_data = &m_filtered_sample_data;
		}
		else
		{
			m_sample_data = &m_full_sample_data;
		}

		//
		// Sort the sample
		//
		sort_sample();
	}

	//
	// If required, emit the sample to stdout
	//
#ifndef _WIN32
	if(m_output_type != sinsp_table::OT_CURSES)
	{
#endif
		if(m_output_type == sinsp_table::OT_RAW)
		{
			print_raw(m_sample_data, time_delta);
		}
		else if(m_output_type == sinsp_table::OT_JSON)
		{
			print_json(m_sample_data, time_delta);
		}
		else
		{
			ASSERT(false);
		}
#ifndef _WIN32
	}
#endif

	//
	// Restore the lists used for event processing
	//
	m_types = &m_premerge_types;
	m_table = &m_premerge_table;
	m_n_fields = m_n_premerge_fields;
	m_vals_array_sz = m_premerge_vals_array_sz;
	m_fld_pointers = m_premerge_fld_pointers;
	m_extractors = &m_premerge_extractors;

	return m_sample_data;
}
예제 #6
0
int do_search_metadata(FILE *input, char *zone_name, option_flags oflags) {
    int item_count  = 0;
    int error_count = 0;

    rodsEnv env;
    rcComm_t *conn = rods_login(&env);
    if (!conn) goto error;

    while (!feof(input)) {
        size_t jflags = JSON_DISABLE_EOF_CHECK | JSON_REJECT_DUPLICATES;
        json_error_t load_error;
        json_t *target = json_loadf(input, jflags, &load_error);
        if (!target) {
            if (!feof(input)) {
                logmsg(ERROR, "JSON error at line %d, column %d: %s",
                       load_error.line, load_error.column, load_error.text);
            }

            continue;
        }

        item_count++;
        if (!json_is_object(target)) {
            logmsg(ERROR, "Item %d in stream was not a JSON object; skipping",
                   item_count);
            error_count++;
            json_decref(target);
            continue;
        }

        json_t *results = NULL;

        if (has_collection(target)) {
            baton_error_t resolve_error;
            resolve_collection(target, conn, &env, oflags, &resolve_error);

            if (add_error_report(target, &resolve_error)) {
                error_count++;
            }
            else {
                baton_error_t search_error;
                results = search_metadata(conn, target, zone_name, oflags,
                                          &search_error);
                if (add_error_report(target, &search_error)) {
                    error_count++;
                    print_json(target);
                }
                else {
                    print_json(results);
                }
            }
        }
        else {
            baton_error_t search_error;
            results = search_metadata(conn, target, zone_name, oflags,
                                      &search_error);
            if (add_error_report(target, &search_error)) {
                error_count++;
                print_json(target);
            }
            else {
                print_json(results);
            }
        }

        if (unbuffered_flag) fflush(stdout);

        if (results) json_decref(results);
        json_decref(target);
    } // while

    rcDisconnect(conn);

    if (error_count > 0) {
        logmsg(WARN, "Processed %d items with %d errors",
               item_count, error_count);
    }
    else {
        logmsg(DEBUG, "Processed %d items with %d errors",
               item_count, error_count);
    }

    return error_count;

error:
    if (conn) rcDisconnect(conn);

    logmsg(ERROR, "Processed %d items with %d errors", item_count, error_count);

    return 1;
}
예제 #7
0
static void
handle_events (int argc, char *argv[])
{
#define BUFFER_SIZE 4096
  char *buf;
  const struct inotify_event *event;
  int i;
  ssize_t len;
  char *ptr;
  const char *subdir = NULL;

  buf = malloc (BUFFER_SIZE);
  if (buf == NULL)
    return;

  for (;;)
    {
      len = read (inotify_fd, buf, BUFFER_SIZE);
      if (len == -1 && errno != EAGAIN)
	{
	  exit (EXIT_FAILURE);
	}

      if (len <= 0)
	break;

      for (ptr = buf; ptr < buf + len;
	   ptr += sizeof (struct inotify_event) + event->len)
	{
          struct Maildir *maildir = NULL;

	  event = (const struct inotify_event *) ptr;

	  if (event->mask & IN_ISDIR)
	    {
	      continue;
	    }

	  for (maildir = maildirs; maildir; maildir = maildir->next)
	    {
	      if (event->wd == maildir->fd_cur)
		{
		  subdir = "cur";
		  break;
		}
	      if (event->wd == maildir->fd_new)
		{
		  subdir = "new";
		  break;
		}
	    }

	  if (subdir == NULL)
	    error (EXIT_FAILURE, errno, "Could not find descriptor for %s",
		   subdir);

	  if (event->mask & IN_MOVED_TO)
	    {
	      char *path;

	      asprintf (&path, "%s/%s/%s", maildir->name, subdir, event->name);
	      if (path == NULL)
		error (EXIT_FAILURE, errno, "Could not allocate path for %s",
		       subdir);

	      if (message_is_read (event->name) == 0)
		{
                  if (opt_json)
                    {
                      char *from = NULL, *subject = NULL;
                      get_email_from_subject (path, &from, &subject);
                      print_json (maildir->pretty_name, path, from, subject);
                    }
		  else if (print_subject)
		    {
                      char *subject = NULL;
                      get_email_from_subject (path, NULL, &subject);
		      printf ("New message: %s : %s\n", path, subject);
		      free (subject);
		    }
		  else
		    {
		      printf ("%s\n", path);
		    }
		}
	      free (path);
	    }
	}
    }

  free (buf);
}
예제 #8
0
char *cJSON_PrintUnformatted(cJSON *item,int padding,int estimate)	{return print_json(item,0,padding,estimate);}
예제 #9
0
/* Render a cJSON item/entity/structure to text. */
char *cJSON_Print(cJSON *item,int padding,int estimate)				{return print_json(item,1,padding,estimate);}
예제 #10
0
int do_modify_metadata(FILE *input, metadata_op operation,
                       option_flags oflags) {
    int item_count  = 0;
    int error_count = 0;

    rodsEnv env;
    rcComm_t *conn = rods_login(&env);
    if (!conn) goto error;

    while (!feof(input)) {
        size_t flags = JSON_DISABLE_EOF_CHECK | JSON_REJECT_DUPLICATES;
        json_error_t load_error;
        json_t *target = json_loadf(input, flags, &load_error);
        if (!target) {
            if (!feof(input)) {
                logmsg(ERROR, "JSON error at line %d, column %d: %s",
                       load_error.line, load_error.column, load_error.text);
            }

            continue;
        }

        item_count++;
        if (!json_is_object(target)) {
            logmsg(ERROR, "Item %d in stream was not a JSON object; skipping",
                   item_count);
            error_count++;
            json_decref(target);
            continue;
        }

        baton_error_t path_error;
        char *path = json_to_path(target, &path_error);

        if (add_error_report(target, &path_error)) {
            error_count++;
        }
        else {
            json_t *avus = json_object_get(target, JSON_AVUS_KEY);
            if (!json_is_array(avus)) {
                error_count++;
                set_baton_error(&path_error, -1,
                                "AVU data for %s is not in a JSON array", path);
                add_error_report(target, &path_error);
            }
            else {
                rodsPath_t rods_path;
                resolve_rods_path(conn, &env, &rods_path, path, oflags,
                                  &path_error);
                if (add_error_report(target, &path_error)) {
                    error_count++;
                }
                else {
                    for (size_t i = 0; i < json_array_size(avus); i++) {
                        json_t *avu = json_array_get(avus, i);
                        baton_error_t mod_error;
                        modify_json_metadata(conn, &rods_path, operation, avu,
                                             &mod_error);

                        // FIXME: this only records the last error
                        if (add_error_report(target, &mod_error)) {
                            error_count++;
                        }
                    }

                    if (rods_path.rodsObjStat) free(rods_path.rodsObjStat);
                }
            }
        }

        print_json(target);
        if (unbuffered_flag) fflush(stdout);

        json_decref(target);
        if (path) free(path);
    } // while

    rcDisconnect(conn);

    if (error_count > 0) {
        logmsg(WARN, "Processed %d items with %d errors",
               item_count, error_count);
    }
    else {
        logmsg(DEBUG, "Processed %d items with %d errors",
               item_count, error_count);
    }

    return error_count;

error:
    if (conn) rcDisconnect(conn);

    logmsg(ERROR, "Processed %d items with %d errors", item_count, error_count);

    return 1;
}