Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Get uptime of remote computer using WMI

// description of your code here
Get uptime of remote computer using WMI, I'm not sure the convert time function is correct, it gives different results than psinfo.exe

Private Function GetUptime() As String


        Dim WMIScope As Management.ManagementScope
        Dim WMIConnectionOptions As New Management.ConnectionOptions
        Dim strRootDomain As String
        Dim objRootLDAP As DirectoryEntry
        Dim query As ManagementObjectSearcher
        Dim queryCollection As ManagementObjectCollection
        Dim oq As New System.Management.ObjectQuery
        Dim mo As New ManagementObject
        Dim perftimestamp As String = ""
        Dim perttimefreq As String = ""
        Dim counter As String = ""
        Dim iUptimeInSec As Long = 0

        With WMIConnectionOptions
            .Impersonation = System.Management.ImpersonationLevel.Impersonate
            .Authentication = System.Management.AuthenticationLevel.Packet

        End With

        WMIScope = New Management.ManagementScope("\\" & _
        Strcomputer & "\root\cimv2", WMIConnectionOptions)

        objRootLDAP = New DirectoryEntry("LDAP://RootDSE")
        strRootDomain = objRootLDAP.Properties.Item("rootDomainNamingContext").Value.ToString

        oq = New System.Management.ObjectQuery("select * from Win32_PerfRawData_PerfOS_System")
        query = New ManagementObjectSearcher(WMIScope, oq)
        queryCollection = query.Get()

        For Each oReturn As ManagementObject In queryCollection
            perftimestamp = oReturn("Timestamp_Object").ToString()
            perttimefreq = oReturn("Frequency_Object").ToString()
            counter = oReturn("SystemUpTime").ToString()
        Next

        ' Calculation in seconds:
        iUptimeInSec = Convert.ToInt64((Double.Parse(perftimestamp) - Double.Parse(counter)) / Double.Parse(perttimefreq))

        ' convert the seconds
        Return ConvertTime(iUptimeInSec)

    End Function

    Private Function ConvertTime(ByVal seconds As Long) As String

        Dim ConvSec As Long
        Dim ConvMin As Long
        Dim ConvHour As Long
        Dim ConvDays As Long

        Math.DivRem(seconds, 60, ConvSec)

        Math.DivRem(seconds, 3600, ConvMin)
        ConvMin = ConvMin / 60

        Math.DivRem(seconds, (3600 * 24), ConvHour)
        ConvHour = ConvHour / 3600

        ConvDays = (seconds / (3600 * 24))

        Return (ConvDays.ToString() + " days " + ConvHour.ToString() + " hours " + ConvMin.ToString() + " minutes " + ConvSec.ToString() + " seconds ")

    End Function

'or you could try this method which still doesn't match psinfo or systeminfo but it is closer

Private Function GetUptime2() As String

        Dim WMIScope As Management.ManagementScope
        Dim WMIConnectionOptions As New Management.ConnectionOptions
        Dim strRootDomain As String
        Dim objRootLDAP As DirectoryEntry
        Dim query As ManagementObjectSearcher
        Dim queryCollection As ManagementObjectCollection
        Dim oq As New System.Management.ObjectQuery
        Dim mo As New ManagementObject
        Dim perftimestamp As String = ""
        Dim perttimefreq As String = ""
        Dim counter As String = ""
        Dim iUptimeInSec As Long = 0
        Dim LastBootUpTime As DateTime
        Dim dateNow As DateTime = Date.Now

        With WMIConnectionOptions
            .Impersonation = System.Management.ImpersonationLevel.Impersonate
            .Authentication = System.Management.AuthenticationLevel.Packet

        End With

        WMIScope = New Management.ManagementScope("\\" & _
        Strcomputer & "\root\cimv2", WMIConnectionOptions)

        objRootLDAP = New DirectoryEntry("LDAP://RootDSE")
        strRootDomain = objRootLDAP.Properties.Item("rootDomainNamingContext").Value.ToString

        oq = New System.Management.ObjectQuery("Select * from Win32_OperatingSystem")

        query = New ManagementObjectSearcher(WMIScope, oq)
        queryCollection = query.Get()

        For Each oReturn As ManagementObject In queryCollection

            LastBootUpTime = System.Management.ManagementDateTimeConverter.ToDateTime(oReturn("LastBootUpTime").ToString)
            
        Next

        Dim ts As TimeSpan = dateNow.Subtract(LastBootUpTime)
        Console.WriteLine(ts.Days.ToString + " days " + ts.Hours.ToString + " hours " + ts.Minutes.ToString + " minutes " + ts.Seconds.ToString + " seconds ")

    End Function

Simple process watchdog

I had a problem where an important monitoring daemon was committing sepuku every night between 3 and 4 am. While debugging it, I whipped up the script below.

Basically, it checks to see if the process ID file is present, and if it is, checks that the process is actually running. If not, it invokes the restart command.

It outputs info to syslog so you can check it out the next day.

Written in perl, requires Sys::Syslog. Tested on Linux (your ps command details may vary on other unices). Not worth bothering with this on windows.

#!/usr/bin/perl

use Sys::Syslog qw(:standard :macros);

my $pidfile = "/usr/local/bs/adm/uxmon.pid";
my $check_command = "uxmon";
my $start_command = "/etc/init.d/bigsister restart";

openlog("ProcessWatcher", "pid",LOG_LOCAL0);

if (! -e $pidfile) {
    syslog(LOG_WARNING, "Process ID file not found, restarting application");
    &restart_proc;
    exit;
}

open (IN, $pidfile) || syslog(LOG_ERR, "Couldn't read $pidfile- $!");
my $pid = <IN>;
chomp $pid;

my $ret = `ps -p $pid -o comm=`;
if ($ret != $check_command) {
    syslog(LOG_ERR, "Return value mismatch, restarting Process");
    &restart_proc;
    exit;
}



sub restart_proc() {
    my $msg = `$start_command`;
    syslog (LOG_INFO, "Restarting Process returned: $msg");
}


« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS