Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@adoprog
Last active August 29, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adoprog/14261ac207a37079fd4f to your computer and use it in GitHub Desktop.
Save adoprog/14261ac207a37079fd4f to your computer and use it in GitHub Desktop.
Sample code - Opserver API
var allNodes = DashboardData.AllNodes.Where(s => !s.IsUnwatched).Where(s => s.CPULoad >= 0 || s.MemoryUsed >= 0).ToList();
var downNodes = allNodes.Where(x => x.Status == NodeStatus.Down);
Thread.Sleep(3000);
float inBps = 0;
float outBps = 0;
var totalRam = 0.0;
var totalDisk = 0.0;
foreach (var node in allNodes)
{
// Let's get the metrics from VM hosts only
if (!node.IsVMHost)
{
continue;
}
foreach (var i in node.PrimaryInterfaces)
{
inBps += i.InBps.HasValue ? i.InBps.Value : 0;
outBps += i.OutBps.HasValue ? i.OutBps.Value : 0;
}
totalRam += node.TotalMemory.HasValue ? node.TotalMemory.Value : 0;
totalDisk += node.Volumes.Sum(volume => volume.Size.HasValue ? volume.Size.Value : 0);
}
inBps = inBps / 1024 / 1024;
outBps = outBps / 1024 / 1024;
totalRam = totalRam / 1024 / 1024 / 1024;
totalDisk = totalDisk / 1024 / 1024 / 1024;
Console.WriteLine(string.Format("You have {0} servers", allNodes.Count()));
Console.WriteLine(string.Format("{0} of them are down", downNodes.Count()));
Console.WriteLine(string.Format("Network Upload: {0:0.00} MB", inBps));
Console.WriteLine(string.Format("Network Download: {0:0.00} MB", outBps));
Console.WriteLine(string.Format("Total RAM: {0:0.00} GB", totalRam));
Console.WriteLine(string.Format("Total HDD: {0:0.00} GB", totalDisk));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment