#!/bin/sh
#$Id: sm_notify,v 1.13 2013/08/26 16:43:26 sesam Exp $
#******************************************************************************
#
# File      : sm_notify
# Copyright : (c) SEP AG  D-83607 Holzkirchen
# Project   : SESAM Backup Utility
# Date      : 2017-05-16
#
#******************************************************************************
#
# Description : Template for the SESAM notify interface.
#               It is called from SESAM after finishing a backup, media change
#               media init or SESAM day change.
#
# Parameters  : $1 = Calling module see below
#               $2 = message text
#
# Modules     : ALL_JOBS_DONE
#                 Will be triggered after the last planned Sesam event of the Sesam day
#                 has been finished. Serves as KeepAlive report and Sesam day summary.
#               INIT
#                 A initialize (preparation) of a medium for backup has been finished succesfully.
#               GET_VOLUME
#                 A medium has been requested for backup and the operation has been
#                 finished successfully.
#               BACKUP
#                 A backup process has been finished successfully.
#               BACKUP_GROUP
#                 After the last backup task of a taskgroup has been finished successfully.
#               RESTORE
#                 A restore process has been finished successfully.
#               COPY
#                 A migration operation has been finished successfully. 
#                 A migration operation contains one or more saveset migration processes.
#               COPY_SAVESET
#                 A migration task process has been finished successfully. 
#                 This event will be triggerd on migration of each saveset 
#                 in a migration oparation.
#               COMMAND
#                 A Sesam command event has been finished
#               NEWDAY
#                 A Sesam NewDay event has been started
#               SM_NEWDAY
#                 A Sesam NewDay event has been finished
#
#     This is a user programable shell script with 2 parameters.
#     Please insert your specific code-lines after this comment.
#     Take care of not inserting any command waiting for an answer from
#     the user ( OK-buttons etc. ) - this may cause a complete blocking of
#     backups during the night.
#
#*******************************************************************************
# Beschreibung: Dies ist die Notify-Schnittstelle der Sesam Sicherungssoftware.
#               Sie wird von unterschiedlichen Modules aufgerufen, wenn die
#               Abarbeitung erfolgreich war.
#
# Parameter   : $1 = Aufrufendes Modul, siehe unten
#               $2 = Textmeldung
#
# Module      : ALL_JOBS_DONE
#                 Wird ausgeloest nach Beendigung des letzten geplanten Events des 
#                 Sesam-Tages. Dient als KeepAlive-Meldung und Tageszusammenfassung.
#               INIT
#                 Das Initialisieren (Bereitstellen) eines Mediums wurde erfolgreich beendet.
#               GET_VOLUME
#                 Ein Medium wird wird zum Backup angefordert (z.B. Einlegen des Bandes
#                 in ein Bandlaufwerk).
#               BACKUP
#                 Ein Backup-Prozess wurde erfolgreich beendet.
#               BACKUP_GROUP
#                 Nachdem der letzte Backup-Job einer Auftragsgruppe beendet wurde.
#               RESTORE
#                 Ein Restore-Prozess wurde erfolgreich beendet.
#               COPY
#                 Eine Migrations-Operation wurde erfolgreich beendet. 
#                 Eine Migrations-Operation besteht aus einer oder mehreren Saveset Migrationen.
#               COPY_SAVESET
#                 Die Migration eines Savesets wurde erfolgreich abgeschlossen. 
#                 Dieses Event wird bei jedem Saveset einer Migrations-Operation ausgeloest.
#               COMMAND
#                 Ein Sesam Command wurde beendet.
#               NEWDAY
#                 Ein Sesam Tageswechsel wurde gestartet.
#               SM_NEWDAY
#                 Ein Sesam Tageswechsel wurde beendet.
#
#     Dies ist ein benutzerprogrammierbares Shell Skript mit 2 Parametern.
#     Fuegen Sie bitte Ihre eigenen Kommandozeilen nach diesem Kommentar ein,
#     wobei Kommandos, die eine Antwort erwarten ( OK-Buttons etc. ) vermieden
#     werden sollten, da sie Sicherungen waehrend der Nacht blockieren koennten.
#
#*******************************************************************************
#
# Example 


read_ini()
{
  . `grep -i '^sm_ini=' /etc/sesam2000.ini|cut -d"=" -f2` 2>/dev/null
}

send_mail()
{
   ## Set mail subject
   ## 3rd parameter is the mail user
   mail_subject="Sesam: $1"
   mail_text="$2"
   mail_user="$3"
   sm_smtp -A "$mail_user" -s "$mail_subject" -m "$mail_text" 
}

send_mail_file()
{
   ## Set mail subject
   ## 4th parameter is the mail user
   mail_subject="Sesam: $1 $2"
   mail_text="$3"
   mail_user="$4"
   ## Send Sesam status file as mail body
   sm_smtp -A "$mail_user" -s "$mail_subject" -M "$mail_text" 
}


send_status_mail()
{
   ## Set mail subject
   ## 2nd parameter is the mail user
   mail_subject="Sesam backup status: $1"
   mail_user="$2"
   ## Send Sesam status as mail text and protocol file as mail attachment
   sm_smtp -A "$mail_user" -s "$mail_subject" -M "gv_dayfile:" -a "gv_prot:" 
}

unload_tape()
{
   ## Unload tape from a stand alone drive. First parameter is sesam drive no. 
   ## Send Sesam status as mail text and protocol file as mail attachment
   sm_drive dismount $1 unload 
}

load_to_slot()
{
   ## Load tape back from drive into slot
   ## 1st parameter is sesam loader no.
   ## 2nd parameter is sesam drive no.
   sm_loader unload -l $1 -d $2
}

# function for reporting backup state last sesam date
report_backups_last_sesam_day()
{
   ## 1st parameter is language
   ## 2nd parameter is the mail user
   lang=$1
   mail_user=$2
   # investigate yesterday date
   yesterday="$(sm_glbv r gv_yesterday)"
   # adding time part for compatibility
   yesterday_time="${yesterday} 00:00:00"
   # define different subject lines regarding to language
   if [ "$lang" = "de" ]; then
      subject="SEP sesam - Sicherungsstatus des Sesam Tages $yesterday"
   else
      subject="SEP sesam - Backup report sesam day $yesterday"
   fi
   # report backup state last sesam day
   sm_cmd start report overview-backups -B "$yesterday_time" -E "$yesterday_time" -D "sesam_date" -l "$lang"  > "${gv_rw_tmp}/backups_last_sesam_day.html"
   sm_smtp -A "$mail_user" -s "$subject" -M ${gv_rw_tmp}/backups_last_sesam_day.html -a "${gv_rw_tmp}/backups_last_sesam_day.html"
}

# function for reporting backup state last sesam date for a specified location
report_backups_last_sesam_day_location()
{
   ## 1st parameter is language
   ## 2nd parameter is the location id
   ## 3rd parameter is the mail user
   lang=$1
   # Specify mandantory location id as second parameter
   if [ -z "$2" ]; then
      echo "missing second parameter for function"
      exit 1
   fi 
   location=$2
   mail_user=$3
   # investigate yesterday date
   yesterday="$(sm_glbv r gv_yesterday)"
   # adding time part for compatibility
   yesterday_time="${yesterday} 00:00:00"
   # define different subject lines regarding to language
   if [ "$lang" = "de" ]; then
      subject="SEP sesam - Sicherungsstatus des Sesam Tages $yesterday Standort $location"
   else
      subject="SEP sesam - Backup report sesam day $yesterday for location $location"
   fi
   # report backup state last sesam day for specified location
   sm_cmd start report overview-backups -B "$yesterday_time" -E "$yesterday_time" -D "sesam_date" -l "$lang" -p '{\"location_id\":'$location'}' > "${gv_rw_tmp}/backups_last_sesam_day_location_$location.html"
   sm_smtp -A "$mail_user" -s "$subject" -M ${gv_rw_tmp}/backups_last_sesam_day_location_$location.html -a "${gv_rw_tmp}/backups_last_sesam_day_location_$location.html"
}

# function for reporting backup state last 24 hours from execution time
report_backups_last_24h()
{
   ## 1st parameter is language
   ## 2nd parameter is the mail user
   lang=$1
   mail_user=$2
   # investigate date and time for last 24 hours
   # day_time is date and time of today
   day_time="$(date '+%Y-%m-%d %H:%M:%S')"
   # yesterday_time is the date and time in the past
   yesterday_time="$(date -d  "1 day ago" '+%Y-%m-%d %H:%M:%S')"
   # define different subject lines regarding to language
   if [ "$lang" = "de" ]; then
      subject="SEP sesam - Sicherungsstatus der letzten 24 Stunden vom $yesterday_time"
   else
      subject="SEP sesam - Backup report last 24 hours from $yesterday_time"
   fi
   # report backup state last 24 hours
   sm_cmd start report overview-backups -B "$yesterday_time" -E "$day_time" -D "start_time" -l "$lang"  > "${gv_rw_tmp}/backups_last_24h.html"
   sm_smtp -A "$mail_user" -s "$subject" -M ${gv_rw_tmp}/backups_last_24h.html -a "${gv_rw_tmp}/backups_last_24h.html"
}

# function for reporting backup state last 24 hours for a specified location
report_backups_last_24h_location()
{
   ## 1st parameter is language
   ## 2nd parameter is the location id
   ## 3rd parameter is the mail user
   lang=$1
   # Specify mandantory location id as second parameter
   if [ -z "$2" ]; then
      echo "missing second parameter for function"
      exit 1
   fi
   location=$2
   mail_user=$3
   # investigate date and time for last 24 hours
   # day_time is date and time of today
   day_time="$(date '+%Y-%m-%d %H:%M:%S')"
   # yesterday_time is the date and time in the past
   yesterday_time="$(date -d  "1 day ago" '+%Y-%m-%d %H:%M:%S')"
   # define different subject lines regarding to language
   if [ "$lang" = "de" ]; then
      subject="SEP sesam - Sicherungsstatus der letzten 24 Stunden von $day_time Standort $location"
   else
      subject="SEP sesam - Backup report last 24 hours from $yesterday_time for location $location"
   fi
   # report backup state last 24 hours for specified location
   sm_cmd start report overview-backups -B "$yesterday_time" -E "$day_time" -D "start_time" -l "$lang" -p '{\"location_id\":'$location'}' > "${gv_rw_tmp}/backups_last_24h_location_$location.html" 
   sm_smtp -A "$mail_user" -s "$subject" -M ${gv_rw_tmp}/backups_last_24h_location_$location.html -a "${gv_rw_tmp}/backups_last_24h_location_$location.html"
}

# function for reporting backup storage location last 24 hours from execution time
report_backup_storage_last_24h()
{
   ## 1st parameter is language
   ## 2nd parameter is the mail user
   lang=$1
   mail_user=$2
   # investigate date and time for last 24 hours
   # day_time is date and time of today
   day_time="$(date '+%Y-%m-%d %H:%M:%S')"
   # yesterday_time is the date and time in the past
   yesterday_time="$(date -d  "1 day ago" '+%Y-%m-%d %H:%M:%S')"
   # define different subject lines regarding to language
   if [ "$lang" = "de" ]; then
      subject="SEP sesam - Speicherbericht der Sicherungen der letzten 24 Stunden vom $yesterday_time"
   else
      subject="SEP sesam - Backup storage report last 24 hours from $yesterday_time"
   fi
   # report backup storage location last 24 hours
   sm_cmd start report backup-storage-report -B "$yesterday_time" -E "$day_time" -D "start_time" -l "$lang"  > "${gv_rw_tmp}/backup_storage_report_last_24h.html"
   sm_smtp -A "$mail_user" -s "$subject" -M ${gv_rw_tmp}/backup_storage_report_last_24h.html -a "${gv_rw_tmp}/backup_storage_report_last_24h.html"
}

# function for reporting backup storage location last 24 hours for a specified location
report_backup_storage_last_24h_location()
{
   ## 1st parameter is language
   ## 2nd parameter is the location id
   ## 3rd parameter is the mail user
   lang=$1
   # Specify mandantory location id as second parameter
   if [ -z "$2" ]; then
      echo "missing second parameter for function"
      exit 1
   fi
   location=$2
   mail_user=$3
   # investigate date and time for last 24 hours
   # day_time is date and time of today
   day_time="$(date '+%Y-%m-%d %H:%M:%S')"
   # yesterday_time is the date and time in the past
   yesterday_time="$(date -d  "1 day ago" '+%Y-%m-%d %H:%M:%S')"
   # define different subject lines regarding to language
   if [ "$lang" = "de" ]; then
      subject="SEP sesam - Speicherbericht der Sicherungen der letzten 24 Stunden von $day_time Standort $location"
   else
      subject="SEP sesam - Backup storage report last 24 hours from $yesterday_time for location $location"
   fi
   # report backup storage location last 24 hours for specified location
   sm_cmd start report backup-storage-report -B "$yesterday_time" -E "$day_time" -D "start_time" -l "$lang" -p '{\"location_id\":'$location'}' > "${gv_rw_tmp}/backup_storage_report_last_24h_location_$location.html" 
   sm_smtp -A "$mail_user" -s "$subject" -M ${gv_rw_tmp}/backup_storage_report_last_24h_location_$location.html -a "${gv_rw_tmp}/backup_storage_report_last_24h_location_$location.html"
}

# function for reporting backup storage location last 24 hours from execution time
report_failed_jobs_last_24h()
{
   ## 1st parameter is language
   ## 2nd parameter is the mail user
   lang=$1
   mail_user=$2
   # investigate date and time for last 24 hours
   # day_time is date and time of today
   day_time="$(date '+%Y-%m-%d %H:%M:%S')"
   # yesterday_time is the date and time in the past
   yesterday_time="$(date -d  "1 day ago" '+%Y-%m-%d %H:%M:%S')"
   # define different subject lines regarding to language
   if [ "$lang" = "de" ]; then
      subject="SEP sesam - Fehlgeschlagene Sicherungen der letzten 24 Stunden vom $yesterday_time"
   else
      subject="SEP sesam - Failed backup report last 24 hours from $yesterday_time"
   fi
   # report of failed jobs last 24 hours
   sm_cmd start report failed-jobs-report -B "$yesterday_time" -E "$day_time" -D "start_time" -l "$lang"  > "${gv_rw_tmp}/failed_jobs_report_last_24h.html"
   sm_smtp -A "$mail_user" -s "$subject" -M ${gv_rw_tmp}/failed_jobs_report_last_24h.html -a "${gv_rw_tmp}/failed_jobs_report_last_24h.html"
}

# function for reporting used media last 24 hours
report_used_media_last_24h()
{
   ## 1st parameter is language
   ## 2nd parameter is the mail user
   lang=$1
   mail_user=$2
   # investigate date and time for last 24 hours
   # day_time is date and time of today
   day_time="$(date '+%Y-%m-%d %H:%M:%S')"
   # yesterday_time is the date and time in the past
   yesterday_time="$(date -d  "1 day ago" '+%Y-%m-%d %H:%M:%S')"
   # define different subject lines regarding to language
   if [ "$lang" = "de" ]; then
      subject="SEP sesam - Benutze Medien der letzten 24 Stunden vom $yesterday_time"
   else
      subject="SEP sesam - SEP sesam - Used media report last 24 hours from $yesterday_time"
   fi
   # report of used media last 24 hours
   sm_cmd start report used-media-report -B "$yesterday_time" -E "$day_time" -D "start_time" -l "$lang"  > "${gv_rw_tmp}/used_media_report_last_24h.html"
   sm_smtp -A "$mail_user" -s "$subject" -M ${gv_rw_tmp}/used_media_report_last_24h.html -a "${gv_rw_tmp}/used_media_report_last_24h.html"
}

#--------------- MAIN ----------------------

read_ini
. "${gv_rw_ini}/sesam2000.profile" >/dev/null 2>&1
lang=$(read_ini lang)
mail_user="sesam"
location="0"

LOGFILE="${gv_rw_lgc}/sm_notify_`sm_glbv r gv_daylbl 2>/dev/null`.log"
echo "$0" "$*" >> "${LOGFILE}"

# In Grolar by default "send_status_mail" was activated in ALL_JOBS_DONE
# Since Beefalo by default "report_backups_last_24_hours" and "report_migrations_last_24_hours" is activated in NEWDAY
case $1 in
      ALL_JOBS_DONE)
         # send_status_mail "$2" "$mail_user"
         # load_to_slot 1 2
         # unload_tape 2
         ;;
      INIT)
         # send_mail "$1" "$2" "$mail_user"
         ;;
      GET_VOLUME)
         # send_mail "$1" "$2" "$mail_user"
         ;;
      BACKUP)
         # send_mail "$1" "$2" "$mail_user"
         ;;
      RESTORE)
         # send_mail_file "$1" "$2" "$3" "$mail_user"
         ;;
      BACKUP_GROUP:)
         # send_mail "$1" "$2" "$mail_user"
         ;;
      COPY)
         # send_mail "MIGRATION operation" "$2" "$mail_user"
         ;;
      COPY_SAVESET)
         # send_mail "MIGRATION task" "$2" "$mail_user"
         ;;
      COMMAND)
         # send_mail "MIGRATION task" "$2" "$mail_user"
         ;;
      NEWDAY)
         # send_mail "NEWDAY event has been started" "$2" "$mail_user"
         # report_backups_last_sesam_day "$lang" "$mail_user"
         # report_backups_last_sesam_day_location "$lang" "$location" "$mail_user"
         report_backups_last_24h "$lang" "$mail_user"
         # report_backups_last_24h_location "$lang" "$location" "$mail_user"
         report_backup_storage_last_24h "$lang" "$mail_user"
         # report_backup_storage_last_24h_location "$lang" "$location" "$mail_user"
         report_failed_jobs_last_24h "$lang" "$mail_user"
         # report_used_media_last_24h "$lang" "$mail_user"
         ;;
      REPORT)
         # send_mail "NEWDAY event has been started" "$2" "$mail_user"
         ;;
      SM_NEWDAY)
         # send_mail "NEWDAY event has been finished" "$2" "$mail_user"
         ;;
      *)
      ;;
esac

exit
