File size: 5,333 Bytes
30ddf21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import requests
import json
import time
from datetime import datetime, timezone
import urllib3

# Suppress the console warnings when we do our fallback check
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

TARGET_SERVICES = [
    {"name": "KTU Main Website", "url": "https://ktu.edu.in"},
    {"name": "KTU Login Portal", "url": "https://app.ktu.edu.in"},
    {"name": "KTU API Server", "url": "https://api.ktu.edu.in"}
]

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
}

def ping_servers():
    print("Running KTU Smart Stealth Ping...")
    results = {}
    down_count = 0
    ssl_count = 0

    for svc in TARGET_SERVICES:
        start_time = time.time()
        try:
            # STEP 1: Normal strict ping
            response = requests.get(svc["url"], headers=HEADERS, timeout=15)
            ping_ms = int((time.time() - start_time) * 1000)
            
            if response.status_code < 400:
                results[svc["name"]] = {"status": "Online", "ping": ping_ms}
                print(f"{svc['name']}: Online ({ping_ms}ms)")
            elif response.status_code < 500:
                results[svc["name"]] = {"status": f"Online (HTTP {response.status_code})", "ping": ping_ms}
                print(f"{svc['name']}: Online but returning Client Error (HTTP {response.status_code})")
            else:
                # This will cleanly catch 500, 502, 503, 504 Gateway errors
                results[svc["name"]] = {"status": f"Offline (HTTP {response.status_code})", "ping": "Error"}
                print(f"{svc['name']}: Offline (HTTP {response.status_code})")
                down_count += 1
                
        except requests.exceptions.SSLError:
            # STEP 2: Server threw an SSL error. Let's do a fallback check to verify if the server is actually alive.
            try:
                fallback_start = time.time()
                fallback = requests.get(svc["url"], headers=HEADERS, timeout=15, verify=False)
                ping_ms = int((time.time() - fallback_start) * 1000)
                
                if fallback.status_code < 400:
                    results[svc["name"]] = {"status": "Online", "ping": ping_ms}
                    print(f"{svc['name']}: Online (CA Mismatch Bypassed)")
                elif fallback.status_code < 500:
                    results[svc["name"]] = {"status": f"Online (HTTP {fallback.status_code})", "ping": ping_ms}
                    print(f"{svc['name']}: Online but returning Client Error (CA Mismatch, HTTP {fallback.status_code})")
                else:
                    results[svc["name"]] = {"status": f"Offline (HTTP {fallback.status_code})", "ping": "Error"}
                    print(f"{svc['name']}: Offline (HTTP {fallback.status_code})")
                    down_count += 1
                    
            except requests.exceptions.Timeout:
                # Captures if the server hangs infinitely during fallback
                results[svc["name"]] = {"status": "Offline (Connection Timeout)", "ping": "Timeout"}
                print(f"{svc['name']}: Offline (Connection Timeout)")
                down_count += 1
                
            except requests.exceptions.RequestException:
                # If the fallback ALSO crashes, it means KTU is having a genuine, massive SSL/Connection failure.
                results[svc["name"]] = {"status": "SSL Issue", "ping": "Blocked"}
                print(f"{svc['name']}: Genuine SSL/Connection Issue Detected")
                ssl_count += 1
                
        except requests.exceptions.Timeout:
             # Captures if the server hangs infinitely on the very first try
             results[svc["name"]] = {"status": "Offline (Connection Timeout)", "ping": "Timeout"}
             print(f"{svc['name']}: Offline (Connection Timeout)")
             down_count += 1
             
        except requests.exceptions.RequestException:
            results[svc["name"]] = {"status": "Offline (Network Error)", "ping": "Error"}
            print(f"{svc['name']}: Offline (Network/Routing Error)")
            down_count += 1

    # Determine Overall Status
    if down_count == len(TARGET_SERVICES):
        overall = "Major Outage"
    elif down_count > 0:
        overall = "Partial Outage"
    elif ssl_count > 0:
        overall = "SSL Issues Detected"
    else:
        overall = "All Systems Operational"

    utc_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
    
    new_record = {
        "timestamp": utc_time,
        "overall_status": overall,
        "services": results
    }

    try:
        with open("status-history.json", "r") as f:
            history = json.load(f)
    except (FileNotFoundError, json.JSONDecodeError):
        history = []

    history.insert(0, new_record)
    history = history[:12] # Keep only the last 12 entries to prevent the file from getting too large

    with open("status-history.json", "w") as f:
        json.dump(history, f, indent=4)

    print(f"Logbook updated! Overall Status: {overall}")

if __name__ == "__main__":
    ping_servers()