#!/bin/bash
# Copyright 2015 Lucid Networks sloeckle

source /etc/default/nexp-tools
export DISPLAY=:0
export EMAIL=$HOSTNAME'@lucidnetworks.net'
repeatinterval=30
time=$(date +"%m-%d-%Y_%T")
rand=$(od -N 4 -t uL -An /dev/urandom | tr -d " ") && echo $rand
dir=/tmp/$rand
ipfulleth0=$(ip addr show eth0 | grep inet)
ipaddreth0=$(echo $ipfulleth0 | awk '{print $2}')
ipfulltun0=$(ip addr show tun0 | grep inet)
ipaddrtun0=$(echo $ipfulltun0 | awk '{print $2}')
networkinfo="N-EXP Network Information for $HOSTNAME:\n\teth0: $ipaddreth0\n\ttun0: $ipaddrtun0"
mkdir $dir &>/dev/null
scrot $dir/$time.png &>/dev/null
convert $dir/$time.png -colorspace Gray $dir/$time.tif
ocr=$(tesseract $dir/$time.tif stdout)
text="$ocr\n\n\n$networkinfo\n"

## Google browser specific error checks

if [[ $ocr == *"Aw, Snap"* || $ocr == *"Aw,Snap"* || $ocr == *"Aw‘ Snap‘"* ]]
then
  echo -e $text | mutt -s "$HOSTNAME Google Aw, Snap! Crash. Taking Action!" -a $dir/$time.png -- $MAILTO
  killall chrome &>/dev/null
  /usr/bin/google-chrome $CHROMEOPTS file:///usr/share/nexp-tools/nexp.jpg &
  for i in {15..0}; do echo -ne "Waiting: $i "'\r'; sleep 1; done; echo
  time=$(date +"%m-%d-%Y_%T")
  scrot $dir/$time.png &>/dev/null
  convert $dir/$time.png -colorspace Gray $dir/$time.tif
  ocr=$(tesseract $dir/$time.tif stdout)
  text="$ocr\n\n\n$networkinfo\n"
  echo -e $text | mutt -s "$HOSTNAME Google Aw, Snap! Crash. Action Completed!" -a $dir/$time.png -- $MAILTO
fi

if [[ $ocr == *"Shockwave Flash has crash"* ]]
then
  echo -e $text | mutt -s "$HOSTNAME Google Shockware Flash Crash. Taking Action!" -a $dir/$time.png -- $MAILTO
  killall chrome &>/dev/null
  /usr/bin/google-chrome $CHROMEOPTS file:///usr/share/nexp-tools/nexp.jpg &
  for i in {15..0}; do echo -ne "Waiting: $i "'\r'; sleep 1; done; echo
  time=$(date +"%m-%d-%Y_%T")
  scrot $dir/$time.png &>/dev/null
  convert $dir/$time.png -colorspace Gray $dir/$time.tif
  ocr=$(tesseract $dir/$time.tif stdout)
  text="$ocr\n\n\n$networkinfo\n"
  echo -e $text | mutt -s "$HOSTNAME Google Shockware Flash Crash. Action Completed!" -a $dir/$time.png -- $MAILTO
fi

## Web server error checks

if [[ $ocr == *"500 Internal Server Error"* ]]
then
  echo -e $text | mutt -s "$HOSTNAME 500 Internal Server Error. Taking Action!" -a $dir/$time.png -- $MAILTO
  killall chrome &>/dev/null
  /usr/bin/google-chrome $CHROMEOPTS file:///usr/share/nexp-tools/nexp.jpg &
  for i in {15..0}; do echo -ne "Waiting: $i "'\r'; sleep 1; done; echo
  time=$(date +"%m-%d-%Y_%T")
  scrot $dir/$time.png &>/dev/null
  convert $dir/$time.png -colorspace Gray $dir/$time.tif
  ocr=$(tesseract $dir/$time.tif stdout)
  text="$ocr\n\n\n$networkinfo\n"
  echo -e $text | mutt -s "$HOSTNAME 500 Internal Server Error. Action Completed!" -a $dir/$time.png -- $MAILTO
fi

if [[ $ocr == *"An error occurred."* && $ocr == *"nginx"* ]]
then
  echo -e $text | mutt -s "$HOSTNAME Unknown NGINX Server Error. Taking Action!" -a $dir/$time.png -- $MAILTO
  killall chrome &>/dev/null
  /usr/bin/google-chrome $CHROMEOPTS file:///usr/share/nexp-tools/nexp.jpg &
  for i in {15..0}; do echo -ne "Waiting: $i "'\r'; sleep 1; done; echo
  time=$(date +"%m-%d-%Y_%T")
  scrot $dir/$time.png &>/dev/null
  convert $dir/$time.png -colorspace Gray $dir/$time.tif
  ocr=$(tesseract $dir/$time.tif stdout)
  text="$ocr\n\n\n$networkinfo\n"
  echo -e $text | mutt -s "$HOSTNAME Unknown NGINX Server Error. Action Completed!" -a $dir/$time.png -- $MAILTO
fi

## Sprinklr specific error checks

if [[ $ocr == *"Command Center > Settings > Playlisx"* || $ocr == *"Command Cemer > Settings > Playlisl"* || $ocr == *"Command Center > Settings Seavch"* ]]
then
  echo -e $text | mutt -s "$HOSTNAME Stuck at Sprinklr Playlist. Taking Action!" -a $dir/$time.png -- $MAILTO
  killall chrome &>/dev/null
  /usr/bin/google-chrome $CHROMEOPTS file:///usr/share/nexp-tools/nexp.jpg &
  for i in {15..0}; do echo -ne "Waiting: $i "'\r'; sleep 1; done; echo
  time=$(date +"%m-%d-%Y_%T")
  scrot $dir/$time.png &>/dev/null
  convert $dir/$time.png -colorspace Gray $dir/$time.tif
  ocr=$(tesseract $dir/$time.tif stdout)
  text="$ocr\n\n\n$networkinfo\n"
  echo -e $text | mutt -s "$HOSTNAME Stuck at Sprinklr Playlist. Action Completed!" -a $dir/$time.png -- $MAILTO
fi
if [[ $ocr == *"create your ﬁrsl Dashboard"* ]]
then
  echo -e $text | mutt -s "$HOSTNAME Sprinklr Create Your First Dashboard Problem!" -a $dir/$time.png -- $MAILTO
fi
if [[ $ocr == *"Your accoum has been locked"* ]]
then
  if [ $REPEATNOTIFICATION == true ]; then
	  if [ -f /var/run/nexp-tools/sprinklraccountlock ]; then
		source /var/run/nexp-tools/sprinklraccountlock
		errorcount=$((errorcount+1))
		echo "errorcount=$errorcount" > /var/run/nexp-tools/sprinklraccountlock
			if [ $errorcount -ge $repeatinterval ]; then
				echo -e $text | mutt -s "$HOSTNAME Sprinklr Account Locked Error!"  -a $dir/$time.png -- $MAILTO
				echo "errorcount=1" > /var/run/nexp-tools/sprinklraccountlock
			else
				echo "errorcount=$errorcount" > /var/run/nexp-tools/sprinklraccountlock
			fi
	  else
		echo "errorcount=1" > /var/run/nexp-tools/sprinklraccountlock
		echo -e $text | mutt -s "$HOSTNAME Sprinklr Account Locked Error!"  -a $dir/$time.png -- $MAILTO
	  fi
  else
	if [ ! -f /var/run/nexp-tools/sprinklraccountlock ]; then
		echo "errorcount=1" > /var/run/nexp-tools/sprinklraccountlock
		echo -e $text | mutt -s "$HOSTNAME Sprinklr Account Locked Error!"  -a $dir/$time.png -- $MAILTO
	else
		source /var/run/nexp-tools/sprinklraccountlock
		errorcount=$((errorcount+1))
		echo "errorcount=$errorcount" > /var/run/nexp-tools/sprinklraccountlock
	fi
  fi		
else
  if [ -f /var/run/nexp-tools/sprinklraccountlock ]; then
	source /var/run/nexp-tools/sprinklraccountlock
	text+="\tError iteration = $errorcount\n"
	echo -e $text | mutt -s "$HOSTNAME Sprinklr Account Locked Resolved!"  -a $dir/$time.png -- $MAILTO
	rm /var/run/nexp-tools/sprinklraccountlock &>/dev/null			
  fi
fi

rm -rf $dir &>/dev/null
