Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

I've never written a python script before, but I've heard comments for years about how much simpler and cleaner it is to code in python than perl.  So, after my previous series of perl blogs, I got inspired to port my perl script to python.  And, thanks to Piers Harding coding the python connector to the SAP NW RFC SDK, this was possible.

Installation was fairly easy.  Of course, running Linux, python was already there.  So I downloaded PyYAML (a requirement for sapnwrfc on python),  uncompressed it, and installed with:

$ tar xvzf PyYAML-3.08.tar.gz 
$ cd PyYAML-3.08
$ sudo python setup.py install

Then, I downloaded and uncompressed sapnwrfc for python, and installed it:

$ tar xvzf sapnwrfc-0.06.tar.gz 
$ cd sapnwrfc-0.06
$ sudo python setup.py install

And I was off to the races.

But how to code in python?  The first place I looked was the examples that come with sapnwrfc.  Whenever I'm writing a new script, I like to find someone else's script I can steal copy and modify to save some time.  The example scripts made it clear how to define the connection to my ERP system, as well as the syntax for calling a function module.  So, a short script that executed TH_SERVER_LIST took only a few minutes.

Next, I had to find out how to add the search text.  This was relatively simple, using python support for regular expressions.  Very similar to how perl does it.

After that, a quick google search revealed how to send an smtp alert; copy and paste the code, and - voilà!

Now, I don't get caught up in all the bickering over which programming language is better, or cooler, or cleaner, or mauve.  I'm sure either one can get the job done quite nicely.  I'll probably stick with perl, since that's what I'm used to, but this was a fun exercise.

And with that, here's the script.  I'm sure it's not great, as my first and only attempt at python so far, but it's complete and it works.

#!/usr/bin/python

import sapnwrfc
import re
import smtplib

sapnwrfc.base.config_location = 'rqa.yml'
sapnwrfc.base.load_config()
conn = sapnwrfc.base.rfc_connect()

searchtext = ['Communication error', 'Error in TemSe Management', 'session.*deleted']
dumptext = ""

### Enumerate the list of available servers
fa = conn.discover("TH_SERVER_LIST")
a = fa.create_function_call()
a.invoke()
z = a.LIST.value

### Log on to the XMB Interface
fb = conn.discover("SXMI_LOGON")
b = fb.create_function_call()
b.EXTCOMPANY("TWDC")
b.EXTPRODUCT("RFCTEST")
b.INTERFACE("XMB")
b.VERSION("0.1")
b.invoke()
y = b.SESSIONID.value
# print "Session ID: ", y

### For each app server, read its system log
for i in range(len(z)):
    fc = conn.discover("SXMI_XMB_SYSLOG_READ")
    c = fc.create_function_call()
    c.SERVER_NAME(z[i]['NAME'].rstrip())
    c.EXTERNAL_USER_NAME("GOOFY")
    c.invoke()
    x = c.SYSLOG_TBL.value
    for i in range(len(x)):
        # print x[i]['LINE'].rstrip()
        for s in searchtext:
            if re.search(s, x[i]['LINE']):
                dumptext = dumptext + x[i]['LINE'].rstrip() + "\n"



### Log off of the XMB Interface
fd = conn.discover("SXMI_LOGOFF")
d = fd.create_function_call()
d.INTERFACE("XMB")
d.invoke()

conn.close()

### send alert
if dumptext:
    print "Emailing an alert on the following: \n", dumptext
    SERVER = "localhost"
    FROM = "goofy@company.com"
    TO = ["basis.team@company.com"]
    SUBJECT = "Email alert from python"
    TEXT = dumptext
    message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, message)
    server.quit()

 

And here it is being executed:

3 Comments