REPORT ZALERTS NO STANDARD PAGE HEADING LINE-SIZE 132.
******************************************************
* This program monitores the SAP red alerts, and in  *
* case of an alert, sends an express mail to the     *
* administrator, including the alert type and the    *
* description of the error. The program should be    *
* scheduled to run in batch in every hour or so.     *
* The alert thesholds should be configured using     * 
* RZ06. The program was tested on 3.1H               *
****************************************************** 
INCLUDE RSALINC1.
INCLUDE RSALICSL.

DATA: TXT(12),
      CL TYPE I,
      SERVER LIKE SPFIS-APSERVER,
      SERVER_TYPE  TYPE I,
      RED_FLAG TYPE I.
DATA: BEGIN OF EMAIL_DATA.
        INCLUDE STRUCTURE SODOCCHGI1.
DATA: END OF EMAIL_DATA.
DATA: BEGIN OF EMAIL_SEND OCCURS 10.
        INCLUDE STRUCTURE SOMLRECI1.
DATA: END OF EMAIL_SEND.
DATA: BEGIN OF DATA_TAB OCCURS 20,
      LINE(255),
END OF DATA_TAB.

START-OF-SELECTION.
* Get the server data
  CALL 'C_SAPGPARAM' ID 'NAME'  FIELD 'rdisp/myname'
                     ID 'VALUE' FIELD SERVER.
  SRV_NAME  = SERVER.
  ISRV_TYPE = SERVER_TYPE.

* Get the performance data
  PERFORM GET_ALERTS.

* Extend the performance data with texts
* OP. system: CPU-Load, Paging, Swap-Space, File-System alerts
  PERFORM CRT_OSCO_load_txt USING OSCO_load.
  PERFORM CRT_OSCO_page_TXT USING OSCO_page.
  PERFORM CRT_OSCO_swap_TXT USING OSCO_swap.
  PERFORM CRT_OSCO_file_TXT USING OSCO_file.
* Enque directory/entry usage %
  PERFORM CRT_ENQU_TXT USING ENQU.
* Sysyem log: max entries/hour, special messages alert
  PERFORM CRT_SYSLOG_TXT.
* Roll/Page file used% alerts
  PERFORM CRT_RLPG_TXT USING RLPG.
* Dispatcher queue used% alert
  PERFORM CRT_DPQU_TXT USING DPQU.
* Trace Switch
  PERFORM CRT_TRACE_TXT.
* Performance: dialog/update1/update2/batch/spool response/wait/run time
  PERFORM CRT_PERF_TXT USING PERFDIA.
  PERFORM CRT_PERF_TXT USING PERFVB.
  PERFORM CRT_PERF_TXT USING PERFV2.
  PERFORM CRT_PERF_TXT USING PERFBTC.
  PERFORM CRT_PERF_TXT USING PERFSPO.
* Buffer hit ratio,  directory used%, space used% alerts
  PERFORM CRT_BUFF_TXT USING BUFFABAP.
  PERFORM CRT_BUFF_TXT USING BUFFTAB1.
  PERFORM CRT_BUFF_TXT USING BUFFTABL.
  PERFORM CRT_BUFF_TXT USING BUFFPRES.
  PERFORM CRT_BUFF_TXT USING BUFFCUA.
  PERFORM CRT_BUFF_TXT USING BUFFDBTT.
  PERFORM CRT_BUFF_TXT USING BUFFDBFT.
  PERFORM CRT_BUFF_TXT USING BUFFDBIR.
  PERFORM CRT_BUFF_TXT USING BUFFDBSN.
* ABAP: abap error, update error, sql error
  PERFORM CRT_ABAP_TXT USING ABAPERR.
  PERFORM CRT_ABAP_TXT USING ABAPVB.
  PERFORM CRT_ABAP_TXT USING ABAPSQL.
* DB alerts: missing indices, freespace problems, arch. stuck, backup
  PERFORM CRT_GENP_TXT USING DB_INDICES.
  PERFORM CRT_GENP_TXT USING DB_FREESPC.
  PERFORM CRT_GENP_TXT USING DB_ARCSTUCK.
  PERFORM CRT_GENP_TXT USING DB_BACKUP.
* Spool delay
  PERFORM CRT_GENP_TXT USING GENP_SPO.
* ???
  PERFORM CRT_GENP_TXT USING GENP_ARCH.
  PERFORM CRT_GENP_TXT USING GENP_03.
  PERFORM CRT_GENP_TXT USING GENP_04.
  PERFORM CRT_GENP_TXT USING GENP_05.
  PERFORM CRT_GENP_TXT USING GENP_06.
  PERFORM CRT_GENP_TXT USING GENP_07.
  PERFORM CRT_GENP_TXT USING GENP_08.
  PERFORM CRT_GENP_TXT USING GENP_09.
  PERFORM CRT_GENP_TXT USING GENP_10.
  PERFORM CRT_GENP_TXT USING GENP_11.
  PERFORM CRT_GENP_TXT USING GENP_12.
  PERFORM CRT_GENP_TXT USING GENP_13.
  PERFORM CRT_GENP_TXT USING GENP_14.
  PERFORM CRT_DB_GENP_TXT USING GENP_15.
*Perf. Summary
  PERFORM CREATE_SUM_TEXT.

* Process the result table
  SORT DETAILS BY TYP SUBTYP.
  LOOP AT DETAILS.
    IF DETAILS-TIME <> '0' AND
       DETAILS-DISABLED NE '1'.
* Get the alert type
      PERFORM GET_ALERT_NAM_TXT USING DETAILS-TYP DETAILS-SUBTYP TXT.
* Fill up the message table for the express mail
      IF DETAILS-VALUE = 2.
        RED_FLAG = 1.
        DATA_TAB = TXT. APPEND DATA_TAB.
        DATA_TAB+5(250) = DETAILS-TXT. APPEND DATA_TAB.
        DATA_TAB = ' '. APPEND DATA_TAB.
*       write: / details-time  color 6,
*                txt           color 6,
*                details-value color 6,
*                (50) details-txt   color 6.
      ELSE.
*       write: / details-time  color 1,
*                txt           color 1,
*                details-value color 1,
*                (50) details-txt   color 1.
      ENDIF.
    ENDIF.
  ENDLOOP.

* Send an express mail with the details of the red alerts
  IF RED_FLAG = 1.
    PERFORM SEND_EXPERESS_MAIL.
  ENDIF.

*---------------------------------------------------------------------*
*       FORM SEND_EXPERESS_MAIL                                       *
*---------------------------------------------------------------------*
FORM SEND_EXPERESS_MAIL.
  EMAIL_DATA-OBJ_NAME = 'MESSAGE'.
  EMAIL_DATA-OBJ_DESCR = 'RED ALERTS!'.
  EMAIL_DATA-OBJ_LANGU = 'E'.
  EMAIL_DATA-SENSITIVTY = 'P'.
  EMAIL_DATA-OBJ_PRIO =  '1'.
  EMAIL_DATA-NO_CHANGE = 'X'.
  EMAIL_DATA-PRIORITY = '1'.

  EMAIL_SEND-RECEIVER = 'KABAIIM'.
  EMAIL_SEND-REC_TYPE = 'B'.
  EMAIL_SEND-EXPRESS = 'X'.
  APPEND EMAIL_SEND.

  CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
       EXPORTING
            DOCUMENT_DATA  = EMAIL_DATA
            DOCUMENT_TYPE  = 'RAW'
            PUT_IN_OUTBOX  = 'X'
       TABLES
            OBJECT_CONTENT = DATA_TAB
            RECEIVERS      = EMAIL_SEND.
ENDFORM.