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! |
What languages are available for completing this task?
|
Well, I usually use bash but perl will do too.
|
How about something like:
Code:
#!/bin/bash |
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 Code:
use generic-server |
Thanks for the suggestions and quick responses. I will try them tomorrow.
|
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: |
Yep! Got it ironed out and your suggestions were key to my success! Thanks again! Officially SOLVED!
|
Glad you got it worked out :)
|
All times are GMT -5. The time now is 06:58 PM. |