pyARS - from python to ARS

Tutorial for usage of pyars.cmdb

pyars.CMDB: introduction

Here is a short example to fetch the list of all classes:

from pyars import cmdb

If this line throws an exception, the CMDB module cannot find the libraries in the system path.

ars=cmdb.CMDB() # initialize the session object
c=ars.Login('server:port', 'user', 'password') # login to the server
r=ars.CMDBGetListClass()  # this call returns a CMDBClassNameIdList

When you want to understand what the function CMDBGetListClass has returned to you, you have to first look at the docstring of the function. It will tell you that:

Output: classNameIdList (CMDBClassNameIdList) or None in case of failure

So, let’s look up the definition of CMDBClassNameIdList in _ccmdbapi63.py (or whatever your version of the CMDB api is). Here you will find something along the lines of:

class CMDBClassNameIdList(Structure):
pass
CMDBClassNameIdList._fields_ = [
# aros.h 428
('numItems', c_uint),
('classNameIdList', POINTER(CMDBClassNameId)),
]

In numItems we get the number of entries in the array classNameIdList. To find out how the CMDBClassNameId is defined is left as an exercise to the reader :-) With that knowledge we can go ahead and print out each classname (together with the namespaceName) in a list.

for i in range(r.numItems):
print '%s:%s' % (r.classNameIdList[i].namespaceName,
r.classNameIdList[i].className)
ars.Logoff()

If you are using an API delivered with the Atrium CMDB prior to version 1.1 patchlevel 002, you must rename all constants with prefix “CMDB” to prefix “AROS” (e.g. AROSClassNameId instead of CMDBClassNameId). The cmdb module provides CMDB equivalents for all AROS function calls, so you don’t have to adapt those names. Unfortunately, the ctypes module will check for AROS data structures; therefore, providing a mapping of CMDB names to AROS names for data structures cannot be easily provided.

Let’s assume you want to get the attributes of the base class. In ccmdb we define the name of the base class, so that you don’t have to change your code between version 1.1 and 2.0 of the CMDB. So the following should work with any CMDB Version 1.1 patch 002 and later.

from pyars import ccmdb
baseclass = ccmdb.CMDBClassNameId(ccmdb.BMC_namespace, ccmdb.BMC_baseclass)
result = ars.CMDBGetAttribute(baseclass, 'AssetID')
print '''Name: %s
attributeId: %s
dataType: %d
attributeType: %d''' % (result.attributeName,
result.attributeId,
result.dataType,
result.attributeType)
# now a nicer output
print '''Name: %s
attributeId: %s
dataType: %s
attributeType: %s''' % (result.attributeName,
result.attributeId,
ccmdb.cmdb_const['CMDB_ATTR_DATA_TYPE'][result.dataType],
ccmdb.cmdb_const['CMDB_ATTR_TYPE'][result.attributeType])

And then, you might want to fetch several attributes at once: Here is an example that starts with one attribute:

cn = ccmdb.CMDBClassNameId('BMC', 'BMC_FileSystemShare')
item = 'Path'
filler = '\\0' * (255-len(item))
tempArray = (ccmdb.ARNameType * 1)()
tempArray[0][:] = (item + filler)[:]
nl = ccmdb.ARNameList(1, tempArray)
r = a.CMDBGetMultipleAttribute(cn, True, True, nl)