-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkExporter.cpp
110 lines (94 loc) · 2.91 KB
/
NetworkExporter.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
// Criado por Pedro Henrique Toth
// Nenhum direito reservado
// 20/08/2021
//#include <bits/stdc++.h>
#include <ctime>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <string>
using namespace std;
string data[255][5];
int list_size = 0;
void Write_Metrics() {
string to_file;
to_file = "Network Exporter - All data get from lsof -i -n -P";
to_file = to_file + "\n";
to_file = to_file + "format: network_exporter_port-index_data";
to_file = to_file + "\n";
to_file = to_file + "Data - Proccess Name, PID, User, IP Version, Transport Layer, Port";
to_file = to_file + "\n";
for (int x = 0; x <= list_size - 1; x++) {
to_file = to_file + "network_exporter_" + to_string(x) + "_" + data[x][0] + "\n";
to_file = to_file + "network_exporter_" + to_string(x) + "_" + data[x][1] + "\n";
to_file = to_file + "network_exporter_" + to_string(x) + "_" + data[x][2] + "\n";
to_file = to_file + "network_exporter_" + to_string(x) + "_" + data[x][3] + "\n";
to_file = to_file + "network_exporter_" + to_string(x) + "_" + data[x][4] + "\n";
to_file = to_file + "network_exporter_" + to_string(x) + "_" + data[x][5] + "\n";
}
ofstream myfile;
myfile.open("example.txt");
myfile << to_file;
myfile.close();
}
void what_time_is() {
time_t now = time(0);
char * dt = ctime( & now);
tm * ltm = localtime( & now);
string timestamp;
timestamp = to_string(1900 + ltm -> tm_year) + "-";
timestamp = timestamp + to_string(ltm -> tm_mon) + "-";
timestamp = timestamp + to_string(ltm -> tm_mday) + "---";
timestamp = timestamp + to_string(ltm -> tm_hour) + "-";
timestamp = timestamp + to_string(ltm -> tm_min) + "-";
timestamp = timestamp + to_string(ltm -> tm_sec);
cout << timestamp << "\n";
}
void get_Words(string str, int x_pos) {
istringstream ss(str);
string word;
int y_index = 0;
while (ss >> word) {
cout << "Y: " << to_string(y_index) << "\n";
data[x_pos][y_index] = word;
cout << data[x_pos][y_index] << "\n";
y_index++;
}
}
string exec(string command) {
char buffer[256];
string result = "";
// Open pipe to file
FILE * pipe = popen(command.c_str(), "r");
if (!pipe) {
return "popen failed!";
}
// read till end of process:
while (!feof(pipe)) {
// use buffer to read and add to result
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
void read_file() {
std::ifstream input("/tmp/network_exporter_output.txt");
int line_count = 0;
for (std::string line; getline(input, line);) {
cout << "X: " << to_string(line_count) << "\n";
get_Words(line, line_count);
line_count++;
list_size++;
}
}
int main() {
string ls = exec("lsof -i -n -P | grep LISTEN | awk '{print $1, $2, $3, $5, $8, $9}'");
std::ofstream out("/tmp/network_exporter_output.txt");
out << ls;
out.close();
read_file();
Write_Metrics();
}