Skip to main content

Overview

The NMISNG::DB module provides database connectivity and operations for NMIS. It manages MongoDB connections, collections, and provides helper methods for database operations. Version: 9.6.5 Location: lib/NMISNG/DB.pm

Database Connection

connect()

Establishes connection to MongoDB database.
config
hash
required
Configuration hash containing database credentials:
  • db_server: MongoDB server address
  • db_name: Database name
  • db_username: Username for authentication
  • db_password: Password for authentication
use NMISNG::DB;

my $db_config = {
    db_server => 'localhost:27017',
    db_name => 'nmis',
    db_username => 'nmis',
    db_password => 'password'
};

my $db = NMISNG::DB::connect($db_config);

Collections

NMIS uses several MongoDB collections:
CollectionPurpose
nodesNode configuration and properties
inventoryInterface and component inventory
eventsActive and historical events
opstatusOperational status cache
queueJob queue for scheduled tasks
logsActivity and audit logs

Database Operations

get_db_handle()

Returns the MongoDB database handle.
my $dbh = $nmisng->get_db_handle();

get_collection()

Returns a specific MongoDB collection.
name
string
required
Collection name (e.g., ‘nodes’, ‘inventory’, ‘events’)
my $nodes_collection = $nmisng->get_collection('nodes');
my $cursor = $nodes_collection->find({group => 'Network'});

Index Management

ensure_indexes()

Creates or updates database indexes for optimal query performance.
$nmisng->ensure_indexes();
This creates indexes on:
  • nodes: name (unique), cluster_id, group
  • inventory: node_uuid, concept, enabled
  • events: node_uuid, event, time, level

Database Maintenance

dbcleanup()

Removes old data based on retention policies.
age
number
Age in days for data retention (default from config)
# Clean events older than 90 days
$nmisng->dbcleanup(age => 90);

compact_database()

Compacts database files to reclaim disk space.
$db->compact_database();
Compaction can be resource-intensive. Run during maintenance windows.

Query Helpers

get_nodes_model()

Query nodes with filtering and pagination.
my $model = $nmisng->get_nodes_model(
    filter => {group => 'Network'},
    fields => {name => 1, host => 1},
    sort => {name => 1},
    limit => 100
);

get_inventory_model()

Query inventory items.
my $model = $nmisng->get_inventory_model(
    filter => {concept => 'interface', enabled => 1}
);

get_events_model()

Query events with filtering.
my $model = $nmisng->get_events_model(
    filter => {level => 'Major', current => 1}
);

Connection Management

Authentication

NMIS uses SCRAM-SHA-256 authentication:
my $client = MongoDB::MongoClient->new(
    host => "mongodb://localhost:27017",
    username => $config->{db_username},
    password => $config->{db_password},
    auth_mechanism => 'SCRAM-SHA-256',
    ssl => $config->{db_ssl} // 0
);

Connection Pooling

MongoDB driver manages connection pooling automatically:
  • Default max pool size: 100
  • Idle timeout: 10 minutes
  • Connection timeout: 30 seconds

Error Handling

eval {
    my $result = $collection->insert_one($document);
};
if ($@) {
    $nmisng->log->error("Database error: $@");
}

Performance Considerations

Indexes

Always query using indexed fields:
  • nodes.name
  • inventory.node_uuid
  • events.time

Projections

Limit returned fields:
my $cursor = $collection->find(
    {group => 'Network'},
    {projection => {name => 1, host => 1}}
);

Bulk Operations

Use bulk operations for multiple updates:
my $bulk = $collection->initialize_ordered_bulk_op;
$bulk->insert_one($_) for @documents;
$bulk->execute();

Configuration

Database settings in Config.nmis:
'database' => {
    'db_server' => 'localhost:27017',
    'db_name' => 'nmis',
    'db_username' => 'nmis',
    'db_password' => 'password',
    'db_ssl' => 0,
    'db_query_timeout' => 30000
}

See Also

Build docs developers (and LLMs) love