Using nxshell to automate bulk operations

From NetXMS Wiki
Revision as of 14:11, 9 November 2016 by Victor (talk | contribs)
Jump to navigation Jump to search

Introduction

NxShell is based on Jython and provide access to NetXMS Java API using interactive shell. NxShell is build as single jar file, which includes all required libraries.

Download: http://www.netxms.org/download/nxshell-1.2.13.jar (change 1.2.13 to required version)

Usage

Start as interactive shell:

java -jar nxshell-1.2.15.jar

Execute script "test.py":

java -jar nxshell-1.2.15.jar test.py

When NxShell is started, it tries to get server IP, login and password from command line parameters. In interactive mode, user will be asked for details, otherwise default values will be used.

Start as interactive shell, with IP and Login provided (password will be asked):

java -Dnetxms.server=127.0.0.1 -Dnetxms.login=admin -jar nxshell-1.2.15.jar

Properties

These properties should be set with JVM's "-D" option. Please make sure that all "-D" options are before "-jar".

Parameter Default Value
netxms.server 127.0.0.1
netxms.login admin
netxms.password netxms
netxms.encryptSession true

Scripting

For details on API please refer to javadoc at http://www.netxms.org/documentation/javadoc/latest/.

NxShell provide user with already connected and synchronised session to simplify scripting. Most required packages are imported as well to minimize typing.

Global Variables

Variable Type Notes
session org.netxms.client.NXCSession
s org.netxms.client.NXCSession Alias for "session"

Helper Functions

Sample Scripts

Create Container and some nodes

parentId = objects.GenericObject.SERVICEROOT # Infrastructure Services root
cd = NXCObjectCreationData(objects.GenericObject.OBJECT_CONTAINER, "Sample Container", parentId);
containerId = session.createObject(cd) # createObject return ID of newly created object
print '"Sample Container" created, id=%d' % (containerId, )

flags = NXCObjectCreationData.CF_DISABLE_ICMP | \
        NXCObjectCreationData.CF_DISABLE_NXCP | \
        NXCObjectCreationData.CF_DISABLE_SNMP
for i in xrange(0, 5):
    name = "Node %d" % (i + 1, )
    cd = NXCObjectCreationData(objects.GenericObject.OBJECT_NODE, name, containerId);
    cd.setCreationFlags(flags);
    cd.setPrimaryName("0.0.0.0") # Create node without IP address
    nodeId = session.createObject(cd)
    print '"%s" created, id=%d' % (name, nodeId)

Change Object comments

for name in open("nodes.txt").readlines():
    node = session.findObjectByName(name.strip())
    if node:
        comments = "New Comment"
        existingComments = node.getComments() # will return null if there are no comments
        if existingComments:
            comments += "\n" + existingComments
        session.updateObjectComments(node.getObjectId(), comments)

Manage / Unmanage interfaces based on the name

(works for any Object).

from org.netxms.client.objects import GenericObject, Node, Interface

for name in open("nodes.txt").readlines():
    node = session.findObjectByName(name.strip())
    if node:
        for interface in node.getAllChilds(GenericObject.OBJECT_INTERFACE): # 3 == interface
            name = interface.getObjectName()
            if name.startswith('lo'):
                session.setObjectManaged(interface.getObjectId(), True)
            else:
                session.setObjectManaged(interface.getObjectId(), False)

Disable SNMP polling for node

from org.netxms.client.objects import Node

for name in open("nodes.txt").readlines():
    node = session.findObjectByName(name.strip())
    if node:
        md = NXCObjectModificationData(node.getObjectId())
        newFlags = node.getFlags() | Node.NF_DISABLE_SNMP
        md.setObjectFlags(newFlags)
        session.modifyObject(md)

Export list of interfaces to CSV file

import csv, sys

w = csv.writer(sys.stdout, dialect='excel')
w.writerow(['node_id', 'interface_id', 'name', 'ip', 'mask']) # Header

for node in [o for o in s.getAllObjects() if isinstance(o, objects.Node)]: # filter all objects for objects.Node
    for interface in node.getAllChilds(objects.GenericObject.OBJECT_INTERFACE):
        w.writerow([
            node.getObjectId(),
            interface.getObjectId(),
            node.getObjectName(),
            interface.getPrimaryIP().getHostAddress(),
            interface.getSubnetMask().getHostAddress()
        ])

List free(administratively down) ethernet ports on switches

import csv
import sys

w = csv.writer(sys.stdout, dialect='excel')
w.writerow(['node_id', 'interface_id', 'name', 'ip', 'mask']) # Header

for node in filter(lambda x: isinstance(x, objects.Node), s.getAllObjects()):
    allInterfaces = node.getAllChilds(objects.GenericObject.OBJECT_INTERFACE)
    # iftype=6 - ethernetCsmacd, http://www.net-snmp.org/docs/mibs/interfaces.html#IANAifType
    #            or check IFTYPE_* constants in src/nms_common.h
    interfaces = filter(lambda i: i.getIfType==6 and i.getOperState() == objects.Interface.ADMIN_STATE_DOWN, allInterfaces)
    for interface in interfaces:
        w.writerow([
            node.getObjectId(),
            interface.getObjectId(),
            node.getObjectName(),
            interface.getPrimaryIP().getHostAddress(),
            interface.getSubnetMask().getHostAddress()
        ])

Set expected interface state

import org.netxms.client.NXCObjectModificationData

newExpectedState=0 # 0..UP

for node in [o for o in s.getAllObjects() if isinstance(o, objects.Node)]: # filter all objects for objects.Node
	for interface in node.getAllChilds(objects.GenericObject.OBJECT_INTERFACE):
		currentExpectedState = interface.getExpectedState()
		print 'Node "%s" interface "%s" had expected state %d, setting to %d' % (node.getObjectName(), interface.getObjectName(), currentExpectedState, newExpectedState)
		data = NXCObjectModificationData(interface.getObjectId())
		data.setExpectedState(newExpectedState)
		session.modifyObject(data)

Show all alarms

for alarm in session.getAlarms().values():
 print '%s (%d): %s' % (session.getObjectName(alarm.getSourceObjectId()), alarm.getCurrentSeverity(), alarm.getMessage())

Show all nodes with matching SNMP OID

OID mask is either passed as command line parameter, or hardcoded value used.

import sys
from fnmatch import fnmatch

if len(sys.argv) > 1:
  mask = sys.argv[1]
else:
  mask = ".1.3.6.1.*"

nodes = filter(lambda x: isinstance(x, objects.Node), s.getAllObjects())
matched = filter(lambda x: fnmatch(x.getSnmpOID(), mask), nodes)
for node in matched:
  print node.getObjectId(), node.getObjectName(), node.getPrimaryIP().getHostAddress()

Find MAC address

MAC address to find passed as parameter.

import sys
mac = MacAddress.parseMacAddress(sys.argv[1].upper())
if not mac:
    print 'Cannot parse MAC address'
else:
    print 'Parsed MAC:', mac
    cp = s.findConnectionPoint(mac)
    if not cp:
        'No connection found'
    else:
        host = s.findObjectById(cp.getLocalNodeId())
        bridge = session.findObjectById(cp.getNodeId())
        iface = s.findObjectById(cp.getInterfaceId())

        if bridge and iface:
            if cp.getType() == ConnectionPointType.WIRELESS:
                if host:
                    print "Node %s is connected to wireless access point %s/%s" % (host.getObjectName(), bridge.getObjectName(), iface.getObjectName())
                else:
                    if cp.getLocalIpAddress():
                        print "Node with IP address %s and MAC address %s is connected to wireless access point %s/%s" % (cp.getLocalIpAddress().getHostAddress(), cp.getLocalMacAddress(), bridge.getObjectName(), iface.getObjectName())
                    else:
                        print "Node with MAC address %s is connected to wireless access point %s/%s" % (cp.getLocalMacAddress(), bridge.getObjectName(), iface.getObjectName())
            else:
                if host:
                    print "Node %s is %s connected to network switch %s port %s" % (host.getObjectName(), "directly" if cp.getType() == ConnectionPointType.DIRECT else "indirectly", bridge.getObjectName(), iface.getObjectName())
                else:
                    if cp.getLocalIpAddress():
                        print "Node with IP address %s and MAC address %s is %s connected to network switch %s port %s" % (cp.getLocalIpAddress().getHostAddress(), cp.getLocalMacAddress(), "directly" if cp.getType() == ConnectionPointType.DIRECT else "indirectly", bridge.getObjectName(), iface.getObjectName())
                    else:
                        print "Node with MAC address %s is %s connected to network switch %s port %s" % (cp.getLocalMacAddress(), "directly" if cp.getType() == ConnectionPointType.DIRECT else "indirectly", bridge.getObjectName(), iface.getObjectName())
        else:
            print 'No connection found'

Reset retention time for all DCIs on all nodes to default

from org.netxms.client.datacollection import DataCollectionConfiguration, DataCollectionObject
from org.netxms.client.objects import Node, Template

for o in filter(lambda x: (isinstance(x, Node) or isinstance(x, Template)), session.getAllObjects()):
   config = session.openDataCollectionConfiguration(o.getObjectId())
   for dc in config.getItems():
      if dc.getTemplateId() == 0:
         dc.setRetentionTime(0)
         config.modifyObject(dc)
   config.close()
   print "%s %s updated" % ("Node" if isinstance(o, Node) else "Template", o.getObjectName())