0

Just started looking at c# programming I am trying to write a program that will eventually grab some server metrics %CPU Utilization, %Memory Utilization etc from remote hosts and then post them to InfluxDB using an API call over HTTP. So the Objective is:

To Collect CPU% Util
1. Collate Information of a Server i.e Unixtimestamp|hostip|hostname|value
2. Keep Collecting this metric once every second
3. Post this to Influxdb via a URL

I have made a start on it and only managed to get as far as getting the hostname of localhost.

This is my first project in c# any help would be great. see below code so far. I am using Visual Studio 2017 Community fyi.

using System;
using System.Net;
using System.Diagnostics;

namespace PostMetricsApp
{
  class IpProto
  {
     public static void GetMyIpAddress();
     {
        string hostName = Dns.GetHostName(); // Retrive the Name of HOST  
        Console.WriteLine(hostName);
        // Get IPAddress  
        string hostipadd = Dns.GetHostEntry(hostName).AddressList[0].ToString();
        Console.WriteLine("My IP Address is :" + hostipadd);
        Console.ReadKey();
     }
  }    
  class MemMetric
  {
     static void Main(string[] args)
     {     
        PerformanceCounter ramCounter = new PerformanceCounter();
        ramCounter.CategoryName = "Memory";
        ramCounter.CounterName = "Available MBytes";
        while (true)
        {
            var hostip = IpProto.host;
            string hostname = Environment.MachineName;
            Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            int totalhits = 0;
            float ramPercent = ramCounter.NextValue();

            if (ramPercent >= 5)
            {
                var unused = ramCounter.NextValue(); // first call will always return 0
                System.Threading.Thread.Sleep(1000); // wait a second, then try again
                //Console.WriteLine("Cpu usage: " + cpuCounter.NextValue() + "%");
                Console.WriteLine(unixTimestamp + " "+ hostip + " " + hostname + " " + ramCounter.NextValue() + "MB");
                totalhits += 1;   
            }
            else
            {
                totalhits = 0;
            }
        }
     }
  }
}
jab
  • 396
  • 3
  • 15
  • 1
    Why do beginners always pick *massive* tasks? What do you know about the remote servers? Their host names? That they exist? Anything? – Fildor Sep 14 '17 at 12:58
  • @Fildor: Beginners don't always pick massive tasks, but they are inherently incapable of **knowing** that something **is** massive when they're starting to solve it. [Relevant XKCD](https://xkcd.com/1425/). – Flater Sep 18 '17 at 09:41
  • @Flater true. But sometimes it seems like there is a bias to pick the massive ones - knowing it or not. – Fildor Sep 18 '17 at 09:43
  • @Fildor: Your obvservation in and of itself is a bias. If a beginner picks an easy task, then they're likely to complete it by themselves and we never hear about it. Logically, we usually only hear about the tasks that are unsolvable (for the beginner), which of course disproportionately favors complex situations over simple ones. In other words, SO deals with two "problem" situations: A task that is too complex for the asker, or an asker who is too underskilled for the task (arguably, they're the same thing, if you look at it as a beginner or as a veteran). – Flater Sep 18 '17 at 09:47
  • @Flater It seems you are taking my comment much too seriously ... – Fildor Sep 18 '17 at 09:50

1 Answers1

0

It isn't very clear what you are trying to achieve here, I guess you want to determine the Internet IP of the local host, on which your Application is running.

For several reasons, which I won't discuss here, it can be tricky to determine from which IP a host talks to the Internet. Some ways of doing this are:

External Service

Connect to an external server that tells you the IP address from which that was started (your internet/external IP address). Valid examples seem to be this and this.

This is probably one of the easiest/most reliable ways of doing this, as long as the remote host is up and provides you with your IP. It's almost guaranteed to be the right one. Obviously this means your application relies on this external service. Note: these host might not like to be queried often, so it would be better to cache the IP and only refresh it once ever 30 mins or so.

Alternative

There are alternatives, as tracing packets using the Windows tracert.exe console application. This still requires an external host as target for the trace, but many hosts can act as such a target. The downside is that this method is, compared to the above, rather complicated and prone to issues.

r41n
  • 908
  • 7
  • 18
  • 1
    `determine the Internet IP of the local host` It's also important to notice that a machine can have more than one IP address, e.g. if there are two network cards which are both connected to a network. If you don't know which network card you need the IP from, it may be hard to figure out _which_ of your IP addresses is the correct one for your case. – Flater Sep 18 '17 at 09:44
  • @Flater, absolutely, especially if you try to connect to that host and it's listening only on one of the two IPs. – r41n Sep 18 '17 at 10:21