Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions day-01/system_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pyscipt to Check CPU Usages

import psutil

def check_cpu_usages(name,usages,limit):
if usages > limit:
print("CPU alert email Send High Use...")
else:
print("Usages is Ok")

for i in range(5):
cpu_limit = int(input("Enter CPU Threshold"))
memory_limit = int(input("Enter Memory Threshold"))
disk_limit = int(input("Enter Disk Threshhold"))
break
print("\n Checking Systems Usages...")

cpu_usage = psutil.cpu_percent(1)
memory_usage = psutil.virtual_memory().percent
disk_usage = psutil.disk_usage('/').percent

check_cpu_usages("CPU", cpu_usage, cpu_limit)

check_cpu_usages("Memory", memory_usage, memory_limit)

check_cpu_usages("Disk", disk_usage, disk_limit)
56 changes: 56 additions & 0 deletions day-02/api_data_fetcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import requests
import json

BASE_URL = "https://pokeapi.co/api/v2/pokemon/"

def get_Pokemon(pokemon_name):
url = BASE_URL + pokemon_name.lower()
headers = {
"Accept": "application/json"
}

try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
data = response.json()

print("API is working... \n")

selected_data = {
"name": data["name"],
"id": data["id"],
"height": data["height"],
"weight": data["weight"],
}
print(json.dumps(selected_data, indent=4))

with open("output.json", "w", encoding="utf-8") as f:
json.dump(selected_data, f, indent=4)

print("Saving data to output.json ")

return selected_data

except requests.exceptions.RequestException as e:
print("API request failed ❌")
print("Error:", e)


except requests.exceptions.HTTPError:
print(f"❌ Pokémon '{pokemon_name}' not found!")

except ValueError:
print("Invalid JSON response ❌")


# pokemon_data = get_Pokemon()
def main():
pokemon_name = input("Enter Pokemon Name...Eg.Pikachu,ditto").strip()
# print("Eg.Pikachu,ditto")

if not pokemon_name:
print("Pokemon cannot be empty")

get_Pokemon(pokemon_name)

main()
6 changes: 6 additions & 0 deletions day-02/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ditto",
"id": 132,
"height": 3,
"weight": 40
}
81 changes: 81 additions & 0 deletions day-03/system_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pyscipt to Check CPU Usages Updated with Listed Changes
import psutil
from typing import Optional


def get_threshold(prompt: str) -> Optional[int]:
try:
value = int(input(prompt))
if not 0 <= value <= 100:
raise ValueError("Threshold must be between 0 and 100.")
return value
except ValueError as error:
print(f"[INPUT ERROR] {error}")
return None


def check_resource_usage(resource_name: str, usage: float, limit: int) -> None:
try:
if usage > limit:
print(
f"[ALERT] {resource_name} usage HIGH: "
f"{usage}% (limit: {limit}%)"
)
else:
print(f"[OK] {resource_name} usage normal: {usage}%")
except Exception as error:
print(f"[ERROR] Unable to check {resource_name}: {error}")


def get_system_usage() -> dict:
try:
return {
"CPU": psutil.cpu_percent(interval=1),
"Memory": psutil.virtual_memory().percent,
"Disk": psutil.disk_usage("/").percent,
}
except Exception as error:
print(f"[SYSTEM ERROR] Failed to fetch system usage: {error}")
return {}


def run_monitoring() -> None:
cpu_limit = get_threshold("Enter CPU threshold : ")
memory_limit = get_threshold("Enter Memory threshold : ")
disk_limit = get_threshold("Enter Disk threshold : ")

if None in (cpu_limit, memory_limit, disk_limit):
print("Invalid input detected. Returning to menu.\n")
return

print("\nChecking system usage...\n")

usage_data = get_system_usage()
if not usage_data:
print("No system data available. Returning to menu.\n")
return

check_resource_usage("CPU", usage_data["CPU"], cpu_limit)
check_resource_usage("Memory", usage_data["Memory"], memory_limit)
check_resource_usage("Disk", usage_data["Disk"], disk_limit)
print()


def main() -> None:
while True:
print("1. Enter thresholds")
print("2. Exit")

choice = input("Select an option (1 or 2): ").strip()

if choice == "1":
run_monitoring()
elif choice == "2":
print("Exiting program")
break
else:
print("[ERROR] Invalid choice. Please select 1 or 2.\n")


if __name__ == "__main__":
main()