-
Notifications
You must be signed in to change notification settings - Fork 52
/
navcat.cc
195 lines (175 loc) · 5.51 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#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"
// #include <execution>
#include "CLI/CLI.hpp"
#include "version.hh"
static char program[]="navcat";
using namespace std;
extern const char* g_gitHash;
// get all stations (numerical) from a directory
static vector<uint64_t> getSources(string_view dirname)
{
shared_ptr<DIR> dir;
if(auto ptr = opendir(&dirname[0]))
dir=shared_ptr<DIR>(ptr, closedir);
else
unixDie("Listing metrics from statistics storage "+(string)dirname);
struct dirent *result=0;
vector<uint64_t> ret;
for(;;) {
errno=0;
if(!(result = readdir(dir.get()))) {
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 bool operator==(const timespec& a, const timespec& b)
{
return a.tv_sec == b.tv_sec && a.tv_nsec && b.tv_nsec;
}
// send protobuf data from the named directories and stations, between start and stoptime
void sendProtobuf(const vector<string>& dirs, vector<uint64_t> stations, time_t startTime, time_t stopTime=0)
{
timespec start;
start.tv_sec = startTime;
start.tv_nsec = 0;
// so we have a ton of files, and internally these are not ordered
vector<pair<timespec,string> > rnmms;
for(;;) {
rnmms.clear();
for(const auto& dir : dirs) {
cerr<<"Gathering data from "<<humanTime(start.tv_sec)<<" from "<<dir<<".. ";
vector<uint64_t> srcs = stations.empty() ? getSources(dir) : stations;
int count=0;
for(const auto& src: srcs) {
string fname = getPath(dir, start.tv_sec, src);
FILE* ptr = fopen(fname.c_str(), "r");
shared_ptr<FILE> fp;
if(ptr) {
fp = shared_ptr<FILE>(ptr, fclose);
}
else {
fname+=".zst";
struct stat statbuf;
if(stat(fname.c_str(), &statbuf) == 0) {
ptr = popen(("zstdcat "+fname).c_str(), "r");
if(!ptr)
continue;
fp = shared_ptr<FILE>(ptr, pclose);
}
else
continue;
}
string msg;
struct timespec ts;
unsigned int offset=0;
while(getRawNMM(fp.get(), ts, msg, offset)) {
// don't drop data that is only 5 seconds too old
if(make_pair(ts.tv_sec + 5, ts.tv_nsec) >= make_pair(start.tv_sec, start.tv_nsec)) {
rnmms.push_back({ts, msg});
++count;
}
}
// cerr<<"Harvested "<<rnmms.size()<<" events out of "<<looked<<endl;
// fp will close itself
}
cerr<<" added "<<count<<endl;
}
// cerr<<"Sorting data"<<endl;
// sort(execution::par, rnmms.begin(), rnmms.end(), [](const auto& a, const auto& b)
sort(rnmms.begin(), rnmms.end(), [](const auto& a, const auto& b)
{
return std::tie(a.first.tv_sec, a.first.tv_nsec)
< std::tie(b.first.tv_sec, b.first.tv_nsec);
});
auto newend=unique(rnmms.begin(), rnmms.end());
cerr<<"Removed "<<rnmms.end() - newend <<" duplicates, ";
rnmms.erase(newend, rnmms.end());
cerr<<"sending data"<<endl;
unsigned int count=0;
for(const auto& nmm: rnmms) {
if(nmm.first.tv_sec > stopTime)
break;
std::string buf="bert";
uint16_t len = htons(nmm.second.size());
buf.append((char*)(&len), 2);
buf += nmm.second;
//fwrite(buf.c_str(), 1, buf.size(), stdout);
writen2(1, buf.c_str(), buf.size());
++count;
}
cerr<<"Done sending " << count<<" messages"<<endl;
if(3600 + start.tv_sec - (start.tv_sec%3600) < stopTime)
start.tv_sec = 3600 + start.tv_sec - (start.tv_sec%3600);
else {
break;
}
}
}
int main(int argc, char** argv)
{
bool doVERSION{false};
CLI::App app(program);
string beginarg, endarg;
vector<string> storages;
int galwn{-1};
app.add_option("--storage,-s", storages, "Locations of storage files");
vector<uint64_t> stations;
app.add_flag("--version", doVERSION, "show program version and copyright");
app.add_option("--begin,-b", beginarg, "Begin time (2020-01-01 00:00, or 12:30)");
app.add_option("--end,-e", endarg, "End time. Now if omitted");
app.add_option("--stations", stations, "only send data from listed stations");
app.add_option("--gal-wn", galwn, "Galileo week number to report on");
CLI11_PARSE(app, argc, argv);
if(doVERSION) {
showVersion(program, g_gitHash);
exit(0);
}
time_t startTime, stopTime;
if(galwn >=0) {
startTime=utcFromGST(galwn, 0);
stopTime=startTime + 7*86400;
}
else if(!beginarg.empty()) {
startTime = parseTime(beginarg);
stopTime = endarg.empty() ? time(0) : parseTime(endarg);
}
else {
cerr<<"No time range specified, use -b or --gal-wn"<<endl;
return 1;
}
cerr<<"Emitting from "<<humanTime(startTime) << " to " << humanTime(stopTime) << endl;
if(!stations.empty()) {
cerr<<"Restricting to stations:";
for(const auto& s : stations)
cerr<<" "<<s;
cerr<<endl;
}
sendProtobuf(storages, stations, startTime, stopTime);
}