#!/bin/bash NAGIOSBASE=/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}; do sed -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 services for 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 } } }" test done ## First we find all services with something missing EMPTYSERVICES=` 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 them for SERVICE in ${EMPTYSERVICES}; do sed -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:]]*// } } }" test done ## Make a list of all service groups SERVICEGROUPS=` 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 groups for SERVICEGROUP in ${SERVICEGROUPS}; do OUTPUT="$(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}" fi done echo EMPTYSERVICEGROUPS : ${EMPTYSERVICEGROUPS} ## And finally, we comment them out for SERVICEGROUP in ${EMPTYSERVICEGROUPS}; do sed -i " :t /define.*servicegroup.*{/,/}/ { /}/!{ $!{ N; bt } } /servicegroup_name[[:blank:]]*${SERVICEGROUP}[[:blank:]]*\n/ { s/^/#/g s/\n/\n#/g } }" test done