Beispiel #1
0
RequestInfo
end_request(RequestInfo ri, Request req) 
{
	RequestInfo tmp;
	stop_usage(req->usage);
	debug("Request %s:%i%s",req->host,req->port,req->path);
	dump_usage(req->usage);
	for (tmp = ri; tmp; tmp = tmp->next) {
		if(cmp(tmp->host,req->host))
			if (cmp(tmp->path,req->path)) {
				++tmp->hits;
				tmp->time = (req->usage->time + (tmp->hits-1) * tmp->time) / tmp->hits;
				return ri;
			}
	}
	tmp = (RequestInfo)reserve(sizeof(struct request_info_struct));	
	tmp->next = ri;
	tmp->host = req->host;
	tmp->path = req->path;
	tmp->time = req->usage->time;
	tmp->hits = req->usage->hits;
	return tmp;
}
Beispiel #2
0
int dump_help(FILE *stream) {
    dump_version(stream);
    dump_usage(stream);

    return 0;
}
Beispiel #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;

}
Beispiel #4
0
int main(int argc, char* argv[]) {
	int c;
	char *dbname = (char *)DBNAME;
	char *dbinst = (char *)DBINST;
	char *hostname = (char *)HOSTNAME;
	char *datadir = NULL;
	char *verdesc = NULL;
	char *verstr = NULL;
	char *prefix = NULL;
	unsigned int version = 0;
	char *schemastr = NULL;
	unsigned short usage = 0;
	unsigned short dblist = 0;
	unsigned short schemalist = 0;
	unsigned short prefixlist = 0;
	unsigned short trusted = 1;
	unsigned char term = '\0';

	// source files
	char udd_sql3[BUFLEN] = NULLSTR;
	char cert_yaml[BUFLEN] = NULLSTR;
	char gfxids_yaml[BUFLEN] = NULLSTR;
	char iconids_yaml[BUFLEN] = NULLSTR;
	char typeids_yaml[BUFLEN] = NULLSTR;
	char blueprints_yaml[BUFLEN] = NULLSTR;

	// SQL things
	int db3_rc;
	SQLRETURN ret;
	SQLCHAR connStrOut[BUFLEN] = NULLSTR;
	SQLCHAR dsn[BUFLEN] = NULLSTR;
	SQLCHAR auth[BUFLEN] = NULLSTR;
	SQLCHAR usr[BUFLEN] = NULLSTR;
	SQLCHAR pwd[BUFLEN] = NULLSTR;
	SQLSMALLINT connStrLen;

	// set globals
	GZIP_FLAG = 1;
	JSONP_FLAG = 1;
	SCHEMA = 0;
	JSON_DIR = NULL;
	H_ENV = SQL_NULL_HENV;
	H_DBC = SQL_NULL_HDBC;
	H_DBC2 = SQL_NULL_HDBC;
	DB3_UD = NULL;

	while ((c = getopt(argc, argv, "i:o:d:u:p:n:N:s:x:hvXDSZP")) != -1) {
		switch (c) {
		case 'i':
			datadir = (char *)malloc(BUFLEN);
			strlcpy(datadir, optarg, BUFLEN);
			term = datadir[strlen(datadir) - 1];
			if (term != PATHSEP) {
				strlcat(datadir, SZPATHSEP, BUFLEN);
			}
			break;
		case 'o':
			JSON_DIR = (char *)malloc(BUFLEN);
			strlcpy(JSON_DIR, optarg, BUFLEN);
			term = JSON_DIR[strlen(JSON_DIR) - 1];
			if (term != PATHSEP) {
				strlcat(JSON_DIR, SZPATHSEP, BUFLEN);
			}
			break;
		case 'x':
			prefix = (char *)malloc(BUFLEN);
			strlcpy(prefix, optarg, BUFLEN);
			break;
		case 'd':
			dbname = (char *)malloc(BUFLEN);
			strlcpy(dbname, optarg, BUFLEN);
			break;
		case 'n':
			verstr = (char *)malloc(BUFLEN);
			strlcpy(verstr, optarg, BUFLEN);
			break;
		case 'N':
			verdesc = (char *)malloc(BUFLEN);
			strlcpy(verdesc, optarg, BUFLEN);
			break;
		case 's':
			schemastr = (char *)malloc(BUFLEN);
			strlcpy(schemastr, optarg, BUFLEN);
			break;
		case 'S':
			schemalist = 1;
			break;
		case 'v':
			printf("sdd2json version %d.%d.%d", SDD2JSON_V_MAJOR, SDD2JSON_V_MINOR, SDD2JSON_V_PATCH);
			return 0;
		case 'D':
			dblist = 1;
			break;
		case 'u':
			trusted = 0;
			strlcpy(usr, optarg, BUFLEN);
			break;
		case 'p':
			trusted = 0;
			strlcpy(pwd, optarg, BUFLEN);
			break;
		case 'P':
			JSONP_FLAG = 0;
			break;
		case 'Z':
			GZIP_FLAG = 0;
			break;
		case 'X':
			prefixlist = 1;
		case '?':
			printf("\n");
			usage = 1;
			break;
		}
	}

	if (1 == schemalist) {
		for (unsigned int i = 0; i < VERS_N; i++) {
			printf("%s:\tversion %d;\tschema %d\n", VERS[i].version_desc, VERS[i].version_id, VERS[i].schema_id);
		}
		return 0;
	}

	if (1 == prefixlist) {
		printf("\tcrp\n");
		printf("\tdgm\n");
		printf("\tinv\n");
		printf("\tmap\n");
		printf("\tram\n");
		printf("\tsta\n");
		return 0;
	}

	if (usage < 1 && datadir == NULL) {
		fprintf(stderr, "EVE static data dir path is required\n");
		usage = 1;
	}

	if (usage < 1 && verstr == NULL) {
		fprintf(stderr, "static data version ID is required\n");
		usage = 1;
	}

	if (usage > 0) return dump_usage();

	// parse version/schema from params and known values
	if (verstr != NULL) {
		version = atoi(verstr);
	}
	if (version == 0) {
		fprintf(stderr, "the static data version ID provided is invalid: %s\n", verstr);
		return 1;
	}
	unsigned int found_version = 0;
	for (unsigned int i = 0; i < VERS_N; i++) {
		if (VERS[i].version_id == version) {
			found_version = 1;
			SCHEMA = VERS[i].schema_id;
			if (NULL == verdesc) {
				verdesc = (char *)malloc(BUFLEN);
				strlcpy(verdesc, VERS[i].version_desc, BUFLEN);
			}
			break;
		}
	}
	if (1 != found_version) {
		fprintf(stderr, "warning: using unknown static data version ID: %d\n", version);
	}
	if (schemastr != NULL) {
		if (0 != SCHEMA) {
			fprintf(stderr, "warning: overriding known schema ID\n");
		}
		SCHEMA = atoi(schemastr);
	}
	if (0 == SCHEMA) {
		fprintf(stderr, "schema ID is required\n");
		return 1;
	}
	printf("static data: '%s', version %d, schema %d\n", verdesc, version, SCHEMA);

	// validate input/schema
	printf("checking input: %s - ", datadir);
	if (ACCESS(datadir, 0) != 0) {
		printf("err\n");
		fprintf(stderr, "could not access output path\n");
		return 1;
	}
		
	if (SCHEMA >= 100038) {
		strlcpy(cert_yaml, datadir, BUFLEN);
		strlcat(cert_yaml, "certificates.yaml", BUFLEN);
		if (ACCESS(cert_yaml, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", cert_yaml);
			return 1;
		}
	}

	if (SCHEMA >= 100038) {
		strlcpy(gfxids_yaml, datadir, BUFLEN);
		strlcat(gfxids_yaml, "graphicIDs.yaml", BUFLEN);
		if (ACCESS(gfxids_yaml, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", gfxids_yaml);
			return 1;
		}
	}

	if (SCHEMA >= 100038) {
		strlcpy(iconids_yaml, datadir, BUFLEN);
		strlcat(iconids_yaml, "iconIDs.yaml", BUFLEN);
		if (ACCESS(iconids_yaml, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", iconids_yaml);
			return 1;
		}
	}

	if (SCHEMA >= 100038) {
		strlcpy(typeids_yaml, datadir, BUFLEN);
		strlcat(typeids_yaml, "typeIDs.yaml", BUFLEN);
		if (ACCESS(typeids_yaml, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", typeids_yaml);
			return 1;
		}
	}

	if (SCHEMA >= 100038) {
		strlcpy(udd_sql3, datadir, BUFLEN);
		strlcat(udd_sql3, "universeDataDx.db", BUFLEN);
		if (ACCESS(udd_sql3, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", udd_sql3);
			return 1;
		}
	}

	if (SCHEMA >= 100038) {
		strlcpy(blueprints_yaml, datadir, BUFLEN);
		strlcat(blueprints_yaml, "blueprints.yaml", BUFLEN);
		if (ACCESS(blueprints_yaml, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", blueprints_yaml);
			return 1;
		}
	}

	printf("OK\n");

	// validate output
	if (JSON_DIR == NULL) {
		JSON_DIR = (char *)malloc(2 * BUFLEN);
		strlcpy(JSON_DIR, datadir, 2 * BUFLEN);
		strlcat(JSON_DIR, SDD, 2* BUFLEN);
		strlcat(JSON_DIR, SZPATHSEP, 2 * BUFLEN);
	}
	printf("checking output: %s - ", JSON_DIR);
	if (ACCESS(JSON_DIR, 0) != 0) {
		if (MKDIR(JSON_DIR) != 0) {
			printf("err\n");
			fprintf(stderr, "could not create output path\n");
			return 1;
		}
		if (ACCESS(JSON_DIR, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access output path\n");
			return 1;
		}
	}
	printf("OK\n");

	// connect to SQLLITE dbs
	printf("connecting to [%s] - ", udd_sql3);

	db3_rc = sqlite3_open(udd_sql3, &DB3_UD);
	if (SQLITE_OK != db3_rc) return dump_db3_error(DB3_UD, 1);
	printf("OK\n");

	// connect to SQL server
	if (0 != trusted) {
		SNPRINTF(auth, BUFLEN, "Trusted_Connection=yes");
	}
	else {
		SNPRINTF(auth, BUFLEN, "User ID=%s;Password=%s", usr, pwd);
	}
	printf("connecting to [%s\\%s] using [%s] - ", hostname, dbinst, auth);

	ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HENV, &H_ENV);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1);
	ret = SQLSetEnvAttr(H_ENV, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3_80, 0);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1);

	if (dblist > 0) {
		printf("list drivers\n");
		unsigned short ret = check_drivers(1);
		close_handles();
		return ret;
	}

	if (0 != check_drivers(0)) {
		printf("err\n");
		fprintf(stderr, "SQL server driver not found\n");
		close_handles();
		return 1;
	}

	ret = SQLAllocHandle(SQL_HANDLE_DBC, H_ENV, &H_DBC);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1);
	ret = SQLAllocHandle(SQL_HANDLE_DBC, H_ENV, &H_DBC2);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1);

	SNPRINTF(dsn, BUFLEN, "Driver={%s};Server=%s\\%s;Database=%s;%s;", SQLDRV, hostname, dbinst, dbname, auth);
	ret = SQLDriverConnect(H_DBC, NULL, dsn, strnlen(dsn, BUFLEN), connStrOut, BUFLEN, &connStrLen, SQL_DRIVER_NOPROMPT);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_DBC, H_DBC, 1);
	ret = SQLDriverConnect(H_DBC2, NULL, dsn, strnlen(dsn, BUFLEN), connStrOut, BUFLEN, &connStrLen, SQL_DRIVER_NOPROMPT);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_DBC, H_DBC2, 1);

	printf("OK\n");

	// create metainfo file
	char metafile[BUFLEN] = NULLSTR;
	strlcpy(metafile, JSON_DIR, BUFLEN);
	strlcat(metafile, "metainf.json", BUFLEN);
	FILE *f = fopen(metafile, "w");
	if (f == NULL) {
		printf("err\n");
		fprintf(stderr, "error opening meta file: %s", metafile);
		close_handles();
		return 1;
	}
	printf("OK\n");
	fprintf(f, "{\n");
	fprintf(f, "\"formatID\":%d,\n", FORMAT_ID);
	fprintf(f, "\"schema\":%d,\n", SCHEMA);
	fprintf(f, "\"copy\":\"%s\",\n", CCPR);
	fprintf(f, "\"version\":%d,\n", version);
	fprintf(f, "\"verdesc\":\"%s\",\n", verdesc);
	fprintf(f, "\"tables\":{\n");

	// do stuff
	int rc = 0;
	int comma = 0;

	if (NULL == prefix || strncmp(prefix, "crp", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		rc = create_crp(f);
		if (rc != 0) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	if (NULL == prefix || strncmp(prefix, "dgm", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		rc = create_dgm(f, cert_yaml);
		if (rc != 0) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	if (NULL == prefix || strncmp(prefix, "inv", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		rc = create_inv(f, typeids_yaml, iconids_yaml);
		if (rc != 0) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	if (NULL == prefix || strncmp(prefix, "map", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		rc = create_map(f);
		if (0 != rc) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	if (NULL == prefix || strncmp(prefix, "ram", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		ret = create_ram(f, blueprints_yaml);
		if (!SQL_SUCCEEDED(ret)) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	if (NULL == prefix || strncmp(prefix, "sta", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		ret = create_sta(f);
		if (!SQL_SUCCEEDED(ret)) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	fprintf(f, "\n}\n"); // end of sources

	// clean up connections
	fprintf(f, "}\n");
	fclose(f);
	printf("metainf - ", metafile);
	post_file("metainf");

	close_handles();

	free(datadir);
	free(verdesc);
	free(verstr);
	free(prefix);
	free(JSON_DIR);

	printf("\nall done!\n");

	return 0;
}
Beispiel #5
0
int main(int argc, char *argv[])
{
  FILE *fp ;
  int nt,nosmear ;
  size_t nobj   ;
  Real *vary_matrix ;
  Real *coeff ;
  char vary_out[80] ;
  int i ;
  const int local_src = 0 ;
  char src_name[NAME_LEN] ;
  size_t  name_len = NAME_LEN ;
  char coeff_file[NAME_LEN] ;
  int which_source ; 
  int arg_pt ;
  enum which_corr { NOT_SET = -10 , SINGLE_SRC , VARY_SRC} ;
  int what_src = NOT_SET ;

  int what_fold = NO_TIME_AVERAGE ; /** default is not to average over time ***/
   int byte_rev_flag ; 

  /*********..........**********........*****************/

  printf("=============================================================\n");
  printf(" Extraction of LS and SS correlators from variational matrix \n");
  printf("=============================================================\n");

  if( argc < 3  )
  {
    dump_usage(argv[0]) ;
  }
  strcpy(vary_out,argv[1]);
  which_source = atoi(argv[2]);

  /** parse the input arguments *******/
  arg_pt = 2 ;
  while( arg_pt < argc )
  {

    if( strcmp(argv[arg_pt],"-s" ) == 0 )
    {
      if( what_src != NOT_SET ) dump_usage(argv[0]) ;
      which_source = atoi(argv[arg_pt + 1]);
      arg_pt += 2 ;
      what_src = SINGLE_SRC  ;
    }
    else if( strcmp(argv[arg_pt],"-c" ) == 0 )
    {
      if( what_src != NOT_SET ) dump_usage(argv[0]) ;
      strcpy(coeff_file ,argv[arg_pt+1]);
      arg_pt += 2 ;
      what_src = VARY_SRC  ;
    }
    else if( strcmp(argv[arg_pt],"-fold" ) == 0 )
    {
      what_fold = AVERAGE_TIME  ;
      arg_pt += 1 ;
    }
    else
    {
      dump_usage(argv[0]);
    }


  }


  /**** open the file and read the header  ******/
  fp = read_vary_header(vary_out, local_src,which_source,
			src_name,&nosmear, &nt,&byte_rev_flag ) ;


  printf("The number of timeslices = %d\n",nt);
  printf("The number of smearing functions = %d\n",nosmear);

  /* reserve memory for the variational matrix *****/
  nobj        =  (size_t) nt*nosmear*nosmear  ;
  vary_matrix =  (Real *)  calloc( nobj , sizeof(Real) );

  /** read the variational matrix from disk *****/
  if( fread(vary_matrix,sizeof(Real),nobj,fp) != nobj   )
  {
    printf("There was an error during the reading of the variational matrix \n");
    exit(1);
  }

  /*** close the file ****/
  if( fclose(fp) != 0 )
  {
    printf("There was an error during the closing of %s \n",vary_out);
    exit(1);
  }

  /***  byte_reverse if required ***/

  if(  byte_rev_flag == do_byte_rev    )
  {
    byte_rev_array((int*) vary_matrix, (int) nobj); 
  }




  printf("I have read the variational matrix from the file %s\n",vary_out);

  /* write the correlatorsto file *****/ 
  if( what_src == SINGLE_SRC  )
  {
    if( which_source < 0 || which_source >= nosmear)
    {
      printf("ERROR which_source = %d is out of range 0 to %d\n",which_source,nosmear);
      exit(2);
    }
    printf("smearing_function_file  = %s\n",src_name);

    dump_LSandSS_corr(vary_matrix,which_source, vary_out, local_src,what_fold,nt, nosmear);
  }
  else if( what_src == VARY_SRC  )
  {
    coeff = (Real *) calloc( (size_t) nosmear, sizeof(Real) );
    read_coeff_file(coeff_file ,coeff, nosmear);
    dump_vary_LSandSS_corr(vary_matrix,coeff, vary_out, local_src,what_fold,nt, nosmear);
    free(coeff);
  }
  else
  {
    printf("Bad option chosen \n");
    dump_usage(argv[0]);
  }
  
  if(what_fold == AVERAGE_TIME   )
  {
    printf("\nThe correlators will be averaged over positive and negative times\n");
  }


  /* Free up the memory used in the calculation ***/
  free(vary_matrix);

  return 0 ;

}