我开发了一个C#WPF监控应用程序,可以检查与其他服务器的连接。在远程服务器上,我们在某个端口上运行应用程序服务。如果我们只是telnet到该服务器的端口,我们会看到服务名称正在运行。 我正在忙着将我的概念验证应用程序转换为WPF应用程序,以便它使用端点和Socket.BeginConnect等不断检查在远程服务器上运行的连接和服务,该工作100%。 但是,我想监视几个服务器,每个服务器检查与其他远程服务器的连接 - 所以我想要一种方法能够从一个远程服务器运行测试到另一个远程服务器并在我的WPF应用程序上本地获取结果。 解决此问题的一种方法是在远程服务器上运行收集此信息的应用程序,然后获取对该数据的访问权限,但如果可能的话,我想尽量避免这种情况。
这是我的代码 - 目前非常原始,非常概念验证。 我想在网络上的笔记本电脑上运行这个应用程序,并监控其他服务器:
// CPU Usage ----------------------------------------------------------
private string getUsageCPU()
{
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
return cpuCounter.NextValue() + "%";
}
// --------------------------------------------------------------------
// Memory Usage -------------------------------------------------------
private string getUsageRAM()
{
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
return ramCounter.NextValue() + " MB";
}
// --------------------------------------------------------------------
// Service Status -----------------------------------------------------
private string getServicesStatus(string service)
{
string status = "";
ServiceController sc = new ServiceController(service);
try
{
status = sc.Status.ToString();
}
catch (Exception ex)
{
status = "not found: " + ex.Message;
}
return status;
}
// --------------------------------------------------------------------
// Connectivity Test --------------------------------------------------
private string getTelnetResponse(string host, int port)
{
string result = "";
IPAddress ipAddress = null;
try {
IPHostEntry ipHostInfo = Dns.GetHostEntry(host);
if (!IPAddress.TryParse(host, out ipAddress))
{
ipAddress = ipHostInfo.AddressList[0];
}
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Need to run this from the Server we are monitoring not from where the application is running.
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client, "This is a test<EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
result = String.Format("Response received : {0}", response);
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
labelValueTelnet.Text = (e.ToString());
}
return result;
}
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone = new ManualResetEvent(false);
private static ManualResetEvent sendDone = new ManualResetEvent(false);
private static ManualResetEvent receiveDone = new ManualResetEvent(false);
// The response from the remote device.
private static String response = String.Empty;
// State object for receiving data from remote device.
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
private void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
////labelValueTelnet.Text = String.Format("Socket connected to {0}", client.RemoteEndPoint.ToString());
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception)
{
//labelValueTelnet.Text = (e.ToString());
}
}
private void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
labelValueTelnet.Text = (e.ToString());
}
}
private void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
labelValueTelnet.Text = (e.ToString());
}
}
private void Send(Socket client, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
}
private void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
//labelValueTelnet.Text = String.Format("Sent {0} bytes to server.", bytesSent);
// Signal that all bytes have been sent.
sendDone.Set();
}
catch (Exception e)
{
labelValueTelnet.Text = (e.ToString());
}
}
// --------------------------------------------------------------------