int main(int argc, char *argv[]) {
    if (argc < 2) {
        fprintf(stderr,
                "usage: procstatlog poll_interval [procname ...] > procstat.log\n\n"
                "\n"
                "Scans process status every poll_interval seconds (e.g. 0.1)\n"
                "and writes data from /proc/stat, /proc/*/stat files, and\n"
                "other /proc status files every time something changes.\n"
                "\n"
                "Scans all processes by default.  Listing some process name\n"
                "substrings will limit scanning and reduce overhead.\n"
                "\n"
                "Data is logged continuously until the program is killed.\n");
        return 2;
    }

    long poll_usec = (long) (atof(argv[1]) * 1000000l);
    if (poll_usec <= 0) {
        fprintf(stderr, "illegal poll interval: %s\n", argv[1]);
        return 2;
    }

    struct data *old_stats = malloc(sizeof(struct data));
    old_stats->name = NULL;
    old_stats->value = NULL;
    while (1) {
        struct timeval before, after;
        gettimeofday(&before, NULL);
        printf("T + %ld.%06ld\n", before.tv_sec, before.tv_usec);

        struct data *new_stats = read_stats(argv + 2, argc - 2);
        diff_stats(old_stats, new_stats);
        free_stats(old_stats);
        old_stats = new_stats;
        gettimeofday(&after, NULL);
        printf("T - %ld.%06ld\n", after.tv_sec, after.tv_usec);

        long elapsed_usec = (long) after.tv_usec - before.tv_usec;
        elapsed_usec += 1000000l * (after.tv_sec - before.tv_sec);
        if (poll_usec > elapsed_usec) usleep(poll_usec - elapsed_usec);
    }

    return 0;
}
Esempio n. 2
0
int main(int argc, const char **argv)
{

  vector<const char*> args;

  Config_t config;
  char *config_file;
  int i;

  global_config = &config;   //** Make the global point to what's loaded
  memset(global_config, 0, sizeof(Config_t));  //** init the data
  global_network = NULL;

  if (argc < 2) {
     printf("ibp_server [-d] config_file\n\n");
     printf("-d          - Run as a daemon\n");
     printf("config_file - Configuration file\n");
     return(0);
  }

  int astart = 1;
  int daemon = 0;
  if (strcmp(argv[astart], "-d") == 0) {
     daemon = 1;
     argv[astart] = "";
     astart++;
  } 

  config_file = (char *)argv[astart];
 
  argv_to_vec(argc, argv, args);
  parse_config_options(args);     // These are for EBOFS 


  //*** Open the config file *****
  printf("Config file: %s\n\n", config_file);

  GKeyFile *keyfile;
  GKeyFileFlags flags;
  GError *error = NULL;

  keyfile = g_key_file_new();
  flags = G_KEY_FILE_NONE;

  /* Load the GKeyFile from disk or return. */
  if (!g_key_file_load_from_file (keyfile, config_file, flags, &error)) {
    g_error (error->message);
    return(-1);
  }

  
  //** Parse the global options first ***
  parse_config(keyfile, &config);

  init_thread_slots(2*config.server.max_threads);  //** Make pigeon holes

  dns_cache_init(1000);
  init_subnet_list(config.server.iface[0].hostname);  

  //*** Install the commands: loads Vectable info and parses config options only ****
  install_commands(keyfile);

  g_key_file_free(keyfile);   //Free the keyfile context

  set_starttime();

  log_preamble(&config);

  configure_signals();   //** Setup the signal handlers

  //*** Set up the shutdown variables  
  pthread_mutex_init(&shutdown_lock, NULL);
  pthread_mutex_unlock(&shutdown_lock);
  shutdown_now = 0;

  //*** Make the searchable version of the resources ***
  config.rl = create_resource_list(config.res, config.n_resources);
//  log_printf(0, "Looking up resource 2 and printing info.....\n")
//  print_resource(resource_lookup(config.rl, "2"), log_fd());

  init_stats(config.server.stats_size);
  lock_alloc_init();

  //***Launch as a daemon if needed***
  if (args.size() == 2) {    //*** Launch as a daemon ***
     if ((strcmp(config.server.logfile, "stdout") == 0) || 
         (strcmp(config.server.logfile, "stderr") == 0)) {
        log_printf(0, "Can't launch as a daemom because log_file is either stdout or stderr\n");  
        log_printf(0, "Running in normal mode\n");  
     } else if (fork() == 0) {    //** This is the daemon
        log_printf(0, "Running as a daemon.\n");
        flush_log();
        fclose(stdin);     //** Need to close all the std* devices **
        fclose(stdout);
        fclose(stderr);

        char fname[1024];
        fname[1023] = '\0';
        snprintf(fname, 1023, "%s.stdout", config.server.logfile);
        assert((stdout = fopen(fname, "w")) != NULL);
        snprintf(fname, 1023, "%s.stderr", config.server.logfile);
        assert((stderr = fopen(fname, "w")) != NULL);
//        stdout = stderr = log_fd();  //** and reassign them to the log device         
printf("ibp_server.c: STDOUT=STDERR=LOG_FD() dnoes not work!!!!!!!!!!!!!!!!!!!!!!!!\n");
     } else {           //** Parent exits
        exit(0);         
     }    
  }

//  test_alloc();   //** Used for testing allocation speed only

  //*** Initialize all command data structures.  This is mainly 3rd party commands ***
  initialize_commands();

  //** Launch the garbage collection threads
  for (i=0; i<config.n_resources; i++) launch_resource_cleanup_thread(&(config.res[i]));
  //*** Start the activity log ***
  alog_open();

  server_loop(&config);     //***** Main processing loop ******

  //*** Shutdown the activity log ***
  alog_close();

  //*** Destroy all the 3rd party structures ***
  destroy_commands();

  lock_alloc_destroy();

  destroy_thread_slots();

  shutdown(&config);

  free_resource_list(config.rl);

  free_stats();

  log_printf(0, "main: Completed shutdown. Exiting\n");
//  close_log();
//  close_debug();
}
Esempio n. 3
0
int
main(int argc, char *argv[]) {
    struct sigaction sa;
    char c;
    int option_index = 0;
    
    // Program name
    program_name = strrchr(argv[0], '/');
    if (program_name)
        program_name ++;
    else
        program_name = argv[0];
        
    // Parse command line options
    do {
        c = getopt_long(argc, argv, short_options, long_options, &option_index);

        switch (c) {

        case -1:
            break;
            
        case 'r':
            capture_file = fopen(optarg, "r");
            if (!capture_file) {
                LOGGER(ERROR, "Cannot open file '%s': %s\n", optarg,
                        strerror(errno));
                return EXIT_FAILURE;
                
            }
            break;
            
        case 'l':
            specified_addresses = 1;
            if (parse_addresses(optarg)) {
                LOGGER(ERROR, "Error parsing local addresses\n");
                return EXIT_FAILURE;
                
            }
            
            break;
            
        case 'p':
            port = strdup(optarg);
            break;
            
        case 'f':
            if (!check_format(optarg)) {
                LOGGER(ERROR, "Bad format provided: `%s'\n", optarg);
                return EXIT_FAILURE;
            }
            
            global_options.format = optarg;
            
            break;
            
        case 't':
            global_options.interval = strtoul(optarg, NULL, 10);
            if (interval <= 0 || interval >= MAX_OUTPUT_INTERVAL) {
                LOGGER(ERROR, "Bad interval provided\n");
                return EXIT_FAILURE;
            }
            
            break;
            
        case 'n':
            global_options.iterations = strtol(optarg, NULL, 10);
            if (interval < 0) {
                LOGGER(ERROR, "Bad iterations provided\n");
                return EXIT_FAILURE;
            }
            
            break;
            
        case 'T':
            global_options.threshold = strtol(optarg, NULL, 10) * 1000;
            if (global_options.threshold < 0) {
                LOGGER(ERROR, "Bad threshold provided\n");
                return EXIT_FAILURE;
            }
            
            break;

        case 'd':
            global_options.server = strdup(optarg);
            
            break;
        case 'i':
	        global_options.interface = strdup(optarg);

            break;


        case 'c':
            global_options.is_client = 1;
            break;

        case 's':
            global_options.header = optarg;
            global_options.show_header = 1;
            break;
            
        case 'S':
            global_options.show_header = 0;
            break;
            
        case 'h':
            dump_help(stdout);
            return EXIT_SUCCESS;

        case 'V':
            dump_version(stdout);
            return EXIT_SUCCESS;

        default:
            dump_usage(stderr);
            return EXIT_FAILURE;

        }

    }
    while (c != -1);
    
	if(! global_options.interface) {
        global_options.interface = "any";
	}

    if(global_options.is_client) {
        if(!global_options.server) {
            LOGGER(ERROR, "%s -d destination server is required.\n", argv[0]);
            return 0;
        }
    }
	if(global_options.server) {
		global_options.is_client = 1;
	} 
    if(!port) {
    	LOGGER(ERROR, "%s -p port is required.\n", argv[0]);
        return 0;
   	}

    // Set up signals
    sa.sa_handler = terminate;
    sigemptyset(&sa.sa_mask);
    sigaddset(&sa.sa_mask, SIGTERM);
    sigaddset(&sa.sa_mask, SIGINT);
    sa.sa_flags = 0;
    sa.sa_restorer = NULL;
    
    sigaction(SIGTERM, &sa, NULL);
    sigaction(SIGINT, &sa, NULL);
    
    // Get local addresses
    if (!specified_addresses && get_addresses() != 0)
        return EXIT_FAILURE;
    
    // Operations timestamp
    time(&timestamp);
    
    // Stats
    init_stats();
    
    if (capture_file) {
        output_offline_start(&global_options);

        offline_capture(capture_file);
        
        fclose(capture_file);
        
    }
    else {
        // Fire up capturing thread
        pthread_create(&capture_thread_id, NULL, capture, NULL);
        
		if(!global_options.threshold) {
        // Options thread
        	pthread_create(&output_thread_id, NULL, output_thread, &global_options);
        	pthread_kill(output_thread_id, SIGINT);
		}
        
        pthread_join(capture_thread_id, NULL);
        
    }
        
    free_stats();
    free_addresses();
	
	free(global_options.server);
    
    return EXIT_SUCCESS;

}