using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Management;
using System.Configuration;
using System.Threading;
namespace ServerMonitoringConsole
{
class Program
{
static string clientName = "myName";
static string clientPwd = "myPwd";
static string domain = "ntlmdomain:myDomain";
static List<int> cpuLoads = new List<int>();
static string errorStr = "";
static void Main(string[] args)
{
int load = 0;
int avg = 0;
long mem = 0;
int cpuTimePeriod = 60;
string[] stringServers = { "serv1", "serv2" };
ConnectionOptions co = new ConnectionOptions();
co.Username = clientName;
co.Password = clientPwd;
co.Authority = domain;
List<ManagementScope> msList = new List<ManagementScope>();
msList.Add(new ManagementScope(@"\\" + stringServers[0] + @"\root\cimv2", co));
msList.Add(new ManagementScope(@"\\" + stringServers[1] + @"\root\cimv2", co));
ObjectQuery sq = new ObjectQuery("SELECT * FROM Win32_Processor");
List<ManagementObjectSearcher> mosList = new List<ManagementObjectSearcher>();
foreach (ManagementScope ms in msList)
{
ms.Connect();
mosList.Add(new ManagementObjectSearcher(ms, sq));
}
while (true)
{
load = 0;
int counter = 0;
while (cpuLoads.Count <= cpuTimePeriod)
{
Thread.Sleep(1000);
foreach (string server in stringServers)
{
GetCPUPerformance(server, ref load, mosList[counter]);
cpuLoads.Add(load);
counter++;
}
}
avg = (int)cpuLoads.Sum() / cpuTimePeriod;
cpuLoads.Clear();
}
}
static void GetCPUPerformance(string computerName, ref int cpuLoad, ManagementObjectSearcher mos)
{
using (ManagementObjectCollection moObjs = mos.Get())
{
foreach (ManagementObject mo in moObjs)
{
try
{
cpuLoad = Convert.ToInt32(mo["LoadPercentage"]);
}
finally
{
mo.Dispose();
}
}
}
}
}
}
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.