メモリ使用率をcsvとjsonに出力する

## この記事について

memory使用率をログに保存するshellscriptの自分用テンプレです。CPU使用率出力shellscriptとペアで作ったものです。

<img width= 700 src = https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/565396/b8c2f60a-4962-a779-18de-58602a9f4f26.png>


## csv出力

項目は任意で、その時に必要なものを出力します。手元に残るscriptをテンプレ代わりに置きます。

```sh:chk_mem_csv.sh
#!/bin/bash

# CSVファイル名を設定
datetime=$(date '+%Y%m%d-%H%M%S')
csv_file="mem_info_${datetime}.log"

# CSVファイルのヘッダーを設定
header="timestamp,main_display,Xwayland,pipewire-pulse,pipewire,wayfire,suricata,NM,wf-panel-pi"
echo $header > $csv_file

# 最新のログを取得しcsvに書き出す関数
get_mem_info() {
    timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    main_display_usage=$(top -bn1 | grep "python3" | sort -rk10  | head -n 1 | awk '{print $10}')
    Xwayland_usage=$(top -bn1 | grep "Xwayland" | sort -rk10  | head -n 1 | awk '{print $10}')
    pipewire_pulse_usage=$(top -bn1 | grep "pipewir+" | sort -rk10  | head -n 1 | awk '{print $10}')
    pipewire_usage=$(top -bn1 | grep "pipewire" | sort -rk10  | head -n 1 | awk '{print $10}')
    wayfire_usage=$(top -bn1 | grep "wayfire" | sort -rk10  | head -n 1 | awk '{print $10}')
    suricata_usage=$(top -bn1 | grep "Suricat+" | sort -rk10 | head -n 1 | awk '{print $10}')
    nm_usage=$(top -bn1 | grep "Network+" | sort -rk10  | head -n 1 | awk '{print $10}')
    wf_panel_pi_usage=$(top -bn1 | grep "wf-pane+" | sort -rk10  | head -n 1 | awk '{print $10}')

    echo "${timestamp},${main_display_usage},${Xwayland_usage},${pipewire_pulse_usage},${pipewire_usage},${wayfire_usage},${suricata_usage},${nm_usage},${wf_panel_pi_usage}"
}

while true; do
    payload=$(get_mem_info)
    echo $payload >> $csv_file

    sleep 10
done
```

こんな感じに出力されます。

<img width= 700 src = https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/565396/550c9d18-7a92-0218-c1df-df6f09782be9.png>


## json出力

jsonでも出力させます。

```sh:chk_mem_json.sh
#!/bin/bash

THING_NAME=$(uname -n)
# 最新のログを取得しPublishする関数
get_mem_info() {
    timestamp=$(date +%s)
    main_display_usage=$(top -bn1 | grep "python3" | sort -rk10  | head -n 1 | awk '{print $10}')
    Xwayland_usage=$(top -bn1 | grep "Xwayland" | sort -rk10  | head -n 1 | awk '{print $10}')
    pipewire_pulse_usage=$(top -bn1 | grep "pipewir+" | sort -rk10  | head -n 1 | awk '{print $10}')
    pipewire_usage=$(top -bn1 | grep "pipewire" | sort -rk10  | head -n 1 | awk '{print $10}')
    wayfire_usage=$(top -bn1 | grep "wayfire" | sort -rk10  | head -n 1 | awk '{print $10}')
    suricata_usage=$(top -bn1 | grep "Suricat+" | sort -rk10 | head -n 1 | awk '{print $10}')
    nm_usage=$(top -bn1 | grep "Network+" | sort -rk10  | head -n 1 | awk '{print $10}')
    wf_panel_pi_usage=$(top -bn1 | grep "wf-pane+" | sort -rk10  | head -n 1 | awk '{print $10}')

    echo "{\"Timestamp\":\"$timestamp\",\"main_display\":\"$main_display_usage\",\"Xwayland\":\"$Xwayland_usage\",\"pipewire_pulse\":\"$pipewire_pulse_usage\",\"pipewire\":\"$pipewire_usage\",\"wayfire\":\"$wayfire_usage\",\"suricata\":\"$suricata_usage\",\"nm\":\"$nm_usage\",\"wf_panel_pi\":\"$wf_panel_pi_usage\"}"
}


while true; do

    payload=$(get_cpu_info)
    echo $payload
    topic=${THING_NAME}_mos/${THING_NAME}_logs/mem-info
    echo $topic

    mosquitto_pub -h 127.0.0.1 -t "$topic" -m "$payload"

    sleep 10
done
```

DEMOとしてlocalhostでpubsubさせました。

```sh:pub
~ $ ./chk_mem_json.sh
{"Timestamp":"1720444462","main_display":"81.2","Xwayland":"18.8","pipewire_pulse":"0.0","pipewire":"0.0","wayfire":"6.2","suricata":"","nm":"0.0","wf_panel_pi":"0.0"}
VNCcamServer1_mos/VNCcamServer1_logs/mem-info
{"Timestamp":"1720444474","main_display":"76.5","Xwayland":"20.0","pipewire_pulse":"0.0","pipewire":"0.0","wayfire":"0.0","suricata":"","nm":"0.0","wf_panel_pi":"0.0"}
VNCcamServer1_mos/VNCcamServer1_logs/mem-info
{"Timestamp":"1720444485","main_display":"76.5","Xwayland":"20.0","pipewire_pulse":"0.0","pipewire":"0.0","wayfire":"6.2","suricata":"","nm":"0.0","wf_panel_pi":"0.0"}
VNCcamServer1_mos/VNCcamServer1_logs/mem-info
```

```sh:sub
~ $ mosquitto_sub -h 127.0.0.1 -t "VNCcamServer1_mos/VNCcamServer1_logs/mem-info" -v
VNCcamServer1_mos/VNCcamServer1_logs/mem-info {"Timestamp":"1720444462","main_display":"81.2","Xwayland":"18.8","pipewire_pulse":"0.0","pipewire":"0.0","wayfire":"6.2","suricata":"","nm":"0.0","wf_panel_pi":"0.0"}
VNCcamServer1_mos/VNCcamServer1_logs/mem-info {"Timestamp":"1720444474","main_display":"76.5","Xwayland":"20.0","pipewire_pulse":"0.0","pipewire":"0.0","wayfire":"0.0","suricata":"","nm":"0.0","wf_panel_pi":"0.0"}
VNCcamServer1_mos/VNCcamServer1_logs/mem-info {"Timestamp":"1720444485","main_display":"76.5","Xwayland":"20.0","pipewire_pulse":"0.0","pipewire":"0.0","wayfire":"6.2","suricata":"","nm":"0.0","wf_panel_pi":"0.0"}
```