Daniel Berlin on Security IT Audit, SAP security, developmentā€¦ and all the rest

28May 13

Source – ZS_STAD_EXTRACT_RFC_CALLS

*&---------------------------------------------------------------------*
*& Report ZS_STAD_EXTRACT_RFC_CALLS
*&---------------------------------------------------------------------*
*& Purpose: Save called RFC function modules & groups from STAD
*& Author : Daniel Berlin
*& Version: 1.0.2
*&---------------------------------------------------------------------*
*& Table      : ZSSTAD_RFC_DATA
*& Description: Storage table for report ZS_STAD_EXTRACT_RFC_CALLS
*& Fields     : datum TYPE swncdatum   (key)
*&              mandt TYPE char3       (key)
*&              uname TYPE swncuname   (key)
*&              fumod TYPE swnctxt64   (key)
*&              fugrp TYPE rs38l_area  (key)
*&              ncall TYPE integer
*&---------------------------------------------------------------------*
*& Table      : ZSSTAD_LASTRUN
*& Description: Last run time of report ZS_STAD_EXTRACT_RFC_CALLS
*& Fields     : datum TYPE swncdatum   (key)
*&              uzeit TYPE swncuzeit   (key)
*&---------------------------------------------------------------------*
 
REPORT zs_stad_extract_rfc_calls.
 
DATA: lastrun         TYPE         zsstad_lastrun
    , duration        TYPE         swncuzeit
    , problems        TYPE         sapwlpstrc-counter
    , total_recs_read TYPE         sapwlsfrer-recs_read
    , loghandle       TYPE         balloghndl
    , protocol        TYPE         sapwlminut      OCCURS 0
    , rfc_returns     TYPE         sapwlsfrer      OCCURS 0
    , server_list     TYPE         msxxlist        OCCURS 0
    , all_stats       TYPE         stad_allstats
    , stats_line      TYPE LINE OF stad_allstats
    , rfcsv_line      TYPE LINE OF swnc_t_subrfcs
    , lt_result       TYPE         zsstad_rfc_data OCCURS 0
                                                   WITH HEADER LINE.
 
INITIALIZATION.
 
  AUTHORITY-CHECK OBJECT 'S_ADMI_FCD'
                      ID 'S_ADMI_FCD' FIELD 'ST0R'.
 
  IF sy-subrc <> 0.
    MESSAGE 'Missing authorization.' TYPE 'W'.              "#EC NOTEXT
    LEAVE PROGRAM.
  ENDIF.
 
START-OF-SELECTION.
 
  " --- Read date/time of last run, then set to current date/time
 
  SELECT SINGLE * INTO lastrun FROM zsstad_lastrun.
 
  IF sy-dbcnt = 1.
    UPDATE zsstad_lastrun SET datum = sy-datum
                              uzeit = sy-uzeit.
  ELSE.
    lastrun-datum = sy-datum.
    lastrun-uzeit = sy-uzeit.
    INSERT zsstad_lastrun FROM lastrun.
 
    " --- Fallback for last run date: 1 hour ago
 
    IF lastrun-uzeit < 3600.
      " Set date to yesterday, if < 1 hour passed since midnight
      lastrun-datum = lastrun-datum - 1.
    ENDIF.
    lastrun-uzeit = lastrun-uzeit - 3600.
  ENDIF.
 
  " --- Determine timespan between last and current run
 
  " Last run dates, which are too far in the past, might lead to a
  " BCD_OVERFLOW runtime error - which is intentionally not caught.
  " Schedule it every hour, so you'll never have to deal with that!
 
* TRY.
  CALL FUNCTION 'DURATION_DETERMINE'
    EXPORTING
      unit       = 'S'
    IMPORTING
      duration   = duration
    CHANGING
      start_date = lastrun-datum
      start_time = lastrun-uzeit
      end_date   = sy-datum
      end_time   = sy-uzeit
    EXCEPTIONS
      OTHERS     = 1.
*   CATCH cx_root.
*     ...
* ENDTRY.
 
  IF sy-subrc <> 0.
    MESSAGE 'Cannot determine timespan.' TYPE 'E'.          "#EC NOTEXT
    LEAVE PROGRAM.
  ENDIF.
 
  " --- Read statistical data
 
  CALL FUNCTION 'SWNC_STAD_READ_STATRECS'
    EXPORTING
      read_time       = duration
      read_start_date = lastrun-datum
      read_start_time = lastrun-uzeit
    IMPORTING
      problems        = problems
      total_recs_read = total_recs_read
      loghandle       = loghandle
    TABLES
      protocol        = protocol
      rfc_returns     = rfc_returns
      server_list     = server_list
    CHANGING
      all_stats       = all_stats
    EXCEPTIONS
      OTHERS          = 0.
 
  " --- Extract required information from statistics
 
  LOOP AT all_stats INTO stats_line.
    CHECK stats_line-record-tasktype = 'FE'.  " Type FE => RFC
 
    lt_result-datum = stats_line-record-startdate.
    lt_result-mandt = stats_line-record-mandt.
    lt_result-ncall = 1.
 
    LOOP AT stats_line-subrfcsv INTO rfcsv_line.
      lt_result-uname = rfcsv_line-caller_user_name.
      lt_result-fumod = rfcsv_line-function_name.
 
      SELECT SINGLE area INTO lt_result-fugrp FROM enlfdir
              WHERE funcname = rfcsv_line-function_name.
 
      APPEND lt_result.
    ENDLOOP.
 
    CLEAR lt_result.
  ENDLOOP.
 
  " --- Save data to table ZSSTAD_RFC_DATA
 
  LOOP AT lt_result.
    SELECT COUNT(*) FROM zsstad_rfc_data
                   WHERE datum = lt_result-datum
                     AND mandt = lt_result-mandt
                     AND uname = lt_result-uname
                     AND fumod = lt_result-fumod
                     AND fugrp = lt_result-fugrp.
 
    IF sy-dbcnt = 0.
      INSERT zsstad_rfc_data FROM lt_result.
    ELSE.
      UPDATE zsstad_rfc_data
         SET ncall = ncall + 1
       WHERE datum = lt_result-datum
         AND mandt = lt_result-mandt
         AND uname = lt_result-uname
         AND fumod = lt_result-fumod
         AND fugrp = lt_result-fugrp.
    ENDIF.
  ENDLOOP.
Comments (0) Trackbacks (0)

No comments yet... you could be the first!




Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.


No trackbacks yet.