forked from berthubert/galmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavcat.cc
172 lines (149 loc) · 4.15 KB
/
navcat.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#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>
#include <unistd.h>
#include <stdexcept>
#include <sys/types.h>
#include "storage.hh"
#include <dirent.h>
#include <inttypes.h>
#include "navmon.hh"
using namespace std;
void unixDie(const std::string& str)
{
throw std::runtime_error(str+string(": ")+string(strerror(errno)));
}
time_t parseTime(std::string_view in)
{
time_t now=time(0);
vector<string> formats({"%Y-%m-%d %H:%M", "%Y%m%d %H%M", "%H:%M", "%H%M"});
for(const auto& f : formats) {
struct tm tm;
memset(&tm, 0, sizeof(tm));
localtime_r(&now, &tm);
tm.tm_isdst = -1;
tm.tm_sec = 0;
char* res = strptime(&in[0], f.c_str(), &tm);
if(res && !*res) {
cerr<<"Matched on "<<f<<endl;
return mktime(&tm);
}
}
throw runtime_error("Can only parse %Y-%m-%d %H:%M");
}
vector<uint64_t> getSources(string_view dirname)
{
DIR *dir = opendir(&dirname[0]);
if(!dir)
unixDie("Listing metrics from statistics storage "+(string)dirname);
struct dirent *result=0;
vector<uint64_t> ret;
for(;;) {
errno=0;
if(!(result = readdir(dir))) {
closedir(dir);
if(errno)
unixDie("Reading directory entry "+(string)dirname);
else
break;
}
if(result->d_name[0] != '.') {
uint64_t src;
if(sscanf(result->d_name, "%08" PRIx64, &src)==1)
ret.push_back(src);
}
}
sort(ret.begin(), ret.end());
return ret;
}
static size_t writen2(int fd, const void *buf, size_t count)
{
const char *ptr = (char*)buf;
const char *eptr = ptr + count;
ssize_t res;
while(ptr != eptr) {
res = ::write(fd, ptr, eptr - ptr);
if(res < 0) {
throw runtime_error("failed in writen2: "+string(strerror(errno)));
}
else if (res == 0)
throw EofException();
ptr += (size_t) res;
}
return count;
}
void sendProtobuf(string_view dir, time_t startTime, time_t stopTime=0)
{
pair<uint64_t, uint64_t> start = {startTime, 0};
// so we have a ton of files, and internally these are not ordered
map<string,uint32_t> fpos;
vector<NavMonMessage> nmms;
for(;;) {
auto srcs = getSources(dir);
nmms.clear();
for(const auto& src: srcs) {
string fname = getPath(dir, start.first, src);
int fd = open(fname.c_str(), O_RDONLY);
if(fd < 0)
continue;
uint32_t offset= fpos[fname];
if(lseek(fd, offset, SEEK_SET) < 0) {
cerr<<"Error seeking: "<<strerror(errno) <<endl;
close(fd);
continue;
}
cerr <<"Seeked to position "<<fpos[fname]<<" of "<<fname<<endl;
NavMonMessage nmm;
uint32_t looked=0;
while(getNMM(fd, nmm, offset)) {
// don't drop data that is only 5 seconds too old
if(make_pair(nmm.localutcseconds() + 5, nmm.localutcnanoseconds()) >= start) {
nmms.push_back(nmm);
}
++looked;
}
cerr<<"Harvested "<<nmms.size()<<" events out of "<<looked<<endl;
fpos[fname]=offset;
close(fd);
}
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;
writen2(1, buf.c_str(), buf.size());
}
if(3600 + start.first - (start.first%3600) < stopTime)
start.first = 3600 + start.first - (start.first%3600);
else {
break;
}
}
}
int main(int argc, char** argv)
{
signal(SIGPIPE, SIG_IGN);
if(argc < 3) {
cout<<"Syntax: navcat storage start stop"<<endl;
return(EXIT_FAILURE);
}
time_t startTime = parseTime(argv[2]);
time_t stopTime = parseTime(argv[3]);
cerr<<"Emitting from "<<humanTime(startTime) << " to " << humanTime(stopTime) << endl;
sendProtobuf(argv[1], startTime, stopTime);
}