0% found this document useful (0 votes)
14 views2 pages

System Monitoring and Notification Script

The document is a Python script that monitors system status, including CPU usage, RAM usage, and CPU temperature. It captures screenshots and logs the system status, sending notifications via LINE when significant changes occur. The script runs in a continuous loop, checking the system status at defined intervals and logging any changes detected.

Uploaded by

6641470035
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

System Monitoring and Notification Script

The document is a Python script that monitors system status, including CPU usage, RAM usage, and CPU temperature. It captures screenshots and logs the system status, sending notifications via LINE when significant changes occur. The script runs in a continuous loop, checking the system status at defined intervals and logging any changes detected.

Uploaded by

6641470035
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import os

import time
import psutil
import pyautogui
import requests
from datetime import datetime
import wmi

# ===== CONFIG =====


LINE_CHANNEL_ACCESS_TOKEN =
'8/rEtXj31WqrUR3yvGyfcT6kz0t/8Uo7qGbTJNExFKGmphTliVDZsM4+Fd7fy+Hs3qX51eEIMdr4PpTMyT
tAV84vWp5DgF4I9q3LcSSYEAXFDY8rICK8QR37PS0RpjqwwWlFNaBVuJ5Xi0S3LhrxkAdB04t89/1O/
w1cDnyilFU='
USER_ID = 'U3981ff3e94ae2a30b81e0ff5b675e200'

SCREENSHOT_PATH = "[Link]"
LOG_FILE = "[Link]"
CHECK_INTERVAL = 10
CHANGE_THRESHOLD = 5

# ===== ฟังก์ชัน =====

def capture_screenshot(path=SCREENSHOT_PATH):
screenshot = [Link]()
[Link](path)
print(f"[✓] แคปหน้าจอ: {path}")

def get_cpu_temp():
try:
w = [Link](namespace="root\\wmi")
temperature_info = w.MSAcpi_ThermalZoneTemperature()[0]
temp_celsius = (temperature_info.CurrentTemperature / 10.0) - 273.15
return round(temp_celsius, 1)
except:
return None

def get_system_status():
return {
"cpu": psutil.cpu_percent(interval=1),
"ram": psutil.virtual_memory().percent,
"temp": get_cpu_temp()
}

def has_changed(current, previous, threshold=CHANGE_THRESHOLD):


if previous is None:
return True
for key in current:
if current[key] is None:
continue
if abs(current[key] - [Link](key, 0)) >= threshold:
return True
return False

def log_status(status, filename=LOG_FILE):


now = [Link]().strftime("%Y-%m-%d %H:%M:%S")
with open(filename, "a", encoding="utf-8") as f:
[Link](f"[{now}] CPU: {status['cpu']}% | RAM: {status['ram']}% | TEMP:
{status['temp']}°C\n")
print(f"[✓] บันทึก log แล้ว")
# อัปโหลดภาพไปยังโฮสต์ของคุณ (กรณีนี้ยังไม่มี URL ภาพ จึงส่งข้อความอย่างเดียวก่อน)
def send_line_message_with_text(text):
url = '[Link]
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {LINE_CHANNEL_ACCESS_TOKEN}"
}
data = {
"to": USER_ID,
"messages": [
{
"type": "text",
"text": text
}
]
}
response = [Link](url, headers=headers, json=data)
if response.status_code == 200:
print("[✓] ส่งข้อความผ่าน LINE แล้ว")
else:
print("❌ ส่งข้อความล้มเหลว:", [Link])

# ===== Main Loop =====


last_status = None

while True:
current_status = get_system_status()

if has_changed(current_status, last_status):
capture_screenshot()
log_status(current_status)

msg = (
f"📊 มีการเปลี่ยนแปลงระบบ:\n"
f" CPU: {current_status['cpu']}%\n"
f"📈 RAM: {current_status['ram']}%\n"
f" TEMP: {current_status['temp']} °C\n"
f"🕒 เวลา: {[Link]().strftime('%H:%M:%S')}"
)

send_line_message_with_text(msg)

# หากต้องการส่งภาพด้วย → ต้องอัปโหลดภาพ [Link] ไปยังเว็บ แล้วใช้


ลิงก์ภาพแบบนี้:
# {
# "type": "image",
# "originalContentUrl": "[Link]
# "previewImageUrl": "[Link]
# }

last_status = current_status

[Link](CHECK_INTERVAL)

You might also like