Skip to content

Commit

Permalink
adding file name rotation to stats output
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmcgrew committed May 12, 2021
1 parent dcd8ed6 commit e619f51
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ MERC_H += pkt_processing.h
MERC_H += pcap_file_io.h
MERC_H += pcap_reader.h
MERC_H += rnd_pkt_drop.h
MERC_H += rotator.h
MERC_H += signal_handling.h

MERC_OBJ = $(MERCC:%.cc=%.o) $(MERC:%.c=%.o)
Expand Down
21 changes: 16 additions & 5 deletions src/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <unistd.h>
#include <string>
#include <atomic>
#include "rotator.h"
#include "libmerc/libmerc.h"

class controller {
Expand All @@ -18,11 +19,12 @@ class controller {
const char *stats_filename,
size_t num_secs) :
mc{merc_ctx},
stats_file{stats_filename},
stats_file{stats_filename, ".json.gz"},
num_secs_between_writes{num_secs},
count{num_secs},
controller_thread{},
shutdown_requested{false}
shutdown_requested{false},
has_run_at_least_once{false}
{
if (mc == nullptr) {
throw "error: null mercury context passed to control thread";
Expand All @@ -35,19 +37,22 @@ class controller {
}

private:

mercury_context mc;
std::string stats_file;
rotator stats_file;
size_t num_secs_between_writes;
size_t count;
std::thread controller_thread;
std::atomic<bool> shutdown_requested;
bool has_run_at_least_once;

void run_tasks() {
while (shutdown_requested.load() == false) {
if (count == 0) {
count = num_secs_between_writes;
if (mercury_write_stats_data(mc, stats_file.c_str()) == false) {
fprintf(stderr, "error: could not write stats file %s\n", stats_file.c_str());
const char *fname = stats_file.get_next_name();
if (mercury_write_stats_data(mc, fname) == false) {
fprintf(stderr, "error: could not write stats file %s\n", fname);
}
}
--count;
Expand All @@ -64,6 +69,12 @@ class controller {
if(controller_thread.joinable()) {
controller_thread.join();
}
if (!has_run_at_least_once) {
const char *fname = stats_file.get_current_name();
if (mercury_write_stats_data(mc, fname) == false) {
fprintf(stderr, "error: could not write stats file %s\n", fname);
}
}
}

};
Expand Down
2 changes: 1 addition & 1 deletion src/mercury.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ int main(int argc, char *argv[]) {

controller *ctl = nullptr;
if (cfg.stats_filename) {
ctl = new controller{mc, "mercury-stats.json.gz", 2};
ctl = new controller{mc, cfg.stats_filename, 30};
}

pthread_t output_thread;
Expand Down
56 changes: 56 additions & 0 deletions src/rotator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// rotator.h
//
// file name rotation

#ifndef ROTATOR_H
#define ROTATOR_H

#include <string>

class rotator {
public:

rotator(const char *filename,
const char *filename_extension=nullptr) :
file_number{0},
base_name{filename},
extension{filename_extension},
file_name{filename}
{
file_name.append(extension);
}

const char *get_next_name() {
/*
* create filename that includes sequence number and date/timestamp
*/
file_name = base_name;

char file_num[MAX_HEX];
snprintf(file_num, MAX_HEX, "%x", file_number++);
file_name.append("-").append(file_num);

char time_str[128];
struct timeval now;
gettimeofday(&now, NULL);
strftime(time_str, sizeof(time_str) - 1, "%Y-%m-%d-%H-%M-%S", localtime(&now.tv_sec));
file_name.append("-").append(time_str);

file_name.append(extension);

return file_name.c_str();
}

const char *get_current_name() const {
return file_name.c_str();
}

private:
unsigned int file_number;
std::string base_name; // prefix common to all files
std::string extension; // extension, if any
std::string file_name; // name of current file

};

#endif // ROTATOR_H

0 comments on commit e619f51

Please sign in to comment.