Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

Python script for detecting if servers go down

So here is my code:

from os import system
from datetime import datetime
import time
import os
import subprocess
import sys 

def status(ip_addr):
    return os.system('ping ' + ip_addr + '> nul') == 0

 statut[]

print("##################################")
print("Current time: ", str(datetime.now()))
print(" ")
with open('data.txt', 'r+') as adds:
  add = [addrs.strip() for addrs in adds.readlines()]
for website in add:
    stat = status(website)
    if stat == 1:
        stats = " is up!"
        statut[website] = 1
    else:
        stats = " is down!"
        statut[website] = 0
    print(website, stats)
print("##################################")

while True:
    print("Current time: ", str(datetime.now()))
    print(" ")
    with open('data.txt', 'r+') as adds:
      add = [addrs.strip() for addrs in adds.readlines()]
    for website in add:
        stat = status(website)
        if stat != statut[website]:
            stats = " is up!"
            statut[website] = stat
        print(website, stats)
    print("##################################")
time.sleep(240)

What I want to make out of it is to firstly learn if a server is up/down and after that, check at every 240 sec if it went the other way around - I can't however use boolean array "statut" like I intended. I would really apreciate some help with how I could make it work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are just looking for a state change you could do something like this:

from os import system
from datetime import datetime
import time


def server_status(ip_addr):
    if system('ping ' + ip_addr + '> nul') == 0:
        return 'up'
    else:
        return 'down'


status_history = {}

print("##################################")
print("Current time: ", str(datetime.now()))
print(" ")
with open('data.txt', 'r+') as adds:
    ipaddress = [addrs.strip() for addrs in adds.readlines()]

# Set inital state
for server in ipaddress:
    status_history[server] = server_status(server)
    print(f"{server} is {status_history[server]}")
print("##################################")

while True:
    print("Current time: ", str(datetime.now()))
    print(" ")

    for server in ipaddress:
        if server_status(server) != status_history[server]:
            status_history[server] = server_status(server)
            print(f"{server} has switched state to {status_history[server]}")
        else:
            print(f"{server} is still {status_history[server]}")
    print("##################################")
    time.sleep(10)

I would put a timeout on the while loop just in case and possible make some parts more configurable, such as the sleep.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...