如何在几秒钟内获得 IBM AIX 机器的正常运行时间?

发布于 2024-08-18 04:03:08 字数 262 浏览 14 评论 0 原文

我正在编写一个 Perl 脚本,我需要在商店中的所有机器(即 Linux、SunOS 和 AIX)中以秒为单位的正常运行时间来进行一些计算。我有办法获得 linux (/proc/uptime) 和 SunOS (kstat -p unix:0:system_misc:boot_time) 的正常运行时间,这要归功于本网站上的另一篇文章,但我可以找到一种获得的好方法适用于 AIX。我真的不喜欢用 reg-ex 解析正常运行时间的想法,因为当机器运行了几秒、几分钟、几天或一年多时,正常运行时间就会发生变化。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮世清欢 2024-08-25 04:03:08

此 C 代码片段适用于 AIX 6.1。
我不能给你源文章,因为我只剩下源代码。

#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 );
}

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 );
}
海之角 2024-08-25 04:03:08

解析last(1)的输出?

找到一个仅在启动时创建/刷新的文件/目录并统计它?

坦率地说,使用不同的正则表达式来处理 uptime 的不同可能输出听起来不错。

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.

温柔戏命师 2024-08-25 04:03:08

为新感兴趣的绊脚石回答旧线程。

我们将制作一个名为 getProcStartTime 的轻量级 C 程序,您将有很多其他用途。给定 PID,它会告诉您进程何时启动。我相信,即使该过程是几个月或几年前开始的,您仍​​然会得到精确到秒的时间戳。将此源代码保存为名为 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;
  }
}

然后编译它:

gcc getProcStartTime.c -o getProcStartTime

这是神奇的逻辑:AIX 就像 Linux 一样,有一个名为 init 的进程,PID 为 1。不会被杀死或重新启动。所以PID 1的启动时间就是你的服务器的启动时间。

./getProcStartTime 1

在我的服务器上,返回Wed Apr 23 10:33:30 2014;你的会有所不同。

请注意,我最初专门为此目的创建了 getProcStartTime,但现在我在各种其他脚本中使用它。想知道Oracle数据库已经运行了多久?找到 Oracle PMON 的 PID,并将该 PID 作为 getProcStartTime 之后的参数传递。

如果您确实希望输出为整数秒,则修改上面的代码将是一个简单的编程练习。名称 getProcUptime 只是一个建议。然后您可以调用:

./getProcUptime 1

更新: AIX 6.1/7.1 的源代码和预编译二进制文件已放在我的 Github 存储库中:https://github.com/Josholith/getProcStartTime

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文