bashpowershellnagios

convert a nagios config file to csv


I have a Nagios directory which contains some servers config files :

define host {
   host_name            server1.srv
   hostgroups           linux-servers+holmes
   check_interval           5
}

define host {
   host_name            server2.srv
   hostgroups           linux-servers+holmes
   check_interval           5
}   

I would like to reformat that data into CSV so I would get :

host_name,hostgroups,check_interval
server1.srv,linux-servers+holmes,5
server2.srv,linux-servers+holmes,5

I am happy to do this with either bash or powershell but I am not enough of a scripting guru to know how to do this...If anyone has a suggestion that would be greatly appreciated :) !


Solution

  • This awk should work :

    awk 'BEGIN{print "host_name,hostgroups,check_interval"} /host_name/{v1=$2} /hostgroups/{v2=$2} /check_interval/{v3=$2} /}/{print v1","v2","v3; v1=v2=v3=""}' file