-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.cpp
More file actions
200 lines (180 loc) · 6.2 KB
/
Copy pathmonitor.cpp
File metadata and controls
200 lines (180 loc) · 6.2 KB
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
// monitor.cpp
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <thread>
#include <regex>
using namespace std;
using Clock = chrono::steady_clock;
using namespace chrono;
struct Domain {
string name;
string status = "N/A";
optional<double> latency_ms; // null -> no dato
time_t last_check = 0;
Clock::time_point next_check_due = Clock::now();
};
static const string DOMAINS_FILE = "domains.txt";
static const minutes CHECK_EVERY = minutes(10);
string now_str(time_t t) {
if (t == 0) return "-";
char buf[64];
tm tmv{};
localtime_r(&t, &tmv);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tmv);
return string(buf);
}
// Ejecuta `ping -c 1 -W 2 host` y devuelve {up, latency_ms}
pair<bool, optional<double>> ping_once(const string& host) {
// -c 1 -> 1 paquete ; -W 2 -> timeout 2s
string cmd = "ping -c 1 -W 2 " + host + " 2>&1";
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe) return {false, nullopt};
string output;
char buffer[256];
while (fgets(buffer, sizeof(buffer), pipe)) {
output += buffer;
}
int rc = pclose(pipe);
// Buscar 'time=XX ms'
regex r(R"(time=([\d\.]+)\s*ms)");
smatch m;
if (regex_search(output, m, r)) {
double ms = stod(m[1].str());
return {true, ms};
}
// Si no hay time pero el rc es 0, lo marcamos como up (sin latencia)
if (WIFEXITED(rc) && WEXITSTATUS(rc) == 0) {
return {true, nullopt};
}
return {false, nullopt};
}
void save_domains(const vector<Domain>& v) {
ofstream f(DOMAINS_FILE);
for (auto& d : v) f << d.name << "\n";
}
vector<Domain> load_domains() {
vector<Domain> v;
ifstream f(DOMAINS_FILE);
string s;
while (getline(f, s)) {
// limpiar espacios
string t;
for (char c: s) if (!isspace((unsigned char)c)) t += c;
if (!t.empty()) {
Domain d;
d.name = t;
d.next_check_due = Clock::now(); // forzar primera comprobación
v.push_back(d);
}
}
return v;
}
void clear_screen() {
// ANSI clear
cout << "\033[2J\033[H";
}
void print_table(const vector<Domain>& v) {
cout << "Monit. dominios (ping cada " << CHECK_EVERY.count() << " min)\n";
cout << "Archivo: " << DOMAINS_FILE << " — Añadir dominio: escribe abajo y ENTER — Salir: deja vacío y pulsa ENTER\n\n";
// cabecera
cout << left
<< setw(32) << "Dominio"
<< setw(10) << "Estado"
<< setw(14) << "Latencia(ms)"
<< setw(20) << "Último check"
<< "\n";
cout << string(32+10+14+20, '-') << "\n";
for (auto& d : v) {
string lat = d.latency_ms.has_value() ? to_string(*d.latency_ms) : "-";
cout << left
<< setw(32) << d.name.substr(0,31)
<< setw(10) << d.status
<< setw(14) << lat
<< setw(20) << now_str(d.last_check)
<< "\n";
}
cout << "\nAñadir dominio (ENTER para no añadir y refrescar): ";
cout.flush();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<Domain> domains = load_domains();
// Si no hay dominios, sugerimos algunos
if (domains.empty()) {
for (string d : {"example.com", "google.com"}) {
domains.push_back(Domain{
.name=d,
.status="N/A",
.latency_ms=nullopt,
.last_check=0,
.next_check_due=Clock::now()
});
}
save_domains(domains);
}
while (true) {
// Comprobaciones debidas
auto now = Clock::now();
for (auto& d : domains) {
if (now >= d.next_check_due) {
auto res = ping_once(d.name);
d.last_check = time(nullptr);
if (res.first) {
d.status = "UP";
d.latency_ms = res.second;
} else {
d.status = "DOWN";
d.latency_ms = nullopt;
}
d.next_check_due = now + CHECK_EVERY;
}
}
// Pintar
clear_screen();
print_table(domains);
// Entrada de usuario no bloqueante larga complicaría; usamos una corta con timeout manual:
// Leemos una línea con timeout "suave": damos ~3s para teclear, si no, refrescamos y seguimos.
// Para simplificar, aquí: leemos con std::getline pero con un truco de tiempo limitado.
// Implementación simple: ponemos cin.rdbuf()->in_avail() para ver si hay algo.
// Si hay entrada, la tomamos; si no, dormimos 3s y refrescamos.
bool had_input = false;
for (int i=0; i<30; ++i) { // 3s en pasos de 100ms
if (cin.rdbuf()->in_avail() > 0) { had_input = true; break; }
this_thread::sleep_for(100ms);
}
if (had_input) {
string newdom;
getline(cin, newdom);
// trim
auto ltrim = [](string& s){ s.erase(s.begin(), find_if(s.begin(), s.end(), [](int ch){return !isspace(ch);})); };
auto rtrim = [](string& s){ s.erase(find_if(s.rbegin(), s.rend(), [](int ch){return !isspace(ch);}).base(), s.end()); };
ltrim(newdom); rtrim(newdom);
if (!newdom.empty()) {
// validar un poco (sin espacios, sin barra)
if (newdom.find(' ') != string::npos || newdom.find('/') != string::npos) {
// ignorar entrada rara
} else {
// ¿ya existe?
bool exists = any_of(domains.begin(), domains.end(), [&](const Domain& d){ return d.name == newdom; });
if (!exists) {
Domain d;
d.name = newdom;
d.status = "N/A";
d.latency_ms = nullopt;
d.last_check = 0;
d.next_check_due = Clock::now(); // comprobar al instante
domains.push_back(d);
save_domains(domains);
}
}
}
} else {
// sin entrada: seguimos
}
}
return 0;
}