Files
librenms-librenms/app/Models/Vminfo.php
Tony Murray 087d588102 Vmware vminfo modernize (#15008)
* Vmware vminfo
Remove legacy file and migrate to OS discovery

* tighter

* ios_stp-vlans working correctly now

* Make vmwVmGuestOS nullable

* Discover os info too

* VM Info module

* Apply fixes from StyleCI

* Fix log severity

* Fix log severity (more)

* VM Info module

* Poll with ESXi too because it is lightweight
add test data

* poller data now too

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
2023-10-05 19:49:26 -05:00

75 lines
2.0 KiB
PHP

<?php
namespace App\Models;
use Config;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Str;
use LibreNMS\Interfaces\Models\Keyable;
use LibreNMS\Util\Html;
use LibreNMS\Util\Number;
use LibreNMS\Util\Rewrite;
class Vminfo extends DeviceRelatedModel implements Keyable
{
use HasFactory;
protected $table = 'vminfo';
public $timestamps = false;
protected $fillable = [
'vm_type',
'vmwVmVMID',
'vmwVmDisplayName',
'vmwVmGuestOS',
'vmwVmMemSize',
'vmwVmCpus',
'vmwVmState',
];
public function getStateLabelAttribute(): array
{
return Html::powerStateLabel($this->vmwVmState);
}
public function getMemoryFormattedAttribute(): string
{
return Number::formatBi($this->vmwVmMemSize * 1024 * 1024);
}
public function getOperatingSystemAttribute(): string
{
if (Str::contains($this->vmwVmGuestOS, 'tools not installed')) {
return 'Unknown (VMware Tools not installed)';
} elseif (Str::contains($this->vmwVmGuestOS, 'tools not running')) {
return 'Unknown (VMware Tools not running)';
} elseif (empty($this->vmwVmGuestOS)) {
return '(Unknown)';
} else {
return Rewrite::vmwareGuest($this->vmwVmGuestOS);
}
}
public function scopeGuessFromDevice(Builder $query, Device $device): Builder
{
$where = [$device->hostname];
if (Config::get('mydomain')) {
$where[] = $device->hostname . '.' . Config::get('mydomain');
}
return $query->whereIn('vmwVmDisplayName', $where);
}
public function parentDevice(): HasOne
{
return $this->hasOne(\App\Models\Device::class, 'hostname', 'vmwVmDisplayName');
}
public function getCompositeKey()
{
return $this->vm_type . $this->vmwVmVMID;
}
}