Añadido CheckSAI.sh

This commit is contained in:
2025-04-27 23:26:12 +02:00
parent d423f5dc53
commit b6e7b789b1
+140
View File
@@ -0,0 +1,140 @@
import subprocess
import platform
import sys
from colorama import init, Fore, Style
# Inicialización de colorama para el uso de colores en la salida
init(autoreset=True)
def run_upsc_command(ups_name):
try:
result = subprocess.run(["upsc", f"{ups_name}@localhost:4500"], capture_output=True, text=True, check=True)
return result.stdout
except subprocess.CalledProcessError as e:
print(f"{Fore.RED}Error running upsc command: {e}")
return None
except Exception as e:
print(f"{Fore.RED}Unexpected error: {e}")
return None
def parse_upsc_output(output):
parsed_output = {}
lines = output.split("\n")
for line in lines:
if ":" in line:
try:
key, value = line.split(": ", 1)
parsed_output[key] = value
except ValueError:
print(f"{Fore.RED}Error parsing line: {line}")
return parsed_output
def beautify_upsc_output(parsed_output):
print(f"{Fore.BLUE}UPS Information")
print("-" * 70)
units = {
"battery.charge": "%",
"battery.voltage": "V",
"output.frequency": "Hz",
"input.voltage": "V",
"output.voltage": "V",
"ups.load": "%",
}
for key, value in parsed_output.items():
pretty_key = key.replace('.', ' ').title()
pretty_value = f"{Fore.GREEN}{value} {units.get(key, '')}{Style.RESET_ALL}"
print(f"{Fore.YELLOW}{pretty_key:<40}{Fore.WHITE}: {pretty_value}")
print("-" * 70)
def check_values(parsed_output, normal_ranges):
print(f"\n{Fore.BLUE}Value Checks")
print("-" * 50)
alerts = []
for key, (min_value, max_value) in normal_ranges.items():
try:
value = float(parsed_output.get(key, 0))
except ValueError:
print(f"{Fore.RED}⚠️ Error: {key.replace('.', ' ').title()} has a non-numeric value.")
continue
if min_value <= value <= max_value:
print(f"{Fore.GREEN}{key.replace('.', ' ').title()} is normal.")
else:
alerts.append(f"{key.replace('.', ' ').title()} is {value}, should be {min_value}-{max_value}")
print(f"{Fore.RED}⚠️ {key.replace('.', ' ').title()} is outside normal range ({min_value}-{max_value}).")
print("-" * 50)
if alerts:
print(f"{Fore.RED}Alerts: {', '.join(alerts)}")
def system_info():
print(f"\n{Fore.BLUE}System Information")
print("-" * 70)
system_info = {
"Platform": platform.platform(),
"Hostname": platform.node(),
"Processor": platform.processor(),
}
for key, value in system_info.items():
print(f"{Fore.YELLOW}{key:<40}{Fore.WHITE}: {Fore.GREEN}{value}{Style.RESET_ALL}")
print("-" * 70)
def show_help():
print(f"""{Fore.BLUE}{Style.BRIGHT}UPS Monitor Script Help{Style.RESET_ALL}
{Fore.GREEN}This script retrieves and displays information about an Uninterruptible Power Supply (UPS) and the system it's running on.{Style.RESET_ALL}
{Fore.BLUE}{Style.BRIGHT}UPS Information:{Style.RESET_ALL}
{Fore.YELLOW}{Style.DIM}- battery.charge{Style.RESET_ALL}: Battery charge level in percentage
{Fore.YELLOW}{Style.DIM}- battery.voltage{Style.RESET_ALL}: Battery voltage level in volts
{Fore.YELLOW}{Style.DIM}- output.frequency{Style.RESET_ALL}: Input frequency in Hertz
{Fore.YELLOW}{Style.DIM}- input.voltage{Style.RESET_ALL}: Input voltage in volts
{Fore.YELLOW}{Style.DIM}- output.voltage{Style.RESET_ALL}: Output voltage in volts
{Fore.YELLOW}{Style.DIM}- ups.load{Style.RESET_ALL}: Load level in percentage
{Fore.BLUE}{Style.BRIGHT}System Information:{Style.RESET_ALL}
{Fore.YELLOW}{Style.DIM}- Platform{Style.RESET_ALL}: Operating system platform
{Fore.YELLOW}{Style.DIM}- Hostname{Style.RESET_ALL}: Hostname of the system
{Fore.YELLOW}{Style.DIM}- Processor{Style.RESET_ALL}: Processor information""")
def main():
if "-h" in sys.argv or "--help" in sys.argv:
show_help()
return
ups_name = "nutdev1"
upsc_output = run_upsc_command(ups_name)
if upsc_output:
parsed_output = parse_upsc_output(upsc_output)
normal_ranges = {
"battery.charge": (50, 100),
"battery.voltage": (20, 30),
"output.frequency": (48, 52),
"input.voltage": (220, 240),
"output.voltage": (220, 240),
"ups.load": (0, 80),
}
beautify_upsc_output(parsed_output)
check_values(parsed_output, normal_ranges)
system_info()
else:
print(f"{Fore.RED}Couldn't retrieve UPS information.")
if __name__ == "__main__":
main()