]> git.sesse.net Git - nms/blob - web/ext/totalcount.cpp
Add lots of TG06 graph stuff.
[nms] / web / ext / totalcount.cpp
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/wait.h>
4 #include <malloc.h>
5 #include <vector>
6 #include <algorithm>
7 #include <pqxx/pqxx>
8 #include "graph.h"
9 #include "flowpusher.h"
10 #include "flowutil.h"
11
12 namespace pqxx {
13         template<>
14         void from_string<long long>(const char *from, long long &to)
15         {
16                 to = atoll(from);
17         }
18 }
19                                                 
20 int main(int argc, char **argv)
21 {
22         int width = 1000, height = 500;
23         unsigned long long traffic = 0ULL;
24         pqxx::connection conn("dbname=nms host=localhost user=nms password=seesahS4");
25
26         std::vector<flow_element> flow;
27         int last_port = -1;
28         
29         mallopt(M_TRIM_THRESHOLD, -1);
30         
31         int num_total = 0;
32         pqxx::work t(conn, "fetch_all");
33 //      pqxx::icursorstream::icursorstream cstream(t, "select port,extract(epoch from time) as time,bytes_in,bytes_out from polls natural join switches where ((switchtype='es3024' and port < 25) or (switchtype='summit400' and port > 1)) order by switch,port,time", "fetch_all", 500);
34         pqxx::icursorstream::icursorstream cstream(t, "select port,extract(epoch from time) as time,bytes_in,bytes_out from polls natural join switches where switch=1447 order by switch,port,time", "fetch_all", 500);
35         
36         double prev_x;
37         unsigned long long prev_y1, prev_y2;
38         
39         for ( ;; ) {
40                 pqxx::result res;
41                 
42                 cstream >> res;
43                 if (res.empty()) 
44                         break;
45
46                 for (unsigned i = 0; i < res.size(); ++i) {
47                         int port = res[i][0].as<int>();
48                         double x = res[i][1].as<double>();
49                         unsigned long long y1 = res[i][2].as<long long>(), y2 = res[i][3].as<long long>();
50
51                         if (port != last_port) {
52                                 if (last_port != -1) {
53                                         fprintf(stderr, "%.2f TB (%u)\n", traffic / double(1024.0 * 1024.0 * 1024.0 * 1024.0), ++num_total);
54                                 }
55
56                                 // reset
57                                 last_port = port;
58                                 prev_x = x;
59                                 prev_y1 = y1;
60                                 prev_y2 = y2;
61                                 continue;
62                         }
63
64                         unsigned long long yf1, yf2;
65                         FlowPusher::find_diff(x, prev_x, y1, prev_y1, y2, prev_y2, yf1, yf2);
66                         traffic += 0.5 * (yf1 + yf2);
67                         
68                         prev_x = x;
69                         prev_y1 = y1;
70                         prev_y2 = y2;
71                 }
72         }
73         fprintf(stderr, "%.2f TB\n", traffic / double(1024.0 * 1024.0 * 1024.0 * 1024.0));
74 }