#!/bin/bash

###############################################################################
# gentcucci - Generate Total Control User Count (and temp) Config and Index
#             (pronounced like Gent Cookie) an MRTG Tool
#
# Version 1.0.0
#
# Copyright and written by Kevin Benton, 1999.
#
# Please be sure that if you choose to use this code that you let Tobi or
# myself know that it's useful to you.  Tobi - thanks for writing MRTG and
# making it freely available to those of us who weren't quite so creative.
#
# NOTE 1 - At the time of this writing, we've been using this script for over
# a year and it's worked like a champ.  Don't let the version number fool
# you - this stuff is tried and tested.
#
# NOTE 2 - Please be sure to pick the target method below.  Look for
# "INTENTIONAL ERROR" in this file, uncomment the appropriate line and delete
# the two "error" lines as directed.
#
# NOTE 3 - This could have been written in Perl to make it a lot faster, but at
# the time I wrote it, I didn't know enough Perl to do it.  I certainly didn't
# feel like spending the time to write it in C.  I may soon rewrite this
# entire script in Perl not only to make it faster, but to clean things as far
# as temp files go and what not.
#
# NOTE 4 - tcf2mrtg returns the number of users on-line for a particular
# target.  This works on both ARCs and NetServers and gives us the ability to
# split apart DSP's.  It can of course be adapted to whatever your platform
# needs require.  If you only need data for the HARC aggregated together, use
# the MIB .1.3.6.1.4.1.429.4.2.1.10.0 or whatever is current for the HARC code
# you're using.
###############################################################################

###############################################################################
# store in users index file

stf() {
  echo "$1" >>$TEMP_FILE
}

###############################################################################
# store in temperature index file

stfd() {
  echo "$1" >>$TEMP_FILE2
}

###############################################################################
# store in temperature config

stod() {
  echo "$1" >>$degree_file
}

###############################################################################
# store in users config

stou() {
  echo "$1" >>$output_file
}

###############################################################################

########################## CONFIGURATION PARAMETERS ###########################
#    output_dir is where MRTG will be storing configs.  A directory www should
#    exist under that with the directories tcusercount and tcdegree below that.
export output_dir=/usr/local/mrtg-2.8.12/local-config
#    output_file is the MRTG user count config file.
export output_file=$output_dir/mrtg.tcusercount.cfg
#    degree_file is the MRTG chassis temperature config file.
export degree_file=$output_dir/mrtg.tcdegree.cfg
#    ro_comm is the read only SNMP community to use to grab data with.
export ro_comm=public
########################## CONFIGURATION PARAMETERS ###########################

export TEMP_FILE=/tmp/gentcuc.$$
export TEMP_FILE2=/tmp/gentcuc.$$.2
rm -f $TEMP_FILE 2>/dev/null >/dev/null
trap "rm -f $TEMP_FILE $TEMP_FILE2 2>/dev/null >/dev/null ; exit" 1 2 15
touch $TEMP_FILE

index=${output_dir}/www/tcusercount/index.htm
bigindex=${output_dir}/www/tcusercount/bigindex.htm
degreeindex=${output_dir}/www/tcdegree/index.htm
bigdegreeindex=${output_dir}/www/tcdegree/bigindex.htm

# Create mrtg.tcusercount.cfg header
cat >$output_file <<SH_EOF1
##################################################################
# CAUTION: This file is automatically generated.  Do not edit.   #
#	   Changes should be made in gwcs.list only.             #
##################################################################

Workdir:    $output_dir
Refresh:      300
Interval:     5
WriteExpires: Yes

Directory[^]: www/tcusercount/
PageTop[^]: 
PageTop[\$]: Total Control Chassis User Count
Options[^]: gauge noinfo
YLegend[^]: Users On
ShortLegend[^]: Users
Title[$]: Total Control Chassis User Count
XZoom[^]: 2.0
YZoom[^]: 2.0

SH_EOF1

# Create mrtg.tcdegree.cfg header
cat >$degree_file <<SH_EOF2
##################################################################
# CAUTION: This file is automatically generated.  Do not edit.   #
#	   Changes should be made in nmcs.list only.             #
##################################################################

Workdir:    $output_dir
Refresh:      300
Interval:     5
WriteExpires: Yes
Directory[^]: www/tcdegree/
PageTop[^]: 
PageTop[\$]: Total Control Chassis Temperature
Options[^]: gauge noinfo nopercent
YLegend[^]: Fahrenheit
ShortLegend[^]: Temp
Title[$]: Total Control Chassis Temperature
XZoom[^]: 2.0
YZoom[^]: 2.0

SH_EOF2

# Create master index temp file
stf ""
stf "Total Control SED_REPLACE_WITH_NAME"
stf ""
stf "
Total Control SED_REPLACE_WITH_NAME
"
stf "Updated `date`<P>"
stf "Click here for SED_REPLACE_WITH_SIZE size images
"
stf "
"

# Process into mrtg.tcusercount.cfg and master user count index from gwcs.list
while read line
do
  if echo "$line" | egrep '^#' >/dev/null 2>/dev/null
  then
    continue
  fi
  if [ -z "$line" ]
  then
    continue
  fi
  target="`echo \"$line\" | cut -d: -f1 | sed -e 's/+/ /g'`"
  tag="`echo \"$line\" |         \
        cut -d: -f1 |            \
        sed -e 's/+/_/g' |       \
        sed -e 's/\_.*\@/_/g' |  \
        cut -d@ -f2`"
  title="`echo \"$line\" | cut -d: -f2`"
  maxb="`echo \"$line\" | cut -d: -f3`"
  absm="`echo \"$line\" | cut -d: -f4`"
INTENTIONAL ERROR: YOU MUST PICK 1 OF THE 2 TARGETS BELOW AND DELETE THIS LINE
#  stou "Target[$tag]: \`/usr/local/mrtg-2.8.12/local-config/tcf2mrtg \"$target\"\`"
#  stou "Target[$tag]: .1.3.6.1.4.1.429.4.2.1.10.0:${ro_comm}@$target"
INTENTIONAL ERROR: YOU MUST PICK ONE OF THE TARGETS ABOVE AND DELETE THIS LINE
  stou "Title[$tag]: $title"
  stou "PageTop[$tag]: $title"
  if [ ! "$absm" = "0" ]
  then
    stou "AbsMax[$tag]: $absm"
  fi
  stou "MaxBytes[$tag]: $maxb"
  stou "WithPeak[$tag]: ymwd"
  stou ""
  stf "  "
  stf "     "
  stf "        
$title
"
  stf "        `/home/httpd/cgi-bin/tcnowstat \"$tag\" \"$maxb\"`
"
  stf "         Potential Users: $maxb
"
  stf "     Today
5 Min"
  stf "     "
  stf "      "
  stf "    "
  stf "  
"
  stf "  "
  stf "     This Week
30 Min"
  stf "     "
  stf "      "
  stf "    "
  stf "  
"
done <gwcs.list

stfd ""
stfd "Total Control SED_REPLACE_WITH_NAME"
stfd ""
stfd "
Total Control SED_REPLACE_WITH_NAME
"
stfd "Updated `date`<P>"
stfd "Click here for SED_REPLACE_WITH_SIZE size images
"
stfd "
"

# Process into mrtg.tcusercount.cfg and master user count index from gwcs.list
while read line
do
  if echo "$line" | egrep '^#' >/dev/null 2>/dev/null
  then
    continue
  fi
  if [ -z "$line" ]
  then
    continue
  fi
  name="`echo \"$line\" | cut -d: -f1 | sed -e 's/+/ /g'`"
  tag="`echo \"$line\" | cut -d: -f1 | sed -e s/+/_/g`"
  title="`echo \"$line\" | cut -d: -f2`"
  stod "##################################################################"
  stod "Target[$tag]: .1.3.6.1.4.1.429.1.2.2.5.0&.1.3.6.1.4.1.429.1.2.2.5.0:public@$name * 1.8 + 32"
  stod "Title[$tag]: $title"
  stod "PageTop[$tag]: $title"
  stod "MaxBytes[$tag]: 200"
  stfd "     "
  stfd "        
$title
"
  stfd "        `/home/httpd/cgi-bin/tcnowtemp \"$tag\"`
"
  stfd "     Today
5 Min"
  stfd "     "
  stfd "      "
  stfd "    "
  stfd "  
"
  stfd "  "
  stfd "     This Week
30 Min"
  stfd "     "
  stfd "      "
  stfd "    "
  stfd "  
"
done <nmcs.list

# close the indexes up tight
stf "
"
stfd "
"

# create normal and big indexes for user count and temperature.
cat $TEMP_FILE | sed -e 's/SED_REPLACE_WITH_INDEX.HTM/bigindex.htm/g'   | \
                 sed -e 's/SED_REPLACE_WITH_SIZE/double/g'              | \
                 sed -e 's/SED_REPLACE_WITH_NAME/User Count Analysis/g' | \
                 sed -e 's/SED_REPLACE_WITH_PICSIZE//g' >$index
cat $TEMP_FILE | sed -e 's/SED_REPLACE_WITH_INDEX.HTM/index.htm/g'      | \
                 sed -e 's/SED_REPLACE_WITH_SIZE/normal/g'              | \
                 sed -e 's/SED_REPLACE_WITH_NAME/User Count Analysis/g' | \
         sed -e 's/SED_REPLACE_WITH_PICSIZE/WIDTH=1000 HEIGHT=270/g' >$bigindex
cat $TEMP_FILE2 | sed -e 's/SED_REPLACE_WITH_INDEX.HTM/bigindex.htm/g'   | \
                  sed -e 's/SED_REPLACE_WITH_SIZE/double/g'              | \
                  sed -e 's/SED_REPLACE_WITH_NAME/Chassis Temperature/g' | \
                  sed -e 's/SED_REPLACE_WITH_PICSIZE//g' >$degreeindex
cat $TEMP_FILE2 | sed -e 's/SED_REPLACE_WITH_INDEX.HTM/index.htm/g'      | \
                  sed -e 's/SED_REPLACE_WITH_SIZE/normal/g'              | \
                  sed -e 's/SED_REPLACE_WITH_NAME/Chassis Temperature/g' | \
   sed -e 's/SED_REPLACE_WITH_PICSIZE/WIDTH=1000 HEIGHT=270/g' >$bigdegreeindex
rm $TEMP_FILE $TEMP_FILE2
