I am setting up a new Nagios install using OMD & check_mk instead of doing everything by hand this time. The switches do not have proper hostnames in DNS so check_mk provides a method to specify the IP in the config file. I was converting the configuration file for some switches last night and I tried for a few minutes to do it one shot but then threw in the towel.
Example input:
define host{
use generic-switch
host_name BAR.MDF.MODEL
alias BAR.MDF.MODEL
parents FOO.MDF.MODEL.01
address 10.1.1.1
}
Required output:
ipaddresses = {
"BAR.MDF.MODEL" : "10.1.1.1",
}
Here is what I did.
I knew that IPs were always after host_name so it is fairly safe/consistent
{ awk '/host_name/ || /address/ { print $2 }' < switch.cfg; } >> out.txt
which got me
BAR.MDF.MODEL
10.1.1.1
Then I did this
{ awk '!/^1/{ getline ip; print $0, ip; next }' < out.txt; } >> out2.txt
to get this:
BAR.MDF.MODEL 10.1.1.1
the rest of the formating was done in vim via ‘norm’ & macros etc
"BAR.MDF.MODEL" : "10.1.1.1",
Try if this could work for you. Only
awk.Assuming following data of
infileRun this
awkscript:That yields: