In my case I wanted to monitor the activity of a program which dialled up automatically. This would also apply if you wanted to set up automatic mail collection as well. As I connect to the internet using a connection script (which can then be called automatically), I felt that another simple script ought to be able to create me a log file.
An extract from today's file is as follows :-
==================================================
Logfile entry for :-
Tue Jan 18 00:08:17 GMT 2000
Uptime :- 12:08am up 2 days, 8:11, 3 users, load average: 2.66, 2.29, 1.81
Connect Tue Jan 18 00:47:08
GMT 2000
Disconnect Tue Jan 18
00:47:47 GMT 2000
Connect Tue Jan 18 04:34:02
GMT 2000
Disconnect Tue Jan 18
04:34:31 GMT 2000
Connect Tue Jan 18 06:48:12
GMT 2000
Disconnect Tue Jan 18
07:08:22 GMT 2000
Connect Tue Jan 18 12:11:24
GMT 2000
Disconnect Tue Jan 18
12:12:06 GMT 2000
Connect Tue Jan 18 17:28:11
GMT 2000
Disconnect Tue Jan 18
17:29:00 GMT 2000
Connect Tue Jan 18 19:56:49
GMT 2000
Disconnect Tue Jan 18
20:04:42 GMT 2000
===================================================
This has been created by adding the following lines to
the start of my connect script :-
echo "Connect " >> /home/logs/dialup.log date >> /home/logs/dialup.log
With the corresponding "Disconnect" added to my disconnect script.
For those who are struggling with the above (like I did at first):-
echo = like it says, echo's what follows (in this case some text "in speech marks" anywhere you want.
>> = appends (Note APPENDS (adds)) to a file.
If you use > this creates a new file and wipes the old one!!!!
These are called "redirection operators" and are very handy.
/home/logs/dialup.log = location and filename.
Under Lunix (no mistake), if you didn't know, it is good practice (and sometimes essential) to specify the absolute location of the file, and not rely on "current path" as with DOS.
date = extract of system date, used by itself will echo current date and time to the screen.
The old redirection operator appends it to the logfile. i.e. at the time the script is executed, the date and time is added to the log.
Simple eh ?.
Taking a step back, you really need to decide how you want to keep the logfile. In my case I decided to have a log file for today, one for yesterday, and a history log containing all the connections. This is probably a bit over the top, but gave me the chance to try scheduling the creation of files etc. using cron. This is not as hard as it first appears, as most modern distributions make simple, but repetitve jobs easy to schedule. In my case, using SuSE, any script placed in the directory /etc/cron.daily/ will be run each day at midnight, without having to create a special cron job. The same applies to /etc/cron.hourly/ and /etc/cron.monthly/
Check your distribution - they all do it but the location may differ. Creation of the actual script files has been covered at length elsewhere, together with the use of a suitable text editor. I prefer to create a script file using
touch filename
then use kedit. Do it your own way of course. The same applies to making a script executable - there is more than one way, as long as it ends up with the correct permissions. So here is the daily logging script :-
#! /bin/bash
# rg December 1999
# file to create a log file for each day
# and a total history log.
# this script should be run only once daily
# After any changes copy this file to /etc/cron.daily/
# and it will then run at midnight every day by the
system.
cat /home/logs/dialup.log.yesterday >> /home/logs/dialup.log.history
# move yesterdays data into main log file (append)
mv /home/logs/dialup.log /home/logs/dialup.log.yesterday
# move todays data into yesterdays file, over writing.
echo " " > /home/logs/dialup.log
# create new log file for today, and add a blank line
echo "Logfile entry for :- " >> /home/logs/dialup.log
date >> /home/logs/dialup.log
echo " " >> /home/logs/dialup.log
echo "Uptime :- " >> /home/logs/dialup.log
uptime >> /home/logs/dialup.log
echo " " >> /home/logs/dialup.log
# start a new log file for today and create the header
info
As a simple person, each command has been put on a new
line. This makes it easy for me to understand it later. Clever people and
guru's put lots of things on the same line just to make it hard to follow.
In order to see the log files, without opening them
in a text editor, I also created the following scripts, made them excutable,
and copied them into /usr/bin/ which means they can be typed in at the
command line, and will execute from anywhere i.e. they are on the path.
Again there are many ways to do this and purists may not like my simple
(i.e. one user) solution.
#!/bin/bash
# /usr/bin/td
# file to display daily log
tail -n 20 /home/logs/dialup.log
#!/bin/bash
# /usr/bin/yd
# file to display yesterdays daily log
tail -n 20 /home/logs/dialup.log.yesterday
The tail command is good, as used above just the last 20 lines of the specified file are echoed to the screen. Of course you could use an alias, but then it wouldn't be a script would it ?!!!
Finally, you may (those in the know) be wondering why I didn't use the built in log file created by the system i.e. /var/log/messages
Have you looked at this file ??
#!/bin/bash
# filename = getlog
# file to extract connect times
# from /var/log/messages
# to /home/logs/longlog
echo "Extracting total Connect time from system message
log"
echo "File Extracted" > /home/logs/longlog
date >> /home/logs/longlog
echo " " >> /home/logs/longlog
grep 'Connect time' /var/log/messages >> /home/logs/longlog
echo " " >> /home/logs/longlog
echo "File Extracted" >> /home/logs/longlog
date >> /home/logs/longlog
echo "All stored in /home/logs/longlog text file"
If you run this script, then tail the longlog file, there you have it. Notice the grep command - brilliant for such simple tasks. Try :-
man grep
if you haven't heard of it before. Example of what you get (last few lines only):-
Jan 18 06:43:03 amd pppd[8339]:
Connect time 0.5 minutes.
Jan 18 07:08:22 amd
pppd[8384]: Connect time 19.9 minutes.
Jan 18 12:12:07 amd
pppd[8906]: Connect time 0.4 minutes.
Jan 18 17:29:00 amd
pppd[9408]: Connect time 0.5 minutes.
Jan 18 20:04:42 amd
pppd[9661]: Connect time 7.6 minutes.
File Extracted Tue Jan 18 21:32:19 GMT 2000
Now you can check your phone bill line by line!!!