Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need help in creating a script that will run once a day in afternoon and collect the information of each partition(Disk Space , Total and Used) and send it to my email.

Please help im very very new in this scripting thing.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Provided you have an MTA configured on that machine to accept and relay mail for you (on a server that should be the case) try this:

$ df -h | mail -s "Filesystem usage report for `hostname`" myemail@domain.tld

(MTA = Postfix, Exim, etc.)

Example emailed report

If that fits your needs, add it to your crontab to run every day:

$ crontab -e

An editor will open. Add a line like this:

@daily df -h | mail -s ...

Save and close.

This will make it run with the other daily tasks. If you need a report on a specific time of the day or error logging to a specific address, please read about the cron syntax (a lot of this is on the Internet - here's one random website). For example:

MAILTO=myerroraddress@domain.tld
# at 5 a.m every day:
0 5 * * * mycommand

In case you can't send out mail on that machine directly, read this or the answer by @hakermania on how to do that (many more ways exist).

share|improve this answer
    
I have not installed any mail server on my TEST PC. Can u please provide any good resource that can be used to install Posfix or Exim. I have searched for Posfix installation. most of them are very confusing . Which one should I install Postfix, Exim or Sendmail. Thanks –  OmiPenguin Sep 16 '12 at 11:37
    
@UmairMustafa If it's just a PC and you're unfamiliar with MTA configurations I recommend you to use a script that uses authentication for a remote SMTP server instead. A lot depends on your situation - like whether you have a mail relay server in your network already. –  gertvdijk Sep 16 '12 at 11:41
    
Situation is this that I will first try it on my Test PC then later I will assign the same Script to each server in company running on linux. So tell me do I have to install Postfix first . If yes then I would really appreciate if u provide some assistance. –  OmiPenguin Sep 16 '12 at 13:19
    
@UmairMustafa Configuring your mail gateway and relays is a completely differrent question. Try asking that kind of question on serverfault.com. –  gertvdijk Sep 16 '12 at 13:22
add comment

In order to make the script running you will need to setup a cron job with it: How do I setup a cronjob ?

Now, inside your script, you will need to do something like this:

#!/bin/bash
#script that simply saves the output of df -h to an output file
#which is sent as an attachment to an e-mail

#a) save the output of the command:
temp_file=$(mktemp)

df -h > $temp_file 2> /dev/null

/root/email.py recipient@gmail.com "Title here" "Body here. The current date and time is $(date)" "$temp_file"

sleep 3
rm -rf $temp_file

As you can see, I'm calling a python script from within your root path (not readable but nobody else but root himself) which takes the following arguments:

"recipient-email" "title-of-email" "body-of-email" "attachment"

This python script is this:

#!/usr/bin/python
import os, re
import sys
import smtplib

from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.MIMEText import MIMEText


SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587


sender = 'youremailhere@gmail.com'
password = "yourpasswordhere"
recipient = sys.argv[1]
subject = ''
message = sys.argv[3]

def main():
    msg = MIMEMultipart()
    msg['Subject'] = sys.argv[2]
    msg['To'] = recipient
    msg['From'] = sender

    part = MIMEText('text', "plain")
    part.set_payload(message)
    msg.attach(part)

    session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)

    session.ehlo()
    session.starttls()
    session.ehlo

    session.login(sender, password)

    fp = open(sys.argv[4], 'rb')
    msgq = MIMEBase('audio', 'audio')
    msgq.set_payload(fp.read())
    fp.close()
    # Encode the payload using Base64
    filename=sys.argv[4]
    msgq.add_header('Content-Disposition', 'attachment', filename=filename)
    msg.attach(msgq)
    # Now send or store the message
    qwertyuiop = msg.as_string()



    session.sendmail(sender, recipient, qwertyuiop)

    session.quit()
    os.system('notify-send "Your disk space related email has been sent."')

if __name__ == '__main__':
    main()

Of course you will need to provide it with your gmail email and password at the top of the script (sender and password variables). If you install the libnotify-bin package, then, if the email was successfully sent, you will be notified with a desktop notification.

So, summing up, you will need to setup a cron job with the above bash script. This bash script will save the output of df -h inside a temporary file, which will be sent via a python script to a recipient email of your choice (as far as I remember, the sender email must be gmail).

PS: The above solution will show the free and total disk space only of your mounted filesystems. If this is a problem please inform me so as to extend my answer about how to automatically mount all the available filesystems and then run df -h.

share|improve this answer
    
I recommend using the sendemail package for sending mails to remote SMTP servers directly instead. –  gertvdijk Sep 18 '12 at 1:17
    
@gertvdijk please feel free to edit the answer and include yours as well, pointing out why sendemail may be a better option. Or add it as an answer! –  hakermania Sep 18 '12 at 1:59
add comment

You can use this script to check the disk usage

#!/bin/bash

limit=85
email=you@domain.com
host=`hostname`
out=`df -k | grep "^/dev" | awk '{ if($5 > $limit) print "\nDisk space is critial on " $1,$5,$6 "\n"}'`
usr/bin/mail -s "Disk Space Alert on $host: $out" $email

Use cron to make the script run automatically. Check this online cron generator for helping you in setting it up.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.