目次
サーバの稼働日時取得 linux command
今回は、linux コマンドで、サーバの稼働日時を取得する方法 をご紹介したいと思います。
検証環境
今回は以下の二つの環境で動作確認を行っています。
# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) # cat /etc/os-release NAME="Ubuntu" VERSION="16.04.2 LTS (Xenial Xerus)"
取得コマンド
サーバの稼働日時を取得
出力結果より、6月11日(日) 21:48 がサーバの稼働日時であることがわかる。
$ last -1 reboot | grep reboot | awk '{print $5, $6, $7, $8}' Sun Jun 11 21:48
サーバの稼働時間を取得
出力結果より、63日と20時間37分 サーバの稼働していることがわかる。
# uptime | awk '{print $2,$3,$4,$5}' up 63 days, 20:37,
取得コマンド詳細
last コマンド
- 「last」 コマンドのみだと、最近ログインしたユーザが全て表示される
- 「last -1 reboot」で、直近のrebootエントリが出力される
- 「last -1 reboot | grep reboot」で、必要な行を抜き出す
- 「awk ‘{print $5, $6, $7, $8}’」で、必要な項目を選択して抜き出している
(1) # last stepuser pts/0 w0109-49-135-81- Mon Aug 14 18:14 still logged in ... ... ... root pts/0 w0109-49-135-81- Thu May 4 21:57 - 22:20 (00:23) reboot system boot 3.10.0-327.36.3. Thu May 4 21:40 - 23:24 (8+01:43) wtmp begins Thu May 4 21:40:54 2017 (2) # last -1 reboot reboot system boot 3.10.0-514.16.1. Sun Jun 11 21:48 - 18:36 (63+20:47) wtmp begins Thu May 4 21:40:54 2017 (3) # last -1 reboot | grep reboot reboot system boot 3.10.0-514.16.1. Sun Jun 11 21:48 - 18:44 (63+20:56) (4) # last -1 reboot | grep reboot | awk '{print $5, $6, $7, $8}' Sun Jun 11 21:48
複数サーバの稼働日時を取得するコマンド
以下のような場合を想定
- ホスト名 = 「sample_host**」
- ホスト名の末尾「**」に「01~20」までの数字が入る
※1,2,3 ~ と二桁に満たない場合は、0で埋められる
なお、[username]には任意のユーザ名を入力すること
for i in `seq 1 20` do val=$(printf %02d $i) hostname=sample_host${val} echo "------ ${hostname} ------" ssh [username]@${hostname} last -1 reboot | grep reboot | awk '{print $5, $6, $7, $8}' done
以下が出力結果の例
複数サーバに対して、稼働開始日が取得できていることがわかる
------ sample_host01 ------ Thu Aug 10 11:24 ------ sample_host02 ------ Thu Aug 10 11:24 ------ sample_host03 ------ Thu Aug 10 11:24 ... ... ... ... ------ sample_host18 ------ Thu Aug 10 11:24 ------ sample_host19 ------ Thu Aug 10 11:24 ------ sample_host20 ------ Thu Aug 10 11:24
ちなみに、0埋めで使用した「printf」コマンドは「printf %03d」や「printf %04d」とすることで「001」や「0001」と桁を調整することができる。
uptime コマンド
- 現在時刻 = 17:34:52
- 稼働時間 =up 63 days, 19:46
- 現在のログインユーザ数 = 1 user
- ロードアベレージ = load average: 0.08, 0.06, 0.05
# uptime 17:34:52 up 63 days, 19:46, 1 user, load average: 0.08, 0.06, 0.05
お疲れさまでした。
いかがでしょうか。サーバの稼働日時を調べることができたでしょうか。
個人での学習ではあまり役に立たないかもしれないですが、業務では知っておくと何かと役に立つと思いますで参考としてください。
コメント