开发者

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

开发者 https://www.devze.com 2022-12-16 06:50 出处:网络
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/u

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开发者_JAVA百科 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\n", 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\n");
    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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号