#!/bin/ksh

#############################################################################
##
## iMS monitoring and alarm script
##
## This script is part an example framework of what can be done.
## There are MANY ways that you can take this from here.
##
## File: $RMTALogParser
##	 A script to parse the mail.log files
##
## How to use this script:
##
## Do not call this script directly unless you have defined the following 
## input variables - INDEX_FILE BODY_FILE TOTAL_FILE TEMP_FILE 
## and LOGFILE
#############################################################################

$AWK '{
           if (log_process == 1 ) {
               if (($6 ~/E/) && ($4 !~ />/) && ($5 ~/ims-ms/)) {
                   printf("%s:%s_E:%s\n", $2,$5,$7)
                   channel["ims-ms_E"]++
               } else if (($6 ~/E/) && ($5 !~ /ims-ms/)) {
                   printf("%s:%s:%s\n", $2,$5,$7)
                   channel[$5]++
               } else if (($5 ~/D/) && ($4 !~ />/) && ($4 ~/ims-ms/)) {
                   printf("%s:%s_D:%s\n", $2,$4,$6) 
                   channel["ims-ms_D"]++
               }
           } else {
              if (($5 ~/E/) && ($3 !~ />/) && ($4 ~/ims-ms/)) {
                  printf("%s:%s_E:%s\n", $2,$4,$6)
                  channel["ims-ms_E"]++
              } else if (($5 ~/E/) && ($3 !~ />/)) {
                  printf("%s:%s:%s\n", $2,$4,$6)
                  channel[$4]++
              } else if (($4 ~/D/) && ($3 !~ />/) && ($3 ~/ims-ms/)) {
                  printf("%s:%s_D:%s\n", $2,$3,$5)
                  channel["ims-ms_D"]++
              } 
           }
        }
        END {
            i = 0
            for (channel_name in channel)
                i++
            print i > index_file
            for (channel_name in channel)
                print channel_name >> index_file
        }' index_file="$INDEX_FILE" log_process="$LOGOPTION" $LOGFILE | \
        $AWK '{FS=":"
              tens_Units = $2/10
              split(tens_Units, digits, ".") 
	      hh = $1
	      if (hh < 10) {
              		if (digits[2] < 5 ) {
              		    printf("%s:%1d0 %s %s\n", $1, ($2/10), $4, $5)
              		} else { 
              		    printf("%s:%1d5 %s %s\n", $1, ($2/10), $4, $5) 
              		}
              } else { 
              		if (digits[2] < 5 ) {
              		    printf("%2d:%1d0 %s %s\n", $1, ($2/10), $4, $5)
              		} else { 
              		    printf("%2d:%1d5 %s %s\n", $1, ($2/10), $4, $5) 
              		}
              }
        }' index_file="$INDEX_FILE" >> $TEMP_FILE

	grep -v ^: $TEMP_FILE > ${TEMP_FILE}.tmp
	mv ${TEMP_FILE}.tmp ${TEMP_FILE}

        $AWK '{
            time[$1]++
        }
        END {
            i = 0
            for (increment in time)
                i++
            print i >> index_file
            for (increment in time)
                print increment >> index_file
        }' index_file="$INDEX_FILE" $TEMP_FILE


        $AWK -v index_file="$INDEX_FILE" 'BEGIN {
                  getline < index_file
                  num_channels = $1
                  for (i = 0; i < num_channels; i++) {
                      getline < index_file
                      channel_names[$1] = $1
                  }
                  getline < index_file
                  num_increment = $1
                  for (i = 0; i < num_increment; i++) {
                      getline < index_file
                      increment[$1] = $1
                  }
              }
              {
                      channel_count[$2, $1]++
              }
              END {
                 printf("QUEUE")
                 for (channel in channel_names)
                     printf(" %9s", channel)
                 printf("     TOTAL\n")
                 for (time in increment) {
                     printf("%5s", time) >> body_file
                     bucket_total = 0
                     for (channel in channel_names) {
                         if (!channel_count[channel, time])
                             printf("         0") >> body_file
                         else {
                             printf(" %9s", channel_count[channel, time]) >> body_file
                             total[channel] = total[channel] + channel_count[channel, time]
                         }
                         if (channel != "ims-ms_D")
                             bucket_total = bucket_total + channel_count[channel, time]
                     }
                     printf(" %9s\n", bucket_total) >> body_file
                     total[0] = total[0] + bucket_total
                 }
                 printf("TOTAL") > total_file
                 for (channel in channel_names) {
                     printf(" %9s", total[channel]) >> total_file
                 }
                 printf(" %9s\n", total[0]) >> total_file
             }' body_file="$BODY_FILE" total_file=$TOTAL_FILE $TEMP_FILE >> $SMTP_FILE

        touch $BODY_FILE ; sort $BODY_FILE >> $SMTP_FILE
        cat $TOTAL_FILE >> $SMTP_FILE
        printf "\n"  >> $SMTP_FILE

        $AWK -v index_file="$INDEX_FILE" 'BEGIN {
                  getline < index_file
                  num_channels = $1
                  for (i = 0; i < num_channels; i++) {
                      getline < index_file
                      channel_names[$1] = $1
                  }
              }
              {
                  if ($3 < 10) {
                      offset=$3
                      ++size[$2, offset]
                  } else if ($3 < 100) {
                      offset=$3/10 + 10
                      ++size[$2, offset]
                  } else if ($3 < 1000) {
                      offset=$3/100 + 20
                      ++size[$2, offset]
                  } else if ($3 < 10000) {
                      offset=$3/1000 +30
                      ++size[$2, offset]
                  } else
                      ++size[$2, 40]
             }
             END {
                 fact=1
                 offset=1
                 printf("  SIZE (KB)|")
                 for (channel in channel_names)
                     printf(" %9s", channel)
                 printf("      TOTAL\n")
                 for (x=1; x<5; x++) {
                     for (y=1; y<10; y++) {
                         total = 0
                         printf("%5s-%5s|", y*fact, (y+1)*fact)
                         for (channel in channel_names) {
                             if (!size[channel, offset])
                                 printf("         0")
                             else
                                 printf(" %9s", size[channel, offset])
                             total = total + size[channel, offset]
                         }
                         printf(" %9s\n", total)
                         ++offset
                     }
                     ++offset
                     fact=fact*10
                 }
                 total = 0
                 printf("10000+     |", y*fact, (y+1)*fact)
                 for (channel in channel_names) {
                     if (!size[channel, 40])
                         printf("         0")
                     else
                         printf(" %9s", size[channel, 40])
                     total = total + size[channel, 40]
                 }
                 printf(" %9s\n", total)
             }' $TEMP_FILE >> $SMTP_FILE

     	## now send the report file over

    	# $smtpclient -o $alarmsender -r $queuenotifyaddress -s "$SUBJECT_SMTP" -f $SMTP_FILE

    	##
    	## cleanup the temp files 
    	##

     	rm -f $INDEX_FILE 
	rm -f $BODY_FILE 
	rm -f $TOTAL_FILE
	rm -f $TEMP_FILE 

## end of script
