-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmyiptop.cpp
207 lines (162 loc) · 4.19 KB
/
myiptop.cpp
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
196
197
198
199
200
201
202
203
204
205
206
207
// (C) 2020-2022 by folkert van heusden <[email protected]>, released under Apache License v2.0
#include <fcntl.h>
#include <map>
#include <ncurses.h>
#include <poll.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "stats_utils.h"
#include "utils.h"
constexpr char shm_name[] = "/myip";
constexpr int size = 8192;
void ncurses_ui(const uint8_t *const p, const uint8_t *const p_end)
{
initscr();
cbreak();
intrflush(stdscr, FALSE);
noecho();
nonl();
refresh();
meta(stdscr, TRUE);
idlok(stdscr, TRUE);
idcok(stdscr, TRUE);
leaveok(stdscr, FALSE);
keypad(stdscr, TRUE);
int maxx = 0, maxy = 0;
getmaxyx(stdscr, maxy, maxx);
WINDOW *win_names = newwin(maxy, 16, 0, 0);
WINDOW *win_values = newwin(maxy, maxx - 18, 0, 18);
std::map<std::string, std::map<std::string, uint64_t *> > values;
const uint8_t *cur_p = p;
while(cur_p < p_end && cur_p[16]) {
std::string name = (char *)&cur_p[16];
std::size_t underscore = name.find('_');
std::string prefix = name.substr(0, underscore);
auto it = values.find(prefix);
if (it == values.end()) {
std::map<std::string, uint64_t *> new_pair;
new_pair.insert({ name, (uint64_t *)&cur_p[0] });
values.insert({ prefix, new_pair });
}
else {
it->second.insert({ name, (uint64_t *)&cur_p[0] });
}
cur_p += 48;
}
int cursor = 0;
pollfd fds[] { { 0, POLLIN, 0 } };
for(;;) {
int nr = 0;
werase(win_names);
werase(win_values);
time_t t = time(nullptr);
tm tm { 0 };
localtime_r(&t, &tm);
mvwprintw(win_names, 0, 0, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec);
for(auto & it : values) {
mvwprintw(win_names, nr + 1, 1, "%c%s", nr == cursor ? '>' : ' ', it.first.c_str());
if (nr == cursor) {
int nr2 = 0;
for(auto & it_entries : it.second) {
mvwprintw(win_values, nr2 % maxy, (nr2 / maxy) * 38, "%s", it_entries.first.c_str());
uint64_t *cnt_p = it_entries.second;
uint64_t *cnt_p2 = &cnt_p[1];
if (*cnt_p2)
mvwprintw(win_values, nr2 % maxy, (nr2 / maxy) * 38 + 29, "%.2f", *cnt_p / double(*cnt_p2));
else
mvwprintw(win_values, nr2 % maxy, (nr2 / maxy) * 38 + 29, "%lu", *cnt_p);
nr2++;
}
}
nr++;
}
wrefresh(win_names);
wrefresh(win_values);
wmove(win_names, cursor + 1, 0);
doupdate();
if (poll(fds, 1, 500)) {
int c = getch();
if (c == KEY_UP && cursor > 0)
cursor--;
else if (c == KEY_DOWN && cursor < values.size() - 1)
cursor++;
else if (c == 'q')
break;
}
}
endwin();
}
void help()
{
printf("-j json output (one-shot)\n");
printf("-c x display output x times and then exit (not for json)\n");
printf("-n ncurses ui\n");
}
int main(int argc, char *argv[])
{
int count = -1;
bool json = false, nc = false;
int c = 0;
while((c = getopt(argc, argv, "jc:nh")) != -1) {
if (c == 'j')
json = true;
else if (c == 'c')
count = atoi(optarg);
else if (c == 'n')
nc = true;
else if (c == 'h') {
help();
return 0;
}
}
int fd = shm_open(shm_name, O_RDONLY, 0444);
if (fd == -1) {
perror("shm_open");
exit(1);
}
struct stat sb;
if (fstat(fd, &sb) == -1) {
perror("fstat");
exit(1);
}
uint8_t *const p = (uint8_t *)mmap(nullptr, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
perror("mmap");
exit(1);
}
if (json) {
std::vector<std::pair<const std::string, const fifo_stats *> > dummy;
std::string out = stats_to_json(p, dummy, sb.st_size);
printf("%s\n", out.c_str());
}
else if (nc)
ncurses_ui(p, &p[sb.st_size]);
else {
int nr = 0;
for(;count == -1 || nr++ < count;) {
printf("\n");
uint8_t *const p_end = &p[sb.st_size];
uint8_t *cur_p = p;
while(cur_p < p_end && cur_p[16]) {
uint64_t *cnt_p = (uint64_t *)cur_p;
uint64_t *cnt_p2 = (uint64_t *)(cur_p + 8);
if (*cnt_p2)
printf("%s\t%.2f\n", &cur_p[16], *cnt_p / double(*cnt_p2));
else
printf("%s\t%lu\n", &cur_p[16], *cnt_p);
cur_p += 48;
}
if (nr < count || count == -1)
sleep(1);
}
}
munmap(p, size);
close(fd);
}