CPU使用率をcsvとjsonに出力する

## この記事について

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_cpu_csv.sh
#!/bin/bash

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

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

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

    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_cpu_info)
    echo $payload >> $csv_file

    sleep 10
done
```

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

<img width= 700 src = https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/565396/4eee66a9-8488-43c7-ca46-99d06db30904.png>


## json出力

同じ内容をjsonで出力させます。なぜjsonかと言えばこれですよね。

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

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

    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/cpu-info
    echo $topic

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

    sleep 10
done
```

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

```sh:pub
~ $ ./chk_cpu_json.sh
{"Timestamp":"1720440267","main_display":"68.8","Xwayland":"12.5","pipewire_pulse":"0.0","pipewire":"0.0","wayfire":"6.2","suricata":"","nm":"0.0","wf_panel_pi":"0.0"}
VNCcamServer1_mos/VNCcamServer1_logs/cpu-info
{"Timestamp":"1720440279","main_display":"64.7","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/cpu-info
{"Timestamp":"1720440290","main_display":"64.7","Xwayland":"12.5","pipewire_pulse":"0.0","pipewire":"0.0","wayfire":"6.2","suricata":"","nm":"0.0","wf_panel_pi":"0.0"}
VNCcamServer1_mos/VNCcamServer1_logs/cpu-info
```

```sh:sub
~ $ mosquitto_sub -h 127.0.0.1 -t "VNCcamServer1_mos/VNCcamServer1_logs/cpu-info" -v
VNCcamServer1_mos/VNCcamServer1_logs/cpu-info {"Timestamp":"1720440267","main_display":"68.8","Xwayland":"12.5","pipewire_pulse":"0.0","pipewire":"0.0","wayfire":"6.2","suricata":"","nm":"0.0","wf_panel_pi":"0.0"}
VNCcamServer1_mos/VNCcamServer1_logs/cpu-info {"Timestamp":"1720440279","main_display":"64.7","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/cpu-info {"Timestamp":"1720440290","main_display":"64.7","Xwayland":"12.5","pipewire_pulse":"0.0","pipewire":"0.0","wayfire":"6.2","suricata":"","nm":"0.0","wf_panel_pi":"0.0"}
```