void
process_args(int argc, char **argv)
{
	FILE *fp;
	char **sites;
	int n;
	user_table_t *table;
       
	sites = getsites(monitor);
	for (n = 0; n <= N_MONITOR_SITES; ++n) {
		if (sites[n] == NULL)
			break;
	}

	table = user_table_new();

	if (argc == 0) {
		parse_log(stdin, table, sites, n);
		goto end;
	}

	while (argc--) {
		if (is_verbose)
			printf ("=> Parsing logfile: %s\n", *argv);
		fp = fopen(*argv++, "r");

		if (fp == NULL) {
			DEBUG(LOG_DEFAULT, "fopen error\n");
			continue;
		}
		parse_log(fp, table, sites, n);
		fclose(fp);
	}

end:
	if (is_verbose) {
		printf("DONE!\n");
	}

//	printf("Time period;%ld;%ld\n", time_l, time_h);
	
	if (csvfile != NULL)
		user_table_write_csv(table, sites, csvfile);	
	else
		user_table_print(table, sites);

	free (sites);

	user_table_del(&table);
}
Example #2
0
int parse_factor(CALC_ELEMENT ** e)
{
	PARSE_SYMBOL s = get_symbol();
	switch (s) {
	case PARSE_NUMBER:
		*e = create_number(get_current_number());
		accept(s);
		return 0;
		break;
	case PARSE_X:
		*e = create_x();
		accept(s);
		return 0;
		break;
	case PARSE_LOG:
		return parse_log(e);
		break;
	case PARSE_LPAREN:
		return parse_paren_expr(e);
		break;
	default:
		parser_error("\'number\', \'x\', \'log\' or \'(\'");
		return -1;
	}
}
Example #3
0
TEST_F(MPIProfileTest, progress)
{
    struct geopm_prof_c *prof;
    uint64_t region_id[3];
    struct geopm_time_s start, curr;
    double timeout = 0.0;
    int rank;
    int num_node = 0;

    (void) geopm_comm_num_node(MPI_COMM_WORLD, &num_node);
    ASSERT_TRUE(num_node > 1);

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    ASSERT_EQ(0, geopm_prof_create("progress_test", m_shm_key, MPI_COMM_WORLD, &prof));

    ASSERT_EQ(0, geopm_prof_region(prof, "loop_one", GEOPM_POLICY_HINT_UNKNOWN, &region_id[0]));
    ASSERT_EQ(0, geopm_prof_enter(prof, region_id[0]));
    ASSERT_EQ(0, geopm_time(&start));
    while (timeout < 1.0) {
        ASSERT_EQ(0, geopm_time(&curr));
        timeout = geopm_time_diff(&start, &curr);
        geopm_prof_progress(prof, region_id[0], timeout/1.0);
    }
    ASSERT_EQ(0, geopm_prof_exit(prof, region_id[0]));

    timeout = 0.0;
    ASSERT_EQ(0, geopm_prof_region(prof, "loop_two", GEOPM_POLICY_HINT_UNKNOWN, &region_id[1]));
    ASSERT_EQ(0, geopm_prof_enter(prof, region_id[1]));
    ASSERT_EQ(0, geopm_time(&start));
    while (timeout < 2.0) {
        ASSERT_EQ(0, geopm_time(&curr));
        timeout = geopm_time_diff(&start, &curr);
        geopm_prof_progress(prof, region_id[1], timeout/2.0);
    }
    ASSERT_EQ(0, geopm_prof_exit(prof, region_id[1]));

    timeout = 0.0;
    ASSERT_EQ(0, geopm_prof_region(prof, "loop_three", GEOPM_POLICY_HINT_UNKNOWN, &region_id[2]));
    ASSERT_EQ(0, geopm_prof_enter(prof, region_id[2]));
    ASSERT_EQ(0, geopm_time(&start));
    while (timeout < 3.0) {
        ASSERT_EQ(0, geopm_time(&curr));
        timeout = geopm_time_diff(&start, &curr);
        geopm_prof_progress(prof, region_id[2], timeout/3.0);
    }
    ASSERT_EQ(0, geopm_prof_exit(prof, region_id[2]));

    ASSERT_EQ(0, geopm_prof_print(prof, m_log_file.c_str(), 0));

    if (m_is_node_root) {
        ASSERT_EQ(0, parse_log(false));
    }

    ASSERT_EQ(0, geopm_prof_destroy(prof));
}
Example #4
0
/* make sure we have valid hits */
int
test_format (GLog * logger)
{
  if (parse_log (&logger, NULL, 20))
    FATAL ("Error while processing file");

  if ((logger->process == 0) || (logger->process == logger->invalid))
    return 1;
  return 0;
}
Example #5
0
 virtual void write(
     buffer_t const * bufs, 
     size_t len)
 {
     framework::process::Message msg;
     parse_log(msg, bufs, len);
     if (!msg.receiver.empty()) {
         m_msgqueue_.push(msg);
     }
 }
Example #6
0
static void
perform_tail_follow (uint64_t * size1)
{
  uint64_t size2 = 0;
  char buf[LINE_BUFFER];
  FILE *fp = NULL;

  if (logger->piping)
    return;

  size2 = file_size (conf.ifile);

  /* file hasn't changed */
  if (size2 == *size1)
    return;

  if (!(fp = fopen (conf.ifile, "r")))
    FATAL ("Unable to read log file %s.", strerror (errno));
  if (!fseeko (fp, *size1, SEEK_SET))
    while (fgets (buf, LINE_BUFFER, fp) != NULL)
      parse_log (&logger, buf, -1);
  fclose (fp);

  *size1 = size2;
  pthread_mutex_lock (&gdns_thread.mutex);
  free_holder (&holder);
  pthread_cond_broadcast (&gdns_thread.not_empty);
  pthread_mutex_unlock (&gdns_thread.mutex);

  free_dashboard (dash);
  allocate_holder ();
  allocate_data ();

  term_size (main_win);
  render_screens ();
  usleep (200000);      /* 0.2 seconds */
}
static int open_log(struct reenc_ctx *rc)
{
	int flags = opt_fsync ? O_SYNC : 0;

	rc->log_fd = open(rc->log_file, O_RDWR|O_EXCL|O_CREAT|flags, S_IRUSR|S_IWUSR);
	if (rc->log_fd != -1) {
		log_dbg("Created LUKS reencryption log file %s.", rc->log_file);
	} else if (errno == EEXIST) {
		log_std(_("Log file %s exists, resuming reencryption.\n"), rc->log_file);
		rc->log_fd = open(rc->log_file, O_RDWR|flags);
		rc->in_progress = 1;
	}

	if (rc->log_fd == -1)
		return -EINVAL;

	if (!rc->in_progress && write_log(rc) < 0) {
		close_log(rc);
		return -EIO;
	}

	/* Be sure it is correct format */
	return parse_log(rc);
}
Example #8
0
int
main (int argc, char **argv)
{
  int quit = 0;

#if defined(__GLIBC__)
  setup_signal_handlers ();
#endif

  /* command line/config options */
  verify_global_config (argc, argv);
  parse_conf_file (&argc, &argv);
  parse_cmd_line (argc, argv);

  /* initialize storage */
  init_storage ();
  /* setup to use the current locale */
  set_locale ();

#ifdef HAVE_LIBGEOIP
  init_geoip ();
#endif

  /* init logger */
  logger = init_log ();
  set_signal_data (logger);

  /* init parsing spinner */
  parsing_spinner = new_gspinner ();
  parsing_spinner->process = &logger->process;

  /* outputting to stdout */
  if (conf.output_html) {
    ui_spinner_create (parsing_spinner);
    goto out;
  }

  /* init curses */
  set_input_opts ();
  if (conf.no_color || has_colors () == FALSE) {
    conf.color_scheme = NO_COLOR;
    conf.no_color = 1;
  } else {
    start_color ();
  }
  init_colors ();
  init_windows (&header_win, &main_win);
  set_curses_spinner (parsing_spinner);

  /* configuration dialog */
  if (isatty (STDIN_FILENO) && (conf.log_format == NULL || conf.load_conf_dlg)) {
    refresh ();
    quit = render_confdlg (logger, parsing_spinner);
  }
  /* straight parsing */
  else {
    ui_spinner_create (parsing_spinner);
  }

out:

  /* main processing event */
  time (&start_proc);
  if (conf.load_from_disk)
    set_general_stats ();
  else if (!quit && parse_log (&logger, NULL, -1))
    FATAL ("Error while processing file");

  logger->offset = logger->process;

  /* no valid entries to process from the log */
  if (logger->process == 0)
    FATAL ("Nothing valid to process.");

  /* init reverse lookup thread */
  gdns_init ();
  parse_initial_sort ();
  allocate_holder ();

  end_spinner ();
  time (&end_proc);

  /* stdout */
  if (conf.output_html)
    standard_output ();
  /* curses */
  else
    curses_output ();

  /* clean */
  house_keeping ();

  return EXIT_SUCCESS;
}
Example #9
0
static void
server_main(int argc, char **argv)
{
	int did;
	int c;
	struct statvfs vfsbuf;
	int imexit = 0;
	pid_t parent;
	char *root = NULL;
	char *sadmdir = NULL;
	hrtime_t delta;
	int dir = 0;
	int dfd;

	(void) set_prog_name("pkgserv");

	openlog("pkgserv", LOG_PID | LOG_ODELAY, LOG_DAEMON);

	while ((c = getopt(argc, argv, "d:eoN:pP:R:r:")) != EOF) {
		switch (c) {
		case 'e':
			imexit = 1;
			break;
		case 'd':
			sadmdir = optarg;
			if (*sadmdir != '/' || strlen(sadmdir) >= PATH_MAX ||
			    access(sadmdir, X_OK) != 0)
				exit(99);
			break;
		case 'N':
			(void) set_prog_name(optarg);
			break;
		case 'o':
			one_shot = B_TRUE;
			verbose = 0;
			break;
		case 'p':
			/*
			 * We are updating possibly many zones; so we're not
			 * dumping based on a short timeout and we will not
			 * exit.
			 */
			permanent = B_TRUE;
			dumptimeout = 3600;
			break;
		case 'P':
			client_pid = atoi(optarg);
			break;
		case 'R':
			root = optarg;
			if (*root != '/' || strlen(root) >= PATH_MAX ||
			    access(root, X_OK) != 0)
				exit(99);
			break;
		case 'r':
			read_only = B_TRUE;
			one_shot = B_TRUE;
			verbose = 0;
			door = optarg;
			break;
		default:
			exit(99);
		}
	}

	if (one_shot && permanent) {
		progerr(gettext("Incorrect Usage"));
		exit(99);
	}

	umem_nofail_callback(no_memory_abort);

	if (root != NULL && strcmp(root, "/") != 0) {
		if (snprintf(pkgdir, PATH_MAX, "%s%s", root,
		    sadmdir == NULL ? SADM_DIR : sadmdir) >= PATH_MAX) {
			exit(99);
		}
	} else {
		if (sadmdir == NULL)
			(void) strcpy(pkgdir, SADM_DIR);
		else
			(void) strcpy(pkgdir, sadmdir);
	}

	if (chdir(pkgdir) != 0) {
		progerr(gettext("can't chdir to %s"), pkgdir);
		exit(2);
	}

	closefrom(3);

	if (!read_only && establish_lock(LOCK) < 0) {
		progerr(gettext(
		    "couldn't lock in %s (server running?): %s"),
		    pkgdir, strerror(errno));
		exit(1);
	}

	did = door_create(pkg_door_srv, 0, DOOR_REFUSE_DESC);
	if (did == -1) {
		progerr("door_create: %s", strerror(errno));
		exit(2);
	}

	(void) fdetach(door);

	if ((dfd = creat(door, 0644)) < 0 || close(dfd) < 0) {
		progerr("door_create: %s", strerror(errno));
		exit(2);
	}

	(void) mutex_lock(&mtx);

	myuid = geteuid();

	(void) sigset(SIGHUP, signal_handler);
	(void) sigset(SIGTERM, signal_handler);
	(void) sigset(SIGINT, signal_handler);
	(void) sigset(SIGQUIT, signal_handler);

	(void) signal(SIGPIPE, SIG_IGN);

	(void) atexit(finish);

	if (fattach(did, door) != 0) {
		progerr(gettext("attach door: %s"), strerror(errno));
		exit(2);
	}
	(void) close(did);

	ecache = umem_cache_create("entry", sizeof (pkgentry_t),
	    sizeof (char *), NULL, NULL, NULL, NULL, NULL, 0);

	avl_create(list, avlcmp, sizeof (pkgentry_t),
	    offsetof(pkgentry_t, avl));

	IS_ST0['\0'] = 1;
	IS_ST0[' '] = 1;
	IS_ST0['\t'] = 1;

	IS_ST0Q['\0'] = 1;
	IS_ST0Q[' '] = 1;
	IS_ST0Q['\t'] = 1;
	IS_ST0Q['='] = 1;

	parse_contents();
	if (parse_log() > 0)
		pkgdump();

	if (imexit)
		exit(0);

	if (statvfs(".", &vfsbuf) != 0) {
		progerr(gettext("statvfs: %s"), strerror(errno));
		exit(2);
	}

	if (strcmp(vfsbuf.f_basetype, "zfs") == 0)
		flushbeforemark = 0;

	/* We've started, tell the parent */
	parent = getppid();
	if (parent != 1)
		(void) kill(parent, SIGUSR1);

	if (!one_shot) {
		int fd;
		(void) setsid();
		fd = open("/dev/null", O_RDWR, 0);
		if (fd >= 0) {
			(void) dup2(fd, STDIN_FILENO);
			(void) dup2(fd, STDOUT_FILENO);
			(void) dup2(fd, STDERR_FILENO);
			if (fd > 2)
				(void) close(fd);
		}
	}

	lastcall = lastchange = gethrtime();

	/*
	 * Start the main thread, here is where we unlock the mutex.
	 */
	for (;;) {
		if (want_to_quit) {
			pkgdump();
			exit(0);
		}
		/* Wait forever when root or when there's a running filter */
		if (write_locked ||
		    (!one_shot && permanent && dir == changes)) {
			(void) cond_wait(&cv, &mtx);
			continue;
		}
		delta = time_since_(lastchange);
		/* Wait until DUMPTIMEOUT after last change before we pkgdump */
		if (delta < dumptimeout * LLNANOSEC) {
			my_cond_reltimedwait(delta, dumptimeout);
			continue;
		}
		/* Client still around? Just wait then. */
		if (client_pid > 1 && kill(client_pid, 0) == 0) {
			lastchange = lastcall = gethrtime();
			continue;
		}
		/* Wait for another EXITTIMEOUT seconds before we exit */
		if ((one_shot || !permanent) && dir == changes) {
			delta = time_since_(lastcall);
			if (delta < EXITTIMEOUT * LLNANOSEC) {
				my_cond_reltimedwait(delta, EXITTIMEOUT);
				continue;
			}
			exit(0);
		}
		pkgdump();
		dir = changes;
	}

	/*NOTREACHED*/
}
Example #10
0
int main(

  int   argc,
  char *argv[])

  {
  /* Array for the log entries for the specified job */
  FILE *fp;
  int i, j;
  int file_count;
  char *filenames[MAX_LOG_FILES_PER_DAY];  /* full path of logfiles to read */

  struct tm *tm_ptr;
  time_t t, t_save;
  signed char c;
  char *prefix_path = NULL;
  int number_of_days = 1;
  char *endp;
  short error = 0;
  int opt;
  char no_acct = 0, no_svr = 0, no_mom = 0, no_schd = 0;
  char verbosity = 1;
  int wrap = -1;
  int log_filter = 0;
  int event_type;
  char filter_excessive = 0;
  int excessive_count;

#if defined(FILTER_EXCESSIVE)
  filter_excessive = 1;
#endif

#if defined(EXCESSIVE_COUNT)
  excessive_count = EXCESSIVE_COUNT;
#endif

  while ((c = getopt(argc, argv, "qzvamslw:p:n:f:c:")) != EOF)
    {
    switch (c)
      {

      case 'q':

        verbosity = 0;

        break;

      case 'v':

        verbosity = 2;

        break;

      case 'a':

        no_acct = 1;

        break;

      case 's':

        no_svr = 1;

        break;

      case 'm':

        no_mom = 1;

        break;

      case 'l':

        no_schd = 1;

        break;

      case 'z':

        filter_excessive = filter_excessive ? 0 : 1;

        break;

      case 'c':

        excessive_count = strtol(optarg, &endp, 10);

        if (*endp != '\0')
          error = 1;

        break;

      case 'w':

        wrap = strtol(optarg, &endp, 10);

        if (*endp != '\0')
          error = 1;

        break;

      case 'p':

        prefix_path = optarg;

        break;

      case 'n':

        number_of_days = strtol(optarg, &endp, 10);

        if (*endp != '\0')
          error = 1;

        break;

      case 'f':

        if (!strcmp(optarg, "error"))
          log_filter |= PBSEVENT_ERROR;
        else if (!strcmp(optarg, "system"))
          log_filter |= PBSEVENT_SYSTEM;
        else if (!strcmp(optarg, "admin"))
          log_filter |= PBSEVENT_ADMIN;
        else if (!strcmp(optarg, "job"))
          log_filter |= PBSEVENT_JOB;
        else if (!strcmp(optarg, "job_usage"))
          log_filter |= PBSEVENT_JOB_USAGE;
        else if (!strcmp(optarg, "security"))
          log_filter |= PBSEVENT_SECURITY;
        else if (!strcmp(optarg, "sched"))
          log_filter |= PBSEVENT_SCHED;
        else if (!strcmp(optarg, "debug"))
          log_filter |= PBSEVENT_DEBUG;
        else if (!strcmp(optarg, "debug2"))
          log_filter |= PBSEVENT_DEBUG2;
        else if (isdigit(optarg[0]))
          {
          log_filter = strtol(optarg, &endp, 16);

          if (*endp != '\0')
            error = 1;
          }
        else
          error = 1;

        break;

      default:

        error = 1;

        break;
      }
    }    /* END while ((c = getopt(argc,argv,"zvamslw:p:n:f:c:")) != EOF) */


  /* no jobs */

  if ((error != 0) || (argc == optind))
    {
    printf("USAGE: %s [-a|s|l|m|q|v|z] [-c count] [-w size] [-p path] [-n days] [-f filter_type] <JOBID>\n",
           strip_path(argv[0]));

    printf(
      "   -p : path to PBS_SERVER_HOME\n"
      "   -w : number of columns of your terminal\n"
      "   -n : number of days in the past to look for job(s) [default 1]\n"
      "   -f : filter out types of log entries, multiple -f's can be specified\n"
      "        error, system, admin, job, job_usage, security, sched, debug, \n"
      "        debug2, or absolute numeric hex equivalent\n"
      "   -z : toggle filtering excessive messages\n");
    printf(
      "   -c : what message count is considered excessive\n"
      "   -a : don't use accounting log files\n"
      "   -s : don't use server log files\n"
      "   -l : don't use scheduler log files\n"
      "   -m : don't use mom log files\n"
      "   -q : quiet mode - hide all error messages\n"
      "   -v : verbose mode - show more error messages\n");

    printf("default prefix path = %s\n",
           PBS_SERVER_HOME);

#if defined(FILTER_EXCESSIVE)
    printf("filter_excessive: ON\n");
#else
    printf("filter_excessive: OFF\n");
#endif
    return(1);
    }  /* END if ((error != 0) || (argc == optind)) */

  if (wrap == -1)
    wrap = get_cols();

  time(&t);

  t_save = t;

  for (opt = optind;opt < argc;opt++)
    {
    for (i = 0, t = t_save;i < number_of_days;i++, t -= SECONDS_IN_DAY)
      {
      tm_ptr = localtime(&t);

      for (j = 0;j < 4;j++)
        {
        if ((j == IND_ACCT && no_acct) || (j == IND_SERVER && no_svr) ||
            (j == IND_MOM && no_mom)   || (j == IND_SCHED && no_schd))
          continue;

        file_count = log_path(prefix_path, j, tm_ptr, filenames);
        
        /* there can be multiple server and mom log files per day */
        /* traverse filenames until we have got them all */
        
        if (file_count < 0)
          {
          printf("Error getting file names\n");
          continue;
          }

        for (; file_count > 0; file_count--)
          {
          if ((fp = fopen(filenames[file_count-1], "r")) == NULL)
            {
            if (verbosity >= 1)
              perror(filenames[file_count-1]);

            continue;
            }

          if (parse_log(fp, argv[opt], j) < 0)
            {
            /* no valid entries located in file */

            if (verbosity >= 1)
              {
              fprintf(stderr, "%s: No matching job records located\n",
                      filenames[file_count-1]);
              }
            }
          else if (verbosity >= 2)
            {
            fprintf(stderr, "%s: Successfully located matching job records\n",
                    filenames[file_count-1]);
            }

          fclose(fp);
          free(filenames[file_count-1]);
          } /* end of for file_count */
        }
      }    /* END for (i) */

    if (filter_excessive)
      filter_excess(excessive_count);

    qsort(log_lines, ll_cur_amm, sizeof(struct log_entry), sort_by_date);

    if (ll_cur_amm != 0)
      {
      printf("\nJob: %s\n\n",
             log_lines[0].name);
      }

    for (i = 0;i < ll_cur_amm;i++)
      {
      if (log_lines[i].log_file == 'A')
        event_type = 0;
      else
        event_type = strtol(log_lines[i].event, &endp, 16);

      if (!(log_filter & event_type) && !(log_lines[i].no_print))
        {
        printf("%-20s %-5c",
               log_lines[i].date,
               log_lines[i].log_file);

        line_wrap(log_lines[i].msg, 26, wrap);
        }
      }
    }    /* END for (opt) */

  return(0);
  }  /* END main() */
Example #11
0
int main(int argc, char *argv[])
{
	int retval = EXIT_FAILURE, ok = 0, fail = 1, n = 0, i = 0, offset = 0;
	int offsets[MAX_HEAD_SIZE] = { 0 };
	char *buf = NULL, *ptr = NULL, *fname = NULL, *log = NULL;
	size_t size = 0, nsize = 0;

	/* Check usage */
	if(argc < 2 || argv[1][0] == '-')
	{
		fprintf(stderr, USAGE, argv[0]);
		goto end;
	}
	else
	{
		fname = argv[1];
	
		if(argc == 3)
		{
			log = argv[2];
		}
	}

	/* Read in target file */
	buf = file_read(fname, &size);

	if(buf && size > MIN_FILE_SIZE)
	{
		/* Parse in the log file, if any */
		n = parse_log(log, offsets);

		fprintf(stderr, "Processing %d header(s) from %s...\n", n, fname);

		/* Loop through each offset in the integer array */
		for(i=0; i<n; i++)
		{
			ok = 0;
			offset = offsets[i];
			nsize = size - offset;
			ptr = (buf + offset);

			fprintf(stderr, "Processing header at offset %d...", offset);

			/* Identify and patch the header at each offset */
			switch(identify_header(ptr))
			{
				case TRX:
					ok = patch_trx(ptr, nsize);
					break;
				case UIMAGE:
					ok = patch_uimage(ptr, nsize);
					break;
				default:
					fprintf(stderr, "sorry, this file type is not supported.\n");
					break;
			}

			if(ok)
			{
				fail = 0;
				fprintf(stderr, "checksum(s) updated OK.\n");
			}
			else
			{
				fprintf(stderr, "checksum update(s) failed!\n");
			}
		}
	}

	if(!fail)
	{
		if(!file_write(fname, buf, size))
		{
			fprintf(stderr, "Failed to save data to file '%s'\n", fname);
		}
		else
		{
			fprintf(stderr, "CRC(s) updated successfully.\n");
			retval = EXIT_SUCCESS;
		}
	}
	else
	{
		fprintf(stderr, "CRC update failed.\n");
	}

end:
	if(buf) free(buf);
	return retval;
}
Example #12
0
int
main (int argc, char **argv)
{
  int quit = 0;

#if defined(__GLIBC__)
  setup_signal_handlers ();
#endif

  /* command line/config options */
  verify_global_config (argc, argv);
  parse_conf_file (&argc, &argv);
  parse_cmd_line (argc, argv);

  /* initialize storage */
  init_storage ();
  /* setup to use the current locale */
  set_locale ();

#ifdef HAVE_LIBGEOIP
  init_geoip ();
#endif

  /* init logger */
  logger = init_log ();
  /* init parsing spinner */
  parsing_spinner = new_gspinner ();
  parsing_spinner->process = &logger->process;

  /* outputting to stdout */
  if (conf.output_html) {
    ui_spinner_create (parsing_spinner);
    goto out;
  }

  /* init curses */
  set_input_opts ();
  if (conf.no_color || has_colors () == FALSE) {
    conf.color_scheme = NO_COLOR;
    conf.no_color = 1;
  } else {
    start_color ();
  }
  init_colors ();
  init_windows (&header_win, &main_win);
  set_curses_spinner (parsing_spinner);

  /* configuration dialog */
  if (isatty (STDIN_FILENO) && (conf.log_format == NULL || conf.load_conf_dlg)) {
    refresh ();
    quit = verify_format (logger, parsing_spinner);
  }
  /* straight parsing */
  else {
    ui_spinner_create (parsing_spinner);
  }

out:

  /* main processing event */
  time (&start_proc);
  if (conf.load_from_disk)
    set_general_stats ();
  else if (!quit && parse_log (&logger, NULL, -1))
    FATAL ("Error while processing file");

  logger->offset = logger->process;

  /* no valid entries to process from the log */
  if ((logger->process == 0) || (logger->process == logger->invalid))
    FATAL ("Nothing valid to process.");

  /* init reverse lookup thread */
  gdns_init ();
  parse_initial_sort ();
  allocate_holder ();

  end_spinner ();
  time (&end_proc);

  /* stdout */
  if (conf.output_html) {
    /* CSV */
    if (conf.output_format && strcmp ("csv", conf.output_format) == 0)
      output_csv (logger, holder);
    /* JSON */
    else if (conf.output_format && strcmp ("json", conf.output_format) == 0)
      output_json (logger, holder);
    /* HTML */
    else
      output_html (logger, holder);
  }
  /* curses */
  else {
    allocate_data ();
    if (!conf.skip_term_resolver)
      gdns_thread_create ();

    render_screens ();
    get_keys ();

    attroff (COLOR_PAIR (COL_WHITE));
    /* restore tty modes and reset
     * terminal into non-visual mode */
    endwin ();
  }
  /* clean */
  house_keeping ();

  return EXIT_SUCCESS;
}
Example #13
0
static void
get_keys (void)
{
  int search;
  int c, quit = 1, scrll, offset, ok_mouse;
  int *scroll_ptr, *offset_ptr;
  int exp_size = DASH_EXPANDED - DASH_NON_DATA;
  MEVENT event;

  char buf[LINE_BUFFER];
  FILE *fp = NULL;
  unsigned long long size1 = 0, size2 = 0;

  if (!logger->piping)
    size1 = file_size (conf.ifile);
  while (quit) {
    c = wgetch (stdscr);
    switch (c) {
    case 'q':  /* quit */
      if (!scrolling.expanded) {
        quit = 0;
        break;
      }
      collapse_current_module ();
      break;
    case KEY_F (1):
    case '?':
    case 'h':
      load_help_popup (main_win);
      render_screens ();
      break;
    case 49:   /* 1 */
      /* reset expanded module */
      set_module_to (&scrolling, VISITORS);
      break;
    case 50:   /* 2 */
      /* reset expanded module */
      set_module_to (&scrolling, REQUESTS);
      break;
    case 51:   /* 3 */
      /* reset expanded module */
      set_module_to (&scrolling, REQUESTS_STATIC);
      break;
    case 52:   /* 4 */
      /* reset expanded module */
      set_module_to (&scrolling, NOT_FOUND);
      break;
    case 53:   /* 5 */
      /* reset expanded module */
      set_module_to (&scrolling, HOSTS);
      break;
    case 54:   /* 6 */
      /* reset expanded module */
      set_module_to (&scrolling, OS);
      break;
    case 55:   /* 7 */
      /* reset expanded module */
      set_module_to (&scrolling, BROWSERS);
      break;
    case 56:   /* 8 */
      /* reset expanded module */
      set_module_to (&scrolling, REFERRERS);
      break;
    case 57:   /* 9 */
      /* reset expanded module */
      set_module_to (&scrolling, REFERRING_SITES);
      break;
    case 48:   /* 0 */
      /* reset expanded module */
      set_module_to (&scrolling, KEYPHRASES);
      break;
    case 33:   /* Shift+1 */
      /* reset expanded module */
#ifdef HAVE_LIBGEOIP
      set_module_to (&scrolling, GEO_LOCATION);
#else
      set_module_to (&scrolling, STATUS_CODES);
#endif
      break;
#ifdef HAVE_LIBGEOIP
    case 64:   /* Shift+2 */
      /* reset expanded module */
      set_module_to (&scrolling, STATUS_CODES);
      break;
#endif
    case 9:    /* TAB */
      /* reset expanded module */
      collapse_current_module ();
      scrolling.current++;
      if (scrolling.current == TOTAL_MODULES)
        scrolling.current = 0;
      render_screens ();
      break;
    case 353:  /* Shift TAB */
      /* reset expanded module */
      collapse_current_module ();
      if (scrolling.current == 0)
        scrolling.current = TOTAL_MODULES - 1;
      else
        scrolling.current--;
      render_screens ();
      break;
    case 'g':  /* g = top */
      if (!scrolling.expanded)
        scrolling.dash = 0;
      else {
        scrolling.module[scrolling.current].scroll = 0;
        scrolling.module[scrolling.current].offset = 0;
      }
      display_content (main_win, logger, dash, &scrolling);
      break;
    case 'G':  /* G = down */
      if (!scrolling.expanded)
        scrolling.dash = dash->total_alloc - real_size_y;
      else {
        scrll = offset = 0;
        scrll = dash->module[scrolling.current].idx_data - 1;
        if (scrll >= exp_size && scrll >= offset + exp_size)
          offset = scrll < exp_size - 1 ? 0 : scrll - exp_size + 1;
        scrolling.module[scrolling.current].scroll = scrll;
        scrolling.module[scrolling.current].offset = offset;
      }
      display_content (main_win, logger, dash, &scrolling);
      break;
      /* expand dashboard module */
    case KEY_RIGHT:
    case 0x0a:
    case 0x0d:
    case 32:   /* ENTER */
    case 79:   /* o */
    case 111:  /* O */
    case KEY_ENTER:
      if (scrolling.expanded && scrolling.current == HOSTS) {
        /* make sure we have a valid IP */
        int sel = scrolling.module[scrolling.current].scroll;
        if (!invalid_ipaddr (dash->module[HOSTS].data[sel].data))
          load_agent_list (main_win, dash->module[HOSTS].data[sel].data);
        break;
      }
      if (scrolling.expanded)
        break;
      reset_scroll_offsets (&scrolling);
      scrolling.expanded = 1;

      free_holder_by_module (&holder, scrolling.current);
      free_dashboard (dash);
      allocate_holder_by_module (scrolling.current);
      allocate_data ();

      display_content (main_win, logger, dash, &scrolling);
      break;
    case KEY_DOWN:     /* scroll main dashboard */
      if ((scrolling.dash + real_size_y) < (unsigned) dash->total_alloc) {
        scrolling.dash++;
        display_content (main_win, logger, dash, &scrolling);
      }
      break;
    case KEY_MOUSE:    /* handles mouse events */
      ok_mouse = getmouse (&event);
      if (conf.mouse_support && ok_mouse == OK) {
        if (event.bstate & BUTTON1_CLICKED) {
          /* ignore header/footer clicks */
          if (event.y < MAX_HEIGHT_HEADER || event.y == LINES - 1)
            break;

          if (set_module_from_mouse_event (&scrolling, dash, event.y))
            break;
          reset_scroll_offsets (&scrolling);
          scrolling.expanded = 1;

          free_holder_by_module (&holder, scrolling.current);
          free_dashboard (dash);
          allocate_holder_by_module (scrolling.current);
          allocate_data ();

          render_screens ();
        }
      }
      break;
    case 106:  /* j - DOWN expanded module */
      scroll_ptr = &scrolling.module[scrolling.current].scroll;
      offset_ptr = &scrolling.module[scrolling.current].offset;

      if (!scrolling.expanded)
        break;
      if (*scroll_ptr >= dash->module[scrolling.current].idx_data - 1)
        break;
      ++(*scroll_ptr);
      if (*scroll_ptr >= exp_size && *scroll_ptr >= *offset_ptr + exp_size)
        ++(*offset_ptr);
      display_content (main_win, logger, dash, &scrolling);
      break;
      /* scroll up main_win */
    case KEY_UP:
      if (scrolling.dash > 0) {
        scrolling.dash--;
        display_content (main_win, logger, dash, &scrolling);
      }
      break;
    case 2:    /* ^ b - page up */
    case 339:  /* ^ PG UP */
      scroll_ptr = &scrolling.module[scrolling.current].scroll;
      offset_ptr = &scrolling.module[scrolling.current].offset;

      if (!scrolling.expanded)
        break;
      /* decrease scroll and offset by exp_size */
      *scroll_ptr -= exp_size;
      if (*scroll_ptr < 0)
        *scroll_ptr = 0;

      if (*scroll_ptr < *offset_ptr)
        *offset_ptr -= exp_size;
      if (*offset_ptr <= 0)
        *offset_ptr = 0;
      display_content (main_win, logger, dash, &scrolling);
      break;
    case 6:    /* ^ f - page down */
    case 338:  /* ^ PG DOWN */
      scroll_ptr = &scrolling.module[scrolling.current].scroll;
      offset_ptr = &scrolling.module[scrolling.current].offset;

      if (!scrolling.expanded)
        break;

      *scroll_ptr += exp_size;
      if (*scroll_ptr >= dash->module[scrolling.current].idx_data - 1)
        *scroll_ptr = dash->module[scrolling.current].idx_data - 1;
      if (*scroll_ptr >= exp_size && *scroll_ptr >= *offset_ptr + exp_size)
        *offset_ptr += exp_size;
      if (*offset_ptr + exp_size >=
          dash->module[scrolling.current].idx_data - 1)
        *offset_ptr = dash->module[scrolling.current].idx_data - exp_size;
      if (*scroll_ptr < exp_size - 1)
        *offset_ptr = 0;

      display_content (main_win, logger, dash, &scrolling);
      break;
    case 107:  /* k - UP expanded module */
      scroll_ptr = &scrolling.module[scrolling.current].scroll;
      offset_ptr = &scrolling.module[scrolling.current].offset;

      if (!scrolling.expanded)
        break;
      if (*scroll_ptr <= 0)
        break;
      --(*scroll_ptr);
      if (*scroll_ptr < *offset_ptr)
        --(*offset_ptr);
      display_content (main_win, logger, dash, &scrolling);
      break;
    case 'n':
      pthread_mutex_lock (&gdns_thread.mutex);
      search = perform_next_find (holder, &scrolling);
      pthread_mutex_unlock (&gdns_thread.mutex);
      if (search == 0) {
        free_dashboard (dash);
        allocate_data ();
        render_screens ();
      }
      break;
    case '/':
      if (render_find_dialog (main_win, &scrolling))
        break;
      pthread_mutex_lock (&gdns_thread.mutex);
      search = perform_next_find (holder, &scrolling);
      pthread_mutex_unlock (&gdns_thread.mutex);
      if (search == 0) {
        free_dashboard (dash);
        allocate_data ();
        render_screens ();
      }
      break;
    case 99:   /* c */
      if (conf.no_color)
        break;
      load_schemes_win (main_win);
      free_dashboard (dash);
      allocate_data ();
      render_screens ();
      break;
    case 115:  /* s */
      load_sort_win (main_win, scrolling.current,
                     &module_sort[scrolling.current]);
      pthread_mutex_lock (&gdns_thread.mutex);
      free_holder (&holder);
      pthread_cond_broadcast (&gdns_thread.not_empty);
      pthread_mutex_unlock (&gdns_thread.mutex);
      free_dashboard (dash);
      allocate_holder ();
      allocate_data ();
      render_screens ();
      break;
    case 269:
    case KEY_RESIZE:
      endwin ();
      refresh ();
      werase (header_win);
      werase (main_win);
      werase (stdscr);
      term_size (main_win);
      refresh ();
      render_screens ();
      break;
    default:
      if (logger->piping)
        break;
      size2 = file_size (conf.ifile);

      /* file has changed */
      if (size2 != size1) {
        if (!(fp = fopen (conf.ifile, "r")))
          FATAL ("Unable to read log file %s.", strerror (errno));
        if (!fseeko (fp, size1, SEEK_SET))
          while (fgets (buf, LINE_BUFFER, fp) != NULL)
            parse_log (&logger, buf, -1);
        fclose (fp);

        size1 = size2;
        pthread_mutex_lock (&gdns_thread.mutex);
        free_holder (&holder);
        pthread_cond_broadcast (&gdns_thread.not_empty);
        pthread_mutex_unlock (&gdns_thread.mutex);

        free_dashboard (dash);
        allocate_holder ();
        allocate_data ();

        term_size (main_win);
        render_screens ();
        usleep (200000);        /* 0.2 seconds */
      }
      break;
    }
  }
}