-
Notifications
You must be signed in to change notification settings - Fork 0
/
de-info-plugin.php
319 lines (284 loc) · 10.3 KB
/
de-info-plugin.php
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/*
Plugin Name: Server Resource Monitor
Description: Monitor de recursos del servidor en tiempo real
Version: 2.0
Author: Alexis Olivero
author URI: https://oliverodev.com/
*/
// Agregar scripts y estilos necesarios
function enqueue_monitor_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('server-monitor', plugins_url('js/monitor.js', __FILE__), array('jquery'), '1.0', true);
wp_localize_script('server-monitor', 'monitorAjax', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('monitor_nonce')
));
}
add_action('admin_enqueue_scripts', 'enqueue_monitor_scripts');
function get_realtime_data() {
check_ajax_referer('monitor_nonce', 'nonce');
$data = array(
'memory' => get_server_memory_usage(),
'cpu' => get_simple_cpu_usage(),
'services' => get_top_resource_consuming_services(),
'server' => get_server_usage_percentage()
);
wp_send_json_success($data);
}
add_action('wp_ajax_get_realtime_data', 'get_realtime_data');
function get_server_memory_usage() {
$memory_limit = ini_get('memory_limit');
$memory_used = memory_get_usage(true);
$memory_limit_bytes = return_bytes($memory_limit);
return array(
'used' => round($memory_used / 1024 / 1024, 2),
'limit' => $memory_limit,
'percentage' => round(($memory_used / $memory_limit_bytes) * 100, 2)
);
}
function get_simple_cpu_usage() {
try {
if (stristr(PHP_OS, 'win')) {
$cmd = "wmic cpu get loadpercentage /value";
$wmic_output = @shell_exec($cmd);
if (preg_match('/LoadPercentage=(\d+)/', $wmic_output, $matches)) {
$percentage = (int)$matches[1];
} else {
$cmd = "powershell \"Get-Counter '\\Processor(_Total)\\% Processor Time' -SampleInterval 1 -MaxSamples 1 | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue\"";
$ps_output = @shell_exec($cmd);
$percentage = is_numeric(trim($ps_output)) ? round((float)trim($ps_output), 2) : 0;
}
} else {
$load = sys_getloadavg();
$percentage = isset($load[0]) ? round($load[0] * 100 / get_nprocs(), 2) : 0;
}
return array(
'percentage' => $percentage,
'timestamp' => time()
);
} catch (Exception $e) {
error_log('Error en CPU: ' . $e->getMessage());
return array('percentage' => 0, 'timestamp' => time());
}
}
function get_nprocs() {
if (stristr(PHP_OS, 'win')) {
$cmd = "wmic cpu get NumberOfLogicalProcessors /value";
$wmic_output = @shell_exec($cmd);
if (preg_match('/NumberOfLogicalProcessors=(\d+)/', $wmic_output, $matches)) {
return (int)$matches[1];
}
} else {
return (int)trim(shell_exec("nproc"));
}
return 1; // Default to 1 if unable to determine
}
function get_plugins_resource_usage() {
if (!function_exists('get_plugin_data')) {
require_once(ABSPATH . 'wp-admin/includes/plugin.php');
}
$plugins_info = array();
$active_plugins = (array) get_option('active_plugins', array());
foreach ($active_plugins as $plugin_path) {
try {
$full_path = WP_PLUGIN_DIR . '/' . $plugin_path;
if (!file_exists($full_path)) continue;
$time_start = microtime(true);
include_once($full_path);
$time_end = microtime(true);
$plugin_data = get_plugin_data($full_path);
$plugins_info[] = array(
'name' => $plugin_data['Name'],
'impact' => round(($time_end - $time_start) * 1000, 2)
);
} catch (Exception $e) {
error_log("Error midiendo plugin {$plugin_path}: " . $e->getMessage());
}
}
return $plugins_info;
}
function return_bytes($size_str) {
switch (substr($size_str, -1)) {
case 'G': case 'g': return (int)$size_str * 1024 * 1024 * 1024;
case 'M': case 'm': return (int)$size_str * 1024 * 1024;
case 'K': case 'k': return (int)$size_str * 1024;
default: return (int)$size_str;
}
}
function get_quick_response_time() {
$start = microtime(true);
$test_url = admin_url('admin-ajax.php');
$args = array(
'timeout' => 2,
'blocking' => true,
'sslverify' => false
);
$response = wp_remote_head($test_url, $args);
return round((microtime(true) - $start) * 1000, 2);
}
function get_server_usage_percentage() {
$cpu = get_simple_cpu_usage()['percentage'];
$memory = get_server_memory_usage()['percentage'];
return round(($cpu + $memory) / 2, 2);
}
function get_top_resource_consuming_services() {
$services = array();
if (stristr(PHP_OS, 'win')) {
$cmd = "tasklist /FO CSV /NH";
$output = shell_exec($cmd);
$lines = explode("\n", trim($output));
foreach ($lines as $line) {
$columns = str_getcsv($line);
if (count($columns) >= 6) {
$memory_mb = round(intval(str_replace(',', '', $columns[4])) / 1024, 2);
$services[] = array(
'name' => $columns[0],
'pid' => $columns[1],
'memory' => $memory_mb
);
}
}
} else {
$cmd = "ps aux --sort=-%mem | awk 'NR>1 {print $11, $2, $6}' | head -n 10";
$output = shell_exec($cmd);
$lines = explode("\n", trim($output));
foreach ($lines as $line) {
$columns = preg_split('/\s+/', $line);
if (count($columns) >= 3) {
$memory_mb = round(intval($columns[2]) / 1024, 2);
$services[] = array(
'name' => $columns[0],
'pid' => $columns[1],
'memory' => $memory_mb
);
}
}
}
return $services;
}
function server_monitor_page() {
if (!current_user_can('manage_options')) {
wp_die(__('No tienes suficientes permisos para acceder a esta página.'));
}
?>
<div class="wrap">
<h1>Monitor de Recursos en Tiempo Real</h1>
<div class="card" id="memory-card">
<h2>Memoria del Servidor</h2>
<div class="resource-meter">
<div class="meter-bar" id="memory-bar"></div>
</div>
<p>Uso: <span id="memory-used">0</span> MB de <span id="memory-limit">0</span></p>
<p>Porcentaje: <span id="memory-percentage">0</span>%</p>
</div>
<div class="card" id="server-card">
<h2>Uso del Servidor</h2>
<div class="resource-meter">
<div class="meter-bar" id="server-bar"></div>
</div>
<p>Uso total: <span id="server-percentage">0</span>%</p>
</div>
<div class="card">
<h2>Servicios que Consumen Más Recursos</h2>
<table class="widefat">
<thead>
<tr>
<th>Servicio</th>
<th>PID</th>
<th>Memoria (MB)</th>
</tr>
</thead>
<tbody id="services-list"></tbody>
</table>
</div>
</div>
<style>
.card {
background: #fff;
border: 1px solid #ccd0d4;
padding: 15px;
margin: 15px 0;
box-shadow: 0 1px 1px rgba(0,0,0,.04);
}
.resource-meter {
background: #f1f1f1;
height: 20px;
border-radius: 10px;
margin: 10px 0;
overflow: hidden;
}
.meter-bar {
background: #0073aa;
height: 100%;
width: 0%;
border-radius: 10px;
transition: width 0.5s ease-in-out;
max-width: 100%;
}
.card h2 {
margin-top: 0;
}
.high-usage {
color: #d63638;
font-weight: bold;
}
.medium-usage {
color: #dba617;
}
.low-usage {
color: #0073aa;
}
</style>
<script>
jQuery(document).ready(function($) {
function updateMonitorData() {
$.ajax({
url: monitorAjax.ajaxurl,
type: 'post',
data: {
action: 'get_realtime_data',
nonce: monitorAjax.nonce
},
success: function(response) {
if (response.success) {
var data = response.data;
$('#memory-used').text(data.memory.used);
$('#memory-limit').text(data.memory.limit);
$('#memory-percentage').text(data.memory.percentage);
$('#memory-bar').css('width', data.memory.percentage + '%');
$('#server-percentage').text(data.server);
$('#server-bar').css('width', data.server + '%');
var servicesList = $('#services-list');
servicesList.empty();
data.services.forEach(function(service) {
servicesList.append('<tr>' +
'<td>' + service.name + '</td>' +
'<td>' + service.pid + '</td>' +
'<td>' + service.memory + '</td>' +
'</tr>');
});
}
}
});
}
setInterval(updateMonitorData, 5000);
updateMonitorData();
});
</script>
<?php
}
// Registrar el menú del plugin
function server_monitor_menu() {
add_menu_page(
'Monitor de Recursos',
'Monitor de Recursos',
'manage_options',
'server-monitor',
'server_monitor_page',
'dashicons-performance',
90
);
}
add_action('admin_menu', 'server_monitor_menu');
?>