如何记录 Linux 的系统运行时间的统计信息 | Linux 中国

如何记录 Linux 的系统运行时间的统计信息 | Linux 中国Linux/Unix 系统管理员对服务器的系统运行时间有一种奇怪的痴迷。-- Vivek Gite

有用的原文链接请访问文末的“原文链接”获得可点击的文内链接、全尺寸原图和相关文章。

致谢编译自 | https://www.cyberciti.biz/hardware/see-records-statistics-about-a-linux-servers-uptime/ 
 作者 | Vivek Gite
 译者 | MjSeven ? ? ? 共计翻译:21 篇 贡献时间:67 天

Linux/Unix 系统管理员对服务器的系统运行时间有一种奇怪的痴迷。这里有一个关于这个主题的 xkcd 漫画,一个好的系统管理员是一股不可阻挡的力量,他伫立在你家猫咪博客的服务器之前,对抗黑暗势力。

如何记录 Linux 的系统运行时间的统计信息 | Linux 中国

Fig.01: Devotion to Duty https://xkcd.com/705/

我们可以使用 uptime 命令或 w 命令[1] 或 top 命令来判断 Linux 系统运行了多久。我可以使用 tuptime 工具保留每次重新启动的运行时间,以获得系统运行时间的历史和统计报告[2]

这就像 uptime 命令一样,但输出结果更令人印象深刻。最近我发现了另一种称为 uptimed 的工具,用于记录关于机器的系统运行时间和统计信息。让我们看看如何使用 Linux 操作系统上的 uptimed 和 uprecords 来获得运行时间的记录统计信息。

查找系统运行时间非常简单,只需在基于 Linux 的系统上键入以下命令即可:


  1. $ uptime -p

  2. up 2 weeks, 4 days, 7 hours, 28 minutes

要保留有关 uptime 的历史统计信息,请使用 tuptime[2] 或 uptimed 工具。

安装 uptimed

安装 uptimed 的最简单的方式是通过你的软件包管理器,比如 apt/apt-get/yum 这些你的 Linux 发行版的朋友。

在 Debian/Ubuntu Linux 上安装 uptimed

键入以下 apt 命令[3]/apt-get 命令[4]


  1. $ sudo apt-get install uptimed

示例输出:


  1. Reading package lists... Done

  2. Building dependency tree      

  3. Reading state information... Done

  4. The following additional packages will be installed:

  5.  libuptimed0

  6. The following NEW packages will be installed:

  7.  libuptimed0 uptimed

  8. 0 upgraded, 2 newly installed, 0 to remove and 3 not upgraded.

  9. Need to get 40.7 kB of archives.

  10. After this operation, 228 kB of additional disk space will be used.

  11. Do you want to continue? [Y/n] y

  12. Get:1 http://mirrors.linode.com/ubuntu xenial/universe amd64 libuptimed0 amd64 1:0.3.17-4 [9,050 B]

  13. Get:2 http://mirrors.linode.com/ubuntu xenial/universe amd64 uptimed amd64 1:0.3.17-4 [31.6 kB]

  14. Fetched 40.7 kB in 0s (2,738 kB/s)  

  15. Preconfiguring packages ...

  16. Selecting previously unselected package libuptimed0.

  17. (Reading database ... 39163 files and directories currently installed.)

  18. Preparing to unpack .../libuptimed0_1%3a0.3.17-4_amd64.deb ...

  19. Unpacking libuptimed0 (1:0.3.17-4) ...

  20. Selecting previously unselected package uptimed.

  21. Preparing to unpack .../uptimed_1%3a0.3.17-4_amd64.deb ...

  22. Unpacking uptimed (1:0.3.17-4) ...

  23. Processing triggers for systemd (229-4ubuntu21) ...

  24. Processing triggers for ureadahead (0.100.0-19) ...

  25. Processing triggers for man-db (2.7.5-1) ...

  26. Setting up libuptimed0 (1:0.3.17-4) ...

  27. Setting up uptimed (1:0.3.17-4) ...

  28. Processing triggers for libc-bin (2.23-0ubuntu9) ...

  29. Processing triggers for systemd (229-4ubuntu21) ...

  30. Processing triggers for ureadahead (0.100.0-19) ...

在 CentOS/RHEL/Fedora/Oracle/Scientific Linux 上安装 uptimed

首先 在 CentOS/RHEL 使用 EPEL 仓库[5]


  1. $ sudo yum -y install epel-release

然后,键入以下 yum 命令[6]


  1. $ sudo yum install uptimed

示例输出:


  1. Loaded plugins: fastestmirror

  2. Loading mirror speeds from cached hostfile

  3. * base: centos.excellmedia.net

  4. * epel: ftp.cuhk.edu.hk

  5. * extras: centos.excellmedia.net

  6. * updates: centos.excellmedia.net

  7. Resolving Dependencies

  8. --> Running transaction check

  9. ---> Package uptimed.x86_64 0:0.4.0-6.el7 will be installed

  10. --> Finished Dependency Resolution

  11. Dependencies Resolved

  12. ===============================================================================

  13. Package           Arch             Version               Repository      Size

  14. ===============================================================================

  15. Installing:

  16. uptimed           x86_64           0.4.0-6.el7           epel            47 k

  17. Transaction Summary

  18. ===============================================================================

  19. Install  1 Package

  20. Total download size: 47 k

  21. Installed size: 98 k

  22. Is this ok [y/d/N]: y

  23. Downloading packages:

  24. uptimed-0.4.0-6.el7.x86_64.rpm                            |  47 kB   00:01    

  25. Running transaction check

  26. Running transaction test

  27. Transaction test succeeded

  28. Running transaction

  29.  Installing : uptimed-0.4.0-6.el7.x86_64                                  1/1

  30.  Verifying  : uptimed-0.4.0-6.el7.x86_64                                  1/1

  31. Installed:

  32.  uptimed.x86_64 0:0.4.0-6.el7                                                

  33. Complete!

如果你正在使用 Fedora Linux,运行以下 dnf 命令:


  1. $ sudo dnf install uptimed

在 Arch Linux 上安装 uptimed

键入以下 pacman 命令:


  1. $ sudo pacman -S uptimed

在 Gentoo Linux 上安装 uptimed

键入以下 emerge 命令:


  1. $ sudo emerge --ask uptimed

如何配置 uptimed

使用文本编辑器编辑 /etc/uptimed.conf 文件,例如 vim 命令:


  1. $ sudo vim /etc/uptimed.conf

最少设置一个 email 地址来发送记录。假定有个兼容 sendmail 的 MTA 安装在 /usr/lib/sendmail


  1. EMAIL=[email protected].cyberciti.biz

保存并关闭文件。

如何在系统启动时启动 uptimed 服务?

使用 systemctl 命令启动 uptimed 服务:


  1. $ sudo systemctl enable uptimed

我该如何 启动/停止/重启 或者查看 uptimed 服务的状态?


  1. $ sudo systemctl start uptimed ## start it ##

  2. $ sudo systemctl stop uptimed ## stop it ##

  3. $ sudo systemctl restart uptimed ## restart it ##

  4. $ sudo systemctl status uptimed ## view status ##

示例输出:


  1. uptimed.service - uptime record daemon

  2.   Loaded: loaded (/lib/systemd/system/uptimed.service; enabled; vendor preset: enabled)

  3.   Active: active (running) since Thu 2017-11-09 17:49:14 UTC; 18min ago

  4. Main PID: 11137 (uptimed)

  5.   CGroup: /system.slice/uptimed.service

  6.           └─11137 /usr/sbin/uptimed -f

  7. Nov 09 17:49:14 gfs04 systemd[1]: Started uptime record daemon.

如何查看 uptime 记录

只需键入以下命令即可查看 uptimed(8) 程序的统计信息:


  1. $ uprecords

示例输出:

如何记录 Linux 的系统运行时间的统计信息 | Linux 中国

Fig.02: uprecords in action

uprecords 有一些选项:


  1. $ uprecords -?

示例输出:


  1. usage: uprecords [OPTION]...

  2.  -?             this help

  3.  -a             do not print ansi codes

  4.  -b             sort by boottime

  5.  -B             reverse sort by boottime

  6.  -k             sort by sysinfo

  7.  -K             reverse sort by sysinfo

  8.  -d             print downtime seen before every uptimes instead of system

  9.  -c             do not show current entry if not in top entries

  10.  -f             run continously in a loop

  11.  -s             do not print extra statistics

  12.  -w             wide output (more than 80 cols per line)

  13.  -i INTERVAL    use INTERVAL seconds for loop instead of 5, implies -f

  14.  -m COUNT       show a maximum of top COUNT entries instead of 10

  15.  -M             show next milestone

  16.  -v             version information

结论

这是一个极好的小工具,可以显示服务器正常运行时间的记录,以证明机器正常运行时间和你的业务连续性。在相关说明中,你可以看到官方的 XKCD 系统管理员 T恤[7] 因为漫画被制作成衬衫,其中包括背面的新插图。

如何记录 Linux 的系统运行时间的统计信息 | Linux 中国

Fig.03: Sysadmin XKCD shirt features the original comic on the front and a new illustration on the back.


via: https://www.cyberciti.biz/hardware/see-records-statistics-about-a-linux-servers-uptime/

作者:Vivek Gite[9] 译者:MjSeven 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出