rendered paste body#!/bin/bashNAGIOSBASE=/home/hp/src/pluryn## We look for empty hostgroups EMPTYHOSTGROUPS=`sed -n " :t /define.*hostgroup.*{/,/}/ { /}/!{ $!{ N; bt } } /#[[:blank:]]*members.*\n/ ! { /members[[:blank:]]*\n/ { s/^/#/g s/\n/\n#/g p } } }" test | grep "hostgroup_name" | awk '{print $3}'`EMPTYHOSTGROUPS=$(for a in ${EMPTYHOSTGROUPS}; do echo $a; done | sort | uniq)echo EMPTYHOSTGROUPS : ${EMPTYHOSTGROUPS}## then we comment them out for HOSTGROUP in ${EMPTYHOSTGROUPS}; dosed -i " :t /define.*hostgroup.*{/,/}/ { /}/!{ $!{ N; bt } } /hostgroup_name[[:blank:]]*${HOSTGROUP}[[:blank:]]*\n/ { /members[[:blank:]]*\n/ { s/^/#/g s/\n/\n#/g } } }" test done## We then remove all occurences of these hostgroups from servicesfor HOSTGROUP in ${EMPTYHOSTGROUPS}; do sed -i " :t /define.*service.*{/,/}/ { /}/!{ $!{ N; bt } } /#[[:blank:]]*service_description.*\n/ !{ /,${HOSTGROUP}/ { s/,${HOSTGROUP}//g } /${HOSTGROUP},/ { s/${HOSTGROUP},//g } /${HOSTGROUP}/ { s/${HOSTGROUP}//g } } }" testdone## First we find all services with something missingEMPTYSERVICES=`sed -n " :t /define.*service.*{/,/}/ { /}/!{ $!{ N; bt } } /hostgroup_name[[:blank:]]*\n/ { p } /host_name[[:blank:]]*\n/ { p } }" test | grep "service_description" | awk '{print $2}'`EMPTYSERVICES=$(for a in ${EMPTYSERVICES}; do echo $a; done | sort | uniq)EMPTYSERVICES=$(echo ${EMPTYSERVICES} | sed -e 's#/#\\/#g')## Then we fix themfor SERVICE in ${EMPTYSERVICES}; dosed -i " :t /define.*service.*{/,/}/ { /}/!{ $!{ N; bt } } /service_description[[:blank:]]*${SERVICE}[[:blank:]]*\n/ { # Comment out invalid services # empty host_name and empty hostgroup_name /host_name[[:blank:]]*\n/ { /hostgroup_name[[:blank:]]*\n/ { s/^/#/g s/\n/\n#/g } } # only empty host_name, no hostgroup_name /host_name[[:blank:]]*\n/ { /hostgroup_name.*\n/ ! { s/^/#/g s/\n/\n#/g } } # only empty hostgroup_name, no host_name /hostgroup_name[[:blank:]]*\n/ { /host_name.*\n/ ! { s/^/#/g s/\n/\n#/g } } # remove offending empty hostgroup_name /#[[:blank:]]*hostgroup_name[[:blank:]]*\n/ !{ s/hostgroup_name[[:blank:]]*\n[[:blank:]]*// } # remove offending empty host_name /#[[:blank:]]*host_name[[:blank:]]*\n/ !{ s/host_name[[:blank:]]*\n[[:blank:]]*// } } }" testdone## Make a list of all service groupsSERVICEGROUPS=`sed -n " :t /define.*servicegroup.*{/,/}/ { /}/!{ $!{ N; bt } } /#[[:blank:]]*servicegroup_name.*\n/ ! { p } }" test | grep "servicegroup_name" | awk '{print $2}'`SERVICEGROUPS=$(for a in ${SERVICEGROUPS}; do echo $a; done | sort | uniq)echo SERVICEGROUPS : ${SERVICEGROUPS}## Now we look for empty service groupsfor SERVICEGROUP in ${SERVICEGROUPS}; doOUTPUT="$(sed -n " :t /define.*service.*{/,/}/ { /}/!{ $!{ N; bt } } /#[[:blank:]]*service_description.*\n/ ! { /servicegroups[[:blank:]]*${SERVICEGROUP}[[:blank:]]*\n/{ p } } }" test )" if [ -z "${OUTPUT}" ]; then EMPTYSERVICEGROUPS="${EMPTYSERVICEGROUPS} ${SERVICEGROUP}"fidoneecho EMPTYSERVICEGROUPS : ${EMPTYSERVICEGROUPS}## And finally, we comment them outfor SERVICEGROUP in ${EMPTYSERVICEGROUPS}; dosed -i " :t /define.*servicegroup.*{/,/}/ { /}/!{ $!{ N; bt } } /servicegroup_name[[:blank:]]*${SERVICEGROUP}[[:blank:]]*\n/ { s/^/#/g s/\n/\n#/g } }" testdone