Latest tutorial: Making a Movieclip face another Movieclip or point on the stage | Ask Tutorial5!
 

Get tutorials on EMail




How to automatically generate logs and e-mail them

(8 votes)
Written by Michael D.   

In this tutorial, I will explain how to create a central log file with all information you need at a specific time, store it or e-mail it.

This can be acomplished with some basic Linux shell commands and some small tools that are generally avaiable in any system. First of all, create a directory somewhere on your hard drive where you want to store all generated logs. Let's say you pick and create /stats.

Now you have to create a crontab on the root account (with the command crontab -e).
Note: If you have any difficulties in using crontab, read our Cron Tutorial first.

This crontab should contain all the rules needed to get the job done. To generate the logs, use the "output file" option in any shell command. This can be achieved either with the ">" sign or with ">>".

For example if you enter the command "ifconfig eth0 > outputfile.log", any data comming from the command will be written in that file (viewed in shell with the 'more' command):

[root@LinuxSrv /]# ifconfig eth0 > outputfile.log
[root@LinuxSrv /]# more outputfile.log
eth0      Link encap:Ethernet  HWaddr 00:E0:7D:95:7E:EA
          inet addr:10.0.0.100  Bcast:10.255.255.255  Mask:255.0.0.0
          inet6 addr: fe80::2e0:7dff:fe95:7eea/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:136582 errors:22 dropped:9 overruns:6 frame:0
          TX packets:153198 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:86286216 (82.2 MiB)  TX bytes:86638495 (82.6 MiB)
          Interrupt:3 Base address:0x4f00


[root@LinuxSrv /]# ps > outputfile.log
[root@LinuxSrv /]# more outputfile.log
  PID TTY          TIME CMD
11381 pts/1    00:00:00 su
11383 pts/1    00:00:00 bash
11465 pts/1    00:00:00 ps


As you can see, if I reuse the same file name with other command, I will rewrite the file. This is not very useful in our case, so we'll use the ">>" option:

[root@LinuxSrv /]# ifconfig eth0 >> outputfile.log
[root@LinuxSrv /]# ps >> outputfile.log
[root@LinuxSrv /]# more outputfile.log
eth0      Link encap:Ethernet  HWaddr 00:E0:7D:95:7E:EA
          inet addr:10.0.0.100  Bcast:10.255.255.255  Mask:255.0.0.0
          inet6 addr: fe80::2e0:7dff:fe95:7eea/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:136830 errors:22 dropped:9 overruns:6 frame:0
          TX packets:153409 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:86308695 (82.3 MiB)  TX bytes:86661218 (82.6 MiB)
          Interrupt:3 Base address:0x4f00

  PID TTY          TIME CMD
11381 pts/1    00:00:00 su
11383 pts/1    00:00:00 bash
11475 pts/1    00:00:00 ps

In the crontab, write the command outputs for anything you want to log; for pasting from other log files, the usage is the same.

Let's say we want to create a crontab that will generate a file every morning, containing running processes, uptime and last messages.

The cron config should be like this:
54 05 * * * echo MY LOG FILE >> /stats/systemlog
54 05 * * * echo " " >> /stats/systemlog
55 05 * * * uptime >> /stats/systemlog
56 05 * * * ps -A >> /stats/systemlog
57 05 * * * tail /var/log/messages >> /stas/systemlog

You may want to add some blank lines between the commands or words, so use the 'echo' command like in the example above.
To e-mail the log, use the mail MTA available on linux:

00 06 * * * mail -s "MY log on `date`" youruser@yourdomain < /stats/systemlog

This command will send a mail with the subject "My log on <current_full_date> that will include the file /stats/systemlog.

To prevent writing and resending the same file every morning, rename the file after e-mailing it. A date-related name is useful in organizing your data:

01 06 * * * mv  /stats/systemlog "/stats/systemlog_`date`"

This will rename your file according to the current date, making it something like

"systemlog_Sat Sep 05 06:01:01 EEST 2007"; sometimes the shell will replace the spaces with "\" sign for some reason.


Save your work and you are done. You can also add some rules to delete the really old files.

If your server is not online or is shutdown at the time when the crontab should execute, the result will depend of your running configuration. If you are running anacron at system startup for example, it will execute unfinished cron tasks due to downtime, writing the logs and sending the mail at bootup. If your Linux box was not online at 6.00 am when the sendmail tried to send the data, it will retry by default for 5 days to resend it.

Another issue that you should be aware of is the configuration of the sendmail service (usually /etc/mail/sendmail.cf). If it's not configured properlly or your server doesn't have a valid domain name, the sent e-mail could end up being treated as spam by the mail server (being considered to come from localhost.localdomain). You can still configure a filter on most mailing providers to overcome this.

Hope this was useful



Subscribe now via RSS feed and get all the new tutorials


Do you need more help? Ask now!
 

busy
Last Updated ( Wednesday, 05 September 2007 )