Skip to content

Commit

Permalink
v1.10.1 VMware command updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven-D-Foster committed May 28, 2021
1 parent 6c7ea2f commit a1293c8
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 31 deletions.
31 changes: 18 additions & 13 deletions HavokMultimedia.Utilities.Console/Commands/VMwareList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,26 @@ protected override void CreateHelp(CommandHelpBuilder help)
}

private IReadOnlyList<string> ObjectTypeNames => Funcs.Keys
.OrderBy(o => o.FullNameFormatted())
.Select(o => o.Name.Substring("VMware".Length))
.OrderBy(o => o, StringComparer.OrdinalIgnoreCase)
.Select(o => o.Substring("VMware".Length))
.ToList();

private Dictionary<Type, Func<VMware, IEnumerable>> Funcs = new()
private Dictionary<string, Func<VMware, IEnumerable>> Funcs = new()
{
{ typeof(VMwareDatacenter), VMwareDatacenter.Query },
{ typeof(VMwareDatastore), VMwareDatastore.Query },
{ typeof(VMwareFolder), VMwareFolder.Query },
{ typeof(VMwareHost), VMwareHost.Query },
{ typeof(VMwareNetwork), VMwareNetwork.Query },
{ typeof(VMwareResourcePool), VMwareResourcePool.Query },
{ typeof(VMwareStoragePolicy), VMwareStoragePolicy.Query },
{ typeof(External.VMwareVM), External.VMwareVM.Query },
{ typeof(VMwareVMSlim), VMwareVMSlim.Query },
{ nameof(VMwareDatacenter), VMwareDatacenter.Query },
{ nameof(VMwareDatastore), VMwareDatastore.Query },
{ nameof(VMwareFolder), VMwareFolder.Query },
{ nameof(VMwareHost), VMwareHost.Query },
{ nameof(VMwareNetwork), VMwareNetwork.Query },
{ nameof(VMwareResourcePool), VMwareResourcePool.Query },
{ nameof(VMwareStoragePolicy), VMwareStoragePolicy.Query },
{ nameof(External.VMwareVM), External.VMwareVM.Query },
{ "VMwareVM_Quick", VMwareVMSlim.Query },
{ "VMwareVM_WithoutTools", VMwareVMSlim.QueryWithoutToolsInstalled },
{ "VMwareVM_PoweredOff", VMwareVMSlim.QueryPoweredOff },
{ "VMwareVM_PoweredOn", VMwareVMSlim.QueryPoweredOn },
{ "VMwareVM_25%Free", VMwareVMSlim.QueryDiskspace25 },
{ "VMwareVM_10%Free", VMwareVMSlim.QueryDiskspace10 },
};

protected override void ExecuteInternal(VMware vmware)
Expand Down Expand Up @@ -81,7 +86,7 @@ private Func<VMware, IEnumerable> GetFunc(string objectType)
objectType = "VMware" + objectType;
foreach (var kvp in Funcs)
{
if (objectType.EqualsCaseInsensitive(kvp.Key.Name)) return kvp.Value;
if (objectType.EqualsCaseInsensitive(kvp.Key)) return kvp.Value;
}
return null;
}
Expand Down
8 changes: 4 additions & 4 deletions HavokMultimedia.Utilities.Console/External/VMware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ public JToken GetValue(string path, IDictionary<string, string> parameters = nul
{
if (type.EndsWith("service_unavailable", StringComparison.OrdinalIgnoreCase))
{
log.Warn("Service Unavailable: " + path);
log.Debug("Service Unavailable: " + path);

try
{
foreach (var message in obj["value"]?["messages"].OrEmpty())
{
var defaultMessage = message["default_message"]?.ToString().TrimOrNull();
var id = message["id"]?.ToString().TrimOrNull();
if (id != null && message != null) log.Warn(id + " --> " + defaultMessage);
else if (id != null) log.Warn(id);
else if (defaultMessage != null) log.Warn(defaultMessage);
if (id != null && message != null) log.Debug(id + " --> " + defaultMessage);
else if (id != null) log.Debug(id);
else if (defaultMessage != null) log.Debug(defaultMessage);
}
}
catch (Exception e)
Expand Down
101 changes: 91 additions & 10 deletions HavokMultimedia.Utilities.Console/External/VMwareObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public override string ToString()
{
sb.AppendLine(" " + property.Name + ": " + val.ToStringGuessFormat());
}
else
else if (val is IEnumerable)
{
int count = 0;
foreach (var item in (IEnumerable)val)
{
var vitem = item as VMwareObject;
var vitem = (VMwareObject)item;
sb.AppendLine(" " + vitem.GetType().NameFormatted() + "[" + count + "]");
foreach (var prop in vitem.GetProperties())
{
Expand All @@ -95,6 +95,10 @@ public override string ToString()
count++;
}
}
else
{
sb.AppendLine(" " + property.Name + ": " + val.ToStringGuessFormat());
}
}

return sb.ToString();
Expand All @@ -118,6 +122,8 @@ public class VMwareVMSlim : VMwareObject
public string CpuCount { get; }
public string PowerState { get; }

private bool IsPoweredOn => PowerState == null ? false : PowerState.EndsWith("ON", StringComparison.OrdinalIgnoreCase);

public VMwareVMSlim(VMware vmware, JToken obj)
{
VM = obj["vm"]?.ToString();
Expand All @@ -134,26 +140,95 @@ public static IEnumerable<VMwareVMSlim> Query(VMware vmware)
.OrderBy(o => o.Name, StringComparer.OrdinalIgnoreCase);
}

public static IEnumerable<VMwareVMSlim> QueryWithoutToolsInstalled(VMware vmware)
{
var slims = Query(vmware);
var d = new Dictionary<string, VMwareVMSlim>(StringComparer.OrdinalIgnoreCase);
foreach (var slim in slims) d[slim.VM] = slim;

foreach (var full in VMwareVM.Query(vmware))
{
if (!full.IsVMwareToolsInstalled) yield return d[full.VM];
}
}

private class SlimDiskSpace : VMwareVMSlim
{
public IReadOnlyList<VMwareVM.GuestLocalFilesystem> Filesystems { get; set; }
public int PercentFreeThreshhold { get; set; }
public SlimDiskSpace(VMware vmware, JToken obj) : base(vmware, obj)
{
}
public IEnumerable<VMwareVM.GuestLocalFilesystem> FileSystemsCrossingThreshold => Filesystems.Where(o => o.PercentFree != null && o.PercentFree.Value <= PercentFreeThreshhold).OrderBy(o => o.Key);
public override string ToString() => base.ToString() + " " + FileSystemsCrossingThreshold.Select(o => "(" + o.PercentFree.Value + "% free) " + o.Key).ToStringDelimited(" ");
}

public static IEnumerable<VMwareVMSlim> QueryDiskspace10(VMware vmware) => QueryDiskspace(vmware, 10);
public static IEnumerable<VMwareVMSlim> QueryDiskspace25(VMware vmware) => QueryDiskspace(vmware, 25);

public static IEnumerable<VMwareVMSlim> QueryDiskspace(VMware vmware, int percentFreeThreshhold)
{
var dsobjs = vmware.GetValueArray("/rest/vcenter/vm")
.Select(o => new SlimDiskSpace(vmware, o))
.OrderBy(o => o.Name, StringComparer.OrdinalIgnoreCase);
var d = new Dictionary<string, SlimDiskSpace>(StringComparer.OrdinalIgnoreCase);
foreach (var dsobj in dsobjs) d[dsobj.VM] = dsobj;


var fullvms = VMwareVM.Query(vmware);

foreach (var fullvm in fullvms)
{
var dsobj = d[fullvm.VM];
dsobj.Filesystems = fullvm.GuestLocalFilesystems;
dsobj.PercentFreeThreshhold = percentFreeThreshhold;
}

foreach (var dsobj in d.Values.OrderBy(o => o.Name, StringComparer.OrdinalIgnoreCase))
{
if (dsobj.FileSystemsCrossingThreshold.IsEmpty()) continue;
yield return dsobj;
}
}

public static IEnumerable<VMwareVMSlim> QueryPoweredOff(VMware vmware) => Query(vmware).Where(o => !o.IsPoweredOn);
public static IEnumerable<VMwareVMSlim> QueryPoweredOn(VMware vmware) => Query(vmware).Where(o => o.IsPoweredOn);


public override string ToString()
{
return VM.PadRight(10) + Name;
return VM.PadRight(10) + (IsPoweredOn ? " " : "OFF") + " " + Name;
}



}

public class VMwareVM : VMwareObject
{
public class GuestLocalFilesystem : VMwareObject
{
public string Key { get; }
public string FreeSpace { get; }
public string Capacity { get; }
public long? FreeSpace { get; }
public long? Capacity { get; }
public long? Used { get; }
public byte? PercentFree { get; }
public byte? PercentUsed { get; }

public GuestLocalFilesystem(JToken obj)
{
Key = obj["key"]?.ToString();
FreeSpace = obj["value"]?["free_space"]?.ToString();
Capacity = obj["value"]?["capacity"]?.ToString();
var sFreeSpace = obj["value"]?["free_space"]?.ToString();
if (sFreeSpace != null) FreeSpace = long.Parse(sFreeSpace);

var sCapacity = obj["value"]?["capacity"]?.ToString();
if (sCapacity != null) Capacity = long.Parse(sCapacity);

if (FreeSpace == null || Capacity == null) return;

Used = Capacity.Value - FreeSpace.Value;
PercentFree = (byte)((double)FreeSpace.Value / (double)Capacity.Value * (double)100);
PercentUsed = (byte)((double)100 - (double)PercentFree.Value);
}
}

Expand Down Expand Up @@ -361,7 +436,7 @@ public Nic(JToken obj)
public IReadOnlyList<ScsiAdapter> ScsiAdapters { get; }
public IReadOnlyList<Nic> Nics { get; }
public IReadOnlyList<GuestLocalFilesystem> GuestLocalFilesystems { get; }

public bool IsVMwareToolsInstalled { get; }
public VMwareVM(VMware vmware, JToken obj)
{
VM = obj["vm"]?.ToString();
Expand Down Expand Up @@ -407,16 +482,22 @@ public VMwareVM(VMware vmware, JToken obj)
IdentityHostName = obj["host_name"]?.ToString();
}

GuestLocalFilesystems = QueryValueArraySafe(vmware, $"/rest/vcenter/vm/{VM}/guest/local-filesystem").Select(o => new GuestLocalFilesystem(o)).ToList();
var localFilesystem = QueryValueArraySafe(vmware, $"/rest/vcenter/vm/{VM}/guest/local-filesystem").ToArray();
GuestLocalFilesystems = localFilesystem.Select(o => new GuestLocalFilesystem(o)).ToList();

if (obj == null && localFilesystem.Length == 0) IsVMwareToolsInstalled = false;
else IsVMwareToolsInstalled = true;
}

public static IEnumerable<VMwareVM> Query(VMware vmware)
{
foreach (var obj in vmware.GetValueArray("/rest/vcenter/vm"))
foreach (var obj in vmware.GetValueArray("/rest/vcenter/vm").OrderBy(o => o["name"]?.ToString(), StringComparer.OrdinalIgnoreCase))
{
yield return new VMwareVM(vmware, obj);
}
}


}

public class VMwareDatacenter : VMwareObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<ReleaseVersion>1.10.0</ReleaseVersion>
<ReleaseVersion>1.10.1</ReleaseVersion>
<Description>Misc command line helper utilities</Description>
<StartupObject>HavokMultimedia.Utilities.Console.Program</StartupObject>
<ApplicationIcon>Icon.ico</ApplicationIcon>
Expand Down
2 changes: 1 addition & 1 deletion HavokMultimedia.Utilities.Console/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ namespace HavokMultimedia.Utilities.Console
{
public static class Version
{
public static string Value => "1.10.0";
public static string Value => "1.10.1";
}
}
2 changes: 1 addition & 1 deletion HavokMultimedia.Utilities.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ Global
Policies = $0
$0.DotNetNamingPolicy = $1
$1.DirectoryNamespaceAssociation = PrefixedHierarchical
version = 1.10.0
version = 1.10.1
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion HavokMultimedia.Utilities/HavokMultimedia.Utilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<ReleaseVersion>1.10.0</ReleaseVersion>
<ReleaseVersion>1.10.1</ReleaseVersion>
<Description>Various helper methods and functions</Description>
</PropertyGroup>

Expand Down

0 comments on commit a1293c8

Please sign in to comment.