Linuxのメモリやロードアベレージを1コマンドで取得したい。
| 登録日 | :2024/10/10 17:00 |
|---|---|
| カテゴリ | :Linux |
リモートシェルでノードのステータスを取得するときに、何度もSSHコネクションを発行したくない。そのため、一つのコマンドで情報を取得するにはどうしたらいいのかサンプルを作成してみた。
echo "Load Average: $(uptime | awk '{print $10 $11 $12}') | Total Memory: $(free -m | awk '/Mem:/ {print $2}')MB | Used Memory: $(free -m | awk '/Mem:/ {print $3}')MB"
COMMAND="uptime | awk '{print \$10 \$11 \$12}'; free -m | awk '/Mem:/ {print \$2}'; free -m | awk '/Mem:/ {print \$3}'"
data=$(uptime | awk '{print $10 $11 $12}'; free -m | awk '/Mem:/ {print $2}'; free -m | awk '/Mem:/ {print $3}')
echo $data
load=$(uptime | awk '{print $10 $11 $12}')
total=$(free -m | awk '/Mem:/ {print $2}')
used=$(free -m | awk '/Mem:/ {print $3}')
free=$(free -m | awk '/Mem:/ {print $4}')
buff=$(free -m | awk '/Mem:/ {print $6}')
avail=$(free -m | awk '/Mem:/ {print $7}')
echo $load','$total','$used','$free','$buff','$avail
slurmのコマンドを使うと以下のような方法もある
[root@headnode bash]# sinfo -o "%n %C %m %e %O %T"
HOSTNAMES CPUS(A/I/O/T) MEMORY FREE_MEM CPU_LOAD STATE
headnode 0/1/0/1 770 71 0.00 idle
compute01 0/0/1/1 770 79 0.00 down
node1 0/0/1/1 770 N/A N/A drained*
node2 0/0/1/1 770 N/A N/A drained*
[root@headnode bash]#
このコマンドの説明:
%n: ノード名
%C: CPU情報 (割り当て済み/アイドル/その他/合計)
%m: トータルメモリ (MB単位)
%e: 空きメモリ (MB単位)
%O: CPUロード
%T: ノードの状態
Usedメモリを表示するには、total-freeをする必要がある。以下はGBにも変換を加える。
[root@headnode bash]# sinfo -o "%n %C %m %.2f %.4O %T" | awk '{used=$4-$5; printf "%s %s %.1fGB %.1fGB %.2f %s\n", $1, $2, $4/1024, used/1024, $6, $7}'
HOSTNAMES CPUS(A/I/O/T) 0.0GB 0.0GB 0.00
headnode 0/1/0/1 0.0GB 0.0GB 0.00
compute01 0/0/1/1 0.0GB 0.0GB 0.00
node1 0/0/1/1 0.0GB 0.0GB 0.00
node2 0/0/1/1 0.0GB 0.0GB 0.00
[root@headnode bash]#
[root@headnode slurmhost]# sinfo -o "%n %m %.2f %.4O %T" | awk '{used=$3-$4; printf "%s %.1fGB %.1fGB %.2f %s\n", $1, $3/1024, used/1024, $5, $6}'
HOSTNAMES 0.0GB 0.0GB 0.00
headnode 0.0GB 0.0GB 0.00
compute01 0.0GB 0.0GB 0.00
node1 0.0GB 0.0GB 0.00
node2 0.0GB 0.0GB 0.00
[root@headnode slurmhost]#
見直し。
[root@headnode slurmhost]# sinfo -o "%n %m %e %O %t" | awk '{used=$2-$3; printf "%s %.1f %.1f %.1f %.2f %s\n", $1, $2, $3, used, $4, $5}'
HOSTNAMES 0.0 0.0 0.0 0.00 STATE
headnode 770.0 86.0 684.0 0.00 idle
compute01 770.0 91.0 679.0 0.00 idle
node1 770.0 0.0 770.0 0.00 drain*
node2 770.0 0.0 770.0 0.00 drain*
さらに見直し。
[root@headnode slurmhost]# sinfo -o "%n %m %e %O %t" | awk '{used=$2-$3; printf "%s %.2fGB %.2fGB %.2fGB %.2f %s\n", $1, $2/1024, $3/1024, used/1024, $4, $5}'
HOSTNAMES 0.00GB 0.00GB 0.00GB 0.00 STATE
headnode 0.75GB 0.08GB 0.67GB 0.00 idle
compute01 0.75GB 0.09GB 0.66GB 0.00 idle
node1 0.75GB 0.00GB 0.75GB 0.00 drain*
node2 0.75GB 0.00GB 0.75GB 0.00 drain*
[root@headnode slurmhost]#
[root@headnode slurmhost]#
awkの使い方メモ
デフォルトでは空白で区切られる
[root@headnode slurmhost]# sinfo -o "%n %m %e %O %t" | awk '{print $0}'
HOSTNAMES MEMORY FREE_MEM CPU_LOAD STATE
headnode 770 86 0.00 idle
compute01 770 95 0.00 idle
node1 770 N/A N/A drain*
node2 770 N/A N/A drain*
[root@headnode slurmhost]# sinfo -o "%n %m %e %O %t" | awk '{print $1}'
HOSTNAMES
headnode
compute01
node1
node2
[root@headnode slurmhost]# sinfo -o "%n %m %e %O %t" | awk '{print $1 $2}'
HOSTNAMESMEMORY
headnode770
compute01770
node1770
node2770
区切り文字を変更したいときは-Fオプションをつける
[root@headnode slurmhost]# sinfo -o "%n %m %e %O %t" | awk -F: '{print $1,$2}'
HOSTNAMES MEMORY FREE_MEM CPU_LOAD STATE
headnode 770 86 0.00 idle
compute01 770 91 0.00 idle
node1 770 N/A N/A drain*
node2 770 N/A N/A drain*
[root@headnode slurmhost]#
[root@headnode slurmhost]# sinfo -o "%n %m %e %O %t" | awk -F770 '{print $1,$2}'
HOSTNAMES MEMORY FREE_MEM CPU_LOAD STATE
headnode 86 0.00 idle
compute01 91 0.00 idle
node1 N/A N/A drain*
node2 N/A N/A drain*
[root@headnode slurmhost]#