import pandas as pd import random import logging from datetime import datetime, timedelta # Configure logging logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") # Generate random timestamps within the last 90 days def random_timestamp(): start = datetime.now() - timedelta(days=90) return start + timedelta(seconds=random.randint(0, 60 * 60 * 24 * 90)) # Generate random IP addresses def random_ip(): return ".".join(str(random.randint(0, 255)) for _ in range(4)) # Define dataset attributes protocols = ["TCP", "UDP", "ICMP"] actions = ["Allow", "Deny"] threat_levels = ["Low", "Medium", "High", "Critical"] threat_types = ["DDoS", "Brute Force", "SQL Injection", "Port Scan", "Malware"] response_actions = ["Blocked", "Alerted", "Monitored", "Escalated"] # Generate dataset data = { "timestamp": [random_timestamp().strftime("%Y-%m-%d %H:%M:%S") for _ in range(500)], "source_ip": [random_ip() for _ in range(500)], "destination_ip": [random_ip() for _ in range(500)], "protocol": [random.choice(protocols) for _ in range(500)], "port": [random.randint(20, 65535) for _ in range(500)], "action": [random.choice(actions) for _ in range(500)], "threat_level": [random.choice(threat_levels) for _ in range(500)], "threat_type": [random.choice(threat_types) for _ in range(500)], "response_action": [random.choice(response_actions) for _ in range(500)], } # Convert to DataFrame df = pd.DataFrame(data) # Save as CSV with validation output_file = "nsg_dataset.csv" try: df.to_csv(output_file, index=False) logging.info(f"✅ NSG Dataset ({len(df)} records) saved to '{output_file}' successfully!") except Exception as e: logging.error(f"❌ Error saving dataset: {e}") # Preview dataset print(df.head())