Install and Configure OpenNMS on Centos 7: Free Network Monitoring

Posted on

Install and Configure OpenNMS on Centos 7: Free Network Monitoring

Install and Configure OpenNMS on Centos 7: Free Network Monitoring

This guide aims to teach you how to Install and Configure OpenNMS on Centos 7. OpenNMS is invaluable when you need to monitor numerous devices on your network simultaneously. It’s an open-source and free network monitoring web application that automatically detects and monitors services or nodes within the network. Adding more devices or nodes to OpenNMS is also a straightforward process.

Follow the steps outlined below to set up OpenNMS Horizon on Centos 7.

To complete the OpenNMS monitoring setup on Centos 7, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. You can refer to a guide on Initial Server Setup with Centos 7 for assistance with this.

You also need to have PostgreSQL installed on your server. For instructions, consult a guide on How To Install PostgreSQL 15 on Centos 7.

1. Install Java For OpenNMS Monitoring Setup

Since OpenNMS is written in Java, you need Java installed on your server. Begin by updating your system:

sudo yum update -y

Then, install the required packages using the following command:

sudo yum install vim curl wget -y

Now, install Java on Centos 7:

sudo yum install java-11-openjdk-devel -y

Verify your Java installation by checking its version:

java -version
**Output**
openjdk version "11.0.18" 2023-01-17 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.18.0.10-1.el7_9) (build 11.0.18+10-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.18.0.10-1.el7_9) (build 11.0.18+10-LTS, mixed mode, sharing)

2. Set up OpenNMS Monitoring on Centos 7

The OpenNMS packages are not available in the default Centos 7 repository, so you must add them manually.

Import OpenNMS GPG Key

Import the OpenNMS GPG key using the following command:

sudo rpm --import https://yum.opennms.org/OPENNMS-GPG-KEY

Add OpenNMS Repository

Add the OpenNMS repository to your server using the following command:

sudo yum install https://yum.opennms.org/repofiles/opennms-repo-stable-rhel7.noarch.rpm -y

Install OpenNMS on Centos

Now, you can install OpenNMS:

sudo yum install opennms -y

This will install all the necessary dependencies.

3. Configure PostgreSQL for OpenNMS on Centos 7

After installing the dependencies and OpenNMS, you need to configure PostgreSQL.

First, initialize PostgreSQL:

sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
**Output**
 Initializing database ... OK

Then, start and enable the PostgreSQL service:

# sudo systemctl enable postgresql-15
# sudo systemctl start postgresql-15

Verify that PostgreSQL is active and running:

sudo systemctl status postgresql-15
**Output**
● postgresql-15.service - PostgreSQL 15 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-15.service; enabled; vendor preset: disabled)
   Active: **active** (**running**) since Wed 2023-03-29 03:56:39 EDT; 10s ago
     Docs: https://www.postgresql.org/docs/15/static/
  Process: 9064 ExecStartPre=/usr/pgsql-15/bin/postgresql-15-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 9069 (postmaster)
   CGroup: /system.slice/postgresql-15.service
...

Next, switch to the PostgreSQL user:

sudo -i -u postgres

Create OpenNMS Database and User

Create an OpenNMS monitoring user:

createuser -P opennms
Enter password for new role:
Enter it again:

Then, create the OpenNMS database:

createdb -O opennms opennms

Next, secure the default user with a password:

psql -c "ALTER USER postgres WITH PASSWORD 'StrongPassword';"
**Output**
ALTER ROLE

Exit the PostgreSQL shell:

-bash-4.2$ exit

Modify PostgreSQL Access Policy

Open the PostgreSQL access policy file:

sudo vi /var/lib/pgsql/15/data/pg_hba.conf

Find the following lines and replace ident with md5:

# IPv4 local connections:
host    all             all             127.0.0.1/32            **md5**

# IPv6 local connections:
host    all             all             ::1/128                 **md5**

Save and close the file.

To apply the changes, restart PostgreSQL:

sudo systemctl restart postgresql-15

Define database credentials in OpenNMS Config File

Define the database credentials in the OpenNMS monitoring configuration file:

sudo vi /opt/opennms/etc/opennms-datasources.xml

Find the following lines and define your database credentials:

<jdbc-data-source name="opennms"
                    database-name="opennms"
                    class-name="org.postgresql.Driver"
                    url="jdbc:postgresql://localhost:5432/opennms"
                    user-name="opennms"
                    password="opennms-user-password" />

<jdbc-data-source name="opennms-admin"
                    database-name="template1"
                    class-name="org.postgresql.Driver"
                    url="jdbc:postgresql://localhost:5432/template1"
                    user-name="postgres"
                    password="postgres-password" />
</datasource-configuration>

Save and close the file.

4. Manage OpenNMS Monitoring Service on Centos 7

First, initialize OpenNMS by adding the Java settings:

sudo /opt/opennms/bin/runjava -s
**Output**
runjava: Looking for an appropriate JVM...
runjava: Checking for an appropriate JVM in JAVA_HOME...
runjava: Skipping... JAVA_HOME not set.
runjava: Checking JVM in the PATH: "/usr/lib/jvm/java-11-openjdk-11.0.18.0.10-1.el7_9.x86_64/bin/java"...
runjava: Found an appropriate JVM in the PATH: "/usr/lib/jvm/java-11-openjdk-11.0.18.0.10-1.el7_9.x86_64/bin/java"
runjava: Value of "/usr/lib/jvm/java-11-openjdk-11.0.18.0.10-1.el7_9.x86_64/bin/java" stored in configuration file.

Next, initialize the database and detect system libraries in the /opt/opennms/etc/libraries.properties directory:

sudo /opt/opennms/bin/install -dis

Start and enable the OpenNMS service:

# sudo yum install chkconfig -y
# sudo systemctl enable --now opennms

Verify that the OpenNMS service is active and running:

sudo systemctl status opennms
**Output**
   Loaded: loaded (/usr/lib/systemd/system/opennms.service; enabled; vendor preset: disabled)
   Active: **active** (**running**) since Wed 2023-03-29 04:07:14 EDT; 4s ago
  Process: 10624 ExecStartPost=/bin/sleep 3 (code=exited, status=0/SUCCESS)
  Process: 9636 ExecStart=/opt/opennms/bin/opennms -s start (code=exited, status=0/SUCCESS)
 Main PID: 10618 (java)
   CGroup: /system.slice/opennms.service
           └─10617 bash /opt/opennms/bin/opennms -s start
           └─10618 /usr/lib/jvm/java-11-openjdk-11.0.18.0.10-1.el7_9.x86_64/b..

5. Configure Firewall For OpenNMS

Assuming you have enabled firewalld, OpenNMS listens on port 8980 by default. Allow this port through the firewall:

sudo firewall-cmd --permanent --add-port=8980/tcp

Reload the firewall to apply the changes:

sudo firewall-cmd --reload

If SELinux is enabled, allow the port through SELinux:

sudo semanage port -a -t http_port_t -p tcp 8980

6. Access OpenNMS Web Interface

Access OpenNMS Horizon through the web interface by typing your server’s IP address followed by 8980/opennms in your web browser:

http://<server-ip-address>:8980/opennms

Enter admin as the username and password and click Login.

[OpenNMS Login Image]

You should see the OpenNMS dashboard.

[OpenNMS Dashboard Centos 7 Image]

Change the default password by navigating to admin -> Change Password.

[Change default Admin Password OpenNMS Image]

[Add New Admin Password for OpenNMS Image]

7. How To Monitor Systems with OpenNMS

To monitor systems, you need to add them to OpenNMS. Click on the "+" icon.

[Add node to OpenNMS Image]

Enter the required details such as requisition, IP Address, and Node Label, and click Provision.

[OpenNMS Node Credentials Image]

After the node has been added, it will appear under Info -> Nodes.

You can now view graphs and create alerts for the device.

[Monitor Devices with OpenNMS Centos 7 Image]

For more information, you can visit OpenNMS Documentation.

Conclusion

You have now learned how to Install and Configure OpenNMS Monitoring on Centos 7.

Alternative Solutions for Network Monitoring on CentOS 7

While OpenNMS provides a robust and feature-rich solution for network monitoring, alternative tools and approaches exist. Here are two different ways to achieve similar results on CentOS 7:

1. Using Zabbix:

Zabbix is another popular open-source network monitoring solution known for its scalability and flexibility. Unlike OpenNMS, which primarily relies on a push model for data collection, Zabbix uses both push and pull mechanisms, offering greater control over the monitoring process.

  • Explanation: Zabbix offers a wide array of monitoring capabilities, including network bandwidth utilization, CPU load, disk space, service availability, and custom metric collection via agents or protocols like SNMP. It features a web-based interface for configuration, data visualization, and alerting. Zabbix is highly scalable and suitable for large and complex network environments.

  • Installation and Configuration (Simplified):

    1. Install the Zabbix repository:

      rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
      yum clean all
    2. Install Zabbix server, agent, and web interface:

      yum install zabbix-server-mysql zabbix-agent zabbix-web-mysql -y
    3. Create a MySQL database for Zabbix:

      mysql -u root -p
      CREATE DATABASE zabbix CHARACTER SET utf8 COLLATE utf8_bin;
      GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'localhost' IDENTIFIED BY 'your_zabbix_password';
      FLUSH PRIVILEGES;
      exit
    4. Import the initial schema and data:

      zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -u zabbix -p zabbix
    5. Configure the Zabbix server to use the MySQL database:
      Edit /etc/zabbix/zabbix_server.conf and set the following parameters:

      DBHost=localhost
      DBName=zabbix
      DBUser=zabbix
      DBPassword=your_zabbix_password
    6. Start and enable Zabbix services:

      systemctl start zabbix-server
      systemctl enable zabbix-server
      systemctl start zabbix-agent
      systemctl enable zabbix-agent
    7. Configure the Apache web server for Zabbix:
      Edit /etc/httpd/conf.d/zabbix.conf and uncomment/modify the following lines (adjust timezone as needed):

      php_value max_execution_time 300
      php_value memory_limit 128M
      php_value post_max_size 16M
      php_value upload_max_filesize 2M
      php_value date.timezone Europe/Berlin
    8. Restart the Apache web server:

      systemctl restart httpd
    9. Access the Zabbix web interface at http://<your_server_ip>/zabbix. The default credentials are Admin/zabbix.

  • Code Example: While Zabbix configuration is primarily done through the web interface, custom scripts can be used for complex monitoring tasks. For instance, a script to monitor a specific application log file for errors could be executed by the Zabbix agent and the results sent to the Zabbix server. Here’s a simple example in Python:

    #!/usr/bin/env python
    import os
    import sys
    
    log_file = "/var/log/application.log"
    error_count = 0
    
    if os.path.exists(log_file):
        with open(log_file, "r") as f:
            for line in f:
                if "ERROR" in line:
                    error_count += 1
    
    print(error_count)
    sys.exit(0)

    This script counts the number of lines containing "ERROR" in the specified log file and prints the count to standard output. The Zabbix agent can be configured to execute this script periodically and report the output to the Zabbix server.

2. Using Prometheus and Grafana:

Prometheus is a time-series database and monitoring solution designed for dynamic environments. It excels at collecting and storing metrics from various sources. Grafana is a powerful data visualization tool that integrates seamlessly with Prometheus to create dashboards and visualizations.

  • Explanation: Prometheus uses a pull-based model where it scrapes metrics from configured endpoints. These endpoints expose metrics in a specific format. Grafana then queries Prometheus to display the collected data in customizable dashboards. This combination is excellent for monitoring containerized environments and applications that expose metrics endpoints.

  • Installation and Configuration (Simplified):

    1. Install Prometheus:

      • Download the latest Prometheus release from the official website.
      • Extract the archive: tar xvf prometheus-*.tar.gz
      • Create a user for Prometheus: sudo useradd -M -r -s /bin/false prometheus
      • Move the extracted files to /opt/prometheus: sudo mv prometheus-*/prometheus /opt/prometheus/ and sudo mv prometheus-*/promtool /opt/prometheus/
      • Create directories for data and configuration: sudo mkdir /opt/prometheus/data and sudo mkdir /etc/prometheus
      • Move the example configuration file: sudo mv prometheus-*/prometheus.yml /etc/prometheus/prometheus.yml
      • Change ownership: sudo chown -R prometheus:prometheus /opt/prometheus/ and sudo chown prometheus:prometheus /etc/prometheus/prometheus.yml and sudo chown prometheus:prometheus /opt/prometheus/data
      • Create a systemd service file: /etc/systemd/system/prometheus.service
      [Unit]
      Description=Prometheus
      Wants=network-online.target
      After=network-online.target
      
      [Service]
      User=prometheus
      Group=prometheus
      Type=simple
      ExecStart=/opt/prometheus/prometheus 
          --config.file=/etc/prometheus/prometheus.yml 
          --storage.tsdb.path=/opt/prometheus/data 
          --web.console.path=/opt/prometheus/consoles 
          --web.console.templates=/opt/prometheus/console_libraries
      
      [Install]
      WantedBy=multi-user.target
      • Enable and start Prometheus: sudo systemctl enable prometheus and sudo systemctl start prometheus
    2. Install Grafana:

      • Add the Grafana repository:

        cat <<EOF > /etc/yum.repos.d/grafana.repo
        [grafana]
        name=grafana
        baseurl=https://packages.grafana.com/oss/rpm
        repo_gpgcheck=1
        enabled=1
        gpgcheck=1
        gpgkey=https://packages.grafana.com/gpg.key
        sslverify=1
        sslcacert=/etc/pki/tls/certs/ca-bundle.crt
        EOF
      • Install Grafana: sudo yum install grafana -y

      • Start and enable Grafana: sudo systemctl start grafana-server and sudo systemctl enable grafana-server

    3. Configure Prometheus as a data source in Grafana:
      Access the Grafana web interface at http://<your_server_ip>:3000. The default credentials are admin/admin.

      • Go to Configuration -> Data Sources -> Add data source.
      • Select Prometheus.
      • Enter the Prometheus URL (e.g., http://localhost:9090).
      • Save & Test.
  • Code Example: To expose custom metrics for Prometheus to scrape, you might use a library like prometheus_client in Python:

    from prometheus_client import start_http_server, Summary
    import random
    import time
    
    # Create a metric to track time spent and requests made.
    REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
    
    # Decorate function with metric.
    @REQUEST_TIME.time()
    def process_request(t):
        """A dummy function that takes some time."""
        time.sleep(t)
    
    if __name__ == '__main__':
        # Start up the server to expose the metrics.
        start_http_server(8000)
        # Generate some requests.
        while True:
            process_request(random.random())

    This script starts an HTTP server on port 8000 and exposes metrics that Prometheus can scrape. The REQUEST_TIME metric tracks the time spent processing requests to the process_request function. Prometheus would need to be configured to scrape this endpoint (e.g., http://<your_server_ip>:8000). In Grafana, you could then visualize this data to understand request processing times.

These alternative solutions provide different approaches to network and system monitoring. Zabbix offers a comprehensive, agent-based solution, while Prometheus and Grafana provide a powerful, pull-based system suited for modern, dynamic environments. Choosing the right solution depends on your specific needs and infrastructure. Understanding how to Install and Configure OpenNMS on Centos 7 and these alternatives equips you with a wider range of tools for effective network management.

Leave a Reply

Your email address will not be published. Required fields are marked *