If i help You, send me BTC please 1CJGz4UGyL7hTjPeN8j7fu9LRmmWiWSRHi

Nagvis, Nagios, Bash, one-line configuring.

Постов 2 из 174

There was a task recently to add a new group of recipients for nagios notifications. Because for each host we have a separate config I had to change more than a hundred files.
Furthermore it was necessary to correct only those files that contain service descriptions from nagvis configs. Here is one-line solution (divided to sub lines for readability).

IFS=$'\n';pattern="contact_groups L2, L1" && |\
for i in $(cat /usr/share/nagvis-2/etc/maps/Main.cfg |\
grep description= | cut -d= -f2); \
do for j in $(grep -rnw /etc/nagios/hosts/ -e "$i"| cut -d: -f1);do \
if [[ -z $(grep $pattern $j) ]] ; \
then sed -i "/$i/a \\\t$pattern" $j; fi ;done; done;
 
# and with explanation
 
# replace space delimiter with EOL delimiter on "for" loop
IFS=$'\n'; 
# define string to insert
pattern="contact_groups L2, L1"; 
# find all service descriptions from nagvis configs
for i in $(cat /usr/share/nagvis-2/etc/maps/Main.cfg  | grep description= | cut -d= -f2); do 
# find all nagios configs containing service description needed
    for j in $(grep -rnw /etc/nagios/hosts/ -e "$i"| cut -d: -f1); do 
# check if string is not already inserted earlier
        if [[ -z $(grep $pattern $j) ]] ; then 
# insert string after string, containing service description
            sed -i "/$i/a \\\t$pattern" $j; 
        fi;
    done; 
done;

, ,

Menu