IS IT US OR THEM?  -  step-by-step guide
MaTru On-Call, Episode 3 (tutorial)
==========================================================

What this is
------------
us_or_them.py is a ~70-line Python script that answers the 3 a.m. question:
is the outage OURS, or is the whole internet having a bad night?
It checks your own sites plus 15 big "neighbor" sites at the same time,
prints UP/DOWN in color with the milliseconds, and gives you a verdict.
It can also run as a small web dashboard that refreshes every 10 seconds.

It uses only Python's standard library. Nothing to install with pip.


1. Install Python (only if you don't have it)
---------------------------------------------
You need Python 3.8 or newer. Check with:

    python3 --version

  - macOS:   download it from https://www.python.org/downloads/
             (or: brew install python)
  - Windows: download it from https://www.python.org/downloads/
             and tick "Add python.exe to PATH" in the installer.
             On Windows, type "python" instead of "python3" below.
  - Linux:   it's usually already there (sudo apt install python3).


2. Unzip and open a terminal in the folder
------------------------------------------
    cd us-or-them


3. Put YOUR sites in the list
-----------------------------
Open us_or_them.py in any editor. Near the top:

    OURS = ["https://matrutech.com"]

Replace it with the sites you are responsible for, for example:

    OURS = ["https://example.com", "https://api.example.com/health"]

INTERNET is the list of "neighbors": big sites that are almost never down.
You can leave it as it is.


4. Run it in the terminal
-------------------------
    python3 us_or_them.py

What to expect (about 1 second):

    UP     144 ms  https://matrutech.com
    UP      52 ms  https://www.google.com
    ...
    Verdict: Everything's up. Go back to sleep.
    Checked in 0.4 s

The three possible verdicts:
  - "Everything's up. Go back to sleep."         all green
  - "It's US. Wake up the team."                  the neighbors answer, one of yours doesn't
  - "It's THEM (or your Wi-Fi). Check your connection first."
                                                  not a single neighbor answers

Try breaking it on purpose: add a site that doesn't exist to OURS,
like in the video: "https://checkout.matrutech.com", and run it again.
You should see it DOWN in red and the verdict "It's US".


5. Run it as a web dashboard
----------------------------
    python3 us_or_them.py --web

Then open http://localhost:8000 in your browser.
The page runs the checks every time it loads and refreshes itself every
10 seconds. Stop it with Ctrl+C in the terminal.


6. What each part does
----------------------
  check(url)   opens one URL with a 3-second timeout and a User-Agent;
               returns (url, ok, milliseconds). Any error = not ok.
  run(urls)    checks all the URLs at the same time with a
               ThreadPoolExecutor of 10 workers ("ten registers").
  verdict()    any() neighbor up? all() of ours up? -> us, them, or sleep.
  terminal()   prints the colored rows, the verdict and the total time.
  Dashboard    a tiny web server (http.server) that builds an HTML table.


Typical error and how to fix it
-------------------------------
Problem: on a Mac, EVERYTHING shows DOWN in red, even google.com,
and the verdict says "It's THEM (or your Wi-Fi)" while your internet works.

Cause: Python installed from python.org on macOS does not use the system
certificates, so every HTTPS connection fails silently (the script catches
the SSL error and reports "not ok").

Fix: run the certificate installer that comes with Python, once:
  - Finder > Applications > Python 3.x > double-click
    "Install Certificates.command"
  - or in the terminal (change 3.12 to your version):
        /Applications/Python\ 3.12/Install\ Certificates.command
Then run the script again. It's not the internet. It's the certificates.

Other errors:
  - "SyntaxError: expected ':'"      a "def" or "with" line is missing its colon.
  - "Address already in use" (--web) something else uses port 8000: close it,
                                     or change 8000 in the last lines of the file.
  - Slow first run behind a VPN or corporate proxy: some sites may hit the
    3-second timeout. That's the timeout doing its job.


Note
----
This is a teaching tool: the flashlight in your kitchen drawer, not real
production monitoring. Real teams use real monitoring tools for that.

Made by MaTru - MaTru On-Call. License: MIT, (c) 2026 MaTruTech (see README.md).
And remember: he who has backups... laughs last.
