openITCOCKPIT Blog

openITCOCKPIT - Nagios and Naemon configuration import - Beta

05.02.2018


Are you using Nagios or Naemon and want try openITCOCKPIT? No problem! We created a PHP script, which tries to import your existing Nagios or Naemon configuration to a fresh instalation of openITCOCKPIT.

            <div class="alert alert-warning">
              <strong>Warning!</strong>
              <br />
              <b>We highly recommend to use a separate Linux system for this task!</b> For example a virtual machine running <i>Ubuntu Xenial</i>.
              <br />
              Do not run this on your production monitoring server! This will may cause an outage!
            </div>
        </p>
    </div>
    
    <div class="col-md-12 col-sm-12 col-xs-12">
        <h2>Why is this beta?</h2>
        <p>
            Once in a week our developers can choose a random project, they like to work on. We call this <i>Happy Friday</i>.
            <br />
            Unfortunately we don't have the time to finish every project by 100 percent.
            Whenever we created something that could be useful, we publish it to our
            <a href="https://github.com/it-novum" target="_blank">GitHub account</a>.
            <br />
            So maybe we missed a feature, or something is not working as expected but it is still worth to give it a shot.
            This is basically the reason why we call it beta, we don't want to disappoint you &#x1F609;.
            <br />
            Also the migration setup is a bit complex at the moment and it would help, if you are an experienced linux user.
            <br />
            The project was successfully tested with different Nagios and Naemon configurations.
        </p>
    </div>
</div>

Requirements

  • Fresh system of Ubuntu Xenial
  • PHP ≥ 5.5.9
  • PHP Composer
  • MySQL-Server
  • Working Nagios or Naemon configuration
  • Fresh installation of openITCOCKPIT (on a separate machine)

    <div class="col-md-12 col-sm-12 col-xs-12">
        <h2>Limitations</h2>
        <p>
            By writing your configurations files by hand, you have a lot of freedoms.
            Unfortunately it is very hard to automatically import complex configuration files.
            <br />
            Instead of implementing every possible scenario, we decided to implement a default behavior which should fit 90% of the configurations out there.
            <h3>Timeperiods</h3>
            openITCOCKPIT only supports time periods, which are using weekdays defined by there name, like in the following example:
            <p>
define timeperiod{
    timeperiod_name                     24x7
    alias                               24x7
    monday                              00:00-24:00
    tuesday                             00:00-24:00
    wednesday                           00:00-24:00
    thursday                            00:00-24:00
    friday                              00:00-24:00
    saturday                            00:00-24:00
    sunday                              00:00-24:00
}
                So maybe your definition of some complex time periods is broken after the import. If you use complex time periods,
                you should check this after the import process is done.
            </p>
            
            <h3>Host- and Service templates</h3>
            <p>
                openITCOCKPIT requires an host or service template for every host or service, that should be created. You can read more about
                
                
                
                host and service templates in our <a href="/beginners/3-create-your-first-host">Beginners guide</a>. Due to the fact, that the objects which
                are defined with <code>register 0</code> are missing in the database, our import script needs to "guess" them.
                
                <h4>Host templates</h4>
                <p>
                    Every host that will be created by the import script, simply uses the <code>default host</code> template with the id <code>1</code>.
                    <br />
                    This host template exists by default on every openITCOCKPIT system.
                </p>
                
                <h4>Service templates</h4>
                <p>
                    The import script will create an service template of every <code>check command</code>, which is defined in your configuration files.
                    <br />
                    The service template name, will be the same as the command name, converted to uppercase. So the check command <code>check_ping</code> will become
                    the service template <code>CHECK_PING</code>.
                    <br />
                    To keep the templates as generic as possible, the importer will set the following fields <code>check_interval</code>, <code>retry_interval</code>, <code>notification_interval</code>, <code>active_checks_enabled</code> and <code>process_perf_data</code> to the most used value.
                    <br />
                    <br />
                    For example, you have ten Services using the check command <i>check_ping</i> with an <i>check_interval</i> of <code>1</code> and two services with an <i>check_interval</i> of <code>15</code>. Than the service template will use the value <code>1</code> as default value for <code>check_interval</code>.
                    <br />
                    Please notice: The custom value for <i>check_interval</i> is not lost! The service will just overwrite the service template value with <code>15</code>.
                </p>
            </p>
            
            <h3>Host- and Service escalations</h3>
            <p>
                The migration uses <a href="https://statusengine.org/" target="_blank">Statusengine</a> as database backend, which does not support the
                dumping host and service escalation configuration to the database anymore.
                <br />
                For this reason, you need to recreate your host and services escalations after the migration process.
            </p>
            
            <h3>Host- and Service depandencies</h3>
            <p>
                Same as for Host- and Service escalations. You need to recreate your host and services depandencies after the migration process.
            </p>
        </p>
    </div>
    
    <div class="col-md-12 col-sm-12 col-xs-12">
        <h2>Install openITCOCKPIT</h2>
        <p>
            First of all, you need to install an instance of openITCOCKPIT. Please visit the <a href="/download/" target="_blank"> download section</a>
            for more information.
        </p>
    </div>
        
    
    <div class="col-md-12 col-sm-12 col-xs-12">
        <h2>Build up the importer system</h2>
        <p>
            <div class="alert alert-warning">
              <strong>Warning!</strong>
              <br />
              <b>We highly recommend to use a separate Linux system for this task!</b> For example a virtual machine running <i>Ubuntu Xenial</i>.
              <br />
              Do not run this on your production monitoring server! This will may cause an outage!
            </div>
            We will use <b>Ubuntu Xenial (16.04)</b> in this example.
            <br />
            <b>All commands needs to be executed by user <code>root</code>!</b>
        </p>
        <h3>1. Install Naemon Core</h3>
        <p>We will just use this machine to dump your monitoring configuration to a MySQL database. It is <b>not</b> important that the checks really work!</p>
        <p>It does not matter if your current monitoring configuration is running on Nagios 3.x, Nagios 4.x or Naemon.</p>
        <p>
cd /tmp
wget https://labs.consol.de/naemon/release/v1.0.6/ubuntu16.04/amd64/naemon-core_1.0.6_ubuntu16.04_amd64.deb
wget https://labs.consol.de/naemon/release/v1.0.6/ubuntu16.04/amd64/libnaemon_1.0.6_ubuntu16.04_amd64.deb
dpkg -i libnaemon_1.0.6_ubuntu16.04_amd64.deb naemon-core_1.0.6_ubuntu16.04_amd64.deb
apt-get install -f -y
systemctl stop naemon
systemctl stop postfix
        </p>
        
        <h3>2. Install Statusengine database backend</h3>
        <p>Statusengine (2.x) will dump all Naemon configuration items to a MySQL database.</p>
        <p>
apt-get install mysql-server
cd /tmp
wget https://github.com/nook24/statusengine/archive/2.1.3-1.tar.gz
tar xfv 2.1.3-1.tar.gz
cd statusengine-2.1.3-1/
./install.sh
cp statusengine/src/statusengine-naemon-1-0-5.o /opt/statusengine/statusengine-naemon-1-0-5.o
echo "broker_module=/opt/statusengine/statusengine-naemon-1-0-5.o use_log_data=0" > /etc/naemon/module-conf.d/statusengine.cfg
        </p>
        <h4>Database setup</h4>
        <p>
            Now you need to connect Statusengine with your database. Due to the fact, that the system will only live for a very short time, we
            will use the MySQL <code>root</code> user for everything.
        </p>
        <p>
            Open the file <code>/opt/statusengine/cakephp/app/Config/database.php</code> and search for the line <code>public $legacy = array(</code>.
            <br />
            Now pass your mysql root credentials, like in this example:
public $legacy = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => 'passw0rd',
    'database' => 'naemon',
    'prefix' => 'naemon_',
    'encoding' => 'utf8',
);
        </p>
        <p>
            Now we can creat the database and table schema.
cd /tmp/statusengine-2.1.3-1/
mysql -u root -p < sql/naemon.sql
/opt/statusengine/cakephp/app/Console/cake schema update -y --plugin Legacy --file legacy_schema_innodb.php --connection legacy
        </p>
        
        <h3>3. Import your existing configuration</h3>
        <p>
            Copy the file <code>objects.cache</code> from your current monitoring system to <code>/etc/naemon/conf.d/</code>.
scp root@current-monitoring.lan:/opt/naemon/var/objects.cache /etc/naemon/conf.d/objects.cache
        </p>
        <p>
            If you don't know where this file is located on your currently running monitoring machine, open the nagios.cfg or naemon.cfg and look out for the line
            <code>object_cache_file=/opt/naemon/var/objects.cache</code>.
        </p>
        
        <h4>Load your existing configuration</h4>
        <p>
            After you copied the file <code>objects.cache</code>, you are new ready to load the configuration in the file <code>/etc/naemon/naemon.cfg</code>.
            <br />
            Remove all predefined <code>cfg_dir</code> and <code>cfg_file</code> lines and add the following two lines:
cfg_dir=/etc/naemon/conf.d/templates/
cfg_file=/etc/naemon/conf.d/objects.cache
        </p>
        
        <h3>4. Start Naemon Core - in foreground mode</h3>
        <p>
            As already mentioned, we only need this system to dump all configuration objects to the MySQL database.
            <br />
            For this reason, we switch the the user <code>naemon</code> and start Naemon Core in foreground.
sudo -u naemon /bin/bash
naemon /etc/naemon/naemon.cfg
            Press <code>CTRL+C</code> as soon as you can see these line (or after 10 minutes):
            <br />
            <code>Successfully launched command file worker with pid 21162</code>
            <br />
            At this point, all configuration objects where put to the <code>gearman-job-server</code> and we don't need the Naemon Core anymore.
            <br />
            Type <code>exit</code> to switch back to the user <code>root</code>
        </p>
        
        <h3>5. Start Statusengine - in foreground mode</h3>
        <p>
            Now you need to start Statusengine, also in foreground mode, to get all required data into the database.
            <pre><code>/opt/statusengine/cakephp/app/Console/cake statusengine_legacy -w</pre></code>
        </p>
        <p>
            As soon as you can see the line <code>Info: Finished dumping objects</code> you can kill Statusengine by pressing <code>CTRL+C</code>.
            <br />
            Alternatively you can kill it after 10 minutes.
        </p>
    </div>
    
    <div class="col-md-12 col-sm-12 col-xs-12">
        <h2>Migrate configuration to openITCOCKPIT</h2>
        <p>
            The openITCOCKPIT Configuration Importer it self is a PHP script. It will connect the the MySQL database to get all your
            configuration items, and create all objects on the openITCOCKPIT system via the HTTP API.
        </p>
        <h3>1. Install PHP Composer</h3>
apt-get install git php-zip php-mysql

cd /tmp
curl -o composer-setup.php  https://getcomposer.org/installer
php composer-setup.php
cp composer.phar /usr/local/bin/composer
        <h3>2. Download openITCOCKPIT Nagios Migration tool</h3>
        <p>

cd /tmp
git clone https://github.com/it-novum/openITCOCKPIT-configuration-import.git
cd openITCOCKPIT-configuration-import/
composer install
        </p>
        
        <h3>3. Copy resource.cfg</h3>
        <p>
            If you also want to migrate all <code>$USERx$</code> macros, you need to copy your <code>resource.cfg</code> from your current
            monitoring system.
scp root@current-monitoring.lan:/opt/openitc/nagios/etc/resource.cfg /tmp/openITCOCKPIT-configuration-import/resource.cfg
        </p>
        <p>
            If you don't know where this file is located on your currently running monitoring machine, open the nagios.cfg or naemon.cfg and look out for the line
            <code>resource_file=/opt/openitc/nagios/etc/resource.cfg</code>.
        </p>
        
        <h3>4. Configuration</h3>
        <p>You can set various configuration options. Open the file <code>etc/config.yml</code> and adjust the folowing values to your environment</p>
        <p>

# Set the same value, as you use in your nagios.cfg or naemon.cfg.
interval_length: 60

# Path where the Importer will search for the resource.cfg
resource_cfg: resource.cfg

# 1 is the default config type of Statusengine - do not change
config_type: 1

# MySQL database host
mysql_host: 127.0.0.1

# MySQL database user
mysql_user: root

# MySQL database password
mysql_password: passw0rd

# MySQL database
mysql_database: naemon

# MySQL database table prefix
mysql_table_prefix: naemon

# IP address of your openITCOCKPIT V3 System
oitc_host: 192.168.1.10

# e-mail address for login
oitc_user: admin@openitcockpit.io

# Password of the given user
oitc_password: password123
        </p>
        
        <h3>5. Start migration</h3>
        <p>Now you are ready to start the migration process.</p>
        <p>
php bin/Importer.php
        </p>
        
        <div class="col-xs-12">
        <a href="/img/openITCOCKPIT-nagios-importer.png" data-lightbox="repository-blog-set" data-title="openITCOCKPIT Nagios/Naemon configuration importer">
            <img src="/img/openITCOCKPIT-nagios-importer.png" class="img-responsive padding-bottom-15" alt="openITCOCKPIT Nagios/Naemon configuration importer"/>
        </a>
        </div>
        
        <p>
            If the process is finished, you can destroy this machine and continue working with openITCOCKPIT!
        </p>
        
    </div>
    
    <div class="col-md-12 col-sm-12 col-xs-12">
        <h2>Found a bug or have some issues?</h2>
        <p>
            If you found a bug or have some issues running the import script, please
            <a href="https://github.com/it-novum/openITCOCKPIT-configuration-import" target="_blank">create an issue at our GitHub repository</a>.
            
        </p>
    </div>
</div>