Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / bin / releasetools / notify_err
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         notify_err
5 # Description:  Checks the log files for errors and notifies the appropriate
6 #               component owners
7 # Author:       zran@salira.com - modified by Andrew@DeFaria.com
8 # Created:      Fri Jun 27 12:42:28 PDT 2003
9 # Language:     bash
10 # Modification: 
11 #
12 # (c) Copyright 2001-2003, Salira Optical Network Systems, all rights reserved.
13 #
14 ################################################################################
15 # Set me to command name
16 me=$(basename $0)
17
18 # Source /etc/site_parms
19 if [ -f /etc/site_parms ]; then
20   . /etc/site_parms
21 else
22   echo "$me: WARNING: /etc/site_parms does not exist!"
23 fi
24
25 # Set adm_base
26 adm_base="$SITE_TOOLS_PATH/adm"
27
28 # Set adm_fpath
29 adm_fpath=${adm_fpath:-$adm_base/functions}
30
31 # Source functions
32 . $adm_fpath/common
33
34 owner_list="$SITE_TOOLS_PATH/adm/data/owner_list"
35 ssmtp=/usr/sbin/ssmtp
36
37 # Get cards definition
38 . $SITE_TOOLS_PATH/adm/etc/cards
39
40 function usage {
41   if [ ! -z "$1" ]; then
42     error "$1\n"
43   fi
44   display "Usage: $me <cards to check>"
45   display
46   display "Where <cards to check> are a list of cards to check (default: all cards)"
47   exit 1
48 } # usage
49
50 notify_cc="$SITE_ADMIN_EMAIL"
51
52 # This function composes the email header and then appends the portion
53 # of the logfile as the body.
54 function send_email {
55   subject="$1"
56   component="$2"
57   logfile="$3"
58   err_lines="$4"
59
60   # Determine who owns this component
61   grep -qi "^$component" $owner_list
62
63   if [ $? -ne 0 ]; then 
64     warning "Unknown component $component"
65     notify_to=$notify_cc
66   else
67     if [ "$SITE_NAME" = "US" ]; then
68       owner_email=$(grep -i "^$component        " $owner_list | awk '{print $2}')"@salira.com"
69     else
70       owner_email=$(grep -i "^$component        " $owner_list | awk '{print $3}')"@salira.com"
71     fi
72     notify_to=$owner_email
73   fi
74
75   # Compose header
76   echo "From: ccadmin"           > /tmp/$me.msg
77   echo "To: $notify_to"         >> /tmp/$me.msg
78   echo "cc: $notify_cc"         >> /tmp/$me.msg
79   echo "Subject: $subject"      >> /tmp/$me.msg
80        
81   # Append section of logfile
82   tail -$err_lines $logfile >> /tmp/$me.msg
83
84   # Mail it
85   cat /tmp/$me.msg | $ssmtp -t
86
87   if [ $? -eq 0 ]; then
88     verbose "Sent email To: $notify_to Cc: $notify_cc"
89     verbose "Re: $subject"
90     rm -f /tmp/$me.msg
91   else
92     echo "Unable to send email. Message left in /tmp/$me.msg"
93     exit 1
94   fi
95 } # send_email
96
97 # This function returns the number of lines from the end of the logfile
98 # that must be output to show the error. Input is the logfile name and
99 # the search line which denotes the area to start.
100 function error_lines {
101   logfile="$1"
102   search_line="$2"
103
104   # Determine the line number of the start of the error lines
105   startline=$(grep -n "$search_line" $logfile | cut -f1 -d':')
106
107   # Determine the number of lines in logfile
108   endline=$(wc -l $logfile | awk '{print $1}')
109
110   # Calculate the error lines
111   err_lines=$(($endline-$startline+1))
112   
113   # Trim area to no more than 50 lines
114   if (( $err_lines > 50 )); then
115     err_lines=50
116   fi
117
118   return $err_lines
119 } # error_lines
120
121 function check_error {
122   card="$1"
123
124   # Set logfile
125   logfile=$card.build.log
126
127   # If logfile doesn't exist then no error so simply return
128   if [ ! -f $logfile ]; then
129     verbose "Unable to find logfile for card $card"
130     return
131   fi
132
133   # Check for successful build
134   grep -q "^Packing.*superfile" $logfile
135
136   if [ $? -eq 0 ]; then
137     verbose "$card built successfully!"
138     return
139   fi
140
141   grep -q "Timestamp & Version" $logfile
142
143   if [ $? -ne 0 ]; then
144     verbose "$card compile failure"
145     # Loop through the logfile scanning for the "----" line which
146     # denotes a component. If there is an error then we are interested
147     # in the last component so save that.
148     while read line dir component; do 
149       if [ "$line" = "--------------------------------------" ]; then
150         hitline="$dir $component"
151         hitdir=$dir
152         hitcomponent=$component
153
154         # Not sure why we are trying bsp special...
155         if [ "$hitdir" = "(neopon/src/bsp)" ]; then
156           hitcomponent=bsp
157         fi
158       fi
159     done < $logfile
160
161     error_lines $logfile "$hitline"
162     err_lines=$?
163
164     send_email "Build of view: $view_tag Card: $card Component $hitcomponent failed to compile" $hitcomponent $logfile $err_lines
165   else # link error
166     verbose "$card link failure"
167     error_lines $logfile "Timestamp & Version"
168     err_lines=$?
169
170     send_email "Build of view: $view_tag Card: $card failed to link" link $logfile $err_lines
171   fi
172 } # check_error
173
174 # Get parameters
175 while [ $# -ge 1 ]; do
176   case "$1" in
177     -v|-verbose)
178       verbose="yes"
179     ;;
180
181     -d|-debug)
182       debug="yes"
183     ;;
184
185     -u|-usage)
186       usage
187       ;;
188
189     *)
190       cards_to_check="$cards_to_check $1"
191   esac
192   shift
193 done
194
195 # What view are we in?
196 view_tag=$(cleartool pwv -short)
197
198 if [ ! -z "$cards_to_check" ]; then
199   for card in $cards_to_check; do
200     card=$(echo $card | tr [:lower:] [:upper:])
201     verbose "Checking card $card..."
202     check_error $card
203   done
204 else
205   for card in $cards; do
206     card=$(echo $card | tr [:lower:] [:upper:])
207     verbose "Checking card $card..."
208     check_error $card
209   done
210 fi