Developer

Creative scripts: Using MHonArc for Web-enabled log archives

Enable your IT staff to read your daily log files with the ease of Web browsing. It's a simple trick using a script and MHonArc. Let Jack Wallen, Jr. show you how.


As a Chief Engineering Officer or lead systems administrator of a large IT department, you’ll often want to share log files or “daily diaries” with your teams. You could write them up and e-mail them, or you could create a very simple archive of your logs and allow your staff to view them online. By using a few simple Linux tools, this is a task that becomes not only fun but cost-effective and amazingly flexible.

In this Daily Feature, you’ll learn how to install and take advantage of a Perl mail-to-HTML program, MHonArc, intended to post e-mail threads online. Along with MHonArc, you’ll be taking advantage of Linux's bash scripting abilities to write a script that will take your raw text log, send it through MHonArc, and post it online.

MHonArc
You’re not going to be diving too deeply into MHonArc's capability here. In fact, you’ll only be skimming the surface of what MHonArc can actually do (there will be more to come on MHonArc later). You only need this tool’s basic functions to make your project come to life.

You do have to install MHonArc first. The installation is a very simple one. To get MHonArc, hop over to the official site and download the latest file (as of this writing, it’s 2.5.2). The main requirements for getting this application up and running are:
  • ·        Perl 5
  • ·        The Getopt::Long and Time::Local Perl modules

These can be tested by running the following command:
perl -MGetopt::Long -MTime::Local -e ';'

If you get no errors from the above command, you’re set to install. If you get errors, more than likely, you’ll have to reinstall Perl.

Once you know for sure you have the proper requisites, move the MHonArc tar file to the /usr/local/src directory and change to that same directory. Once in /usr/local/src/, issue the following command, where RELEASE is the release number you’re installing:
tar xvzf MHonArcRELEASE.tar.gz

After running this command, you’ll find a new directory, MHonArcRELEASE, where RELEASE is the actual release number. The next step is to change into that directory (cd MHonArcRELEASE will do the trick) and issue the following command:
perl install.me

When the installation starts, you’ll be prompted to answer a number of questions (i.e., location of the Perl executable, where to put the MHonArc files, and so on). Once all the questions are asked, your installation is ready and waiting for you to take advantage of its power.

Testing MHonArc
Before you go ahead with the creation of the bash script, it would be best to get a feel for the mhonarc command itself. MHonArc is strictly a command-line tool and has a syntax like this:
mhonarc [<options>] <mailfolder>

This command takes an e-mail (or raw text, in your case), converts the text into HTML, and creates a threaded archive that is viewable from a browser. So if you create a test file called test with these contents:
this is a test

and run the following command:
mhonarc test

you’ll find three new *.html files in the directory where the command was issued. These new files are:
  • ·        threads.html
  • ·        msg00000.html
  • ·        maillist.html

If you open the maillist.html file in a browser (even a text-based browser like Lynx will work), you’ll see a threaded listing like the one in Figure A.

Figure A
A snippet of the Web browser shows a test file with no subject or date.


If you click on [no subject], the browser will reveal an empty file. Where did this is a test go? This brings out one of the limitations to how you’re implementing MHonArc. When MHonArc is run on a file, it expects to see certain elements that are standard to e-mail (From:, Subject:, and To: fields, for example). You’ll get around this with some tricky bash scripting.

The script
The script you’re going to create will actually do a number of things. Here's what the file will look like. The line beginning with NUM= checks the directory /var/www/html/web/log for the number of files contained within and sets the variable NUM equal to that value. The line beginning with DATE= sets the variable DATE to the current date in the format DD.MM.YYYY.

The line beginning with touch creates a file in /var/www/html/web/log with a file named log$DATE, where $DATE is the value of the DATE variable just set. The next three lines actually take care of some of the formatting necessary for MHonArc to convert your logs properly (so you'll actually see text and see it in an understandable format). Of these three lines, the first line, beginning with echo Subject:, writes (to the top of the log$DATE file) the line Subject: CURRENT_DATE, where CURRENT_DATE is the date set to the variable DATE in the script. This line is necessary for MHonArc to create a readable index of your log files. (It would normally be included in an e-mail header.)

The next line, beginning with echo From:, appends the line From: Jack Wallen, Jr. to the log$DATE file. You can be creative with this line. You can change the line to read this way, which would add the name of whichever user is running the script to the log$DATE file:
echo From:$USER >> /var/www/html/web/log/log$DATE

By doing it this way, you’ll know exactly who ran the log file each day.

The line beginning echo “” appends a blank line after the From line to the log$DATE file. This is necessary to get around the fact that you’re running MHonArc on unformatted text. It would be possible to omit this line if you (and your staff) could remember to add a blank line at the beginning of every log file (fat chance there).

The cat log line actually takes the contents of the log file and concatenates it to the log$DATE file. After this line, you should actually have a log file written in  /var/www/html/web/log ready for manipulation by MHonArc.

The if statement in the script file must do one thing: decide which command to run based on whether there are already files located in /var/www/web/log. If there are no files in that directory, it runs the mhonarc command without the -add switch. If there are files in the /var/www/html/web/log directory, it runs the mhonarc command with the -add switch. Because of this, make sure that you create a directory specifically for your MHonArc'd logs. When you create your first log and run the script on that first log, the script will see that there are no files in /var/www/html/web/log, and the if statement will direct MHonArc to run without the -add switch.

The actual command run from the script does two things: changes to the /var/www/html/web/log directory (so MHonArc will deposit the resulting HTML files in the proper directory) and runs the mhonarc command on the log$DATE file. This command is run (without the -add switch, remember) with the -fromfields switch (this tells MHonArc where to find the From: field that will be displayed in the HTML) and the -title switch (in this script, the title, displayed on the .html page, will be SYSTEM LOGS).

And that does it for the script. After running a test, remember that the if statement will locate files in /var/www/html/web/log and will then run the mhonarc command with the -add switch.

After you’ve written the complete script, save the file (you can name it something like log_auto) and change the executable permissions with the following so that the script can be executed:
chmod ug+x log_auto

Using this system
The use of this system is very simple. You’ll create a file named log in the $HOME directory of the user running the command. This file will be a plain text file and will have the contents of the daily log file. Once you’re done writing this file, you’ll save it (making sure the name is log) and then run the log_auto script.

The next time you run the command must not be on the same date or your log will not be written (it's a daily log, remember). When you go to write the next day’s log file, you’ll erase the contents of the preceding log, save the new contents, and run the log_auto script once again.

Conclusion
Like much of Linux, this system is not limited to daily log files. You can get creative and apply this to a great number of needs. And remember that MHonArc is a much more powerful application than what you’re using it for here. Coming up, you'll get to see how others use MHonArc for its original intent.

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks