Connect Nagios Core 4.5 with Grafana through NDOUtils and MySQL to create powerful monitoring dashboards. This integration provides advanced visualization capabilities, real-time alerting, and comprehensive monitoring insights for your infrastructure.
Prerequisites
- Nagios Core 4.5 installed and configured
- Root or sudo access
- At least 2GB RAM
- 10GB free disk space
What this solves
This tutorial shows you how to integrate Nagios Core 4.5 with Grafana dashboards for advanced monitoring visualization. By connecting Nagios data through NDOUtils to a MySQL database, you'll create powerful Grafana dashboards that provide real-time insights, historical trending, and customizable alerting for your infrastructure monitoring.
Step-by-step installation
Update system packages
Start by updating your package manager to ensure you get the latest versions of all components.
sudo apt update && sudo apt upgrade -y
Install required dependencies
Install the necessary packages for NDOUtils, MySQL, and build tools that will be needed for compilation.
sudo apt install -y mysql-server mysql-client build-essential libmysqlclient-dev \
make gcc g++ automake autoconf libtool pkg-config libssl-dev wget
Configure MySQL for Nagios integration
Start MySQL service and create a dedicated database and user for NDOUtils to store Nagios monitoring data.
sudo systemctl start mysql
sudo systemctl enable mysql
Secure MySQL installation
Run the MySQL security script to set root password and remove test databases for production security.
sudo mysql_secure_installation
Create Nagios database and user
Connect to MySQL and create the necessary database schema and user permissions for NDOUtils.
sudo mysql -u root -p
CREATE DATABASE nagios;
CREATE USER 'nagios'@'localhost' IDENTIFIED BY 'NagiosDBPassword123!';
GRANT ALL PRIVILEGES ON nagios.* TO 'nagios'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Download and compile NDOUtils
Download the NDOUtils source code and compile it to enable Nagios database integration.
cd /tmp
wget https://github.com/NagiosEnterprises/ndoutils/archive/ndoutils-2.1.3.tar.gz
tar -xzf ndoutils-2.1.3.tar.gz
cd ndoutils-ndoutils-2.1.3
Configure and compile NDOUtils
Run the configuration script and compile NDOUtils with MySQL support enabled.
./configure --prefix=/usr/local/nagios --enable-mysql --disable-pgsql
make all
Install NDOUtils binaries
Install the compiled NDOUtils binaries and configuration files to the Nagios directory.
sudo make install
Import NDO database schema
Import the NDOUtils database schema to create the necessary tables for storing Nagios data.
mysql -u nagios -p nagios < db/mysql.sql
Configure NDOUtils daemon
Create the NDOUtils configuration file to connect to MySQL and process Nagios events.
lock_file=/usr/local/nagios/var/ndo2db.lock
config_file=/usr/local/nagios/etc/ndo2db.cfg
db_servertype=mysql
db_host=localhost
db_port=3306
db_name=nagios
db_prefix=nagios_
db_user=nagios
db_pass=NagiosDBPassword123!
max_timedevents_age=1440
max_systemcommands_age=10080
max_servicechecks_age=5760
max_hostchecks_age=5760
max_eventhandlers_age=44640
debug_level=0
debug_verbosity=1
debug_file=/usr/local/nagios/var/ndo2db.debug
max_debug_file_size=1000000
Configure Nagios NDO module
Configure the NDO broker module to send Nagios events to the NDOUtils daemon.
instance_name=default
output_type=unixsocket
output=/usr/local/nagios/var/ndo.sock
tcp_port=5668
output_buffer_items=5000
buffer_file=/usr/local/nagios/var/ndomod.tmp
file_rotation_interval=14400
file_rotation_timeout=60
reconnect_interval=15
reconnect_warning_interval=900
data_processing_options=-1
config_output_options=2
Update Nagios main configuration
Add the NDO broker module to the main Nagios configuration to enable database integration.
sudo cp /usr/local/nagios/bin/ndomod.o /usr/local/nagios/bin/
broker_module=/usr/local/nagios/bin/ndomod.o config_file=/usr/local/nagios/etc/ndomod.cfg
Create NDOUtils systemd service
Create a systemd service file for NDOUtils to manage the daemon automatically.
[Unit]
Description=NDOUtils Database Daemon
After=mysql.service
Requires=mysql.service
[Service]
Type=forking
User=nagios
Group=nagios
ExecStart=/usr/local/nagios/bin/ndo2db -d -c /usr/local/nagios/etc/ndo2db.cfg
PIDFile=/usr/local/nagios/var/ndo2db.lock
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
Set correct permissions
Set the proper ownership and permissions for NDOUtils files and directories.
sudo chown -R nagios:nagios /usr/local/nagios/var/
sudo chown nagios:nagios /usr/local/nagios/etc/ndo2db.cfg
sudo chown nagios:nagios /usr/local/nagios/etc/ndomod.cfg
sudo chmod 640 /usr/local/nagios/etc/ndo2db.cfg
sudo chmod 640 /usr/local/nagios/etc/ndomod.cfg
Enable and start services
Enable and start both NDOUtils and Nagios services to begin data collection.
sudo systemctl daemon-reload
sudo systemctl enable ndo2db
sudo systemctl start ndo2db
sudo systemctl restart nagios
Install Grafana
Install Grafana from the official repository to create monitoring dashboards.
sudo apt-get install -y software-properties-common
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt-get update
sudo apt-get install -y grafana
Configure Grafana
Configure Grafana with security settings and database options for production use.
[server]
http_port = 3000
domain = example.com
root_url = http://example.com:3000/
[security]
admin_user = admin
admin_password = GrafanaAdminPassword123!
secret_key = SW2YcwTIb9zpOOhoPsMm
[database]
type = sqlite3
path = grafana.db
[auth.anonymous]
enabled = false
[log]
mode = file
level = info
Enable and start Grafana
Enable Grafana to start on boot and start the service to access the web interface.
sudo systemctl daemon-reload
sudo systemctl enable grafana-server
sudo systemctl start grafana-server
Configure firewall access
Open firewall ports for Grafana web interface access while maintaining security.
sudo ufw allow 3000/tcp
sudo ufw reload
Create Grafana MySQL data source
Configure Grafana to connect to the Nagios MySQL database as a data source for creating dashboards.
curl -X POST http://admin:GrafanaAdminPassword123!@localhost:3000/api/datasources \
-H "Content-Type: application/json" \
-d '{
"name": "nagios-mysql",
"type": "mysql",
"url": "localhost:3306",
"database": "nagios",
"user": "nagios",
"secureJsonData": {
"password": "NagiosDBPassword123!"
},
"isDefault": true
}'
Create custom Nagios dashboard
Import a pre-configured dashboard JSON to visualize Nagios host and service data in Grafana.
{
"dashboard": {
"id": null,
"title": "Nagios Infrastructure Overview",
"tags": ["nagios", "monitoring"],
"timezone": "browser",
"panels": [
{
"id": 1,
"title": "Host Status Summary",
"type": "stat",
"targets": [
{
"refId": "A",
"rawSql": "SELECT COUNT(*) as total_hosts FROM nagios_hosts WHERE is_active=1",
"format": "table"
}
],
"gridPos": {"h": 8, "w": 6, "x": 0, "y": 0}
},
{
"id": 2,
"title": "Service Status Distribution",
"type": "piechart",
"targets": [
{
"refId": "A",
"rawSql": "SELECT current_state, COUNT(*) as count FROM nagios_servicestatus GROUP BY current_state",
"format": "table"
}
],
"gridPos": {"h": 8, "w": 6, "x": 6, "y": 0}
},
{
"id": 3,
"title": "Recent Alerts",
"type": "table",
"targets": [
{
"refId": "A",
"rawSql": "SELECT h.display_name as Host, s.display_name as Service, ss.output, FROM_UNIXTIME(ss.last_check) as Last_Check FROM nagios_servicestatus ss JOIN nagios_services s ON ss.service_object_id = s.service_object_id JOIN nagios_hosts h ON s.host_object_id = h.host_object_id WHERE ss.current_state > 0 ORDER BY ss.last_check DESC LIMIT 20",
"format": "table"
}
],
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 8}
}
],
"time": {"from": "now-1h", "to": "now"},
"refresh": "30s"
}
}
Import dashboard to Grafana
Import the custom dashboard configuration into Grafana using the REST API.
curl -X POST http://admin:GrafanaAdminPassword123!@localhost:3000/api/dashboards/db \
-H "Content-Type: application/json" \
-d @/tmp/nagios-dashboard.json
Configure Grafana alerting
Set up alert notifications to integrate with your existing notification systems.
curl -X POST http://admin:GrafanaAdminPassword123!@localhost:3000/api/alert-notifications \
-H "Content-Type: application/json" \
-d '{
"name": "email-alerts",
"type": "email",
"settings": {
"addresses": "admin@example.com",
"subject": "Grafana Alert: {{range .Alerts}}{{.AlertName}}{{end}}"
},
"isDefault": true
}'
Verify your setup
Run these commands to verify that all components are working correctly and data is flowing from Nagios to Grafana.
sudo systemctl status ndo2db
sudo systemctl status nagios
sudo systemctl status grafana-server
mysql -u nagios -p -e "SELECT COUNT(*) FROM nagios.nagios_hosts;"
mysql -u nagios -p -e "SELECT COUNT(*) FROM nagios.nagios_servicestatus;"
Access Grafana at http://your-server-ip:3000 and log in with admin credentials. You should see the Nagios Infrastructure Overview dashboard with live monitoring data. For comprehensive monitoring tutorials, check out our guide on monitoring Linux system resources with automated alerts.
Common issues
| Symptom | Cause | Fix |
|---|---|---|
| NDO2DB fails to start | MySQL connection issues | Check database credentials and MySQL service status |
| No data in Grafana | NDOUtils not processing events | Verify ndomod.o is loaded in nagios.cfg and restart Nagios |
| Permission denied errors | Incorrect file ownership | Run chown nagios:nagios on NDOUtils directories |
| Grafana dashboard shows no data | Wrong MySQL query or data source | Test data source connection and verify table names |
| High CPU usage from ndo2db | Too frequent data processing | Increase data_processing_options intervals in ndo2db.cfg |
Next steps
- Set up centralized log aggregation with ELK Stack for comprehensive log analysis
- Configure Nagios distributed monitoring with NRPE for multi-site monitoring
- Implement Grafana high availability clustering for production resilience
- Configure Nagios Business Process Intelligence for service dependency mapping
Automated install script
Run this to automate the entire setup
#!/usr/bin/env bash
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Default values
NAGIOS_DB_PASSWORD="${NAGIOS_DB_PASSWORD:-NagiosDBPassword$(openssl rand -base64 12)}"
MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD:-}"
# Usage function
usage() {
echo "Usage: $0 [--nagios-db-password PASSWORD] [--mysql-root-password PASSWORD]"
echo " --nagios-db-password: Password for nagios database user (default: auto-generated)"
echo " --mysql-root-password: MySQL root password (default: prompt during installation)"
exit 1
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--nagios-db-password)
NAGIOS_DB_PASSWORD="$2"
shift 2
;;
--mysql-root-password)
MYSQL_ROOT_PASSWORD="$2"
shift 2
;;
-h|--help)
usage
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
usage
;;
esac
done
# Cleanup function for rollback
cleanup() {
echo -e "${RED}Installation failed. Cleaning up...${NC}"
systemctl stop ndo2db 2>/dev/null || true
systemctl stop nagios 2>/dev/null || true
rm -rf /tmp/ndoutils-* 2>/dev/null || true
}
trap cleanup ERR
# Check if running as root or with sudo
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}This script must be run as root or with sudo${NC}"
exit 1
fi
# Auto-detect distribution
if [ -f /etc/os-release ]; then
. /etc/os-release
case "$ID" in
ubuntu|debian)
PKG_MGR="apt"
PKG_INSTALL="apt install -y"
PKG_UPDATE="apt update && apt upgrade -y"
MYSQL_PKG="mysql-server mysql-client libmysqlclient-dev"
BUILD_PKG="build-essential make gcc g++ automake autoconf libtool pkg-config libssl-dev wget tar"
;;
almalinux|rocky|centos|rhel|ol)
PKG_MGR="dnf"
PKG_INSTALL="dnf install -y"
PKG_UPDATE="dnf update -y"
MYSQL_PKG="mysql-server mysql-devel"
BUILD_PKG="gcc gcc-c++ make automake autoconf libtool pkgconfig openssl-devel wget tar"
;;
fedora)
PKG_MGR="dnf"
PKG_INSTALL="dnf install -y"
PKG_UPDATE="dnf update -y"
MYSQL_PKG="mysql-server mysql-devel"
BUILD_PKG="gcc gcc-c++ make automake autoconf libtool pkgconfig openssl-devel wget tar"
;;
amzn)
PKG_MGR="yum"
PKG_INSTALL="yum install -y"
PKG_UPDATE="yum update -y"
MYSQL_PKG="mysql-server mysql-devel"
BUILD_PKG="gcc gcc-c++ make automake autoconf libtool pkgconfig openssl-devel wget tar"
;;
*)
echo -e "${RED}Unsupported distribution: $ID${NC}"
exit 1
;;
esac
else
echo -e "${RED}Cannot detect distribution${NC}"
exit 1
fi
echo -e "${GREEN}[1/12] Updating system packages...${NC}"
$PKG_UPDATE
echo -e "${GREEN}[2/12] Installing required dependencies...${NC}"
$PKG_INSTALL $MYSQL_PKG $BUILD_PKG
echo -e "${GREEN}[3/12] Starting and enabling MySQL service...${NC}"
systemctl start mysqld 2>/dev/null || systemctl start mysql
systemctl enable mysqld 2>/dev/null || systemctl enable mysql
echo -e "${GREEN}[4/12] Securing MySQL installation...${NC}"
if [[ -n "$MYSQL_ROOT_PASSWORD" ]]; then
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD';" 2>/dev/null || \
mysqladmin -u root password "$MYSQL_ROOT_PASSWORD"
else
echo -e "${YELLOW}Please run mysql_secure_installation manually after this script completes${NC}"
fi
echo -e "${GREEN}[5/12] Creating Nagios database and user...${NC}"
if [[ -n "$MYSQL_ROOT_PASSWORD" ]]; then
MYSQL_CMD="mysql -u root -p$MYSQL_ROOT_PASSWORD"
else
MYSQL_CMD="mysql -u root"
fi
$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS nagios;"
$MYSQL_CMD -e "CREATE USER IF NOT EXISTS 'nagios'@'localhost' IDENTIFIED BY '$NAGIOS_DB_PASSWORD';"
$MYSQL_CMD -e "GRANT ALL PRIVILEGES ON nagios.* TO 'nagios'@'localhost';"
$MYSQL_CMD -e "FLUSH PRIVILEGES;"
echo -e "${GREEN}[6/12] Downloading NDOUtils...${NC}"
cd /tmp
wget -O ndoutils-2.1.3.tar.gz https://github.com/NagiosEnterprises/ndoutils/archive/ndoutils-2.1.3.tar.gz
tar -xzf ndoutils-2.1.3.tar.gz
cd ndoutils-ndoutils-2.1.3
echo -e "${GREEN}[7/12] Configuring and compiling NDOUtils...${NC}"
./configure --prefix=/usr/local/nagios --enable-mysql --disable-pgsql
make all
echo -e "${GREEN}[8/12] Installing NDOUtils binaries...${NC}"
make install
echo -e "${GREEN}[9/12] Importing NDO database schema...${NC}"
mysql -u nagios -p$NAGIOS_DB_PASSWORD nagios < db/mysql.sql
echo -e "${GREEN}[10/12] Creating NDOUtils configuration...${NC}"
cat > /usr/local/nagios/etc/ndo2db.cfg << EOF
lock_file=/usr/local/nagios/var/ndo2db.lock
config_file=/usr/local/nagios/etc/ndo2db.cfg
db_servertype=mysql
db_host=localhost
db_port=3306
db_name=nagios
db_prefix=nagios_
db_user=nagios
db_pass=$NAGIOS_DB_PASSWORD
max_timedevents_age=1440
max_systemcommands_age=10080
max_servicechecks_age=5760
max_hostchecks_age=5760
max_eventhandlers_age=44640
debug_level=0
debug_verbosity=1
debug_file=/usr/local/nagios/var/ndo2db.debug
max_debug_file_size=1000000
EOF
cat > /usr/local/nagios/etc/ndomod.cfg << EOF
instance_name=default
output_type=unixsocket
output=/usr/local/nagios/var/ndo.sock
tcp_port=5668
output_buffer_items=5000
buffer_file=/usr/local/nagios/var/ndomod.tmp
file_rotation_interval=14400
file_rotation_timeout=60
reconnect_interval=15
reconnect_warning_interval=900
data_processing_options=-1
config_output_options=2
EOF
echo -e "${GREEN}[11/12] Setting proper permissions and ownership...${NC}"
chown -R nagios:nagios /usr/local/nagios/etc/
chmod 640 /usr/local/nagios/etc/ndo2db.cfg
chmod 640 /usr/local/nagios/etc/ndomod.cfg
chown -R nagios:nagios /usr/local/nagios/var/
chmod 755 /usr/local/nagios/var/
# Create systemd service for ndo2db
cat > /etc/systemd/system/ndo2db.service << EOF
[Unit]
Description=Nagios Data Out Daemon
After=mysql.service nagios.service
[Service]
Type=forking
User=nagios
Group=nagios
ExecStart=/usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg
PIDFile=/usr/local/nagios/var/ndo2db.lock
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable ndo2db
# Add broker module to nagios.cfg if it exists
if [[ -f /usr/local/nagios/etc/nagios.cfg ]]; then
if ! grep -q "ndomod.o" /usr/local/nagios/etc/nagios.cfg; then
echo "broker_module=/usr/local/nagios/bin/ndomod.o config_file=/usr/local/nagios/etc/ndomod.cfg" >> /usr/local/nagios/etc/nagios.cfg
fi
fi
echo -e "${GREEN}[12/12] Starting services...${NC}"
systemctl start ndo2db
systemctl restart nagios 2>/dev/null || echo -e "${YELLOW}Nagios service not found - please start it manually${NC}"
echo -e "${GREEN}Installation completed successfully!${NC}"
echo -e "${YELLOW}Configuration Summary:${NC}"
echo "- Database: nagios"
echo "- Database User: nagios"
echo "- Database Password: $NAGIOS_DB_PASSWORD"
echo "- NDO2DB Config: /usr/local/nagios/etc/ndo2db.cfg"
echo "- NDOMOD Config: /usr/local/nagios/etc/ndomod.cfg"
echo ""
echo -e "${YELLOW}Next Steps:${NC}"
echo "1. Configure Grafana to connect to MySQL database 'nagios'"
echo "2. Import Nagios dashboard templates in Grafana"
echo "3. Verify data is flowing: systemctl status ndo2db"
echo "4. Check MySQL tables: mysql -u nagios -p$NAGIOS_DB_PASSWORD nagios -e 'SHOW TABLES;'"
Review the script before running. Execute with: bash install.sh