As i googled around and hadnt found anything for Nagios 3.x i decided to write that script myself. It will only delete the comments of checks which are okay again since Nagios is not taking care of that.
#!/bin/bash
# Deletes comments from host and services no longer in critical status. Developed for Nagios 3.x
# Author: Mathias Decker, <mathias@itgweb.de>
# Date: 27/12/2011
# Global Vars
NAGIOS_CMD='/var/nagios/rw/nagios.cmd'
LOG='/var/nagios/status.dat'
# Remove no longer used service comments
# explanation: convert blanks -> get all comments and 3 lines up -> get rid of tabs -> make 1 line not for -> skip the other 'blocks'
for c in $(cat $LOG | sed 's/ /+blank+/g' | grep -B3 "^ comment_id=" | sed 's/ //g' | sed ':a;N;$!ba;s/\n/;/g' | sed 's/--/\n/g')
do
comment_id=$(echo $c | sed 's/;/\n/g' | grep "^comment_id" | sed 's/comment_id=//g')
host_name=$(echo $c | sed 's/;/\n/g' | grep host_name | sed 's/host_name=//g')
service_name=$(echo $c | sed 's/;/\n/g' | grep service_description | sed 's/service_description=//g')
if [ ! -z $service_name ]
then
service_name=$(echo $service_name | sed 's/+blank+/ /g') #get back the blanks
host_name=$(echo $host_name | sed 's/+blank+/ /g')
state=$(cat $LOG | grep -B15 current_state | grep "$host_name$" -A14 | grep "=$service_name$" -A13 | grep "current_state=" | sed 's/ current_state=//g') # theres a tab in front of current_state
# get all services which got state=0 which is okay
if [ $state = 0 ]
then
# echo "$host_name - $service_name - $comment_id - '$state'" # uncomment for debugging
NOW='date +%s'
echo "[$NOW] DEL_SVC_COMMENT;$comment_id;$NOW" >> $NAGIOS_CMD # this will get rid of the comment
fi
fi
done
#remove no longer used host comments
for c in $(cat $LOG | sed 's/ /+blank+/g' | grep -A3 "hostcomment" | sed 's/ //g' | sed ':a;N;$!ba;s/\n/;/g' | sed 's/--/\n/g')
do
comment_id=$(echo $c | sed 's/;/\n/g' | grep "^comment_id" | sed 's/comment_id=//g')
host_name=$(echo $c | sed 's/;/\n/g' | grep host_name | sed 's/host_name=//g')
if [ ! -z $host_name ]
then
host_name=$(echo $host_name | sed 's/+blank+/ /g')
state=$(cat $LOG | grep -B15 current_state | grep "$host_name$" -A14 -B1 | grep "hoststatus" -A14 | grep "current_state=" | sed 's/ current_state=//g')
if [ $state = 0 ]
then
# echo "$host_name - $comment_id - $state"
NOW='date +%s'
echo "[$NOW] DEL_HOST_COMMENT;$comment_id;$NOW" >> $NAGIOS_CMD
fi
fi
done