LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Read from Input File and Append to Multiple Files (https://www.linuxquestions.org/questions/linux-newbie-8/read-from-input-file-and-append-to-multiple-files-845898/)

bridrod 11-22-2010 05:36 PM

Read from Input File and Append to Multiple Files
 
Sorry for the long message, I wanted to be as clear as possible. I have to add hundreds of new servers to hundreds of configuration files in Nagios. Here is a sample structure:
------------------------------------------------------------------------
servers.txt has:

SERVER3
SERVER4
SERVER5
------------------------------------------------------------------------
service-template.txt has:

define service{
use generic-service
host_name $1
service_description Description1
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!sshd
}
######################################################################
define service{
use generic-service
host_name $1
service_description Description2
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!ntpd
}
######################################################################
define service{
use generic-service
host_name $1
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!xinetd
}
------------------------------------------------------------------------
service-servers-sshd.cfg has:
########################### service to check - SSHD

define service{
use generic-service
host_name SERVER1
service_description Description1
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!sshd
}

define service{
use generic-service
host_name SERVER2
service_description Description1
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!sshd
}

service-servers-ntpd.cfg has:
########################### service to check - NTPD

define service{
use generic-service
host_name SERVER1
service_description Description2
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!ntpd
}

define service{
use generic-service
host_name SERVER2
service_description Description2
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!ntpd
}

service-servers-xinetd.cfg has:
########################### service to check - XINETD

define service{
use generic-service
host_name SERVER1
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!xinetd
}

define service{
use generic-service
host_name SERVER2
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!xinetd
}
------------------------------------------------------------------------
Now I need to add SERVER3, SERVER4 and SERVER5 to each of the service-servers-*.cfg files, following the same structure as the above. Blank line between the "defined services". I knew how to do this when I was adding ALL servers to a single configuration file. I would then merge that file to my final CFG file. Now, my issue is all those service files are separate and I want to append the new servers at the end of each file.

Hope I made it clear, cause right now I am really confused on how to do this!

barriehie 11-22-2010 08:16 PM

What languages are available for completing this task?

bridrod 11-22-2010 08:23 PM

Well, I usually use bash but perl will do too.

grail 11-22-2010 08:47 PM

How about something like:
Code:

#!/bin/bash

declare SSHD NTPD XINETD

SSHD=0
NTPD=1
XINETD=2

IFS='#' entries+=($(awk 'BEGIN{RS="#+\n"}NR==FNR{split($0,servers);next}{tmp="xinetd";if(/sshd/)tmp="sshd";if(/ntpd/)tmp="ntpd";for(x in servers)printf "\n%s",gensub(/\$1/,servers[x],"1");printf "#"}' servers.txt service-template.txt))

while read -r FILE
do
        case $FILE in
            *sshd.cfg)echo -n ${entries[SSHD]} >> $FILE;;
            *ntpd.cfg)echo -n ${entries[NTPD]} >> $FILE;;
            *xinetd.cfg)echo -n ${entries[XINETD]} >> $FILE;;
            *)echo "Not a required file to update";;
        esac
done< <(find <path to files> -type f -name '*cfg')


jschiwal 11-22-2010 09:00 PM

You could read in the server names, in a loop, and set a variable. Then create the config file using a HERE document, which will replace variable with it's value, when creating the config file:
Here is an example:
Code:

:> configfile
for server in SERV1, SERV2, SERV3; do cat <<END >>configfile
use generic-server
host_name $server
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_proc!1:1:!xinetd
}
END
done

This produces this "configfile":
Code:

use generic-server
host_name SERV1,
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_proc!1:1:!xinetd
}
use generic-server
host_name SERV2,
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_proc!1:1:!xinetd
}
use generic-server
host_name SERV3
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_proc!1:1:!xinetd
}

A nice thing about HERE documents, is that it easy to edit the template, which is self contained in the script.

bridrod 11-22-2010 09:37 PM

Thanks for the suggestions and quick responses. I will try them tomorrow.

bridrod 11-23-2010 05:37 PM

Thanks again to you both for helping out. I haven't been able to finish the final script yet but I am getting really close.

I tried both of your approaches. jschiwal's requires a little more work on my end but is somewhat easier to work on. grail's I got much closer to what I was expecting as an end result but required me to go deeper trying to understand the script process.

Either way your solutions were great! I will try finishing it tomorrow and post a follow up if I get stuck again.

Always a pleasure to talk to people like you who really know their game! :cool:

bridrod 11-24-2010 02:02 PM

Yep! Got it ironed out and your suggestions were key to my success! Thanks again! Officially SOLVED!

grail 11-24-2010 07:05 PM

Glad you got it worked out :)


All times are GMT -5. The time now is 06:58 PM.