Move container to new server

Started by curruscanis, June 19, 2015, 11:52:58 PM

Previous topic - Next topic

curruscanis

Is there a fast way to move or export a "Container" from the Infrastructure Services tree on one server to a new server?

Victor Kirhenshtein

Hi!

No, there are no such standard functionality. However, if you are familiar with Python programming language, you can create simple tool using nxshell that will export node configuration from container into file and then import it into another server. More information about nxshell can be found here: https://wiki.netxms.org/wiki/Using_nxshell_to_automate_bulk_operations.

Best regards,
Victor

technikcst

Hi,

I want to post a small example script for importing/exporting. Maybe someone can improve this script - it is kinda usefull if you want to migrate to a new server without copying the old database.

EXPORT

export_nodes.py:

from org.netxms.client.objects import GenericObject, Node, Interface, AbstractObject
import sys
import pickle
import codecs

# START CONFIG
###############################################################################

printTree = True
exportFilename = "exported_nodes.nxe"
rootID = objects.GenericObject.SERVICEROOT # Infrastructure Services Node

##############################################################################
# END CONFIG


rootNode = s.findObjectById(rootID) # the root node object
exportHandle = codecs.open(exportFilename, 'w', 'utf-8')

print 'Exporting items from node %s' % ( rootNode.getObjectName() )

def getSubItems(parent, depth):
    for node in parent.getChildsAsArray():
        name = node.getObjectName()
        nodeId = node.getObjectId()
        nodeClass = node.getObjectClass()
        #print 'Found object: %s (%d) - class: %d' % (name, nodeId, nodeClass)

        if nodeClass == AbstractObject.OBJECT_CONTAINER:
            #print 'Checking subnode...'

            if printTree == True:
                print str(nodeClass) * (depth + 2) + ";" + name

            exportHandle.write(str(nodeClass) * (depth) + ";" + name + "\n")
            getSubItems(node, depth + 1)
        else:
            if printTree == True:
                print str(nodeClass) * (depth) + ";" + name + ";" + node.getPrimaryIP().getHostAddress()

            loc = node.getGeolocation()
            ip = node.getPrimaryIP().getHostAddress()
            attrs = node.getCustomAttributes()
            comment = node.getComments()
            hassnmp = node.hasSnmpAgent()
            hasnx = node.hasAgent()

            # generate custom attribute string
            attrstr = ''
            for key in attrs: # in python 2.x it is iteritems()
                if attrstr != '':
                    attrstr = attrstr + ':#:'
                attrstr = attrstr + key + '=-=' + attrs.get(key)

            exportHandle.write( str(nodeClass) * (depth) + ";" + name + ";" + ip + ";" + \
            loc.getLatitudeAsString() + ";" + loc.getLongitudeAsString() + ";" + attrstr + ";" + \
            str(hassnmp) + ";" + str(hasnx) + ";" + comment + "\n" )
            #print 'Found device: %s' % (name)

# main
getSubItems(rootNode, 1)

print 'Done! (Exported file: %s)' % ( exportFilename )


Export nodes with this command:
Quote
java -Dnetxms.server=127.0.0.1 -Dnetxms.login=admin -Dnetxms.password=netxms -jar nxshell-2.0-M4.jar export_nodes.py

IMPORT

import_nodes.py:

from org.netxms.client.objects import GenericObject, Node, Interface, AbstractObject
import sys
import java
import codecs

# START CONFIG
###############################################################################

printTree = True
importFilename = "exported_nodes.nxe"
rootID = objects.GenericObject.SERVICEROOT # Infrastructure Services Node

##############################################################################
# END CONFIG


rootNode = s.findObjectById(rootID) # the root node object

print 'Importing items from node %s' % ( importFilename )

parentId = rootID
parentTree = [rootID]
with codecs.open(importFilename, 'r', 'utf-8') as f:
    for line in f:
        arr = line.split(';')
        # first element specifies type and depth
        depth = len(arr[0])
        nodetype = int(arr[0][0])
        print 'found item: type = %d, depth = %d' % (nodetype, depth)

        # all fields mapped to variable names
        name = arr[1].strip()

        if nodetype == AbstractObject.OBJECT_NODE:
            ip = arr[2]
            ipaddress = java.net.InetAddress.getByName(ip)
            ipnetmask = java.net.InetAddress.getByName("255.255.255.0")
            lat = arr[3]
            lng = arr[4]
            attrs = arr[5]
            hassnmp = bool(arr[6])
            hasnx = bool(arr[7])
            comment = arr[8].strip()

            flags = 0
            if hasnx == False:
                flags |= NXCObjectCreationData.CF_DISABLE_NXCP
            if hassnmp == False:
                flags |= NXCObjectCreationData.CF_DISABLE_SNMP

        if nodetype == AbstractObject.OBJECT_CONTAINER:
            # create a new container and set it as current root
            print 'Creating Folder: %s in parent: %d' % (name, parentId)
            cd = NXCObjectCreationData(objects.GenericObject.OBJECT_CONTAINER, name, parentTree[depth-1]);
            folderId = session.createObject(cd) # createObject return ID of newly created object

            if (len(parentTree)-1) > depth:
                parentTree[depth] = folderId
            else:
                parentTree.insert(depth, folderId)
        else:
            # create a new node in the current container
            print 'Creating Node: %s - %s' % (name, ip)
            cd = NXCObjectCreationData(objects.GenericObject.OBJECT_NODE, name, parentTree[depth-1]);
            cd.setCreationFlags(flags);
            cd.setComments(comment);
            #cd.setIpAddress(ipaddress);
            #cd.setIpNetMask(ipnetmask);
            cd.setPrimaryName(ip) # Create node with IP address
            nodeId = session.createObject(cd)

        #print 'Line: %s' % (line)

print 'Done!'


Import nodes with this command:
Quote
java -Dnetxms.server=127.0.0.1 -Dnetxms.login=admin -Dnetxms.password=netxms -jar nxshell-2.0-M4.jar import_nodes.py


Especially the import script might need some improvement to allow importing of custom attributes and so on.

I hope this is helpful!