Difference between revisions of "Using nxshell to automate bulk operations"

Jump to navigation Jump to search
m
Text replacement - "^" to "{{deprecated}}"
m (Text replacement - "^" to "{{deprecated}}")
 
(9 intermediate revisions by 5 users not shown)
Line 1: Line 1:
= Introduction =
{{deprecated}}= 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.
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.7.jar (change 1.2.7 to required version)
Download: http://www.netxms.org/download/nxshell-1.2.13.jar (change 1.2.13 to required version)


= Usage =
= Usage =
Start as interactive shell:
Start as interactive shell:
  java -jar nxshell-1.2.5.jar
  java -jar nxshell-{{version}}.jar


Execute script "test.py":
Execute script "test.py":
  java -jar nxshell-1.2.5.jar test.py
  java -jar nxshell-{{version}}.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.
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):
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.5.jar
  java -Dnetxms.server=127.0.0.1 -Dnetxms.login=admin -jar nxshell-{{version}}.jar


= Properties =
= Properties =
Line 190: Line 190:
for node in matched:
for node in matched:
   print node.getObjectId(), node.getObjectName(), node.getPrimaryIP().getHostAddress()
   print node.getObjectId(), node.getObjectName(), node.getPrimaryIP().getHostAddress()
</syntaxhighlight>
== Find MAC address ==
MAC address to find passed as parameter.
<syntaxhighlight lang="python">
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'
</syntaxhighlight>
== Reset retention time for all DCIs on all nodes to default ==
<syntaxhighlight lang="python">
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.allObjects):
  config = session.openDataCollectionConfiguration(o.objectId)
  for dc in config.items:
      if dc.templateId == 0:
        dc.setRetentionTime(0)
        config.modifyObject(dc)
  config.close()
  print "%s %s updated" % ("Node" if isinstance(o, Node) else "Template", o.objectName)
</syntaxhighlight>
== List Inventory Software for all nodes ==
<syntaxhighlight lang="python">
for node in [o for o in s.getAllObjects() if isinstance(o, objects.Node)]:
  print 'Node: ', node.getObjectName()
  try:
    for package in s.getNodeSoftwarePackages(node.getObjectId()):
        print package.getName()
  except:
    print "No package found"
</syntaxhighlight>
== Enter/Leave Maintenance for node ==
<syntaxhighlight lang="python">
from org.netxms.client.objects import Node
import org.netxms.client.NXCSession
NETXMS_NODE = "__NodeName__"
STATE = False or True
node = session.findObjectByName(NETXMS_NODE.strip())
if node:
  session.setObjectMaintenanceMode(node.getObjectId(), STATE)
</syntaxhighlight>
</syntaxhighlight>

Navigation menu