galmon/navnexus.cc

166 lines
4.0 KiB
C++
Raw Normal View History

2019-08-09 16:54:35 -06:00
#include "comboaddress.hh"
#include "sclasses.hh"
#include <thread>
#include <signal.h>
#include "navmon.pb.h"
#include "fmt/format.h"
#include "fmt/printf.h"
#include <mutex>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
2019-08-11 02:43:29 -06:00
#include <unistd.h>
#include <stdexcept>
#include <sys/types.h>
#include "storage.hh"
#include <dirent.h>
#include <inttypes.h>
2019-08-11 02:43:29 -06:00
2019-08-09 16:54:35 -06:00
using namespace std;
std::mutex g_clientmut;
set<int> g_clients;
2019-08-11 02:43:29 -06:00
std::string g_storage;
2019-08-09 16:54:35 -06:00
2019-08-11 02:43:29 -06:00
void unixDie(const std::string& str)
{
throw std::runtime_error(str+string(": ")+string(strerror(errno)));
}
vector<uint64_t> getSources()
{
DIR *dir = opendir(g_storage.c_str());
if(!dir)
unixDie("Listing metrics from statistics storage "+g_storage);
struct dirent *result=0;
vector<uint64_t> ret;
for(;;) {
errno=0;
if(!(result = readdir(dir))) {
closedir(dir);
if(errno)
unixDie("Reading directory entry "+g_storage);
else
break;
}
if(result->d_name[0] != '.') {
uint64_t src;
if(sscanf(result->d_name, "%08" PRIx64, &src)==1)
2019-08-11 02:43:29 -06:00
ret.push_back(src);
}
}
sort(ret.begin(), ret.end());
return ret;
}
2019-08-09 16:54:35 -06:00
2019-08-12 07:54:16 -06:00
void sendSession(int clientfd, ComboAddress client)
try
2019-08-09 16:54:35 -06:00
{
2019-08-12 07:54:16 -06:00
cerr<<"New downstream client "<<client.toStringWithPort() << endl;
pair<uint64_t, uint64_t> start = {0,0};
2019-09-03 12:05:40 -06:00
start.first = time(0) - 24*3600; // 4 hours of backlog
2019-08-12 07:54:16 -06:00
2019-08-12 17:15:25 -06:00
// so we have a ton of files, and internally these are not ordered
2019-08-12 07:54:16 -06:00
map<string,uint32_t> fpos;
2019-09-05 01:35:00 -06:00
vector<NavMonMessage> nmms;
2019-08-11 02:43:29 -06:00
for(;;) {
auto srcs = getSources();
2019-09-05 01:35:00 -06:00
nmms.clear();
2019-08-12 17:15:25 -06:00
for(const auto& src: srcs) {
string fname = getPath(g_storage, start.first, src);
2019-08-12 07:54:16 -06:00
int fd = open(fname.c_str(), O_RDONLY);
if(fd < 0)
continue;
uint32_t offset= fpos[fname];
if(lseek(fd, offset, SEEK_SET) < 0) {
cout<<"Error seeking: "<<strerror(errno) <<endl;
close(fd);
continue;
}
cout <<"Seeked to position "<<fpos[fname]<<" of "<<fname<<endl;
NavMonMessage nmm;
uint32_t looked=0;
while(getNMM(fd, nmm, offset)) {
2019-08-12 17:15:25 -06:00
// don't drop data that is only 5 seconds too old
if(make_pair(nmm.localutcseconds() + 5, nmm.localutcnanoseconds()) >= start) {
2019-08-12 07:54:16 -06:00
nmms.push_back(nmm);
}
++looked;
}
cout<<"Harvested "<<nmms.size()<<" events out of "<<looked<<endl;
fpos[fname]=offset;
close(fd);
2019-08-11 02:43:29 -06:00
}
2019-08-12 07:54:16 -06:00
sort(nmms.begin(), nmms.end(), [](const auto& a, const auto& b)
{
return make_pair(a.localutcseconds(), b.localutcnanoseconds()) <
make_pair(b.localutcseconds(), b.localutcnanoseconds());
});
for(const auto& nmm: nmms) {
std::string out;
nmm.SerializeToString(&out);
std::string buf="bert";
uint16_t len = htons(out.size());
buf.append((char*)(&len), 2);
buf+=out;
SWriten(clientfd, buf);
}
2019-08-12 17:15:25 -06:00
if(3600 + start.first - (start.first%3600) < time(0))
start.first = 3600 + start.first - (start.first%3600);
else {
if(!nmms.empty())
start = {nmms.rbegin()->localutcseconds(), nmms.rbegin()->localutcnanoseconds()};
sleep(1);
}
2019-08-12 07:54:16 -06:00
}
}
catch(std::exception& e) {
cerr<<"Sender thread died: "<<e.what()<<endl;
}
void sendListener(Socket&& s, ComboAddress local)
{
for(;;) {
ComboAddress remote=local;
int fd = SAccept(s, remote);
std::thread t(sendSession, fd, remote);
t.detach();
2019-08-11 02:43:29 -06:00
}
2019-08-12 07:54:16 -06:00
}
int main(int argc, char** argv)
{
signal(SIGPIPE, SIG_IGN);
if(argc != 3) {
cout<<"Syntax: navnexus storage listen-address"<<endl;
return(EXIT_FAILURE);
}
g_storage=argv[1];
2019-08-09 16:54:35 -06:00
2019-08-11 02:43:29 -06:00
ComboAddress sendaddr(argv[2], 29601);
2019-08-12 07:54:16 -06:00
cout<<"Listening on "<<sendaddr.toStringWithPort()<<", storage: "<<g_storage<<endl;
2019-08-09 16:54:35 -06:00
Socket sender(sendaddr.sin4.sin_family, SOCK_STREAM);
SSetsockopt(sender, SOL_SOCKET, SO_REUSEADDR, 1 );
SBind(sender, sendaddr);
SListen(sender, 128);
thread sendThread(sendListener, std::move(sender), sendaddr);
sendThread.detach();
2019-08-12 07:54:16 -06:00
for(;;) {
sleep(5);
}
2019-08-09 16:54:35 -06:00
}