Linux, Unix, /etc

Danger Will Robinson! You are now entering a condescending Unix user zone!
Sponsored links (requires javascript):

Scripts: Administration

clock-sync: keep the system clock accurate

Note use of logger(1) to record the setting of the clock to the system log. This isn't necessary, but it is a handy way of seeing how much the system clock drifts over time. My syslog.conf sends all these local0 messages to /var/log/netdate, so they don't clutter up any more important log file. I run this script from cron every hour, so my clock is normally less than a second out of sync with that at ntp.demon.co.uk, which I'm presuming is very accurate.

 
#!/bin/sh
# clock-sync: update system clock from Demon ntp server;
# and write new date to CMOS if successful

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
progname=`basename $0`
if netdate tcp ntp.demon.co.uk | logger -p local0.notice -t clock-sync
then
    hwclock —systohc --utc
    exit 0
else
    logger -p local0.notice "$progname failed" -t clock-sync
    exit 1
fi


httpd logging: simple set of scripts to look at my logs

I used to keep my website(s) on another machine, which meant I had to do some grep'ing and awk'ing to get the states from the apache server delivered to me in a convenient format.

The first script, updateweblog, gets the records I'm interested in from the main log file. It's a shared server, so this is more efficient.

#!/bin/sh
#updatelog: get latest log records from master file
grep dunne /var/log/httpd/access_log > $HOME/tmp/log

The various scripts that report on the different parts of my site are symbolic links to the script below. So, if this script is called as "linuxlog", it reports web stats for the linux part of my site; and so on.

#!/bin/sh
#httpdlog: query the httpd access log for my data
file=$HOME/tmp/log
name=`basename $0`
case $name in
    corelog) site='/~dunne//*index.html' ;;
    linuxlog) site='-v ireland|weblog|index.html' ;;
    irelandlog) site='/~dunne/ireland/' ;;
    webloglog) site='/~dunne/weblog/' ;;
    alllog) site='/~dunne/' ;;
esac
case $1 in
    #summary: how many
    -s) 
	egrep "`date +'%d/%b'`" $file |
	egrep $site |
	awk '{print $1}' | sort +0 -1 | uniq | wc -l
    ;;
    #hostname/uniq-ip: who
    -h) 
	egrep "`date +'%d/%b'`" $file |
	egrep $site |
	awk '{print $1}' | sort +0 -1 | uniq
    ;;
    #detail: what
    -d) 
	egrep "`date +'%d/%b'`" $file |
	egrep $site 
    ;;
esac

This is the control script that sends me the web stats reports via e-mail. I run it nightly via cron.

#!/bin/sh
#webstats: run httpdlog in a variety of ways
updateweblog
for i in core linux ireland weblog
do
    "$i"log -s | mail -s "$i summary log `date`" dunne
    for j in `"$i"log -u`
    do
	echo "$j	`host $j`" | awk '{printf "%s\t%s\n", $1, $NF}'
    done | mail -s "$i ip/hostname log `date`" dunne
    "$i"log -d | mail -s "$i detailed log `date`" dunne
done


check-diald

This script keeps an eye on a daemon, and restarts it if its exited for some reason. This is for diald, but it's trivial to modify for other daemons.

#!/bin/sh
#check-diald
if [ -f /var/run/diald.pid ]
then
	pid=`cat /var/run/diald.pid`
	result=`ps ax|grep $pid|grep -v grep`
	case $result in
		"") /usr/sbin/diald; exit 1 ;;
		*) exit 0 ;;
	esac
else
	/usr/sbin/diald
	exit 2
fi


restart-named

Used to use this when I had a dial-up connection. It was needed because the named on my server isn't the pirmary or secondary controllor for that server's hostname. Clearly, named should do something sensible when asked about that name when the Net link is down. So, when the link was down, I used to restart named with an alternate config. file and /var/named files, which mapped the server hostname onto the ip address of the ethernet adaptor rather than the Net adaptor.

#!/bin/sh
pid=`cat /var/run/named.pid`
case $pid in
	"") exit 1;;
esac
case $1 in
	netup)
		kill $pid
		/usr/sbin/named -u named -g namedgroup -b /etc/named.conf.netup
	;;
	netdown)
		kill $pid
		/usr/sbin/named -u named -g namedgroup -b /etc/named.conf.netdown
	;;
	*)
		kill -1 $pid
	;;
esac


trim-log-files

There are various ways of keeping your log files from growing indefinitely. This is something I run from cron every quarter.

#!/bin/sh
#trim-log-files
cd /var/log || exit 1
for i in $(find . -type f -maxdepth 1)
do
    filename=$(echo $i | sed 's?^\./\(.*\)?\1?')
    cp -p $i old/$(date +'%Y%m%d').$filename
    > $i
done
cd /var/log/httpd || exit 2
for i in $(find . -type f -maxdepth 1)
do
    filename=$(echo $i | sed 's?^\./\(.*\)?\1?')
    cp -p $i old/$(date +'%Y%m%d').$filename
    > $i
done


checkreject

Check the mail logs for mail that's been blocked by access db or other sendmail rules. Useful to run this from cron last thing every day and get the report mailed to you, to check that you aren't bouncing mail you shouldn't be. Of course, in an ideal world one wouldn't have to bounce mail at all, but there it is... A drawback of the sendmail access database is that the only header you can look at is "From:" which these days is usually forged. You still have to hack sendmail.cf to block mail based on the content of other headers.

#!/bin/sh
#checkreject: look in mail log for mail blocked by access db or other sendmail
#rules
grep "$(date '+%b %_d')" /var/log/mail |
grep 'reject=' 


[back to Scripts index] [back to Linux, Unix, etc] [Main Site] [Weblog]



Contents licensed under the GPL