English 中文(简体)
How can I get the uptime of a IBM AIX box in seconds?
原标题:
  • 时间:2010-01-15 13:14:32
  •  标签:
  • aix
  • uptime

I m writting a Perl script for which I need the uptime in seconds to do some calculation, in all the machine in the shop (i.e. linux, SunOS, and AIX). I have a way to get the uptime for linux (/proc/uptime), and SunOS (kstat -p unix:0:system_misc:boot_time), thanks to an another posting on this site, but I can find a good way of getting it for AIX. I don t really like the idea of parsing uptime with reg-ex, since uptime changes when the machine is been up just sec, mins, days, or over a year.

问题回答

This snippet in C works under AIX 6.1. I can t give you the source article as I only have source code left.

#include <utmpx.h>
int main ( )
{
    int nBootTime = 0;
    int nCurrentTime = time ( NULL );
    struct utmpx * ent;

    while ( ( ent = getutxent ( ) ) ) {
        if ( !strcmp ( "system boot", ent->ut_line ) ) {
            nBootTime = ent->ut_tv.tv_sec;
        }
    }
    printf ( "System was booted %d seconds ago
", nCurrentTime - nBootTime );
}

Parse the output of last(1)?

Find a file/directory that is only created/refreshed at boot time and stat it?

Frankly, using different regexs to handle the different possible outputs from uptime doesn t sound so bad.

Answering old thread for new interested stumblers.

We re going to make a lightweight C program called getProcStartTime that you ll have plenty of other uses for. It tells you when a process was started, given the PID. I believe you will still get a time stamp down to the second even if the process was started months or years ago. Save this source code as a file called getProcStartTime.c:

#include <time.h>
#include <procinfo.h>

main(argc, argv)
char *argv[];
{
  struct procentry64 psinfo;
  pid_t pid;
  if (argc > 1) {
    pid = atoi(argv[1]);
  }
  else {
    printf("Usage : getProcStartTime pid
");
    return 1;
  }
  if (getprocs64(&psinfo, sizeof(struct procentry64), NULL, sizeof(struct fdsinfo64) , &pid, 1) > 0) {
    time_t result;
    result = psinfo.pi_start;
    struct tm* brokentime = localtime(&result);
    printf("%s", asctime(brokentime)); 
    return 0;
  } else {
    perror("getproc64");
    return 1;
  }
}

Then compile it:

gcc getProcStartTime.c -o getProcStartTime

Here s the magic logic: AIX just like Linux has a process called init with PID 1. It can t be killed or restarted. So the start time of PID 1 is the boot time of your server.

./getProcStartTime 1

On my server, returns Wed Apr 23 10:33:30 2014; yours will be different.

Note, I originally made getProcStartTime specifically for this purpose, but now I use it in all kinds of other scripts. Want to know how long an Oracle database has been up? Find the PID of Oracle s PMON and pass that PID as your arg after getProcStartTime.

If you really want the output as an integer number of seconds, it would be an easy programming exercise to modify the code above. The name getProcUptime is just a suggestion. Then you could just call:

./getProcUptime 1

UPDATE: Source code and precompiled binary for AIX 6.1/7.1 have been put on my Github repo here: https://github.com/Josholith/getProcStartTime





相关问题
strndup call is currupting stack frames

I have seen a strange behavior with "strndup" call on AIX 5.3 and 6.1. If I call strndup with size more than the size of actual source string length, then there is a stack corruption after that call. ...

How can I get the uptime of a IBM AIX box in seconds?

I m writting a Perl script for which I need the uptime in seconds to do some calculation, in all the machine in the shop (i.e. linux, SunOS, and AIX). I have a way to get the uptime for linux (/proc/...

How to find dll loaded by a process in AIX?

How to find which dynamic libs .so are loaded currently by a process, also those dll are dynamically loaded with dllopen. I want to debug a process to find out which libs its using currently.

SNMP on AIX 5.3

I need to monitor a system with AIX 5.3, but can t find a good source for the OIDs. Does anybody know the OIDs for CPU, Memory and Harddisk values?

Unix: fast remove directory for cleaning up daily builds

Is there a faster way to remove a directory then simply submitting rm -r -f *directory* ? I am asking this because our daily cross-platform builds are really huge (e.g. 4GB per build). So the ...

热门标签