cat /etc/os-release | grep ID_LIKE
ID_LIKE=debian
arch
x86_64
네트워크 → 터널 → 과정 따라 연결 완료. (관리자 페이지)
디스코드 봇(clover-alarm-bot)

pip3 install discord.py --break-system-packages
export DISCORD_BOT_TOKEN="발급받은_토큰"
export ALARM_CHANNEL_ID="채널_ID_숫자"
export ALARM_SAVE_DIR="$HOME/alarms"
import os
import discord
# ===== 설정 =====
TOKEN = os.environ.get("DISCORD_BOT_TOKEN") # 환경변수로 주입
ALARM_CHANNEL_ID = int(os.environ.get("ALARM_CHANNEL_ID", "0")) # #알림 채널 ID
SAVE_DIR = os.environ.get("ALARM_SAVE_DIR", os.path.expanduser("~/alarms"))
os.makedirs(SAVE_DIR, exist_ok=True)
intents = discord.Intents.default()
intents.message_content = True # 메시지 내용 읽기 (Developer Portal에서도 켜야 함)
client = discord.Client(intents=intents)
def next_alarm_path():
"""alarm1.txt, alarm2.txt ... 형태로 다음 파일 경로 반환"""
n = 1
while True:
path = os.path.join(SAVE_DIR, f"alarm{n}.txt")
if not os.path.exists(path):
return path
n += 1
@client.event
async def on_ready():
print(f"[INFO] 봇 로그인 완료: {client.user} (채널 ID: {ALARM_CHANNEL_ID})")
@client.event
async def on_message(message):
# 본인 메시지는 무시 (단, webhook/Alertmanager 메시지는 author.bot=True 라도 통과시켜야 함)
if message.author.id == client.user.id:
return
if message.channel.id != ALARM_CHANNEL_ID:
return
content_parts = []
if message.content:
content_parts.append(message.content)
# Alertmanager가 Embed 형태로 보내는 경우가 많아서 embed 내용도 같이 저장
for embed in message.embeds:
if embed.title:
content_parts.append(f"[제목] {embed.title}")
if embed.description:
content_parts.append(f"[설명] {embed.description}")
for field in embed.fields:
content_parts.append(f"[{field.name}] {field.value}")
full_text = "\n".join(content_parts).strip()
if not full_text:
full_text = "(빈 메시지 - 원본 확인 필요)"
# 'cpu' 또는 'CPU' 키워드 포함된 메시지만 처리 (대소문자 무관)
if "cpu" not in full_text.lower():
return
path = next_alarm_path()
with open(path, "w", encoding="utf-8") as f:
f.write(f"수신 시각: {message.created_at}\n")
f.write(f"작성자: {message.author} (webhook 여부: {message.webhook_id is not None})\n")
f.write("----\n")
f.write(full_text + "\n")
print(f"[저장됨] {path}")
if __name__ == "__main__":
if not TOKEN:
raise SystemExit("DISCORD_BOT_TOKEN 환경변수가 없습니다.")
if ALARM_CHANNEL_ID == 0:
raise SystemExit("ALARM_CHANNEL_ID 환경변수가 없습니다.")
client.run(TOKEN)


CPU 또는 cpu라는 키워드를 검색하여, alarm1 ~ alarm.txt 가 저장되게끔 했음.
다음엔 웹훅으로 앤서블 자동화 코드로 연결할 예정.