示例#1
0
#!/usr/bin/env python
#####################################################
# This script is a basic example of how to remove a   #
# device into Zenoss Resource Manager using the     #
# Zenoss JSON API and the Zenoss RM API Library    #
#####################################################

import sys
import logging
import zenApiLib
from zenApiDeviceRouterHelper import ZenDeviceUidFinder

device_id = sys.argv[1]

response = ZenDeviceUidFinder(name=device_id)

if response.getCount() == 1:
    device_uid = response.getFirstUid()
else:
    print 'Found %s devices.' % (response.getCount())
    sys.exit(1)

dr = zenApiLib.zenConnector(routerName='DeviceRouter')

delete_response = dr.callMethod('removeDevices',
                                uids=device_uid,
                                hashcheck="",
                                action="delete")

if delete_response['result']['success'] == True:
    print 'Successfully deleted device: %s' % (device_uid)
示例#2
0
        rOut = open(args['outFileName'], 'w')
    else:
        rOut = sys.stdout
    api = zenApiLib.zenConnector(cfgFilePath=args['configFilePath'],
                                 section=args['configSection'],
                                 loglevel=args['loglevel'])
    api.setRouter('DeviceRouter')
    if args['dontMoveGraphData']:
        retainData = False
    else:
        retainData = True

    # Rename specified device
    deviceName = args['deviceName']
    newDeviceName = args['newDeviceName']
    results = ZenDeviceUidFinder(name=deviceName)
    if results.getCount() != 1:
        print >> sys.stderr, 'Skipping "{}", found {} devices.'.format(
            deviceName, results.getCount())
        sys.exit()

    devUid = results.getFirstUid()
    apiResult = api.callMethod('renameDevice',
                               uid=devUid,
                               newId=newDeviceName,
                               retainGraphData=retainData)
    if not apiResult['result']['success']:
        print >> sys.stderr, 'Renaming API call failed'
        pprint(apiResult)
    else:
        print >> rOut, "Renaming {} to {} initiated - Keeping data {}".format(
示例#3
0
        except:
            fail()
    data = {'deviceName': device,
            'deviceClass': "/zport/dmd{}".format(deviceClass)}
    return data


def moveDevice(**data):
    '''
    This makes the API call and returns the result
    '''
    dr = zenApiLib.zenConnector(routerName = router)
    response = dr.callMethod(method, **data)
    if response.get('result', {}).get('success', False) is False:
        raise Exception('API call returned unsucessful result.\n%s' % response)
    return response['result']


if __name__ == '__main__':
    '''
    Build the args and make the API call to add the device
    '''
    data = buildArgs()
    deviceFindResults = ZenDeviceUidFinder(name=data['deviceName'])
    if deviceFindResults.getCount() > 1:
        raise Exception('Multiple devices found that matched "%s"' % data['deviceName'])
    data['uid'] = deviceFindResults.getFirstUid()
    api_response = moveDevice(uids=data['uid'], target=data['deviceClass'])
    print(data)
    print(api_response)
示例#4
0
    data = {
        'deviceName': device,
        'deviceClass': deviceClass,
        'productionState': productionState
    }
    return data


def addDevice(data):
    '''
    This makes the API call and returns the result
    '''
    dr = zenApiLib.zenConnector(routerName=router)
    response = dr.callMethod(method, **data)
    if response.get('result', {}).get('success', False) is False:
        raise Exception('API call returned unsucessful result.\n%s' % response)
    return response['result']['new_jobs'][0]['uuid']


if __name__ == '__main__':
    '''
    Build the args and make the API call to add the device
    '''
    data = buildArgs()
    api_response = addDevice(data)
    print('Created job %s... watching job status' % (api_response))
    jobstatus = watchStatus(api_response, 300)
    print('Job status %s, Success: %s' % (jobstatus[0], jobstatus[1]))
    devfind = ZenDeviceUidFinder(sys.argv[1])
    print('Device UID is %s' % (devfind.first()))
示例#5
0
     except Exception as e:
         print("ERROR getting modelerPlugins!", file=sys.stderr)
         print(pformat(apiResult), file=sys.stderr)
         print(e, file=sys.stderr)
         sys.exit(1)
     for mPlugin in args['modelerPlugins'].split("|"):
         if mPlugin not in validModelerPlugins:
             print("ERROR: Specified modelerPlugin '{}' is not an option. " \
                   "Available plugins are: {}".format(
                     mPlugin,
                     validModelerPlugins
                   ), file=sys.stderr)
             sys.exit(2)
 # Loop over specified devices
 for devName in args['deviceNames']:
     results = ZenDeviceUidFinder(name=devName)
     if results.getCount() != 1:
         print('Skipping "{}", found {} devices.'.format(devName, results.getCount()), file=sys.stderr)
         continue
     devUid = results.getFirstUid()
     apiResult = api.callMethod(
         'remodel',
         deviceUid=devUid,
         collectPlugins='"{}"'.format(args['modelerPlugins']),
         background=True
     )
     if not apiResult['result']['success']:
         print('Remodeling API cal failed')
         pprint(apiResult)
         continue
     else: