LoginSignup
2
1

パルワールド(Palworld)の専用サーバ運用Tips

Last updated at Posted at 2024-01-28

色々横着したい。

サーバスペック

メーカー TRIGKEY
品番 ‎TRIGKEY PC
CPU Ryzen 5 5560U
メモリ テクミヨ ノートPC用メモリ DDR4-3200(PC4-25600) 32GB
ストレージ 謎メーカーのNVME500GB
OS WindowsServer2022 Standard

やってみたかったこと

  1. Windowsのメモリ使用率を監視
  2. 80%を超えたらdiscord Webhook を使ってDiscordに通知
  3. 90%を超えたら再度通知して、RCONを起動しPalServerをshutdown
  4. PalServer-Win64-Test-Cmd.exeのプロセスが消えたらWindows再起動

やること

  1. Pythonをインストール
  2. ARRCONをインストール https://github.com/radj307/ARRCON
  3. Pythonコードを書く
  4. バッチファイル用意する
  5. Discordでウェブフックを作る

image.png

動いたコード

ファイル名:MemoryOver_Discord_push_and_WindowsReboot.py

import psutil
import requests
import subprocess
import time
import os

# Discord Webhook URL
DISCORD_WEBHOOK_URL = 'https://discord.com/api/webhooks/ほげほげ'

# メモリ使用率の閾値
MEMORY_NOTIFY_THRESHOLD_80 = 80
MEMORY_NOTIFY_THRESHOLD_90 = 90

# RCON.batのパス
RCON_BATCH_PATH = r'C:\Script\RCON.bat'

# Discordに通知したかどうかのフラグ
discord_notification_sent_80 = False
discord_notification_sent_90 = False

# RCON.batを実行したかどうかのフラグ
rcon_executed = False

# Discordに通知する関数
def send_discord_notification(content):
    payload = {'content': content}
    headers = {'Content-Type': 'application/json'}
    requests.post(DISCORD_WEBHOOK_URL, json=payload, headers=headers)

# RCON.batを実行する関数
def execute_rcon_batch():
    global rcon_executed
    if not rcon_executed:
        subprocess.run([RCON_BATCH_PATH], shell=True)
        rcon_executed = True

# Windowsを再起動する関数
def restart_windows():
    os.system('shutdown /r /t 0')

while True:
    # 現在のメモリ使用率を取得
    memory_usage = psutil.virtual_memory().percent

    # メモリ使用率が80%を超えた場合に通知
    if memory_usage >= MEMORY_NOTIFY_THRESHOLD_80 and not discord_notification_sent_80:
        notification_message = f'メモリ使用量が{MEMORY_NOTIFY_THRESHOLD_80}%を超えたよ。現在は{memory_usage}%です。'
        send_discord_notification(notification_message)
        discord_notification_sent_80 = True  # 通知済みフラグをセット

    # メモリ使用率が90%を超えた場合に通知とRCON.bat実行
    if memory_usage >= MEMORY_NOTIFY_THRESHOLD_90:
        if not discord_notification_sent_90:
            notification_message = f'メモリ使用量が{MEMORY_NOTIFY_THRESHOLD_90}%を超えたよ。5分以内にログアウトしてね。'
            send_discord_notification(notification_message)
            discord_notification_sent_90 = True  # 通知済みフラグをセット

        # RCON.batを実行
        execute_rcon_batch()

        # PalServer-Win64-Test-Cmd.exeのプロセスが終了するまで待機
        while 'PalServer-Win64-Test-Cmd.exe' in (p.name() for p in psutil.process_iter()):
            time.sleep(30)

        # PalServer-Win64-Test-Cmd.exeのプロセスが終了したらWindowsを再起動
        restart_windows()
        break  # プログラムを終了

RCON.batの中身

@echo off
cd /d C:\app\ARRCON-3.3.7-Windows
echo Shutdown 300 | ARRCON -H xxx.xxx.xxx.xxx -P 25575 -p {AdminPassword}

タスクスケジューラーに設定

全般タブ

  • ユーザーがログインしているかどうかにかかわらず実行する
  • 最上位の特権で実行する

トリガータブ

+システム起動時

操作タブ

操作 プログラムの開始
プログラム/スクリプト python.exeの絶対パス
--- ---
引数の追加 MemoryOver_Discord_push_and_WindowsReboot.py
--- ---
開始(オプション) MemoryOver_Discord_push_and_WindowsReboot.pyのパス

以上

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1