-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxmox.inc_apps.php
263 lines (232 loc) · 11.1 KB
/
proxmox.inc_apps.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
<?php
require_once 'includes/html/application/proxmox.inc.php';
$graphs['proxmox'] = [
'netif',
];
$pmxcl = dbFetchRows('SELECT DISTINCT(`app_instance`) FROM `applications` WHERE `app_type` = ?', ['proxmox']);
$instance = Request::get('instance', $pmxcl[0]['app_instance'] ?? null);
//print_optionbar_start();
echo "<span style='font-weight: bold;'>Proxmox Clusters</span> » ";
$sep = '';
foreach ($pmxcl as $pmxc) {
echo $sep;
$selected = $pmxc['app_instance'] == $instance || (empty($instance) && empty($sep));
if ($selected) {
echo "<span class='pagemenu-selected'>";
}
echo generate_link(\LibreNMS\Util\StringHelpers::niceCase($pmxc->app_instance), ['page' => 'apps', 'app' => 'proxmox', 'instance' => $pmxc['app_instance']]);
if ($selected) {
echo '</span>';
}
$sep = ' | ';
}
print_optionbar_end();
$pagetitle[] = 'Proxmox';
//$pagetitle[] = $instance;
/*
if (isset($vars['vmid'])) {
include 'includes/html/pages/apps/proxmox/vm.inc.php';
$pagetitle[] = $vars['vmid'];
} else {
echo '
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="row">';
foreach (proxmox_cluster_vms($instance) as $pmxvm) {
if($pmxvm['status']=='running'){
echo '<div class="col-sm-4 col-md-3 col-lg-2">' . generate_link($pmxvm['vmid'] . ' (' . $pmxvm['description'] . ')', ['page' => 'apps', 'app' => 'proxmox', 'instance' => $instance, 'vmid' => $pmxvm['vmid']]) . '</div>';
}
}
echo '
</div>
</div>
</div>
</div>
';
}*/
include 'includes/html/pages/apps/proxmox/vm.inc.php';
// START OF FIRST CUSTOM PART
$env = file_get_contents(__DIR__."/opt/librenms/.env");
$lines = explode("\n",$env);
foreach($lines as $line){
preg_match("/([^#]+)\=(.*)/",$line,$matches);
if(isset($matches[2])){
putenv(trim($line));
}
}
$servername = getenv('DB_HOST');
$username = getenv('DB_USERNAME');
$password = getenv('DB_PASSWORD');
$dbname = getenv('DB_USERNAME');
$env = file_get_contents(__DIR__."/opt/librenms/.env");
$lines = explode("\n",$env);
foreach($lines as $line){
preg_match("/([^#]+)\=(.*)/",$line,$matches);
if(isset($matches[2])){
putenv(trim($line));
}
}
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function formatBytes($bytes) {
if ($bytes >= 1073741824) {
return number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
return number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
return number_format($bytes / 1024, 2) . ' KB';
} else {
return $bytes . ' B';
}
}
function formatUptime($seconds) {
$days = floor($seconds / 86400);
$hours = floor(($seconds % 86400) / 3600);
$minutes = floor(($seconds / 60) % 60);
$seconds = $seconds % 60;
$uptimeString = '';
if ($days > 0) {
$uptimeString .= "$days days, ";
}
$uptimeString .= "$hours hours, $minutes minutes, $seconds seconds";
return $uptimeString;
}
// prevent XSS attacks
function escape_html($string) {
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}
$sqlGetVms = 'SELECT * FROM proxmox;';
$result = $conn->query($sqlGetVms);
function generate_box_close() {
return '</div></div>';
}
echo '
<form method="POST" id="column-selector">
<input type="hidden" name="_token" value="' . csrf_token() . '">
<label><input type="checkbox" name="columns[]" value="state" checked' . (in_array('state', $_POST['columns'] ?? []) ? 'checked' : '') . '> State</label>
<label><input type="checkbox" name="columns[]" value="vmid" checked' . (in_array('vmid', $_POST['columns'] ?? []) ? 'checked' : '') . '> VM ID</label>
<label><input type="checkbox" name="columns[]" value="name" checked' . (in_array('name', $_POST['columns'] ?? []) ? 'checked' : '') . '> Name</label>
<label><input type="checkbox" name="columns[]" value="cpu_usage" checked' . (in_array('cpu_usage', $_POST['columns'] ?? []) ? 'checked' : '') . '> CPU Usage</label>
<label><input type="checkbox" name="columns[]" value="cpu_percent" checked' . (in_array('cpu_percent', $_POST['columns'] ?? []) ? 'checked' : '') . '> CPU Percent Usage</label>
<label><input type="checkbox" name="columns[]" value="mem_usage" checked' . (in_array('mem_usage', $_POST['columns'] ?? []) ? 'checked' : '') . '> Memory Used</label>
<label><input type="checkbox" name="columns[]" value="disk_usage" checked' . (in_array('disk_usage', $_POST['columns'] ?? []) ? 'checked' : '') . '> Disk Usage</label>
<label><input type="checkbox" name="columns[]" value="ceph_disks" checked' . (in_array('ceph_disks', $_POST['columns'] ?? []) ? 'checked' : '') . '> Ceph Disks Usage Rate</label>
<label><input type="checkbox" name="columns[]" value="ceph_bigger_disk" checked' . (in_array('ceph_bigger_disk', $_POST['columns'] ?? []) ? 'checked' : '') . '> Ceph Bigger Disk Usage</label>
<label><input type="checkbox" name="columns[]" value="ceph_snapshots" checked' . (in_array('ceph_snapshots', $_POST['columns'] ?? []) ? 'checked' : '') . '> Ceph Snapshots</label>
<label><input type="checkbox" name="columns[]" value="total_snapshots" checked' . (in_array('total_snapshots', $_POST['columns'] ?? []) ? 'checked' : '') . '> Ceph Total Snapshots</label>
<label><input type="checkbox" name="columns[]" value="oldest_snapshot" checked' . (in_array('oldest_snapshot', $_POST['columns'] ?? []) ? 'checked' : '') . '> Oldest Snapshot</label>
<label><input type="checkbox" name="columns[]" value="qemu_info" checked' . (in_array('qemu_info', $_POST['columns'] ?? []) ? 'checked' : '') . '> Qemu Info</label>
<label><input type="checkbox" name="columns[]" value="network_in" checked' . (in_array('network_in', $_POST['columns'] ?? []) ? 'checked' : '') . '> Network IN</label>
<label><input type="checkbox" name="columns[]" value="network_out" checked' . (in_array('network_out', $_POST['columns'] ?? []) ? 'checked' : '') . '> Network OUT</label>
<label><input type="checkbox" name="columns[]" value="uptime" checked' . (in_array('uptime', $_POST['columns'] ?? []) ? 'checked' : '') . '> Uptime</label>
<button type="submit" style="background-color: #007bff; color: white; border: none; padding: 8px 16px; border-radius: 4px; cursor: pointer;">
Apply
</button>
</form>
';
$available_columns = [
'state' => 'State',
'vmid' => 'VM ID',
'name' => 'Name',
'cpu_usage' => 'CPU Usage',
'cpu_percent' => 'CPU Percent Usage',
'mem_usage' => 'Memory Used',
'disk_usage' => 'Disk Usage',
'ceph_disks' => 'Ceph Disks<br>Usage Rate',
'ceph_bigger_disk' => 'Ceph Bigger<br>Disk Usage',
'ceph_snapshots' => 'Ceph Snapshots',
'total_snapshots' => 'Ceph Total<br>Snapshots(GiB)',
'oldest_snapshot' => 'Oldest<br>snapshot(days)',
'qemu_info' => 'Qemu Info',
'network_in' => 'Network IN',
'network_out' => 'Network OUT',
'uptime' => 'Uptime'
];
$selected_columns = $_POST['columns'] ?? array_keys($available_columns);
if ($result->num_rows > 0) {
echo '<table class="table table-condensed table-striped table-hover">';
echo '<thead style="text-align:center; vertical-align:middle;"><tr style="text-align:center; vertical-align:middle;">';
foreach ($selected_columns as $col) {
if (isset($available_columns[$col])) {
echo '<th style="text-align:center; vertical-align:middle;"><pre>' . $available_columns[$col] . '</pre></th>';
}
}
echo '</tr></thead><tbody>';
while ($vm = $result->fetch_assoc()) {
echo '<tr>';
foreach ($selected_columns as $col) {
echo '<td style="text-align:center; vertical-align:middle;">';
switch ($col) {
case 'state':
$statusClass = $vm['status'] === 'running' ? 'text-success' : 'text-danger';
echo '<strong class="' . $statusClass . '">' . escape_html($vm['status']) . '</strong>';
break;
case 'vmid':
echo escape_html($vm['vmid']);
break;
case 'name':
echo escape_html($vm['name']);
break;
case 'cpu_usage':
echo escape_html($vm['cpu']) . ' / ' . escape_html($vm['cpus']);
break;
case 'cpu_percent':
echo escape_html($vm['cpu_percent']) . ' %';
break;
case 'mem_usage':
echo escape_html(formatBytes($vm['mem'])) . ' / ' . escape_html(formatBytes($vm['maxmem']));
break;
case 'disk_usage':
echo escape_html(formatBytes($vm['disk'])) . ' / ' . escape_html(formatBytes($vm['maxdisk']));
break;
case 'ceph_disks':
$ceph_disks = unserialize($vm['ceph_disks']);
if (is_array($ceph_disks)) {
foreach ($ceph_disks as $disk) {
echo escape_html(preg_replace('/vm-\d+-/', '', $disk)) . '<br>';
}
}
break;
case 'ceph_bigger_disk':
echo escape_html($vm['ceph_bigger_disk_percent_usage']) . '%';
break;
case 'ceph_snapshots':
$ceph_snapshots = unserialize($vm['ceph_snapshots']);
if (is_array($ceph_snapshots)) {
foreach ($ceph_snapshots as $snapshot) {
echo escape_html(preg_replace('/vm-\d+-state-/', '', $snapshot)) . '<br>';
}
}
break;
case 'total_snapshots':
echo escape_html($vm['ceph_total_snapshots']);
break;
case 'oldest_snapshot':
echo escape_html($vm['oldest_snapshot']);
break;
case 'qemu_info':
echo escape_html($vm['qemu_info']);
break;
case 'network_in':
echo escape_html(formatBytes($vm['netin']));
break;
case 'network_out':
echo escape_html(formatBytes($vm['netout']));
break;
case 'uptime':
echo escape_html(formatUptime($vm['uptime']));
break;
}
echo '</td>';
}
echo '</tr>';
}
echo '</tbody></table>';
} else {
echo 'No virtual machines found.';
}
$conn->close();
// END OF FIRST CUSTOM PART