Last active
April 30, 2025 10:03
-
-
Save unchama/d192a58c052b3fdb62ce68c825d19d3a to your computer and use it in GitHub Desktop.
USB接続のみ対応の警子ちゃん DN-1500UX をZabbixイベントに応じて光らせるためにChatGPTに書かせたスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# /usr/lib/zabbix/alertscripts/control_dn1500ux_if_higher.sh | |
# DN-1500UX を現在状態より高いSeverity時だけ光らせるスクリプト | |
DEV="/dev/ttyACM0" # 環境によって違う気がするので何卒 | |
SEN="$1" # {ALERT.SENDTO} -> Zabbix の Media type の Script Parameters に指定すること | |
SEV="$2" # {EVENT.SEVERITY} -> Zabbix の Media type の Script Parameters に指定すること | |
RES="$3" # なんか制御用 | |
# ───────────────────────────────────────────── | |
# 1️⃣ シリアルポート初期化 | |
stty -F "$DEV" 9600 cs8 -cstopb -parenb raw -echo | |
# もしRESがRESETなら止めて終了 | |
if [ "$RES" == "RESET" ]; then | |
printf '%s\r' "ACOP 0000" > "$DEV" | |
echo "駆動停止しました" | |
exit 0 | |
fi | |
# 2️⃣ 現在のACOP状態を取得 | |
exec 3<> "$DEV" | |
printf 'ACOP\r' >&3 | |
IFS= read -r -t 2 resp <&3 | |
exec 3>&-; exec 3<&- | |
# 受信例 "ACOP 1003" → "1003" 抜き出し | |
resp="${resp//$'\r'/}" # 改行・CRを一掃 | |
current_code="${resp##* }" | |
echo "現在のACOP($current_code)" | |
# 3️⃣ 現在状態をランクにマッピング | |
# 1=Info, 2=Warning, 3=Average, 4=High, 5=Disaster | |
case "$current_code" in | |
"0010") curr_rank=1 ;; # Info(緑点灯) | |
"0100") curr_rank=2 ;; # Warning(黄点灯) | |
"0300") curr_rank=3 ;; # Average(黄点滅) | |
"1002") curr_rank=4 ;; # High(赤点滅+ブザー) | |
"3001") curr_rank=5 ;; # Disaster(赤高速点滅+ブザー) | |
*) curr_rank=0 ;; # 未知なら最弱扱い | |
esac | |
# 4️⃣ 新Severityをランク&コマンドにマッピング | |
case "$SEV" in | |
"Disaster") new_rank=5; new_CMD="ACOP 3001" ;; | |
"High") new_rank=4; new_CMD="ACOP 1002" ;; | |
"Average") new_rank=3; new_CMD="ACOP 0300" ;; | |
"Warning") new_rank=2; new_CMD="ACOP 0100" ;; | |
*) new_rank=1; new_CMD="ACOP 0010" ;; # Information | |
esac | |
# 5️⃣ 比較&送信 | |
if [ "$new_rank" -gt "$curr_rank" ]; then | |
printf '%s\r' "$new_CMD" > "$DEV" | |
echo "ℹ️ 現在ランク(${curr_rank}) < 新ランク(${new_rank}):新しいライト発動: ${new_CMD}" | |
else | |
echo "ℹ️ 現在ランク(${curr_rank}) ≥ 新ランク(${new_rank}):変更なし" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment