#!/usr/bin/env python
"""
check_storage.py
<NAME>

Simple script to check disk useage and email me if full!

"""

import logging, argparse, os, subprocess
import smtplib
from email.mime.text import MIMEText
from check_report import Check
    
def main():
    # args
    parser = argparse.ArgumentParser(
        description = (" Simple script to check disk space useage"),
    )

    parser.add_argument("--email", default="<EMAIL>")
    parser.add_argument("--db", help="sqlite db file to record sends", default="./disk.db")

    args = parser.parse_args()
    check = Check(args.db)
    res = df()
    
    for dev in res:
        percent = int(res[dev][:-1])

        if percent > 90 and check.should_report():
            report(dev, percent, args.email)
            check.log_report()

def df():

    # hard coded here but whatever
    command = ["df", "-h"]
    output = subprocess.check_output(command)
    arrayed = [line for line in output.split("\n")]
    
    # kill the header
    arrayed.pop()

    check = ['/dev/sdb6','/dev/sdb1']
    res = {}

    for line in arrayed:
        tab_split = line.split()
        if tab_split[0] in check:
            res[tab_split[0]] = tab_split[4]

    return res


def report(dev, percent, email):

    # Create a text/plain message
    email_body = []
    email_body.append("Hello, Kyle\n")
    email_body.append("device: {} is almost full at {}".format(dev,str(percent)))

    msg = MIMEText("\n".join(email_body))

    # header stuff
    # no one else cares but me!
    root  = "<EMAIL>"
    subject = "Disk Check Warning: {}".format(dev)
    
    msg['Subject'] = subject
    msg['From'] = root
    msg['To'] = email
    
    # Send the message via our own SMTP server, but don't include the
    # envelope header.
    s = smtplib.SMTP('localhost')
    s.sendmail(root, [email], msg.as_string())
    s.quit()

if __name__ == "__main__":
    main()
#!/usr/bin/env python
"""
check_storage.py
<NAME>

Simple script to check disk useage and email me if full!

"""

import logging, argparse, os, subprocess
import smtplib
from email.mime.text import MIMEText
from check_report import Check
    
def main():
    # args
    parser = argparse.ArgumentParser(
        description = (" Simple script to check disk space useage"),
    )

    parser.add_argument("--email", default="<EMAIL>")
    parser.add_argument("--db", help="sqlite db file to record sends", default="./disk.db")

    args = parser.parse_args()
    check = Check(args.db)
    res = df()
    
    for dev in res:
        percent = int(res[dev][:-1])

        if percent > 90 and check.should_report():
            report(dev, percent, args.email)
            check.log_report()

def df():

    # hard coded here but whatever
    command = ["df", "-h"]
    output = subprocess.check_output(command)
    arrayed = [line for line in output.split("\n")]
    
    # kill the header
    arrayed.pop()

    check = ['/dev/sdb6','/dev/sdb1']
    res = {}

    for line in arrayed:
        tab_split = line.split()
        if tab_split[0] in check:
            res[tab_split[0]] = tab_split[4]

    return res


def report(dev, percent, email):

    # Create a text/plain message
    email_body = []
    email_body.append("Hello, Kyle\n")
    email_body.append("device: {} is almost full at {}".format(dev,str(percent)))

    msg = MIMEText("\n".join(email_body))

    # header stuff
    # no one else cares but me!
    root  = "<EMAIL>"
    subject = "Disk Check Warning: {}".format(dev)
    
    msg['Subject'] = subject
    msg['From'] = root
    msg['To'] = email
    
    # Send the message via our own SMTP server, but don't include the
    # envelope header.
    s = smtplib.SMTP('localhost')
    s.sendmail(root, [email], msg.as_string())
    s.quit()

if __name__ == "__main__":
    main()
#!/usr/bin/env python check_storage.py <NAME>  Simple script to check disk useage and email me if full! # args # hard coded here but whatever # kill the header # Create a text/plain message # header stuff # no one else cares but me! # Send the message via our own SMTP server, but don't include the # envelope header.