#!/bin/bash

############### NCPA Syntax Converter ################

# Copyright (C) 2010-2014 Nagios Enterprises, LLC
# Version 1.0 - 01/19/2018

# Questions/issues should be posted on the Nagios
# Support Forum at https://support.nagios.com/forum/

# Feedback/recommendations/tips can be sent to
# Ludmil Miltchev at lmiltchev@nagios.com                
 
######################################################

#Defining some variables and functions
BACKUP_DIR=/usr/local/nagiosxi/tmp/services_backup # This is where the original service configs will be backed up
SERVICES_DIR=/usr/local/nagios/etc/services # The original service configs
SERVICES_TEMP_DIR=/usr/local/nagiosxi/tmp/services # This is where the service configs will be temporarily stored for processing
IMPORT_DIR=/usr/local/nagios/etc/import # The import directory
SCRIPTS_DIR=/usr/local/nagiosxi/scripts

CONVERT_SERVICES () {
    # Changing this:
    # check_xi_ncpa!-t 'mytoken' -P 5693 -M 'service/<service>/running' or check_xi_ncpa!-t 'mytoken' -P 5693 -M 'service/AGSService/stopped'
    # to this:
    # check_xi_ncpa_agent!-t 'mytoken' -P 5693 -M 'services' -q 'service=<service>,status=running' or check_xi_ncpa_agent!-t 'mytoken' -P 5693 -M 'services' -q 'service=<service>,status=stopped'
    find $SERVICES_TEMP_DIR -type f -exec sed -i "s/-M 'service\//-M 'services' -q 'service=/g" {} +
    find $SERVICES_TEMP_DIR -type f -exec sed -i "s/-M \"service\//-M 'services' -q \"service=/g" {} + # In case users change single to double quotes
    find $SERVICES_TEMP_DIR -type f -exec sed -i "s/\/running/,status=running/g" {} +
    find $SERVICES_TEMP_DIR -type f -exec sed -i "s/\/stopped/,status=stopped/g" {} +
}

CONVERT_PROCESSES () {
    # Changing this:
    # check_xi_ncpa!-t 'mytoken' -P 5693 -M 'process/<process>/count'
    # to this:
    # check_xi_ncpa_agent!-t 'mytoken' -P 5693 -M 'processes' -q 'name=<process>,match=search,check=true'
    find /usr/local/nagiosxi/tmp/services/ -type f -exec sed -i "s/-M 'process\//-M 'processes' -q 'name=/g" {} +
    find /usr/local/nagiosxi/tmp/services/ -type f -exec sed -i "s/-M \"process\//-M 'processes' -q \"name=/g" {} + # In case users change single to double quotes
    find /usr/local/nagiosxi/tmp/services/ -type f -exec sed -i "s/\/count'/,match=search,check=true'/g" {} +
}

# Start executing the script
echo -e "
This script automates the process of converting the NCPA old syntax to the new one.
It will work only on the default NCPA checks, that were added by the NCPA wizard!
If you modified your default NCPA checks or if you are using custom NCPA checks/commands, you would need to fix the failing checks manually in the CCM.
"

read -p "Do you want to continue with automatic NCPA syntax conversion? [Y/n] " AUTO

        case "$AUTO" in
                "[yY][eE][sS]" | "y" | "Y" | "")
                        echo "Proceeding with NCPA syntax conversion..."
                        ;;
                *)
                        echo "Conversion canceled!"			
                        exit 0
        esac

# Backup old service configs
mkdir -p $BACKUP_DIR
cp $SERVICES_DIR/* $BACKUP_DIR
echo -e "Your service configs were backed up in the $BACKUP_DIR directory."

# Create a 'services' directory under /usr/local/nagiosxi/tmp and copy the configs, containing 'check_xi_ncpa_agent' to it
mkdir -p $SERVICES_TEMP_DIR
grep -Rl check_xi_ncpa $SERVICES_DIR | xargs -d "\n" cp -t $SERVICES_TEMP_DIR

# Getting the timestamp before modifying the configs
TIMESTAMP="$(date)"

# Convert NCPA checks to the new syntax
echo "Converting NCPA services to the new NCPA syntax."
CONVERT_SERVICES
CONVERT_PROCESSES

# Move the modified service configs (newer than $TIMESTAMP) to the import directory
echo "Copying modified service configuration files to the '/usr/local/nagios/etc/import' directory."
find $SERVICES_TEMP_DIR -type f -newermt "$TIMESTAMP" -exec cp -t $IMPORT_DIR {} +

# Removing the /usr/local/nagiosxi/tmp/services directory
rm -rf $SERVICES_TEMP_DIR

# Going to the 'scripts' directory and running the 'reconfigure_nagios.sh' script
echo "Running the 'reconfigure_nagios.sh' script."
cd $SCRIPTS_DIR
./reconfigure_nagios.sh
echo -e "
"
# Giving workarounds for lost perfdata on process count checks
echo -e "Important: The new syntax adds a few extra datastores to your 'process count' checks, which will break your graphs.
You have two options for fixing the graphs:

1. If you don't care about the old perfdata, go to '/usr/local/nagios/share/perfdata/<hostname>', and delete the RRD and XML file for this particular service. The files will be recreated, and graphs will reappear in approximately 15-20 minutes.

2. If you would like to keep the historical data, follow the directions, outlined in the KB article below:

https://support.nagios.com/kb/article/nagios-xi-icmp-and-ping-checks-stopped-graphing-149.html
"

exit 0
