Thursday, October 8, 2009

Monitoring SAP Jobs

On the ISeries/AS400 there is this great product called ROBOT it does job scheduling and it also sends out pages if a job goes into message wait.  I am sure that there are similar products in SAP but the company I work for does not have a tool that will send out messages when a job blows up so I wrote a quick and dirty ABAP program to that.

SAP has a table called TBTCO that contains the status of all jobs that have been run so its a simple process to query that file.
The following is a simple flow of how this process is supposed to work.




As you can see I am using the generic email process that I added to my blog two weeks ago.
You of course can use what ever process you want to send out e-mails.

I did not want to send out e-mails for all job failures so I created a table called ZJOBS_DESC that contains only the job names I am monitoring.
Here is the layout for that file.



I also wanted to be able to send e-mails to multiple people so I created another custom table called ZJOBS_LIST.
Here is the layout of that table.


Well I guess were ready for some ABAP.
Global Variables:
The tables ZEMAIL_* are all part of the generic e-mail process.
tables: zemail_001,
        zemail_002,
        zemail_003a.

data: global_tab_tbtco  type table of tbtco with header line,
      global_tab_desc   type table of zjobs_desc with header line,
      global_tab_list   type table of zjobs_list with header line,
      local_source(100) type c,
      local_date(8)     type c,
      local_time        type sy-timlo,
      local_count       type i,
      local_retain      type sy-datum,
      current_date      type sy-datum,
      current_time      type sy-uzeit.
Following code goes back an hour to get the starting point for querying TBTCO
current_time = sy-uzeit - 3600.
current_date = sy-datum.

if current_time < 0.
  current_time = 230000.
  current_date = sy-datum - 1.
endif.
Query TBTCO
select * from tbtco into table global_tab_tbtco
              where status = 'A'
                and enddate = sy-datum
                and endtime >= current_time.
Now we loop thru the results and check to see if any aborted jobs are in the table ZJOBS_DESC if they are we send out an e-mail.
loop at global_tab_tbtco.
  local_date  = sy-datum.
  local_count = 0.
  get time field local_time.

  select single * from zjobs_desc into global_tab_desc where jobname = global_tab_tbtco-jobname.

  if sy-subrc = 0.
    select single * from zjobs_list into global_tab_list where jobname = global_tab_tbtco-jobname.
    if sy-subrc = 0.
      concatenate global_tab_desc-jobname local_date local_time into zemail_002-source_program.
      zemail_002-com_type        = 'MAL'.
      local_count                = local_count + 1.
      zemail_002-sequence        = local_count.
      zemail_002-email_date      = local_date.
      zemail_002-email_time      = local_time.
      zemail_002-email_address   = global_tab_list-phonenum.
      insert zemail_002.

      zemail_001-status = 'U'.
      local_retain = sy-datum + 180.
      concatenate global_tab_desc-jobname local_date local_time into zemail_001-source_program.
      zemail_001-email_date      = local_date.
      zemail_001-email_time      = local_time.
      zemail_001-kunnr           = global_tab_desc-jobname.
      zemail_001-description     = global_tab_desc-description.
      zemail_001-subject         = global_tab_desc-description.
      zemail_001-retain_until    = local_retain.
      zemail_001-status          = 'U'.
      zemail_001-attachment      = '/usr/sap/transfer/sapout/email/redlight.PDF'.
      zemail_001-name_attach     = 'redlight'.
      zemail_001-type_attachment = 'JPG'.
      zemail_001-created_user    = sy-uname.
      insert zemail_001.

      concatenate global_tab_desc-jobname local_date local_time into zemail_003a-source_program.
      zemail_003a-email_date      = local_date.
      zemail_003a-email_time      = local_time.
      zemail_003a-sequence        = 1.
      zemail_003a-message         = global_tab_desc-description.
      insert zemail_003a.
    endif.
  endif.
endloop.
Again if you don't want to use the generic email process I created then you will want to remove all of the entries to ZEMAIL_* and use your own process but that is it in a nutshell I hope you find it of use. If you would like the source with out all of my comments send me an email.
Here is a couple of books on ABAP that are worth a look at if your new to ABAP.











Reblog this post [with Zemanta]