Artificial Intelligence
Please fill the required field.

write a code in python to monitor disk space quota in linux and send email when soft quota is reached and critical email for quota reach


To monitor disk space quotas in Linux and send email notifications when a soft quota is reached and a critical email when the quota is reached, you can use Python along with the `subprocess` module to get disk usage information and `smtplib` for sending emails. Here is a sample script that accomplishes this task. Make sure to have the necessary permissions to check disk usage and to send emails from the machine where the script is running. ### Prerequisites: 1. Ensure that you have Python installed on your Linux machine. 2. Make sure the `quota` command is available and that you have the necessary permissions to query disk quotas. 3. You'll need to replace placeholders like `smtp_server`, `from_email`, `to_email`, and `password` with actual values. ### Python Script ```python import subprocess import smtplib from email.mime.text import MIMEText import time def check_disk_usage(): # This command can vary based on your setup. # Here, 'quota -u your_username' will display user-specific quotas. result = subprocess.run(['quota', '-u', 'your_username'], stdout=subprocess.PIPE, text=True) output = result.stdout return output def send_email(subject, body): smtp_server = 'smtp.example.com' # Replace with your SMTP server smtp_port = 587 # Replace with your SMTP port (e.g., 587 for TLS) from_email = This email address is being protected from spambots. You need JavaScript enabled to view it.' # Replace with your email address to_email = This email address is being protected from spambots. You need JavaScript enabled to view it.' # Replace with recipient email address password = 'your_email_password' # Replace with your email password msg = MIMEText(body) msg['Subject'] = subject msg['From'] = from_email msg['To'] = to_email with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls() server.login(from_email, password) server.sendmail(from_email, [to_email], msg.as_string()) def parse_quota_info(info): lines = info.splitlines() for line in lines: if "blocks" in line: parts = line.split() # Parsing the quota info, adjust indexes if necessary soft_limit = int(parts[1]) # You may need to adjust this based on output format hard_limit = int(parts[2]) used = int(parts[3]) return soft_limit, hard_limit, used return None, None, None def main(): soft_quota_reached = False critical_quota_reached = False while True: quota_info = check_disk_usage() soft_limit, hard_limit, used = parse_quota_info(quota_info) if used >= hard_limit: if not critical_quota_reached: send_email("Disk Quota Critical Alert", f"Critical alert! You have reached the hard quota of {hard_limit} blocks. You are using {used} blocks.") critical_quota_reached = True elif used >= soft_limit: if not soft_quota_reached: send_email("Disk Quota Warning Alert", f"Warning! You are close to the soft quota of {soft_limit} blocks. You are using {used} blocks.") soft_quota_reached = True else: soft_quota_reached = False critical_quota_reached = False # Check every 10 minutes time.sleep(600) if __name__ == "__main__": main() ``` ### Key Features: - The script retrieves the disk quota information using the `quota` command. - It sends an email when the user's disk usage reaches a "soft quota" and again for a "critical quota." - The script checks every 10 minutes (600 seconds) for updates on the disk usage. ### Notes: - Modify `your_username`, `smtp_server`, `from_email`, and `to_email` to fit your use case. - Ensure that your SMTP settings are correctly set up for sending emails. You may also need to allow less secure apps in your email settings or use OAuth2 for authentication. - Adjust the parsing logic in `parse_quota_info()` according to the specific output format of the `quota` command on your system.