背景
系统使用时间较长,存在大量的后台作业,用户量较大,需要及时监控后台作业状况及系统dump情况,以便及时处理。
功能
2. 查询屏幕填写日期前n秒系统中的dump记录--客制化程序
功能展示
选择屏幕
程序输出
邮件内容
代码
*&---------------------------------------------------------------------*
*& Report ZSYSLOG_NOTICE
*&---------------------------------------------------------------------*
*& Developer : Fireworks
*&---------------------------------------------------------------------*
*& Purpose: 获取最近N秒sap中客制化程序dump记录及作业错误记录并发送
*& 邮件至收件人
*&---------------------------------------------------------------------*
REPORT zsyslog_notice.
TABLES sscrfields.
*-----------------------------------------------------------------------
* class lcl_object DEFINITION
*-----------------------------------------------------------------------
CLASS lcl_object DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF ty_job_result,
jobname TYPE v_op-jobname,
intreport TYPE v_op-intreport,
strtdate TYPE v_op-strtdate,
strttime TYPE v_op-strttime,
status TYPE v_op-status,
END OF ty_job_result.
TYPES: BEGIN OF ty_st22_record,
datum TYPE snap-datum,
uzeit TYPE snap-uzeit,
uname TYPE snap-uname,
errorid TYPE rdumpov-errorid,
rexception TYPE rdumpov-rexception,
gprogram TYPE rdumpov-gprogram,
END OF ty_st22_record.
DATA: gt_job_result TYPE TABLE OF ty_job_result,
gt_st22_record TYPE TABLE OF ty_st22_record.
DATA: gt_content TYPE soli_tab, "Email Content
gv_subject TYPE so_obj_des. "Email Subject
DATA: gv_ld TYPE datum,
gv_lt TYPE uzeit,
gv_cd TYPE datum,
gv_ct TYPE uzeit.
METHODS:
get_job_result ,
get_st22_record ,
email_send IMPORTING pi_subject TYPE so_obj_des
pi_sender TYPE ad_smtpadr OPTIONAL
pt_accept TYPE bcsy_smtpa
pt_content TYPE soli_tab
EXPORTING pe_return TYPE bapiret2,
send_email_notice IMPORTING pt_accept TYPE bcsy_smtpa,
constructor IMPORTING pi_datum TYPE sy-datum
pi_uzeit TYPE sy-uzeit
pi_sec TYPE num8.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
*-----------------------------------------------------------------------
* class lcl_object IMPLEMENTATION
*-----------------------------------------------------------------------
CLASS lcl_object IMPLEMENTATION.
METHOD constructor.
DATA: lv_sec TYPE i.
lv_sec = pi_sec.
CALL METHOD cl_abap_tstmp=>td_add
EXPORTING
date = pi_datum
time = pi_uzeit
secs = lv_sec
IMPORTING
res_date = gv_ld
res_time = gv_lt.
gv_cd = pi_datum.
gv_ct = pi_uzeit.
ENDMETHOD.
METHOD get_job_result.
SELECT *
FROM v_op
INTO CORRESPONDING FIELDS OF TABLE gt_job_result
WHERE ( strtdate = gv_ld AND strttime >= gv_lt
OR strtdate = gv_cd AND strttime <= gv_ct )
AND status IN ( 'A','X' )
.
ENDMETHOD." Get the job with error status
METHOD get_st22_record.
DATA: lt_snap TYPE TABLE OF snap,
ls_snap TYPE snap,
ls_st22_record LIKE LINE OF gt_st22_record.
DATA: BEGIN OF struc,
id(2) TYPE c,
len(3) TYPE c,
text(50) TYPE c,
END OF struc.
DATA e_line TYPE c LENGTH 400.
DATA: pointer TYPE i,
struc_length TYPE i,
lv_end TYPE i.
FIELD-SYMBOLS: <fs_text> TYPE any.
SELECT *
FROM snap
INTO TABLE lt_snap
WHERE seqno = '000'
AND ( datum = gv_ld AND uzeit >= gv_lt
OR datum = gv_cd AND uzeit <= gv_ct )
.
struc_length = 55.
lv_end = 345.
LOOP AT lt_snap INTO ls_snap.
CLEAR: pointer,ls_st22_record,struc,e_line.
MOVE-CORRESPONDING ls_snap TO ls_st22_record.
e_line+0(200) = ls_snap-flist.
e_line+200(200) = ls_snap-flist02.
WHILE pointer < lv_end.
struc = e_line+pointer(struc_length).
IF ( struc-len CO '0123456789' ) AND ( struc-len <> '000' ) AND
( struc-len < '050' ).
ASSIGN struc-text(struc-len) TO <fs_text>.
CASE struc-id.
WHEN 'XC'."exception
ls_st22_record-rexception = <fs_text>.
WHEN 'AP'."dump program
ls_st22_record-gprogram = <fs_text>.
WHEN 'FC'."error id
ls_st22_record-errorid = <fs_text>.
WHEN OTHERS.
ENDCASE.
ELSE.
struc-text = '?????'.
IF struc-len CO '0123456789' AND struc-len <> '000'.
pointer = pointer + 5 + struc-len. " Neuer Versuch
CONTINUE.
ENDIF.
ENDIF.
pointer = pointer + 5 + struc-len. " Neuer Versuch
IF ls_st22_record-errorid IS NOT INITIAL AND
ls_st22_record-gprogram IS NOT INITIAL AND
ls_st22_record-rexception IS NOT INITIAL.
EXIT.
ENDIF.
ENDWHILE.
APPEND ls_st22_record TO gt_st22_record.
ENDLOOP.
ENDMETHOD. " Get the dump
METHOD send_email_notice.
DATA: ls_return TYPE bapiret2,
lv_sendder TYPE ad_smtpadr,
ls_content LIKE LINE OF gt_content.
DATA: ls_job_result LIKE LINE OF gt_job_result,
ls_st22_record LIKE LINE OF gt_st22_record.
DATA: lv_table_ines TYPE i,
lv_status_text TYPE text10.
gv_subject = 'System' && sy-sysid && sy-mandt && '--'.
"作业错误
IF gt_job_result IS NOT INITIAL.
DESCRIBE TABLE gt_job_result LINES lv_table_ines.
gv_subject = gv_subject && 'Job With Error Status: ' && lv_table_ines && ' Rows'.
ls_content-line = cl_bcs_convert=>gc_tab && 'Job With Error Status As Follows:'.
APPEND ls_content TO gt_content.
ls_content-line = cl_bcs_convert=>gc_tab && 'Job Name' && cl_bcs_convert=>gc_tab && 'Start Date'
&& cl_bcs_convert=>gc_tab && 'Start Time' && cl_bcs_convert=>gc_tab && 'Job Status'.
APPEND ls_content TO gt_content.
LOOP AT gt_job_result INTO ls_job_result.
CASE ls_job_result-status.
WHEN 'A'.
lv_status_text = 'Unknow'.
WHEN 'X'.
lv_status_text = 'Canceled'.
WHEN OTHERS.
ENDCASE.
ls_content-line = cl_bcs_convert=>gc_tab && ls_job_result-jobname && cl_bcs_convert=>gc_tab && ls_job_result-strtdate
&& cl_bcs_convert=>gc_tab && ls_job_result-strttime && cl_bcs_convert=>gc_tab && lv_status_text.
APPEND ls_content TO gt_content.
ENDLOOP.
ENDIF.
"dump
DELETE gt_st22_record WHERE gprogram+0(1) <> 'Z' AND gprogram+0(1) <> 'Y' .
IF gt_st22_record IS NOT INITIAL.
DESCRIBE TABLE gt_st22_record LINES lv_table_ines.
gv_subject = gv_subject && 'Customer Program Dump: ' && lv_table_ines && ' Rows'.
ls_content-line = cl_bcs_convert=>gc_tab && 'DUMP As Follows:'.
APPEND ls_content TO gt_content.
ls_content-line = cl_bcs_convert=>gc_tab && 'Program Name' && cl_bcs_convert=>gc_tab && 'Date'
&& cl_bcs_convert=>gc_tab && 'Time' && cl_bcs_convert=>gc_tab && 'User Name'
&& cl_bcs_convert=>gc_tab && 'Exception' && cl_bcs_convert=>gc_tab && 'Error'.
APPEND ls_content TO gt_content.
LOOP AT gt_st22_record INTO ls_st22_record.
ls_content-line = cl_bcs_convert=>gc_tab && ls_st22_record-gprogram && cl_bcs_convert=>gc_tab && ls_st22_record-datum
&& cl_bcs_convert=>gc_tab && ls_st22_record-uzeit && cl_bcs_convert=>gc_tab && ls_st22_record-uname
&& cl_bcs_convert=>gc_tab && ls_st22_record-rexception && cl_bcs_convert=>gc_tab && ls_st22_record-errorid.
APPEND ls_content TO gt_content.
ENDLOOP.
ENDIF.
IF gt_st22_record IS NOT INITIAL OR gt_job_result IS NOT INITIAL.
* lv_sendder = '123@test.com'.
email_send(
EXPORTING
pi_subject = gv_subject
pi_sender = lv_sendder
pt_accept = pt_accept
pt_content = gt_content
IMPORTING
pe_return = ls_return
).
ENDIF.
ENDMETHOD. " Send notification
METHOD email_send.
*&------Send Email Parameter
DATA: lt_contents_txt TYPE soli_tab,
ls_contents_txt TYPE soli,
lt_attach_header TYPE soli_tab,
ls_attach_header TYPE soli,
lt_att_content_hex TYPE solix_tab,
lv_outlength TYPE i,
lv_attch_size TYPE so_obj_len,
lv_content_size TYPE so_obj_len,
lv_send_return TYPE os_boolean.
DATA: lr_send TYPE REF TO cl_bcs,
lr_document TYPE REF TO cl_document_bcs,
lr_sender TYPE REF TO cl_cam_address_bcs,
lr_accept TYPE REF TO cl_cam_address_bcs,
lr_ccaccept TYPE REF TO cl_cam_address_bcs,
lr_sap_user TYPE REF TO cl_sapuser_bcs,
lr_bcs TYPE REF TO cx_bcs.
DATA:lr_cx_send_req_bcs TYPE REF TO cx_send_req_bcs,
lr_cx_address_bcs TYPE REF TO cx_address_bcs.
DATA: lv_accept TYPE ad_smtpadr,
lv_message TYPE bapiret2-message.
DEFINE mcr_fill_return.
pe_return-id = sy-msgid.
pe_return-number = sy-msgno.
pe_return-type = sy-msgty.
pe_return-message_v1 = sy-msgv1.
pe_return-message_v2 = sy-msgv2.
pe_return-message_v3 = sy-msgv3.
pe_return-message_v4 = sy-msgv4.
pe_return-message = &1.
RETURN.
END-OF-DEFINITION.
*&------Create Send Request
TRY.
CALL METHOD cl_bcs=>create_persistent
RECEIVING
result = lr_send.
CATCH cx_send_req_bcs INTO lr_cx_send_req_bcs.
ENDTRY.
IF lr_cx_send_req_bcs IS NOT INITIAL.
lv_message = lr_cx_send_req_bcs->get_text( ).
mcr_fill_return lv_message.
ENDIF.
*&------Get The Sender Email Adress
IF pi_sender IS NOT INITIAL.
TRY.
CALL METHOD cl_cam_address_bcs=>create_internet_address
EXPORTING
i_address_string = pi_sender
i_address_name = 'System Notification'
* i_incl_sapuser =
RECEIVING
result = lr_sender.
CATCH cx_address_bcs INTO lr_cx_address_bcs.
ENDTRY.
IF lr_cx_address_bcs IS NOT INITIAL.
lv_message = lr_cx_address_bcs->get_text( ).
mcr_fill_return lv_message.
ENDIF.
*&---------Set Sender
TRY.
CALL METHOD lr_send->set_sender
EXPORTING
i_sender = lr_sender.
CATCH cx_send_req_bcs INTO lr_cx_send_req_bcs.
lv_message = lr_cx_send_req_bcs->get_text( ).
mcr_fill_return lv_message.
ENDTRY.
ENDIF.
*&------Create Send Document Content
lt_contents_txt = pt_content.
TRY.
IF lr_sender IS NOT INITIAL.
CALL METHOD cl_document_bcs=>create_document
EXPORTING
i_type = 'RAW'
i_subject = pi_subject
i_length = lv_content_size
i_language = sy-langu
* i_importance = '1'
i_sensitivity = 'O'
i_text = lt_contents_txt
* i_hex =
* i_header =
i_sender = lr_sender
* iv_vsi_profile =
RECEIVING
result = lr_document.
ELSE.
CALL METHOD cl_document_bcs=>create_document
EXPORTING
i_type = 'RAW'
i_subject = pi_subject
i_length = lv_content_size
i_language = sy-langu
* i_importance = '1'
i_sensitivity = 'O'
i_text = lt_contents_txt
* i_hex =
* i_header =
* i_sender =
* iv_vsi_profile =
RECEIVING
result = lr_document.
ENDIF.
CATCH cx_document_bcs INTO lr_bcs.
ENDTRY.
IF lr_bcs IS NOT INITIAL.
lv_message = lr_bcs->get_text( ).
mcr_fill_return lv_message.
ENDIF.
*&------Set Send Document For Send Request
TRY.
CALL METHOD lr_send->set_document
EXPORTING
i_document = lr_document.
CATCH cx_send_req_bcs INTO lr_bcs.
ENDTRY.
IF lr_bcs IS NOT INITIAL.
lv_message = lr_bcs->get_text( ).
mcr_fill_return lv_message.
ENDIF.
*&------Add Accept For Email
TRY.
LOOP AT pt_accept INTO lv_accept.
CLEAR lr_accept.
CALL METHOD cl_cam_address_bcs=>create_internet_address
EXPORTING
i_address_string = lv_accept
* i_address_name =
* i_incl_sapuser =
RECEIVING
result = lr_accept.
CALL METHOD lr_send->add_recipient
EXPORTING
i_recipient = lr_accept
i_express = 'X'
i_copy = ''
i_blind_copy = ''
i_no_forward = ''.
ENDLOOP.
CATCH cx_send_req_bcs INTO lr_bcs.
lv_message = lr_bcs->get_text( ).
mcr_fill_return lv_message.
CATCH cx_address_bcs INTO lr_bcs.
lv_message = lr_bcs->get_text( ).
mcr_fill_return lv_message.
ENDTRY.
TRY.
CALL METHOD lr_send->set_send_immediately
EXPORTING
i_send_immediately = 'X'.
CATCH cx_send_req_bcs INTO lr_bcs.
lv_message = lr_bcs->get_text( ).
mcr_fill_return lv_message.
ENDTRY.
*&------Send Email
TRY.
CALL METHOD lr_send->send
EXPORTING
i_with_error_screen = space
RECEIVING
result = lv_send_return.
CATCH cx_send_req_bcs INTO lr_bcs.
lv_message = lr_bcs->get_text( ).
mcr_fill_return lv_message.
ENDTRY.
COMMIT WORK AND WAIT.
SUBMIT rsconn01 WITH mode = '*'"INT'
WITH output = ''
AND RETURN.
ENDMETHOD. " Tool methon to send email
ENDCLASS.
*&---------------------------------------------------------------------*
* Data Definition
*&---------------------------------------------------------------------*
DATA: go_program TYPE REF TO lcl_object,
gt_accept TYPE bcsy_smtpa,
gv_email TYPE char100.
*&---------------------------------------------------------------------*
* Selection Screen definition
*&---------------------------------------------------------------------*
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME.
SELECT-OPTIONS: s_emai FOR gv_email NO INTERVALS MEMORY ID mid1 OBLIGATORY DEFAULT '123@321.com'.
PARAMETERS: p_date TYPE sy-datum DEFAULT sy-datum, " Current date
p_time TYPE sy-uzeit DEFAULT sy-uzeit, " Current time
p_lsec TYPE num8 DEFAULT 86430, " Lastest N seconds
p_job TYPE char1 DEFAULT 'X' AS CHECKBOX,
p_st22 TYPE char1 DEFAULT 'X' AS CHECKBOX.
SELECTION-SCREEN: END OF BLOCK b1.
*&---------------------------------------------------------------------*
* Initialization
*&---------------------------------------------------------------------*
INITIALIZATION.
*&--------------------------------------------------------------------*
* at selection screen *
*&--------------------------------------------------------------------*
AT SELECTION-SCREEN.
*&---------------------------------------------------------------------*
* start of selection
*&---------------------------------------------------------------------*
START-OF-SELECTION.
*& Fill accepter from selection screen
CLEAR gt_accept.
LOOP AT s_emai.
gv_email = s_emai-low.
APPEND gv_email TO gt_accept.
ENDLOOP.
*& Send notice
CREATE OBJECT go_program EXPORTING pi_datum = p_date pi_uzeit = p_time pi_sec = p_lsec.
IF gt_accept IS NOT INITIAL .
IF p_job IS NOT INITIAL.
go_program->get_job_result( ).
ENDIF.
IF p_st22 IS NOT INITIAL.
go_program->get_st22_record( ).
ENDIF.
go_program->send_email_notice( pt_accept = gt_accept ).
ENDIF.
*&---------------------------------------------------------------------*
* end of selection
*&---------------------------------------------------------------------*
END-OF-SELECTION.
*& Write the result to screen
WRITE:/ go_program->gv_subject.
LOOP AT go_program->gt_content INTO DATA(ls_content_temp).
WRITE:/ ls_content_temp-line.
ENDLOOP.
https://blog.csdn.net/xiefireworks/article/details/131610467
PS:展示效果及发送邮件格式较为简陋