Network Monitor – HTML Dashboard with Latency Testing (Level 3)

This project has you create a script that continuously pings IP Addresses or FQDN’s from a list and the dynamically writes an HTML/CSS dashboard. The python script continuously loops, and the HTML document that is created is set to refresh every 4 seconds using the <meta> tag.

In this lab we format the HTML to look good both from the browser and from the source code by using \n and \t text formatting.

We parse the response from the Ping command to determine whether the ping was successful or not. For successful pings we parse out the RTT (Round Trip Time) for the ping to determine that quality of the connection with there server.

Run the Python script from Terminal or VSCode and then go to your home directory and open network.html in your web browser. To make the HTML page accessible from other computers you could install Apache on the system running the script and then simply have the script save network.html into /var/www/html. (You could also rename network.html to index.html for it to be the default landing page on the server)

ping-html.py

import os
from time import sleep

host = ["192.168.1.20", "salesforce.com", "google.com", "cisco.com"]
pause = 10

success = "1 received"
fail = "0 received"

header = f"<meta http-equiv='refresh' content='4'>\n <title>Up/ Down App</title>\n <h1>Network Monitor</h1>"
while True:
  body = "<table>"
  for x in host:
    body = f"{body}\n \t<tr>\n"
    command = f"ping -c 1 {x}"

    response = os.popen(command)
    response = response.read()

    if success in response:
      print(f"{x} is UP")
      body = f"{body}\t\t <td style='background-color:lightgreen;'>{x} is UP</td>"

      time = response.split(" ")
      for x in time:
        if "time=" in x:
          time = x
          time = time.split("=")
          time = time[1]
      print(time)

      time = float(time)
      if time <= 30:
        body =f"{body}\n\t\t <td style='background-color:lightgreen;'>{time}</td>\n "
      elif time > 30 and time <= 100:
        body =f"{body}\n\t\t <td style='background-color:yellow;'>{time}</td>\n "
      elif time > 100 and time <= 200:
        body =f"{body}\n\t\t <td style='background-color:orange;'>{time}</td>\n "
      elif time > 200 and time <= 500:
        body =f"{body}\n\t\t <td style='background-color:lightsalmon;'>{time}</td>\n "
      else:
        body =f"{body}\n\t\t <td style='background-color:purple;'>{time}</td> "

    elif fail in response:
      print(f"{x} is DOWN")
      body = f"{body}\t\t <td style='background-color:lightsalmon;'>{x} is DOWN</td>"
    else:
      print(f"Other Issue Communicating with {x}")
      body = f"{body}\t\t <td style='background-color:purple;'>{x} HAS AN ISSUE</td>"

    body = f"{body} \t </tr>"

  body = f"{body}\n </table>"
  html = f"{header}\n {body}"
  file = open("network.html", "w")
  file.write(html)
  file.close()

  print("*******")
  sleep(pause)  
Code language: Python (python)