Skip to content

Instantly share code, notes, and snippets.

@GABeech
Created January 8, 2016 16:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GABeech/5a17c0f079dd49f2ad73 to your computer and use it in GitHub Desktop.
Save GABeech/5a17c0f079dd49f2ad73 to your computer and use it in GitHub Desktop.
Query Dell Drac c#
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using DracMan.Scanners.DeserializationModels;
using Jil;
using WSManAutomation;
using static System.Console;
namespace DracMan.Scanners
{
class DellDrac
{
private static readonly NameValueCollection Configuration = (NameValueCollection)ConfigurationManager.GetSection("Drac");
public static SystemInfo GatherHardwareInfo(IPAddress ip)
{
var wsman = new WSMan();
var hwReturn = new SystemInfo();
var drives = new List<DriveInfo>();
var rinfo = new List<RaidInfo>();
var procinfo = new List<CpuInfo>();
var meminfo = new List<MemoryInfo>();
hwReturn.CurrentIp = ip;
var wsManFlags = wsman.SessionFlagCredUsernamePassword() | wsman.SessionFlagSkipCNCheck() |
wsman.SessionFlagSkipCACheck() | wsman.SessionFlagUseBasic() | wsman.SessionFlagUTF8();
var wsManOpt = (IWSManConnectionOptions)wsman.CreateConnectionOptions();
var querySchemas = new Dictionary<string, string>
{
{ "CPUView", "http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_CPUView"},
{ "SystemView", "http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemView" },
{ "PDiskView", "http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_PhysicalDiskView"},
{ "VirtualDiskView", "http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_VirtualDiskView" },
{ "MemoryView", "http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_MemoryView" },
//{ "Experiment", "http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_PCIDeviceView" }
};
var rackData = new Dictionary<string,string>()
{
{"DCName", "System.Embedded.1#ServerTopology.1#DataCenterName" },
{"RackName", "System.Embedded.1#ServerTopology.1#RackName" },
{"RackSlot", "System.Embedded.1#ServerTopology.1#RackSlot" }
};
var wsManEndpoint = "https://" + ip + "/wsman";
wsManOpt.UserName = Configuration.Get("User");
wsManOpt.Password = Configuration.Get("Password");
var wsManSession = (IWSManSession)wsman.CreateSession(wsManEndpoint, wsManFlags, wsManOpt);
wsManSession.Timeout = 10000;
try
{
if ((wsManSession.Identify()) == null)
{
return null;
}
}
catch (UnauthorizedAccessException e)
{
e.AddLoggedData("TargetComputer", ip.ToString());
e.LogToExceptional();
//WriteLine($"[Identify] {ip}: Not authroized.");
//Console.WriteLine($"{ip}: {e.Message}\n{e.StackTrace}");
return null;
}
catch (COMException e)
{
e.LogToExceptional();
//WriteLine($"[Identify] {ip}: COM Exception.");
//Console.WriteLine($"{ip}: {e.Message}\n{e.StackTrace}");
return null;
}
catch (Exception e)
{
if (e.Message.Contains("could not connect"))
{
WriteLine($"[Identify] {ip}: Count not connect.");
return null;
}
//WriteLine($"[Identify] {ip}: Error ({e.GetType()}):{e.Message}\n{e.StackTrace}");
e.LogToExceptional();
return null;
}
foreach (var r in rackData)
{
var s = new XmlSerializer(typeof(DCIM_SystemString));
var i = new XmlSerializer(typeof(DCIM_SystemInteger));
var dcimString =
"http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemString?__cimnamespace=root/dcim+InstanceID=";
var dcimInt =
"http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemInteger?__cimnamespace=root/dcim+InstanceID=";
try
{
switch (r.Key)
{
case "DCName":
hwReturn.DataCenterName =
((DCIM_SystemString)
s.Deserialize(
new MemoryStream(Encoding.UTF8.GetBytes(wsManSession.Get(dcimString + r.Value)))))
.CurrentValue;
break;
case "RackName":
hwReturn.RackName =
((DCIM_SystemString)
s.Deserialize(
new MemoryStream(Encoding.UTF8.GetBytes(wsManSession.Get(dcimString + r.Value)))))
.CurrentValue;
break;
case "RackSlot":
hwReturn.RackSlot = ((DCIM_SystemInteger)
i.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(wsManSession.Get(dcimInt + r.Value)))))
.CurrentValue;
break;
default:
break;
}
}
catch (Exception e)
{
if (e.InnerException != null)
{
e.AddLoggedData("InnerException", e.InnerException.ToString()).
AddLoggedData("Message", e.Message).
LogToExceptional();
}
else
{
e.LogToExceptional();
}
}
}
WriteLine($"DC: {hwReturn.DataCenterName} || Rack: {hwReturn.RackName} || Slot: {hwReturn.RackSlot}");
foreach (var schema in querySchemas)
{
try
{
var wsInstance = wsManSession.Enumerate(schema.Value);
while (!wsInstance.AtEndOfStream)
{
var curItem = wsInstance.ReadItem();
using (var r = XmlReader.Create(new StringReader(curItem.ToString())))
{
var xd = XDocument.Load(r);
switch (schema.Key)
{
case "CPUView":
procinfo.Add(new CpuInfo()
{
ClockSpeed =
int.Parse(
xd.Descendants().Single(i => i.Name.LocalName == "MaxClockSpeed").Value),
CpuNumber =
int.Parse(
xd.Descendants()
.Single(i => i.Name.LocalName == "DeviceDescription")
.Value.Split(' ')[1]),
EnabledCores =
int.Parse(
xd.Descendants()
.Single(i => i.Name.LocalName == "NumberOfEnabledCores")
.Value),
HyperThreadedCores =
int.Parse(
xd.Descendants()
.Single(i => i.Name.LocalName == "NumberOfEnabledThreads")
.Value),
Manufacturer =
xd.Descendants().Single(i => i.Name.LocalName == "Manufacturer").Value,
PhysicalCoreCount =
int.Parse(
xd.Descendants()
.Single(i => i.Name.LocalName == "NumberOfProcessorCores")
.Value),
Model = xd.Descendants().Single(i => i.Name.LocalName == "Model").Value,
Fqdd = xd.Descendants().Single(i => i.Name.LocalName == "FQDD").Value
});
break;
case "SystemView":
hwReturn.BIOSVersion =
xd.Descendants().Single(i => i.Name.LocalName == "BIOSVersionString").Value;
hwReturn.ServiceTag =
xd.Descendants().Single(i => i.Name.LocalName == "ChassisServiceTag").Value;
hwReturn.RackUnits =
int.Parse(
xd.Descendants()
.Single(i => i.Name.LocalName == "ChassisSystemHeight")
.Value);
hwReturn.HostName =
xd.Descendants().Single(i => i.Name.LocalName == "HostName").Value;
hwReturn.DracVersion =
xd.Descendants()
.Single(i => i.Name.LocalName == "LifecycleControllerVersion")
.Value;
break;
case "PDiskView":
var cDrive = new DriveInfo()
{
ConnectorNumber =
xd.Descendants().Single(i => i.Name.LocalName == "Connector").Value,
SlotNumber = xd.Descendants().Single(i => i.Name.LocalName == "Slot").Value,
Fqdd = xd.Descendants().Single(i => i.Name.LocalName == "FQDD").Value,
MediaTypeId =
xd.Descendants().Single(i => i.Name.LocalName == "MediaType").Value,
ModelNumber = xd.Descendants().Single(i => i.Name.LocalName == "Model").Value,
SataTypeId =
xd.Descendants().Single(i => i.Name.LocalName == "MaxCapableSpeed").Value,
SerialNumber =
xd.Descendants().Single(i => i.Name.LocalName == "SerialNumber").Value,
SizeInBytes =
long.Parse(
xd.Descendants().Single(i => i.Name.LocalName == "SizeInBytes").Value),
FirmwareRevision =
xd.Descendants().Single(i => i.Name.LocalName == "Revision").Value,
Manufacturer =
xd.Descendants().Single(i => i.Name.LocalName == "Manufacturer").Value
};
if (cDrive.SerialNumber == "")
{
if (drives.Exists(d => d.ModelNumber == cDrive.ModelNumber))
{
drives.First(d => d.ModelNumber == cDrive.ModelNumber).DriveCount++;
}
else
{
drives.Add(cDrive);
}
}
else
{
drives.Add(cDrive);
}
break;
case "VirtualDiskView":
rinfo.Add(new RaidInfo()
{
MemberDrives =
xd.Descendants()
.Where(i => i.Name.LocalName == "PhysicalDiskIDs")
.Select(d => d.Value)
.ToList(),
RaidTypeId =
int.Parse(
xd.Descendants().Single(i => i.Name.LocalName == "RAIDTypes").Value),
VirtualDiskName = xd.Descendants().Single(i => i.Name.LocalName == "Name").Value,
VirtualDiskSizeInBytes =
long.Parse(
xd.Descendants().Single(i => i.Name.LocalName == "SizeInBytes").Value)
});
break;
case "MemoryView":
meminfo.Add(new MemoryInfo()
{
Type =
int.Parse(
xd.Descendants().Single(i => i.Name.LocalName == "MemoryType").Value),
Speed =
int.Parse(xd.Descendants().Single(i => i.Name.LocalName == "Speed").Value),
SizeInMb =
int.Parse(xd.Descendants().Single(i => i.Name.LocalName == "Size").Value),
Rank = int.Parse(xd.Descendants().Single(i => i.Name.LocalName == "Rank").Value),
Socket =
xd.Descendants()
.Single(i => i.Name.LocalName == "DeviceDescription")
.Value.Split(' ')[1],
Bank = xd.Descendants().Single(i => i.Name.LocalName == "BankLabel").Value,
Manufacturer =
xd.Descendants().Single(i => i.Name.LocalName == "Manufacturer").Value,
SerialNumber =
xd.Descendants().Single(i => i.Name.LocalName == "SerialNumber").Value,
PartNumber =
xd.Descendants().Single(i => i.Name.LocalName == "PartNumber").Value
});
break;
case "Experiment":
WriteLine(xd.Document);
ReadLine();
break;
default:
break;
}
}
}
}
catch (UnauthorizedAccessException e)
{
e.AddLoggedData("TargetComputer", ip.ToString()).LogToExceptional();
return null;
}
catch (COMException e)
{
e.AddLoggedData("TargetComputer", ip.ToString()).LogToExceptional();
return null;
}
catch (Exception e)
{
if (e.Message.Contains("could not connect"))
{
WriteLine($"[Identify] {ip}: Count not connect.");
return null;
}
e.AddLoggedData("TargetComputer", ip.ToString()).LogToExceptional();
return null;
}
}
hwReturn.PhysicalDrives = drives;
hwReturn.Raid = rinfo;
hwReturn.Processors = procinfo;
hwReturn.Memory = meminfo;
return hwReturn;
}
public static bool CheckAlive(IPAddress host)
{
var p = new Ping();
return p.Send(host, 1000)?.Status == IPStatus.Success;
}
public static async Task<bool> CheckAliveAsync(IPAddress host)
{
var p = new Ping();
var r = await p.SendPingAsync(host, 1000);
return r.Status == IPStatus.Success;
}
public class SystemInfo
{
public string ServiceTag { get; set; }
public string HostName { get; set; }
public string BIOSVersion { get; set; }
public string DracVersion { get; set; }
public int RackUnits { get; set; }
public List<CpuInfo> Processors { get; set; }
public List<RaidInfo> Raid { get; set; }
public List<DriveInfo> PhysicalDrives { get; set; }
public List<MemoryInfo> Memory { get; set; }
public IPAddress CurrentIp { get; set; }
public string RackName { get; set; }
public string DataCenterName { get; set; }
public string AisleName { get; set; }
public string RoomName { get; set; }
public int RackSlot { get; set; }
}
public class CpuInfo
{
public int CpuNumber { get; set; }
public string Manufacturer { get; set; }
public int ClockSpeed { get; set; }
public int EnabledCores { get; set; }
public int HyperThreadedCores { get; set; }
public int PhysicalCoreCount { get; set; }
public string Model { get; set; }
public string Fqdd { get; set; }
}
public class DriveInfo
{
public string ConnectorNumber { get; set; }
public string SlotNumber { get; set; }
public long SizeInBytes { get; set; }
public long SizeInGigaBytes => ((SizeInBytes/1024)/1024)/1024;
public string ModelNumber { get; set; }
public string SerialNumber { get; set; }
public string MediaTypeId { get; set; }
public string MediaTypeIdDesc
{
get
{
switch (MediaTypeId)
{
case "0":
return "HDD";
case "1":
return "SSD";
default:
return "Unknown";
}
}
}
public string SataTypeId { get; set; }
public string SataTypeIdDesc
{
get
{
switch (SataTypeId)
{
case "0":
return "Unknown";
case "1":
return "SATA 1 (1.5)";
case "2":
return "SATA 2 (3.0)";
case "3":
return "SATA 3 (6.0)";
default:
return "Unknown";
}
}
}
public string Fqdd { get; set; }
public string FirmwareRevision { get; set; }
public string Manufacturer { get; set; }
public int DriveCount { get; set; }
}
public class RaidInfo
{
public string VirtualDiskName { get; set; }
public List<string> MemberDrives { get; set; }
public int RaidTypeId { get; set; }
public string RaidTypeIdDesc
{
get
{
switch (RaidTypeId)
{
case 0:
return "No Raid";
case 2:
return "RAID-0";
case 4:
return "RAID-1";
case 64:
return "RAID-5";
case 2048:
return "RAID-10";
case 8192:
return "RAID-50";
default:
return "Unknown";
}
}
}
public long VirtualDiskSizeInBytes { get; set; }
public long VirtualDiskSizeInGigaBytes => ((VirtualDiskSizeInBytes/1024)/1024)/1024;
}
public class MemoryInfo
{
public int Type { get; set; }
public int Speed { get; set; }
public int SizeInMb { get; set; }
public int SizeInGb => SizeInMb/1024;
public string Bank { get; set; }
public string Manufacturer { get; set; }
public string SerialNumber { get; set; }
public string PartNumber { get; set; }
public int Rank { get; set; }
public string Socket { get; set; }
public string DdrType
{
get
{
/* I'm not going to type all these out, but this is the mapping from the
* MOF File. If we need to add more mappings then we can later.
* ValueMap { "1", "2", "3", "4", "5", "6", "7", "8", "9",
* "10", "11", "12", "13", "14", "15", "16", "17", "18",
* "19", "20", "24", "25","26" },
* Values { "Other", "Unknown", "DRAM", "EDRAM", "VRAM","SRAM", "RAM", "ROM",
* "Flash", "EEPROM","FEPROM","EPROM", "CDRAM", "3DRAM", "SDRAM", "SGRAM", "RDRAM",
* "DDR", "DDR-2", "DDR-2-FB-DIMM", "DDR-3", "FBD2","DDR4"},
*/
switch (Type)
{
case 1:
return "Other";
case 2:
return "Unknown";
case 24:
return "DDR3";
case 26:
return "DDR4";
default:
return "NotCategorized";
}
}
}
}
}
}
@NeoMatrixJR
Copy link

NeoMatrixJR commented Dec 20, 2018

Is there more with this? This looks EXTREMELY useful...but where are these:

using DracMan.Scanners.DeserializationModels;
using Jil;
using WSManAutomation;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment