Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created a page to show all known VMs #8640

Merged
merged 12 commits into from May 21, 2018
71 changes: 71 additions & 0 deletions html/includes/forms/get-vmlist.inc.php
@@ -0,0 +1,71 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2017 Aldemir Akpinar <https://github.com/aldemira>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version. Please see LICENSE.txt at the top level of
* the source code distribution for details.
* @package LibreNMS
* @subpackage webui
* @link http://librenms.org
* @copyright 2018 Aldemir Akpinar
* @author Aldemir Akpinar <aldemir.akpinar@gmail.com>
*/

use LibreNMS\Authentication\Auth;

$vm_query = "SELECT v.vmwVmDisplayName AS vmname, v.vmwVmState AS powerstat, v.device_id AS deviceid, d.hostname AS physicalsrv, d.sysname AS sysname, v.vmwVmGuestOS AS os, v.vmwVmMemSize AS memory, v.vmwVmCpus AS cpu FROM vminfo AS v LEFT JOIN devices AS d ON v.device_id = d.device_id";

$param = [];
if (!Auth::user()->hasGlobalRead()) {
$vm_query .= ' LEFT JOIN devices_perms AS DP ON d.device_id = DP.device_id';
$uidwhere = ' AND DP.user_id = ?';
$uid = [Auth::id()];
} else {
$uidwhere = '';
$uid = [];
}

if (isset($vars['searchPhrase']) && !empty($vars['searchPhrase'])) {
$vm_query .= " WHERE v.vmwVmDisplayName LIKE ? OR d.hostname LIKE ? OR v.vmwVmGuestOS LIKE ? OR d.sysname LIKE ?" . $uidwhere;
$count_query = "SELECT COUNT(v.vmwVmDisplayName) FROM vminfo AS v LEFT JOIN devices AS d ON v.device_id = d.device_id WHERE v.vmwVmDisplayName LIKE ? OR d.hostname LIKE ? OR v.vmwVmGuestOS LIKE ? OR d.sysname LIKE ?" . $uidwhere;
$searchphrase = '%' . $vars['searchPhrase'] . '%';
array_push($param, $searchphrase, $searchphrase, $searchphrase, $searchphrase, $uid);
} else {
$count_query = "SELECT COUNT(*) FROM vminfo ";
}

$order_by = '';
if (isset($vars['sort']) && is_array($vars['sort'])) {
foreach ($vars['sort'] as $key => $value) {
$order_by .= " $key $value";
}
} else {
$order_by = " vmname";
}

$vm_query .= " ORDER BY " . $order_by;

if (is_numeric($vars['rowCount']) && is_numeric($vars['current'])) {
$rowcount = $vars['rowCount'];
$current = $vars['current'];
$vm_query .= " LIMIT ".$rowcount * ($current - 1).", ".$rowcount;
}


if (isset($vars['searchPhrase']) && !empty($vars['searchPhrase'])) {
$vm_arr = dbFetchRows($vm_query, $param);
$rec_count = dbFetchCell($count_query, $param);
} else {
$vm_arr = dbFetchRows($vm_query);
$rec_count = dbFetchCell($count_query);
}

$status = ['current' => $current, 'rowCount' => $rowcount, 'rows' => $vm_arr, 'total' => $rec_count];

header('Content-Type: application/json');
echo _json_encode($status);
5 changes: 5 additions & 0 deletions html/includes/print-menubar.php
Expand Up @@ -215,6 +215,11 @@

echo '<li><a href="'.generate_url(array('page'=>'device-dependencies')).'"><i class="fa fa-group fa-fw fa-lg"></i> Device Dependencies</a></li>';

$vm_count = dbFetchCell('SELECT COUNT(id) from `vminfo`');
if ($vm_count > 0) {
echo '<li><a href="'.generate_url(array('page'=>'vminfo')).'"><i class="fa fa-cog fa-fw fa-lg"></i> Virtual Machines</a></li>';
}

echo '
<li role="presentation" class="divider"></li>
<li><a href="addhost/"><i class="fa fa-plus fa-fw fa-lg" aria-hidden="true"></i> Add Device</a></li>
Expand Down
93 changes: 93 additions & 0 deletions html/pages/vminfo.inc.php
@@ -0,0 +1,93 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2017 Aldemir Akpinar <https://github.com/aldemira>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version. Please see LICENSE.txt at the top level of
* the source code distribution for details.
* @package LibreNMS
* @subpackage webui
* @link http://librenms.org
* @copyright 2018 Aldemir Akpinar
* @author Aldemir Akpinar <aldemir.akpinar@gmail.com>
*/


use LibreNMS\Authentication\Auth;

$pagetitle[] = 'Virtual Machines';
?>
<div class="table-responsive">
<table id="vminfo" class="table table-hover table-condensed table-striped">
<thead>
<tr>
<th data-column-id="deviceid" data-visible="false" data-css-class="deviceid">No</th>
<th data-column-id="sysname" data-visible="false">Sysname</th>
<th data-column-id="vmname" data-type="string">Server Name</th>
<th data-column-id="powerstat" data-type="string" data-formatter="powerstatus">Power Status</th>
<th data-column-id="physicalsrv" data-type="string" data-formatter="hostdev">Physical Server</th>
<th data-column-id="os" data-type="string" data-searchable="false" data-formatter="osname">Operating System</th>
<th data-column-id="memory" data-type="string" data-searchable="false" data-formatter="mem">Memory</th>
<th data-column-id="cpu" data-type="string" data-formatter="cpu" data-searchable="false">CPU</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script>
var grid = $("#vminfo").bootgrid({
rowCount: [50, 100, 250, -1],
ajax: true,
post: function() {
return {
type: "get-vmlist",
};
},
url: "ajax_form.php",
templates: {
header: '<div id="{{ctx.id}}" class="{{css.header}}"> \
<div class="row"> \
<div class="actionBar"><p class="{{css.search}}"></p><p class="{{css.actions}}"></p></div></div></div>'
},
formatters: {
"osname": function(column, row) {
if (row.os == 'E: tools not installed') {
return 'Unknown (VMware tools not installed)';
} else if (row.os == 'E: tools not running') {
return 'Unknown (VMware tools not running)';
} else if (row.os == '') {
return 'Uknown';
} else {
return row.os;
}
},
"powerstatus": function(column, row) {
if (row.powerstat == "powered on") {
var response = '<span class="label label-success">ON</span>';
} else if (row.powerstat == "powered off") {
var response = '<span class="label label-default">OFF</span>';
}
return response;
},
"mem": function(column, row) {
if (row.memory >= 1024) {
tmpNumber = row.memory / 1024;
return tmpNumber.toFixed(2) + ' GB';
} else {
return row.memory + ' MB';
}
},
"cpu": function(column, row) {
return row.cpu + ' CPU';
},
"hostdev": function(column, row) {
return '<a href="device/device='+row.deviceid+'/" class="list-device">'+row.physicalsrv+'</a><br />'+row.sysname;
},
},
});
</script>