Blame contrib/adm-mrtg/adm-mrtg

Packit 667938
#!/usr/bin/ksh 
Packit 667938
Packit 667938
# This script organizes config files for MRTG
Packit 667938
# and restarts the MRTG processes cleanly.
Packit 667938
Packit 667938
# Author: Scott Ripley, U.S. House of Representatives
Packit 667938
# Version: 1.0 (and all that a 1.0 release entails)
Packit 667938
# Contact: scott.ripley@mail.house.gov
Packit 667938
# Copyright: (C) 2001 Scott Ripley 
Packit 667938
Packit 667938
# This program is free software; you can redistribute it and/or
Packit 667938
# modify it under the terms of the GNU General Public License
Packit 667938
# as published by the Free Software Foundation; either version 2
Packit 667938
# of the License, or (at your option) any later version.
Packit 667938
Packit 667938
# This program is distributed in the hope that it will be useful,
Packit 667938
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 667938
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 667938
# See the GNU General Public License for more details.
Packit 667938
Packit 667938
Packit 667938
# Change these directores to match your configuration for MRTG
Packit 667938
MRTG=/usr/local/mrtg/bin
Packit 667938
# TARHOME is the location of the back-up files. It should be another volume
Packit 667938
TARHOME=/usr/local/mrtg/bin
Packit 667938
# Location of log files
Packit 667938
WEBHOME=/usr/local/apache/htdocs/mrtg_images
Packit 667938
Packit 667938
cd $MRTG 
Packit 667938
Packit 667938
# Initialize the variables and temp files
Packit 667938
Packit 667938
typeset -i CFGLINE
Packit 667938
typeset -i MAXLINE
Packit 667938
CFGLINE=1
Packit 667938
MAXLINE=1
Packit 667938
CFG="$MRTG"/mrtgcfgs.run
Packit 667938
CFGNUM="$MRTG"/cfgnum.run
Packit 667938
MCNAME=`uname -n`
Packit 667938
EXIT=N
Packit 667938
DATE=`date '+%m%d%y%H%M' `
Packit 667938
# TARHOME is the location of the back-up files. It should be another volume
Packit 667938
TARHOME=/usr/local/mrtg/bin
Packit 667938
WEBHOME=/usr/local/apache/htdocs/mrtg_images
Packit 667938
Packit 667938
# Set tabs for display columns
Packit 667938
stty tabs
Packit 667938
tabs 10,30,40,45 
Packit 667938
 
Packit 667938
# Main loop
Packit 667938
while [ "$EXIT" = N ]
Packit 667938
do
Packit 667938
	# reads the pid files and inputs them into a file 
Packit 667938
	ls -go *pid | sed 's/[ ][ ]/ /g' | cut -d"." -f1 > "$CFG"
Packit 667938
Packit 667938
	nl -bt -nrz -w2 "$CFG" > "$CFGNUM" # numbers the lines in the file
Packit 667938
Packit 667938
	# sets the number of configs loaded, for error testing
Packit 667938
	MAXLINE=`wc -l "$CFG" | cut -d" " -f7 `
Packit 667938
Packit 667938
	clear
Packit 667938
	printf "\n\nThe following MRTG configs are running on "$MCNAME":\n"
Packit 667938
Packit 667938
	printf "\n\tConfig name\t\tRunning Since\n"
Packit 667938
	printf "\t-----------\t\t-------------\n"
Packit 667938
	
Packit 667938
Packit 667938
		# prints out the appropriate fields on-screen
Packit 667938
		nawk ' { print "\t" $1,$8".cfg \t\t" $5,$6,$7 } ' "$CFGNUM"
Packit 667938
		printf "\n"	
Packit 667938
		printf " Which file would you like to reload? (b to back-up, q to quit) \c"
Packit 667938
		read X
Packit 667938
Packit 667938
	case "$X" in
Packit 667938
	[qQ] )
Packit 667938
		EXIT=Y # I quit!
Packit 667938
	;;
Packit 667938
Packit 667938
	[bB] )
Packit 667938
		printf "\n Backup *.cfg & *.log files? \c"
Packit 667938
		read V
Packit 667938
		case "$V" in
Packit 667938
		'y' | 'Y' )
Packit 667938
			
Packit 667938
			CNAME=MRTG_"$DATE".cfgs.tar
Packit 667938
			tar cvf "$TARHOME"/"$CNAME" "$MRTG"/*.cfg
Packit 667938
			chmod 0400 "$TARHOME"/"$CNAME"
Packit 667938
			LNAME=MRTG_"$DATE".logs.tar
Packit 667938
			find "$WEBHOME" | egrep  -e "\.log$|\.old$" > mrtgtar.run
Packit 667938
			tar cvf "$LNAME" -I mrtgtar.run
Packit 667938
			chmod 0600 "$TARHOME"/"$CNAME"
Packit 667938
			chmod 0600 "$TARHOME"/"$LNAME"
Packit 667938
		#	printf "\nGzipping files..."
Packit 667938
		#	gzip "$TARHOME"/"$CNAME" 
Packit 667938
		#	gzip "$TARHOME"/"$LNAME" 
Packit 667938
			printf "\nConfig files backed up to `ls "$TARHOME"/MRTG_"$DATE".cfg*` "
Packit 667938
			printf "\nLog files backed up to `ls "$TARHOME"/MRTG_"$DATE".log*` "
Packit 667938
			printf "\nPress return to continue..."
Packit 667938
			read U
Packit 667938
			;;
Packit 667938
		* )
Packit 667938
			printf "\nBackup aborted."
Packit 667938
					sleep 3
Packit 667938
			;;
Packit 667938
		esac
Packit 667938
	;;
Packit 667938
Packit 667938
	[0-9][0-9] ) # If the number entered is between 0 and 99...
Packit 667938
Packit 667938
		# and if the number is <= the no. of configs loaded
Packit 667938
		if [ "$X" -le "$MAXLINE" ] # 
Packit 667938
		then
Packit 667938
				
Packit 667938
		FILE=`sed -n ''$X'p' "$CFGNUM" | cut -d" " -f11 ` # put filename into $FILE
Packit 667938
Packit 667938
		echo " Okay to restart "$FILE".cfg? \c"	
Packit 667938
		read Y
Packit 667938
Packit 667938
		case "$Y" in 
Packit 667938
		'y' | 'Y' )
Packit 667938
			FKILL=`cat "$MRTG""/""$FILE"".pid"` # Set the variable with the PID
Packit 667938
			kill -9 $FKILL	 			# Kill it 
Packit 667938
			rm $MRTG/$FILE".pid"			# Remove the file
Packit 667938
			$MRTG"/mrtg" $FILE".cfg"		# Restart MRTG with the config file
Packit 667938
			sleep 8					# Wait for new PID file to be created
Packit 667938
			echo "New PID for "$FILE".cfg is `cat "$MRTG"/"$FILE".pid` "
Packit 667938
		;;
Packit 667938
		*)
Packit 667938
			echo "\nRestart aborted!"
Packit 667938
		;;
Packit 667938
		esac
Packit 667938
		else
Packit 667938
			echo "Entry Invalid! Press return to continue..."
Packit 667938
			read ZZ
Packit 667938
		fi
Packit 667938
	;;
Packit 667938
	*)
Packit 667938
		echo "Entry Invalid! Press return to continue..."
Packit 667938
                read ZZ
Packit 667938
	;;
Packit 667938
	esac
Packit 667938
Packit 667938
done
Packit 667938
	# For security/housekeeping reasons, we clean out the temp files here.
Packit 667938
	cat /dev/null >	 "$MRTG"/mrtgcfgs.run
Packit 667938
	cat /dev/null >	 "$MRTG"/cfgnum.run
Packit 667938
	cat /dev/null >	 "$MRTG"/cfgsed.run
Packit 667938
	cat /dev/null >	 "$MRTG"/mrtgtar.run
Packit 667938
exit 0