Best Practices
Guidelines for writing efficient, maintainable, and robust NXSL scripts in NetXMS.
Performance
- Cache DCI lookups
-
Avoid repeating the same DCI lookup on every poll cycle — resolve the DCI once and reuse the result. Use the DCI access methods on the node object (
findDCIById(),findDCIByName(),findDCIByDescription(),findDCIByTag(),findDCIByTagPattern(),findAllDCIs()); the global functions they replace (FindDCIByDescription(), GetDCIValue(), etc.) are deprecated since version 7.0 and produce compilation warnings.// Good: look up the DCI object once, then read its value dci = $node.findDCIByDescription("CPU Usage"); if (dci != null) { value = dci.currentValue; } - Avoid heavy operations in transformation scripts
-
Transformation scripts run every time a DCI value is collected (often every 60 seconds per DCI). Avoid GetAllNodes() or other functions that iterate large datasets in this context.
- Use trace() judiciously
-
The trace() function writes to the server log with the
nxsl.tracetag. Level0is always written (as an informational message); messages with level greater than0are emitted only when the debug level for thenxsl.tracetag is raised to at least that level. In high-frequency contexts (transformations, status polls), excessive level-0 tracing creates I/O overhead — use higher levels for debug output that should normally stay silent.
Error Handling
- Check for null returns
-
Functions like FindObject(), GetDCIValue(), and
createSNMPTransport()can returnnull. Always verify the result before using it.node = FindObject("Core-Router-01"); if (node == null) { trace(1, "Object not found"); return null; } - Use safe navigation
-
The
?.operator provides concise null handling for chained property access.// Instead of checking each step: name = $node?.name; // Returns null if $node is null
Security
- Trusted object validation
-
When scripts execute actions that affect infrastructure, remember that NetXMS can enforce trusted object validation. See Script Security for details.
Maintainability
- Keep transformation scripts simple
-
A transformation script should do one thing: transform a value. If you need complex logic, move it to a separate script in the Script Library and call it from the transformation.
- Use descriptive variable names
-
Especially in scripts stored in the Script Library that others may maintain.
- Add context annotations
-
When storing scripts in the Script Library, note which script context they are designed for (transformation, EPP filter, auto-bind, etc.).
See Also
-
Script Execution Contexts — understand where your scripts run
-
Script Security — trusted object validation
-
Hook Scripts — server hook points