如何記錄 Linux 的系統執行時間的統計資訊 | Linux 中國
有用的原文連結請訪問文末的“原文連結”獲得可點選的文內連結、全尺寸原圖和相關文章。致謝編譯自 | https://www.cyberciti.biz/hardware/see-records-statistics-about-a-linux-servers-uptime/
作者 | Vivek Gite
譯者 | MjSeven ? ? ? 共計翻譯:20 篇 貢獻時間:65 天
Linux/Unix 系統管理員對伺服器的系統執行時間有一種奇怪的痴迷。這裡有一個關於這個主題的 xkcd 漫畫,一個好的系統管理員是一股不可阻擋的力量,他佇立在你家貓咪部落格的伺服器之前,對抗黑暗勢力。
Fig.01: Devotion to Duty https://xkcd.com/705/
我們可以使用 uptime
命令或 w 命令[1] 或 top
命令來判斷 Linux 系統運行了多久。我可以使用 tuptime
工具保留每次重新啟動的執行時間,以獲得系統執行時間的歷史和統計報告[2]。
這就像 uptime
命令一樣,但輸出結果更令人印象深刻。最近我發現了另一種稱為 uptimed
的工具,用於記錄關於機器的系統執行時間和統計資訊。讓我們看看如何使用 Linux 作業系統上的 uptimed
和 uprecords
來獲得執行時間的記錄統計資訊。
查詢系統執行時間非常簡單,只需在基於 Linux 的系統上鍵入以下命令即可:
$ uptime-p
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]:
$ sudoapt-get install uptimed
示例輸出:
Readingpackage lists...Done
Building dependency tree
Reading state information...Done
The following additional packages will be installed:
libuptimed0
The following NEW packages will be installed:
libuptimed0 uptimed
0 upgraded,2 newly installed,0 to remove and3not upgraded.
Need to get40.7 kB of archives.
Afterthis operation,228 kB of additional disk space will be used.
Do you want to continue?[Y/n] y
Get:1 http://mirrors.linode.com/ubuntu xenial/universe amd64 libuptimed0 amd64 1:0.3.17-4 [9,050 B]
Get:2 http://mirrors.linode.com/ubuntu xenial/universe amd64 uptimed amd64 1:0.3.17-4 [31.6 kB]
Fetched40.7 kB in0s(2,738 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libuptimed0.
(Reading database ...39163 files and directories currently installed.)
Preparing to unpack .../libuptimed0_1%3a0.3.17-4_amd64.deb ...
Unpacking libuptimed0 (1:0.3.17-4)...
Selecting previously unselected package uptimed.
Preparing to unpack .../uptimed_1%3a0.3.17-4_amd64.deb ...
Unpacking uptimed (1:0.3.17-4)...
Processing triggers forsystemd(229-4ubuntu21)...
Processing triggers for ureadahead (0.100.0-19)...
Processing triggers forman-db (2.7.5-1)...
Setting up libuptimed0 (1:0.3.17-4)...
Setting up uptimed (1:0.3.17-4)...
Processing triggers for libc-bin (2.23-0ubuntu9)...
Processing triggers forsystemd(229-4ubuntu21)...
Processing triggers for ureadahead (0.100.0-19)...
在 CentOS/RHEL/Fedora/Oracle/Scientific Linux 上安裝 uptimed
首先 在 CentOS/RHEL 使用 EPEL 倉庫[5]:
$ sudoyum-y install epel-release
然後,鍵入以下 yum 命令[6]:
$ sudoyum install uptimed
示例輸出:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* epel: ftp.cuhk.edu.hk
* extras: centos.excellmedia.net
* updates: centos.excellmedia.net
ResolvingDependencies
-->Running transaction check
--->Package uptimed.x86_64 0:0.4.0-6.el7 will be installed
-->FinishedDependencyResolution
DependenciesResolved
===============================================================================
PackageArchVersionRepositorySize
===============================================================================
Installing:
uptimed x86_64 0.4.0-6.el7 epel 47 k
TransactionSummary
===============================================================================
Install1Package
Total download size:47 k
Installedsize:98 k
Isthis ok [y/d/N]: y
Downloading packages:
uptimed-0.4.0-6.el7.x86_64.rpm |47 kB 00:01
Running transaction check
Running transaction test
Transactiontest succeeded
Running transaction
Installing: uptimed-0.4.0-6.el7.x86_64 1/1
Verifying: uptimed-0.4.0-6.el7.x86_64 1/1
Installed:
uptimed.x86_64 0:0.4.0-6.el7
Complete!
如果你正在使用 Fedora Linux,執行以下 dnf
命令:
$ sudo dnf install uptimed
在 Arch Linux 上安裝 uptimed
鍵入以下 pacman
命令:
$ sudo pacman -S uptimed
在 Gentoo Linux 上安裝 uptimed
鍵入以下 emerge
命令:
$ sudo emerge --ask uptimed
如何配置 uptimed
使用文字編輯器編輯 /etc/uptimed.conf
檔案,例如 vim
命令:
$ sudovim/etc/uptimed.conf
最少設定一個 email 地址來發送記錄。假定有個相容 sendmail 的 MTA 安裝在 /usr/lib/sendmail
。
EMAIL=vivek@server1.cyberciti.biz
儲存並關閉檔案。
如何在系統啟動時啟動 uptimed 服務?
使用 systemctl
命令啟動 uptimed
服務:
$ sudosystemctl enable uptimed
我該如何 啟動/停止/重啟 或者檢視 uptimed 服務的狀態?
$ sudosystemctl start uptimed ## start it ##
$ sudosystemctl stop uptimed ## stop it ##
$ sudosystemctl restart uptimed ## restart it ##
$ sudosystemctl status uptimed ## view status ##
示例輸出:
● uptimed.service -uptime record daemon
Loaded: loaded (/lib/systemd/system/uptimed.service; enabled; vendor preset: enabled)
Active: active (running) since Thu2017-11-0917:49:14 UTC;18min ago
Main PID:11137(uptimed)
CGroup:/system.slice/uptimed.service
└─11137/usr/sbin/uptimed -f
Nov0917:49:14 gfs04 systemd[1]:Starteduptime record daemon.
如何檢視 uptime 記錄
只需鍵入以下命令即可檢視 uptimed(8)
程式的統計資訊:
$ uprecords
示例輸出:
Fig.02: uprecords in action
uprecords
有一些選項:
$ uprecords -?
示例輸出:
usage: uprecords [