Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C#:Getting the 'Uptime' of a group of PC's

Options
  • 04-09-2007 12:11am
    #1
    Closed Accounts Posts: 364 ✭✭


    Hi Folks,
    I'm looking for a bit of help, or more specifically, other peoples on the best solution of getting the uptime (and other related metrics) from a group of PC's on the network using C# for reporting purposes e.g. the info can be used for a user to view from a website. Here are a few possible options that I'm looking at:

    1) The following can be used for getting the uptime of a PC that a program is running:
    int nodeUptime = Environment.TickCount;
    

    However this is no good as I need this to run from a server and get the uptime from a group of PC's. Can this be used for that? I dont want to go down the road of having a Scheduled task run on every PC to run this command in an .EXE and update the info locally to a text file (to be queried by a script/web app).

    2) The ideal solution would be able to run the following DOS command progamatically from the server and get the output:
    uptime \\<computer name>

    This has its own problem of being able to run a DOS cmd within a C# program. I.E:
    System.Diagnostics.Process processCmd;
    processCmd= new System.Diagnostics.Process();
    processCmd.EnableRaisingEvents = false;
    string strMyCmd;
    strMyCmd = <my command?>;\\not sure on how to pass uptime command
    System.Diagnostics.Process.Start("CMD.exe",strMyCmd);
    processCmd.Close();
    

    I would just need to send the output to a text file as I wont need a DB to store data as it will be real time. just a matter of parsing this file (not worried about Parsing, UI etc..)

    3) Use SNMP request. Need to investigate this more. Seems like the option that would get me the solution sooner, however when I want to extend the PC metrics, this option could be messy. Whereas option 2) gives me the option to easily expand (command uptime can have different switches)

    How would you guys approach this problem?
    Thanks in advance.


Comments

  • Registered Users Posts: 169 ✭✭DonnieBrasco


    Hi,

    Could you do something like this:

    Create an application that will start on startup (perhaps as a service)
    Use the Winsock API to allow you interact remotely.

    Essentially you will create a chat program using winsock. When you SEND a certain command/Switch (chat txt) to your client, it will then recognise the incoming txt and perform different routines. A type of chat program would easily allow you to implement switches etc.
    For example – if my chat client receives the following txt “return uptime”
    then the client invokes nodeUptime = Environment.TickCount and returns the results to the server(the other chat participant)

    Or is there something like java's RMI that could be used?
    ...not very graceful solutions and probably would require an application to be running at all times on the clients.

    If you've used another technique could you summarise for curiosity's sake!


  • Closed Accounts Posts: 364 ✭✭Paligulus


    Thanks for the reply.

    Good ideas but I'm limited to what I can do on the actual clients. I also want the app to be as unobstrusive and self contained as possible!

    I've spoke to a few people and they are saying to go with option 2 above (in some cases they suggested that without me mentioning the options!!). As you can run the 'uptime' command from the CMD promp already it seems like the easist way to go where :
    strMyCmd = uptime \\(comp name) >outputforParsing.txt


    I'm a bit out of practice on the coding end of things so I'm open to any suggestions!!!


  • Registered Users Posts: 2,426 ✭✭✭ressem


    Better approach would be to use WMI.

    This is a slightly modified version of
    http://www.csharphelp.com/archives2/archive334.html

    c# code
    using System;
    using System.Management;
    using System.Collections.Generic;
    using System.Text;
    
    namespace wmiconsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                GetWMIuptime();
    
            }
    
            static void GetWMIuptime()
            {
                ConnectionOptions oConn = new ConnectionOptions();
                //oConn.Username = "";
                //oConn.Password = "";
    
                System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\localhost\\root\\cimv2", oConn);
    
    
                System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select LastBootUpTime from Win32_OperatingSystem");
                ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
                ManagementObjectCollection oReturnCollection = oSearcher.Get();
    
                foreach (ManagementObject oReturn in oReturnCollection)
                {
                    // Disk name
                    Console.WriteLine("Last Boot Time : " + oReturn["LastBootUpTime"].ToString());
                }
    
            }
    
        }
    }
    
    

    Replace localhost with the ip or netbios name of the PC and run as a domain user with permissions to read the WMI values.


    An index of the other properties is
    http://msdn2.microsoft.com/en-us/library/aa394554.aspx


  • Closed Accounts Posts: 364 ✭✭Paligulus


    Thanks for the post, that looks ideal. You've given me more that enough to work with. I'll go through it and the links in detail and report how I get on and anything new I learn.

    Cheers again


  • Registered Users Posts: 2,781 ✭✭✭amen


    what do you mean by up time?
    the time the pc has been on with rebooting
    or the time the user has been logged on for?


  • Advertisement
  • Closed Accounts Posts: 364 ✭✭Paligulus


    amen wrote:
    what do you mean by up time?
    the time the pc has been on with rebooting
    or the time the user has been logged on for?


    Well the main thing will be the last time the PC was rebooted. I will prob include other stats aswell along the way. So ideally what ever method I use should have the scope to include other stats later on. Thats another reason why i like the look of WMI as suggested below.


Advertisement