This commit is contained in:
vvzvlad 2024-08-13 04:26:50 +03:00
parent 8ac3ac49a8
commit d4553107a2
2 changed files with 33 additions and 23 deletions

View File

@ -3,10 +3,7 @@ import time
import requests import requests
import socket import socket
import os import os
import sys
ups_ip = "10.31.41.46"
time_threshold = 4
interval = 20
def parse_time(time_str): def parse_time(time_str):
try: try:
@ -38,7 +35,9 @@ def run_command(command):
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
return result.stdout.strip() return result.stdout.strip()
while True: def main(ups_ip, time_threshold, interval):
print("Starting UPS monitoring...")
while True:
oid_on_battery_time = "1.3.6.1.4.1.318.1.1.1.2.2.3.0" oid_on_battery_time = "1.3.6.1.4.1.318.1.1.1.2.2.3.0"
result = run_command(f'/usr/bin/snmpget -v2c -O v -O q -c public {ups_ip} {oid_on_battery_time}') result = run_command(f'/usr/bin/snmpget -v2c -O v -O q -c public {ups_ip} {oid_on_battery_time}')
time_on_battery = parse_time(result) time_on_battery = parse_time(result)
@ -61,3 +60,14 @@ while True:
time.sleep(5) time.sleep(5)
else: else:
time.sleep(interval) time.sleep(interval)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: script.py <ups_ip> <time_threshold> <interval>")
sys.exit(1)
ups_ip = sys.argv[1]
time_threshold = int(sys.argv[2])
interval = int(sys.argv[3])
main(ups_ip, time_threshold, interval)

View File

@ -3,7 +3,7 @@ Description=Service to manage UPS shutdown script
After=network.target After=network.target
[Service] [Service]
ExecStart=/usr/bin/python3 /usr/local/bin/shutdown_ups.py ExecStart=/usr/bin/python3 /usr/local/bin/shutdown_ups.py 10.31.41.46 4 20
Restart=always Restart=always
RestartSec=5s RestartSec=5s