I have a configuration file (from Nagios) with the following structure:
define service{
use pruebaspre-service,srv-pnp
host_name server1.es
servicegroups pruebasdatasourcesoaspre-servicegroup,pruebaspre-servicegroup
service_description Estado DataSource - cfio JUVEPoolDSPoolDS
check_command check_ds_oas!cfio!JUVEPoolDSPoolDS!/opt/oracle/ias10g/10.1.2!1!0
}
define service{
use pruebaspre-service,srv-pnp
host_name server1.es
servicegroups pruebasdatasourcesoaspre-servicegroup,pruebaspre-servicegroup
service_description Estado DataSource - cfio REMEPoolDS
check_command check_ds_oas!cfio!REMEPoolDS!/opt/oracle/ias10g/10.1.2!1!0
}
define service{
use pruebaspre-service,srv-pnp
host_name server2.es
servicegroups pruebasdatasourcesoaspre-servicegroup,pruebaspre-servicegroup
service_description Estado DataSource - cfio iris_usr_irisPoolDS
check_command check_ds_oas!cfio!iris_usr_irisPoolDS!/opt/oracle/ias10g/10.1.3!1!0
}
define service{
use pruebaspre-service,srv-pnp
host_name server2.es
servicegroups pruebasdatasourcesoaspre-servicegroup,pruebaspre-servicegroup
service_description Estado DataSource - cfio REMEPoolDS
check_command check_ds_oas!cfio!REMEPoolDS!/opt/oracle/ias10g/10.1.2!1!0
}
define service{
use pruebaspre-service,srv-pnp
host_name server2.es
servicegroups pruebasdatasourcesoaspre-servicegroup,pruebaspre-servicegroup
service_description Estado DataSource - cfio redt2_usr_redt2PoolDS
check_command check_ds_oas!cfio!redt2_usr_redt2PoolDS!/opt/oracle/ias10g/10.1.3!1!0
}
To register a new service in this file, I need to first find if the service exists, for this I must find that for a check_command the host_name is not registered.
For example, I want to register:
define service {
use pre-service tests, srv-pnp
host_name server1.es
servicegroups testsdatasourcesoaspre-servicegroup, pre-servicegroup tests
service_description DataSource Status - cfio REMEPoolDS
check_command check_ds_oas! cfio! REMEPoolDS! /opt/oracle/ias10g/10.1.2! 1! 0
}
so in the file I should look for it does not exist.
I tried it with a while loop that reads the file and when it finds
check_command check_ds_oas! Cfio! REMEPoolDS! /Opt/oracle/ias10g/10.1.2! 1! 0
it looks for the next one not to be
host_name server1.es
V_NAGIOS_COMMAND=check_ds_oas!cfio!REMEPoolDS!/opt/oracle/ias10g/10.1.2!1!0
if [ `grep ${V_NAGIOS_COMMAND} --count $V_FILE_NAGIOS` -ge 1 ] ; then
while read LINEA_CONFIG
do
V_DIRECTIVA=`echo $LINEA_CONFIG| awk '{print $1} '`
V_VALOR_DIRECTIVA=`echo $LINEA_CONFIG| awk '{print $2} '`
if [ $V_DIRECTIVA = check_command ]&&[ $V_VALOR_DIRECTIVA = $V_NAGIOS_COMMAND ]; then
V_COMANDO_ENCONTRADO=1
elif [ $V_DIRECTIVA = host_name ]&& [ $V_COMANDO_ENCONTRADO = 1 ] ; then
if [ $V_VALOR_DIRECTIVA = $V_MAQUINA ] ; then
((V_EXISTEN_DATOS++))
return 1
else
V_COMANDO_ENCONTRADO=0
fi
fi
done < <(tac $V_FILE_NAGIOS| grep -vE "^#|^$")
fi
but it is very slow.
while read LINEA_CONFIGtowhile read V_DIRECTIVA V_VALOR_DIRECTIVAand remove the two awk lines, that will help a lot.