2014-12-23 19:07:50 +01:00
|
|
|
#include <fstream>
|
2020-05-23 20:55:10 +02:00
|
|
|
#include <iostream>
|
2014-12-23 19:07:50 +01:00
|
|
|
#include <ncurses.h>
|
2020-05-23 20:55:10 +02:00
|
|
|
#include <sstream>
|
2015-05-11 19:10:52 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-05-23 20:55:10 +02:00
|
|
|
#include <string>
|
|
|
|
#include <unistd.h>
|
2014-12-23 19:07:50 +01:00
|
|
|
|
2020-11-15 13:39:55 +01:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
|
|
std::string cli_stats_ipv4_file_path = "/tmp/fastnetmon.dat";
|
|
|
|
|
|
|
|
std::string cli_stats_ipv6_file_path = "/tmp/fastnetmon_ipv6.dat";
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
bool ipv6_mode = false;
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// clang-format off
|
|
|
|
po::options_description desc("Allowed options");
|
|
|
|
desc.add_options()
|
|
|
|
("help", "produce help message")
|
|
|
|
("ipv6", "switch to IPv6 mode");
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
po::variables_map vm;
|
|
|
|
po::store(po::parse_command_line(argc, argv, desc), vm);
|
|
|
|
po::notify(vm);
|
|
|
|
|
|
|
|
if (vm.count("help")) {
|
|
|
|
std::cout << desc << std::endl;
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("ipv6")) {
|
|
|
|
ipv6_mode = true;
|
|
|
|
}
|
|
|
|
} catch (po::error& e) {
|
|
|
|
std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2015-09-28 17:36:02 +02:00
|
|
|
|
2015-05-15 12:55:52 +02:00
|
|
|
// Init ncurses screen
|
2014-12-23 19:07:50 +01:00
|
|
|
initscr();
|
|
|
|
|
2015-05-15 12:55:52 +02:00
|
|
|
// disable any character output
|
2014-12-23 19:07:50 +01:00
|
|
|
noecho();
|
2015-01-26 09:07:21 +01:00
|
|
|
|
2014-12-23 19:07:50 +01:00
|
|
|
// hide cursor
|
|
|
|
curs_set(0);
|
|
|
|
|
2015-05-11 19:10:52 +02:00
|
|
|
// Do not wait for getch
|
|
|
|
timeout(0);
|
|
|
|
|
2014-12-23 19:07:50 +01:00
|
|
|
while (true) {
|
|
|
|
sleep(1);
|
2015-05-15 12:55:52 +02:00
|
|
|
|
2014-12-23 19:07:50 +01:00
|
|
|
// clean up screen
|
|
|
|
clear();
|
|
|
|
|
2015-05-11 19:10:52 +02:00
|
|
|
int c = getch();
|
|
|
|
|
|
|
|
if (c == 'q') {
|
|
|
|
endwin();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2020-11-15 13:39:55 +01:00
|
|
|
|
|
|
|
std::string cli_stats_file_path = cli_stats_ipv4_file_path;
|
|
|
|
|
|
|
|
if (ipv6_mode) {
|
|
|
|
cli_stats_file_path = cli_stats_ipv6_file_path;
|
|
|
|
}
|
|
|
|
|
2015-09-28 17:36:02 +02:00
|
|
|
char* cli_stats_file_path_env = getenv("cli_stats_file_path");
|
|
|
|
|
|
|
|
if (cli_stats_file_path_env != NULL) {
|
|
|
|
cli_stats_file_path = std::string(cli_stats_file_path_env);
|
2020-05-23 20:55:10 +02:00
|
|
|
}
|
2015-09-28 17:36:02 +02:00
|
|
|
|
2015-01-27 20:46:33 +01:00
|
|
|
std::ifstream reading_file;
|
2015-09-28 17:36:02 +02:00
|
|
|
reading_file.open(cli_stats_file_path.c_str(), std::ifstream::in);
|
2014-12-23 19:07:50 +01:00
|
|
|
|
|
|
|
if (!reading_file.is_open()) {
|
2015-09-28 17:36:02 +02:00
|
|
|
std::cout << "Can't open fastnetmon stats file: " << cli_stats_file_path;
|
2020-05-23 20:55:10 +02:00
|
|
|
}
|
2015-05-15 12:55:52 +02:00
|
|
|
|
2015-01-27 20:46:33 +01:00
|
|
|
std::string line = "";
|
|
|
|
std::stringstream screen_buffer;
|
2015-05-15 12:55:52 +02:00
|
|
|
while (getline(reading_file, line)) {
|
|
|
|
screen_buffer << line << "\n";
|
2014-12-23 19:07:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
reading_file.close();
|
|
|
|
|
2015-05-15 12:55:52 +02:00
|
|
|
printw(screen_buffer.str().c_str());
|
2014-12-23 19:07:50 +01:00
|
|
|
// update screen
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End ncurses mode */
|
|
|
|
endwin();
|
|
|
|
}
|