big changes

This commit is contained in:
crate 2024-10-09 22:00:42 +00:00
parent 75711e8700
commit f4927598b3
2 changed files with 78 additions and 18 deletions

View File

@ -2,7 +2,10 @@ import time
import config
import logging
import smtplib
import asyncio
import discord
from nut2 import PyNUTClient
from proxmoxer import ProxmoxAPI
from email.mime.text import MIMEText
from logging.handlers import TimedRotatingFileHandler
@ -11,6 +14,14 @@ gmail_user = config.GMAIL_USER
gmail_password = config.GMAIL_PASS
recipient_email = config.GMAIL_ADDR
bot_token = config.BOT_TOKEN
user_id = config.USER_ID
pve_host = config.PVE_HOST
pve_user = config.PVE_USER
pve_pass = config.PVE_PASS
pve_nodes = ['pve', 'c530']
def setup_logging():
logger = logging.getLogger('powerlog')
logger.setLevel(logging.INFO)
@ -31,6 +42,15 @@ def setup_logging():
logger = setup_logging()
proxmox = ProxmoxAPI(pve_host, user=pve_user, password=pve_pass, verify_ssl=False)
def shutdown_pve(node):
try:
proxmox.nodes(node).status.post(command='shutdown')
logger.warning("Node {node} is shutting down.")
except Exception as e:
logger.error(e)
def monitor_ups():
client = PyNUTClient()
try:
@ -57,6 +77,44 @@ def send_email(subject, body):
except Exception as e:
logger.info(f"Failed to send email: {e}")
client = discord.Client()
@client.event
async def on_ready():
logger.info(f"Client connected as {client.user}")
async def send_discord(message)
try:
user = await client.fetch_user(user_id)
await user.send(message)
logger.info(f"Discord message sent: {message}")
except Exception as e:
logger.error(e)
def pwr_offline(battery):
message = f"{ups_id} UPS is running on battery power! {battery}% charge remaining."
logger.warning(message)
send_email(f"{ups_id}: Power Outage Detected!", message)
asyncio.run(send_discord(message))
def pwr_online(battery):
message = f"{ups_id} UPS power has been restored. {battery}% charge remaining."
logger.warning(message)
send_email(f"{ups_id}: Power On Line", message)
asyncio.run(send_discord(message))
def batt_low(battery):
message = f"{ups_id} battery level low: {battery}% charge remaining."
logger.warning(message)
send_email(f"{ups_id} battery low.", message)
asyncio.run(send_discord(message))
def batt_crit(battery):
message = f"{ups_id} battery level critial: {battery}% charge remaining, Shutting down PVE."
logger.warning(message)
send_email(f"{ups_id} battery critical!!", message)
asyncio.run(send_discord(message))
def main():
logger.info("Starting UPS monitoring service.")
prev_status = None
@ -67,30 +125,32 @@ def main():
battery = ups_data['battery_charge']
if status and status != prev_status:
if status == "OB DISCHRG":
logger.warning(f"Outage detected! Power On Battery. {battery}% charge remaining.")
send_email(
f"{ups_id}: Power Outage Detected",
f"The {ups_id} UPS is running on battery power! {battery}% charge remaining.")
pwr_offline(battery)
elif status == "OL":
logger.info(f"Power On Line. {battery}% charge remaining.")
send_email(
f"{ups_id}: Power On Line",
f"{ups_id} UPS power has been restored! {battery}% charge remaining.")
pwr_online(battery)
else:
logger.info(f"UPS status changed to: {status}")
prev_status = status
elif status == "OB DISCHRG" and battery < 50:
logger.warning(f"Battery level low: {battery}% charge remaining.")
send_email(
f"{ups_id} battery low.",
f"{ups_id} battery level low: {battery}% charge remaining.")
elif status == "OB DISCHRG" and battery < 35:
logger.warning(f"Battery level critical: {battery}% charge remaining.")
send_email(
f"{ups_id} battery critical!!",
f"{ups_id} battery level critial: {battery}% charge remaining.")
time.sleep(30)
if battery < 25:
batt_crit(battery)
for node in pve_nodes:
shutdown_pve(node)
time.sleep(300)
else:
batt_low(battery)
time.sleep(60)
time.sleep(5)
async def client_connect():
while True:
try:
await client.start(bot_token)
except Exception as e:
e = str(e)
logger.error(f"Client couldn't connect! {e}")
await asyncio.sleep(300)
if __name__ == "__main__":
asyncio.run(client_connect())
main()

0
power_2.py Normal file
View File