#!/bin/bash # # Copyright(C) 2010 Mark Clarkson # # This software is provided under the terms of the GNU # General Public License (GPL), as published at: # http://www.gnu.org/licenses/gpl.html . # # File: nagctl # Author: Mark Clarkson # Date: 11 Sep 2010 # Version: 0.11 # Modified: # 2010-09-30 Mark Clarkson # * Added more data cross-checking # # Purpose: This a utility to help with creation of Nagios configuration # files. For more information type, # # ./nagctl -h # # Notes: # # --------------------------------------------------------------------------- # SETINGS - MODIFY AS NEEDED # --------------------------------------------------------------------------- source /etc/nagrestconf/nagctl.conf # # # -------------------- DO NOT MODIFY ANYTHING BELOW ------------------------- # # # --------------------------------------------------------------------------- # GLOBALS # --------------------------------------------------------------------------- VERSION="1.0" # Debugging #echo "$@" >>/tmp/nagctl_debug.log RETVAL=0 # -- Constants ACT_PROPAGATE=1 ACT_PIPECMD=2 ACT_ADD=3 ACT_DELETE=4 ACT_MODIFY=5 ACT_SHOW=6 ACT_CHECK=7 ACT_RESTART=8 ACT_APPLY=9 CHECK_NAGIOSCONFIG=1 RESTART_NAGIOS=1 APPLY_NAGIOSCONFIG=1 APPLY_NAGIOSLASTGOODCONFIG=2 PIPECMD_ENABLEHOSTSVCCHECKS=1 PIPECMD_DISABLEHOSTSVCCHECKS=2 PIPECMD_DISABLESVCCHECK=3 PIPECMD_ENABLESVCCHECK=4 PIPECMD_SCHEDULEHOSTDOWNTIME=5; PIPECMD_DELHOSTDOWNTIME=6; PIPECMD_SCHEDULEHOSTSVCDOWNTIME=7; PIPECMD_DELHOSTSVCDOWNTIME=8; PIPECMD_SCHEDULESVCDOWNTIME=9; PIPECMD_DELSVCDOWNTIME=10; FILE_HOSTTEMPLATES=1 FILE_SERVICETEMPLATES=2 FILE_HOSTS=3 FILE_HOSTGROUPS=4 FILE_SERVICES=5 FILE_CONTACTS=6 FILE_CONTACTGROUPS=7 FILE_SERVICESETS=8 FILE_SERVICEGROUPS=9 FILE_TIMEPERIODS=10 FILE_COMMANDS=11 FILE_SERVICEDEPS=12 FILE_HOSTDEPS=13 FILE_SERVICEESC=14 FILE_HOSTESC=15 FILE_HOSTEXTINFO=16 FILE_SERVICEEXTINFO=17 # lockfile settings LOCK_SLEEPTIME=1 LOCK_RETRY=120 # Definitions of required non-null fields for FILE_XXX csv files. gfilename[$FILE_HOSTTEMPLATES]="hosttemplates" gfilename[$FILE_SERVICETEMPLATES]="servicetemplates" gfilename[$FILE_HOSTS]="hosts" gfilename[$FILE_HOSTGROUPS]="hostgroups" gfilename[$FILE_SERVICES]="services" gfilename[$FILE_CONTACTS]="contacts" gfilename[$FILE_CONTACTGROUPS]="contactgroups" gfilename[$FILE_SERVICESETS]="servicesets" gfilename[$FILE_SERVICEGROUPS]="servicegroups" gfilename[$FILE_TIMEPERIODS]="timeperiods" gfilename[$FILE_COMMANDS]="commands" gfilename[$FILE_SERVICEDEPS]="servicedeps" gfilename[$FILE_HOSTDEPS]="hostdeps" gfilename[$FILE_SERVICEESC]="serviceesc" gfilename[$FILE_HOSTESC]="hostesc" gfilename[$FILE_SERVICEEXTINFO]="serviceextinfo" gfilename[$FILE_HOSTEXTINFO]="hostextinfo" gdelfilereq[$FILE_HOSTTEMPLATES]="1" gdelfilereq[$FILE_SERVICETEMPLATES]="1" gdelfilereq[$FILE_HOSTS]="1" gdelfilereq[$FILE_HOSTGROUPS]="1" gdelfilereq[$FILE_SERVICES]="1 4" gdelfilereq[$FILE_CONTACTS]="1" gdelfilereq[$FILE_CONTACTGROUPS]="1" gdelfilereq[$FILE_SERVICESETS]="1 4" gdelfilereq[$FILE_SERVICEGROUPS]="1" gdelfilereq[$FILE_TIMEPERIODS]="1" gdelfilereq[$FILE_COMMANDS]="1" gdelfilereq[$FILE_SERVICEDEPS]="1|2 3 4|5 6" gdelfilereq[$FILE_HOSTDEPS]="1|2 3|4" gdelfilereq[$FILE_SERVICEESC]="1|2 3" gdelfilereq[$FILE_HOSTESC]="1|2" gdelfilereq[$FILE_SERVICEEXTINFO]="" gdelfilereq[$FILE_HOSTEXTINFO]="" #gfilereq[$FILE_HOSTTEMPLATES]="1 2 6 7 8" gfilereq[$FILE_HOSTTEMPLATES]="1 6 7 8 11 12 14" #gfilereq[$FILE_SERVICETEMPLATES]="1 2 6 8 9 10" gfilereq[$FILE_SERVICETEMPLATES]="1 6 8 9 10 12 13" gfilereq[$FILE_HOSTS]="1 2 3 4" gfilereq[$FILE_HOSTGROUPS]="1 2" gfilereq[$FILE_SERVICES]="1 2 3 4" # gfilereq[$FILE_CONTACTS]="1 2 3" gfilereq[$FILE_CONTACTS]="1 5 6 7 8 9 10" gfilereq[$FILE_CONTACTGROUPS]="1 2 3" gfilereq[$FILE_SERVICESETS]="1 2 3 4" gfilereq[$FILE_SERVICEGROUPS]="1 2" gfilereq[$FILE_TIMEPERIODS]="1 2" gfilereq[$FILE_COMMANDS]="1 2" gfilereq[$FILE_SERVICEDEPS]="1|2 3 4|5 6" gfilereq[$FILE_HOSTDEPS]="1|2 3|4" gfilereq[$FILE_SERVICEESC]="1|2 3 4|5 6 7 8" gfilereq[$FILE_HOSTESC]="1|2 3|4 5 6 7" gfilereq[$FILE_SERVICEEXTINFO]="" gfilereq[$FILE_HOSTEXTINFO]="" gfile_ncols[$FILE_HOSTTEMPLATES]="40" gfile_ncols[$FILE_SERVICETEMPLATES]="43" gfile_ncols[$FILE_HOSTS]="47" gfile_ncols[$FILE_HOSTGROUPS]="8" gfile_ncols[$FILE_SERVICES]="46" gfile_ncols[$FILE_CONTACTS]="24" gfile_ncols[$FILE_CONTACTGROUPS]="4" gfile_ncols[$FILE_SERVICESETS]="46" gfile_ncols[$FILE_SERVICEGROUPS]="8" gfile_ncols[$FILE_TIMEPERIODS]="6" gfile_ncols[$FILE_COMMANDS]="3" gfile_ncols[$FILE_SERVICEDEPS]="11" gfile_ncols[$FILE_HOSTDEPS]="9" gfile_ncols[$FILE_SERVICEESC]="11" gfile_ncols[$FILE_HOSTESC]="10" gfile_ncols[$FILE_SERVICEEXTINFO]="8" gfile_ncols[$FILE_HOSTEXTINFO]="11" SETUP_DIR= SETUP_FILE= # Program args ME="$0" #CMDLINE="$@" # Standard TRUE=1 FALSE=0 # Errors ERROR_NONE=0 ERROR=1 # -- General variables # command line vars gservice= gaction= gfile= gcsv= # command line vars as numbers gnservice= gnaction= gnfile= gncsv= # ---------------------------------------------------------------------------- lockdir() # ---------------------------------------------------------------------------- # Purpose: Create the lock file. # Arguments: None # Returns: Nothing { if lockfile -! -$LOCK_SLEEPTIME -r $LOCK_RETRY $SERVICE_LOCK; then echo "NAGCTL ERROR: Could not gain directory lock. Aborting." exit 1 fi } # ---------------------------------------------------------------------------- unlockdir() # ---------------------------------------------------------------------------- # Purpose: Remove the lock file. # Arguments: None # Returns: Nothing { rm -f $SERVICE_LOCK } # ---------------------------------------------------------------------------- usage() # ---------------------------------------------------------------------------- # Purpose: Show program options. # Arguments: None # Returns: Nothing { echo echo -n "Usage: `basename $ME` [-h] " echo " " echo echo " -h : Display this help text." echo " ACTION : One of four ACTIONS are understood:" echo " show - Query a csv FILE." echo " add - Add a line to a csv FILE" echo " delete - Delete a csv entry from a FILE." echo " modify - Modify an existing csv entry." echo " check - check an ITEM." echo " pipecmd - Send text to the Nagios cmd pipe." echo " propagate - Propagate changes to master." echo " restart - Restart an ITEM." echo " SERVICE : The directory holding the configuration." echo " ITEM : For things that are not add, delete, show or modify." echo " FILE : The file to ACTION. E.g. hosts, services." echo " CSV_LINE : A line of CSV in the same format as the target FILE." echo " QUERY : A query to 'show'." echo } # ---------------------------------------------------------------------------- parse_options() # ---------------------------------------------------------------------------- # Purpose: Parse program options and set globals. # Arguments: None # Returns: Nothing { set -- "$@" [[ -z $4 ]] && set -- "$@" "dummy" while true do case $1 in -h|--help) usage; exit 0 ;; -v|--version) echo "$VERSION"; exit 0 ;; ?*) gservice="$1" gaction="$2" gfile="$3" gcsv="$4" ;; esac shift 1 || break [[ -n $gcsv ]] && break done # Get the rest of the args into gcsv #shift 3 #while true; #do # gcsv="$gcsv $1" # shift 1 || break #done # Strip spaces and urldecode (TODO: more decode) #gcsv=`echo "$gcsv" | sed 's/[[:space:]]*$//;s/,[[:space:]]*/,/g; # s/[[:space:]]*,/,/g;s/%2a/*/gi;s/+/ /g;s/%20/ /g;s/%2b/+/gi; # s/%3a/:/gi;'` [[ -z $gservice || -z $gaction || -z $gfile || -z $gcsv ]] && { echo "NAGCTL ERROR: Syntax error." exit 7 } # Sanity Checks (and set flags) check_folder check_action case $gnaction in 2) check_pipecmditem;; [3456]) check_file;; 10) check_file;; 7) check_checkitem ;; 8) check_restartitem ;; 9) check_applyitem ;; esac } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # CHECKS # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # --------------------------------------------------------------------------- check_folder() # --------------------------------------------------------------------------- # Purpose: Sanity checks. # Arguments: None # Returns: Nothing { [[ -e "$NAG_OBJ_DIR/$gservice" ]] || { echo "NAGCTL ERROR: Non-existent folder ($NAG_OBJ_DIR/$gservice)." exit 8 } } # --------------------------------------------------------------------------- check_pipecmditem() # --------------------------------------------------------------------------- # Purpose: Sanity checks. # Arguments: None # Returns: Nothing { case $gfile in enablehostsvcchecks) gnfile=$PIPECMD_ENABLEHOSTSVCCHECKS ;; disablehostsvcchecks) gnfile=$PIPECMD_DISABLEHOSTSVCCHECKS ;; disablesvccheck) gnfile=$PIPECMD_DISABLESVCCHECK ;; enablesvccheck) gnfile=$PIPECMD_ENABLESVCCHECK ;; schedhstdowntime) gnfile=$PIPECMD_SCHEDULEHOSTDOWNTIME ;; delhstdowntime) gnfile=$PIPECMD_DELHOSTDOWNTIME ;; schedhstsvcdowntime) gnfile=$PIPECMD_SCHEDULEHOSTSVCDOWNTIME ;; delhstsvcdowntime) gnfile=$PIPECMD_DELHOSTSVCDOWNTIME ;; schedsvcdowntime) gnfile=$PIPECMD_SCHEDULESVCDOWNTIME ;; delsvcdowntime) gnfile=$PIPECMD_DELSVCDOWNTIME ;; ?*) echo -e "NAGCTL ERROR: Invalid file." exit 4 ;; esac } # --------------------------------------------------------------------------- check_applyitem() # --------------------------------------------------------------------------- # Purpose: Sanity checks. # Arguments: None # Returns: Nothing { case $gfile in nagiosconfig) gnfile=$APPLY_NAGIOSCONFIG ;; nagioslastgoodconfig) gnfile=$APPLY_NAGIOSLASTGOODCONFIG ;; ?*) echo -e "NAGCTL ERROR: Invalid file." exit 4 ;; esac } # --------------------------------------------------------------------------- check_restartitem() # --------------------------------------------------------------------------- # Purpose: Sanity checks. # Arguments: None # Returns: Nothing { case $gfile in nagios) gnfile=$RESTART_NAGIOS ;; ?*) echo -e "NAGCTL ERROR: Invalid file." exit 4 ;; esac } # --------------------------------------------------------------------------- check_checkitem() # --------------------------------------------------------------------------- # Purpose: Sanity checks. # Arguments: None # Returns: Nothing { case $gfile in nagiosconfig) gnfile=$CHECK_NAGIOSCONFIG ;; ?*) echo -e "NAGCTL ERROR: Invalid file." exit 4 ;; esac } # --------------------------------------------------------------------------- check_file() # --------------------------------------------------------------------------- # Purpose: Sanity checks. # Arguments: None # Returns: Nothing { case $gfile in hosttemplates) gnfile=$FILE_HOSTTEMPLATES ;; servicetemplates) gnfile=$FILE_SERVICETEMPLATES ;; hosts) gnfile=$FILE_HOSTS ;; hostgroups) gnfile=$FILE_HOSTGROUPS ;; servicegroups) gnfile=$FILE_SERVICEGROUPS ;; services) gnfile=$FILE_SERVICES ;; servicesets) gnfile=$FILE_SERVICESETS ;; contacts) gnfile=$FILE_CONTACTS ;; contactgroups) gnfile=$FILE_CONTACTGROUPS ;; timeperiods) gnfile=$FILE_TIMEPERIODS ;; commands) gnfile=$FILE_COMMANDS ;; servicedeps) gnfile=$FILE_SERVICEDEPS ;; hostdeps) gnfile=$FILE_HOSTDEPS ;; serviceesc) gnfile=$FILE_SERVICEESC ;; hostesc) gnfile=$FILE_HOSTESC ;; serviceextinfo) gnfile=$FILE_SERVICEEXTINFO ;; hostextinfo) gnfile=$FILE_HOSTEXTINFO ;; ?*) echo -e "NAGCTL ERROR: Invalid file." exit 4 ;; esac } # --------------------------------------------------------------------------- check_action() # --------------------------------------------------------------------------- # Purpose: Sanity checks. # Arguments: None # Returns: Nothing { case $gaction in show) gnaction=$ACT_SHOW ;; add) gnaction=$ACT_ADD ;; delete) gnaction=$ACT_DELETE ;; modify) gnaction=$ACT_MODIFY ;; pipecmd) gnaction=$ACT_PIPECMD ;; propagate) gnaction=$ACT_PROPAGATE ;; check) gnaction=$ACT_CHECK ;; restart) gnaction=$ACT_RESTART ;; apply) gnaction=$ACT_APPLY ;; ?*) echo -e "NAGCTL ERROR: Invalid action" exit 4 ;; esac } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # SUPPORTING FUNCS # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # --------------------------------------------------------------------------- init() # --------------------------------------------------------------------------- # Purpose: Initialise important vars # Arguments: None # Returns: Nothing { SETUP_DIR="$NAG_OBJ_DIR/$gservice/setup" SETUP_FILE="$SETUP_DIR/${gservice}_$gfile.setup" SERVICE_LOCK="$SETUP_DIR/directory.lock" } # --------------------------------------------------------------------------- check_file_exists_and_is_writeable() # --------------------------------------------------------------------------- # Purpose: Check variable and filename. On error exits right here. # Arguments: $1 - Name of variable containing filename to check for. # Returns: Nothing { local varname file eval file="\$$1" [[ -z $file ]] && { echo -n "NAGCTL ERROR: Variable '$1' is not set." echo " Check nagctl.conf" exit 8 } [[ -e $file ]] || { echo "NAGCTL ERROR: File does not exist, '$file'." exit 9 } [[ -w $file ]] || { echo "NAGCTL ERROR: Cannot write to $file." exit 9 } } # --------------------------------------------------------------------------- check_file_exists_and_is_readable() # --------------------------------------------------------------------------- # Purpose: Check variable and filename. On error exits right here. # Arguments: $1 - Name of variable containing filename to check for. # Returns: Nothing { local varname file eval file="\$$1" [[ -z $file ]] && { echo -n "NAGCTL ERROR: Variable '$1' is not set." echo " Check nagctl.conf" exit 8 } [[ -e $file ]] || { echo "NAGCTL ERROR: File does not exist, '$file'." exit 9 } [[ -r $file ]] || { echo "NAGCTL ERROR: Cannot read from $file." exit 9 } } # --------------------------------------------------------------------------- check_setup_dir() # --------------------------------------------------------------------------- # Purpose: Check for FILE_XXXs and setup dir. Quits on error. # Arguments: None # Returns: Nothing { local sfile sdir # Check for setup directory and csv file sdir="$NAG_OBJ_DIR/$gservice/" [[ -w $sdir ]] || { echo "NAGCTL ERROR: Cannot write to $sdir." exit 9 } [[ -e "$sdir/setup" ]] || mkdir -p $sdir/setup sfile="$NAG_OBJ_DIR/$gservice/setup/${gservice}_$gfile.setup" touch $sfile 2>/dev/null [[ -w $sfile ]] || { echo "NAGCTL ERROR: Cannot write to $sfile." exit 9 } } # ---------------------------------------------------------------------------- check_num_cols() # ---------------------------------------------------------------------------- # Purpose: Check number of columns in csv line. # Arguments: $1 - expected number of columns. # Returns: 0 - success, 1 - error { numcols=`echo "$gcsv" | awk -F, '{ print NF; }'` : $((numcols++)) : $((numcols--)) [[ $numcols -eq "$1" ]] && return 0 return 1 } # ---------------------------------------------------------------------------- check_delete_null_cols() # ---------------------------------------------------------------------------- # Purpose: Check that columns listed in gdelfilereq global are not null. # Arguments: $1 - a FILE_XXX number. # Returns: 0 - success, 1 - error { local col a b declare -i n1 n2 for col in ${gdelfilereq[$1]} do n1=${col%%|*} n2=${col##*|} # If $a has no bar (|) - that is, it holds only one number - then # n1 and n2 will be equal. Otherwise a bar is present and is # interpreted here as an OR. if [[ $n1 -eq 0 || $n2 -eq 0 ]]; then # This is an internal out-of-bounds programming error. echo "NAGCTL ERROR: Internal error - Null gfilereq." exit 99 elif [[ $n1 -eq $n2 ]]; then # Original behaviour - check for single value a=`echo "$gcsv" | cut -d, -f$col` [[ -z $a ]] && return 1 else # Either/OR a=`echo "$gcsv" | cut -d, -f$n1` b=`echo "$gcsv" | cut -d, -f$n2` [[ -z $a && -z $b ]] && return 1 fi done return 0 } # ---------------------------------------------------------------------------- check_null_cols() # ---------------------------------------------------------------------------- # Purpose: Check that columns listed in gfilereq global are not null. # Arguments: $1 - a FILE_XXX number. # Returns: 0 - success, 1 - error { local col a b declare -i n1 n2 for col in ${gfilereq[$1]} do n1=${col%%|*} n2=${col##*|} # If $a has no bar (|) - that is, it holds only one number - then # n1 and n2 will be equal. Otherwise a bar is present and is # interpreted here as an OR. if [[ $n1 -eq 0 || $n2 -eq 0 ]]; then # This is an internal out-of-bounds programming error. echo "NAGCTL ERROR: Internal error - Null gfilereq." exit 99 elif [[ $n1 -eq $n2 ]]; then # Original behaviour - check for single value a=`echo "$gcsv" | cut -d, -f$col` [[ -z $a ]] && return 1 else # Either/OR a=`echo "$gcsv" | cut -d, -f$n1` b=`echo "$gcsv" | cut -d, -f$n2` [[ -z $a && -z $b ]] && return 1 fi done return 0 } # ---------------------------------------------------------------------------- escape_query() { # ---------------------------------------------------------------------------- # Purpose: Escape POSIX REGEX command characters # Arguments: $1 - text to escape # Returns: Nothing # Output: The escaped string echo "$1" | sed 's/\+/\\+/g' } # ---------------------------------------------------------------------------- item_delete() # ---------------------------------------------------------------------------- # Purpose: Look for text in a column and delete the row. # Arguments: $1 - column, $2 - FILE_XXX, $3 - text # Returns: 0 - found, 1 - not found { local query="^" i ncols=${gfile_ncols[$2]} [[ $1 -gt $ncols ]] && { echo -n "NAGCTL ERROR: Cannot search for column $1 when " echo "there are only $ncols columns." exit 11 } term1=`escape_query "$3"` # Build a precise query for i in `seq 1 $(($1-1))`; do query="$query[^,]*,"; done query="$query(([^, ]*) )*$term1( [^,]*,|,|$)" sed -ri -- "/$query/d" ${gservice}_${gfilename[$2]}.setup } # ---------------------------------------------------------------------------- item_delete2() # ---------------------------------------------------------------------------- # Purpose: Look for two items of text respectively in two columns. # and delete the row. column1 must come before column2. # Arguments: $1 - column1, $2 - column2, $3 - FILE_XXX, # $4 - text1, $5 - text2 # Returns: 0 - found, 1 - not found { local query="^" i ncols=${gfile_ncols[$3]} [[ $1 -gt $ncols ]] && { echo -n "NAGCTL ERROR: Cannot search for column $1 when " echo "there are only $ncols columns." exit 11 } term1=`escape_query "$4"` term2=`escape_query "$5"` # Build a precise query for i in `seq 1 $(($1-1))`; do query="$query[^,]*,"; done query="$query(([^, ]*) )*$term1( [^,]*,|,|$)" for i in `seq $(($1+1)) $(($2-1))`; do query="$query[^,]*,"; done query="$query(([^, ]*) )*$term2( [^,]*,|,|$)" sed -ri -- "/$query/d" ${gservice}_${gfilename[$3]}.setup } # ---------------------------------------------------------------------------- item_deleteN() # ---------------------------------------------------------------------------- # Purpose: Look for text in N columns and delete the row(s). # columns must be in numeric sort order. # Arguments: $1 - space separated columns, $2 - FILE_XXX, # $3 - comma separated text, $4 - optional prefix (for # matching empty fields) # Returns: Nothing { local query i ncols=${gfile_ncols[$2]} declare -a colnos=($1) IFS=, read -a txt <<<"$3" [[ ${colnos[0]} -eq 1 ]] && { query="^" } # Build a precise query for i in `seq 1 $((${colnos[0]}-1))`; do query="$query.*,"; done query="$query${txt[0]}" for j in `seq 2 ${#colnos[*]}`; do for i in `seq $((${colnos[$j-2]}+1)) $((${colnos[$j-1]}-1))`; do query="$query,.*" done query="$query,${txt[$j-1]}" done for i in `seq ${colnos[${#colnos[*]}-1]} $(($ncols-1))`; do query="$query,.*" done #TeSt: echo "delete query is '$query'" sed -i -- "/$query/d" ${gservice}_${gfilename[$2]}.setup } # ---------------------------------------------------------------------------- item_exists() # ---------------------------------------------------------------------------- # Purpose: Look for text in a column. # Arguments: $1 - column, $2 - FILE_XXX, $3 - text # Returns: 0 - found, 1 - not found { local query="^" i ncols=${gfile_ncols[$2]} [[ $1 -gt $ncols ]] && { echo -n "NAGCTL ERROR: Cannot search for column $1 when " echo "there are only $ncols columns." exit 11 } term1=`escape_query "$3"` # E.g. col 3 search = ^[^,]*,[^,]*,[^,]*com( |,|$) # Build a precise query for i in `seq 1 $(($1-1))`; do query="$query[^,]*,"; done query="$query(([^, ]*) )*$term1( [^,]*,|,|$)" return `grep -E -qs -- "$query" ${gservice}_${gfilename[$2]}.setup` } # ---------------------------------------------------------------------------- item_exists2() # ---------------------------------------------------------------------------- # Purpose: Look for two items of text respectively in two columns. # column1 must come before column2. # Arguments: $1 - column1, $2 - column2, $3 - FILE_XXX, # $4 - text1, $5 - text2 # Returns: 0 - found, 1 - not found { local query="^" i ncols=${gfile_ncols[$3]} [[ $1 -gt $ncols ]] && { echo -n "NAGCTL ERROR: Cannot search for column $1 when " echo "there are only $ncols columns." exit 11 } term1=`escape_query "$4"` term2=`escape_query "$5"` # Build a precise query for i in `seq 1 $(($1-1))`; do query="$query[^,]*,"; done query="$query(([^, ]*) )*$term1( [^,]*,|,|$)" for i in `seq $(($1+1)) $(($2-1))`; do query="$query[^,]*,"; done query="$query(([^, ]*) )*$term2( [^,]*,|,|$)" return `grep -Eqs -- "$query" ${gservice}_${gfilename[$3]}.setup` } # ---------------------------------------------------------------------------- item_existsN_new() # ---------------------------------------------------------------------------- # # TODO # # Purpose: Look for two items of text respectively in N columns. # columns must be in numeric sort order. # Arguments: $1 - space separated columns, $2 - FILE_XXX, # $3 - comma separated text, $4 - optional prefix (for # matching empty fields) # Returns: 0 - found, 1 - not found { local query i ncols=${gfile_ncols[$2]} declare -a colnos=($1) IFS=, read -a txt <<<"$3" #[[ ${colnos[0]} -eq 1 ]] && { query="^" #} # Build a precise query TODO # E.g. col 3 search = ^[^,]*,[^,]*,[^,]*com( |,|$) for i in `seq 1 $((${colnos[0]}-1))`; do query="$query^[^,]*,"; done query="$query[^,]*${txt[0]}( |,|$)" for j in `seq 2 ${#colnos[*]}`; do for i in `seq $((${colnos[$j-2]}+1)) $((${colnos[$j-1]}-1))`; do query="$query,.*" done query="$query,${txt[$j-1]}" query="${query%,}( |,)${txt[$j-1]}( |,)" done #TeSt: echo "query is '$query'" return `grep -qs -- "$query" ${gservice}_${gfilename[$2]}.setup` } # ---------------------------------------------------------------------------- item_existsN() # ---------------------------------------------------------------------------- # Purpose: Look for two items of text respectively in N columns. # columns must be in numeric sort order. # Arguments: $1 - space separated columns, $2 - FILE_XXX, # $3 - comma separated text, $4 - optional prefix (for # matching empty fields) # Returns: 0 - found, 1 - not found { local query i ncols=${gfile_ncols[$2]} declare -a colnos=($1) IFS=, read -a txt <<<"$3" [[ ${colnos[0]} -eq 1 ]] && { query="^" } # Build a precise query for i in `seq 1 $((${colnos[0]}-1))`; do query="$query.*,"; done query="$query${txt[0]}" for j in `seq 2 ${#colnos[*]}`; do for i in `seq $((${colnos[$j-2]}+1)) $((${colnos[$j-1]}-1))`; do query="$query,.*" done query="$query,${txt[$j-1]}" done for i in `seq ${colnos[${#colnos[*]}-1]} $(($ncols-1))`; do query="$query,.*" done #TeSt: echo "query is '$query'" return `grep -qs -- "$query" ${gservice}_${gfilename[$2]}.setup` } # ---------------------------------------------------------------------------- item_search() # ---------------------------------------------------------------------------- # Purpose: Look for text in a column and display it the entire line. # Arguments: $1 - column, $2 - FILE_XXX, $3 - text # Returns: 0 - found, 1 - not found { local query="^" i ncols=${gfile_ncols[$2]} if [[ $1 -gt $ncols ]]; then echo -n "NAGCTL ERROR: Cannot search for column $1 when " echo "there are only $ncols columns." exit 11 fi term1=`escape_query "$3"` # Build a precise query for i in `seq 1 $(($1-1))`; do query="$query[^,]*,"; done query="$query(([^, ]*) )*$term1( [^,]*,|,|$)" grep -E -h -- "$query" ${gservice}_${gfilename[$2]}.setup } # ---------------------------------------------------------------------------- item_search2() # ---------------------------------------------------------------------------- # Purpose: Look for text in two columns and display it. # column1 must come before column2. # Arguments: $1 - column1, $2 - column2 ,$3 - FILE_XXX, # $4 - text1, $5 - text2 # Returns: 0 - found, 1 - not found { local query="^" i ncols=${gfile_ncols[$3]} if [[ $1 -gt $ncols ]]; then echo -n "NAGCTL ERROR: Cannot search for column $1 when " echo "there are only $ncols columns." exit 11 fi term1=`escape_query "$4"` term2=`escape_query "$5"` # Build a precise query for i in `seq 1 $(($1-1))`; do query="$query[^,]*,"; done query="$query(([^, ]*) )*$term1( [^,]*,|,|$)" for i in `seq $(($1+1)) $(($2-1))`; do query="$query[^,]*,"; done query="$query(([^, ]*) )*$term2( [^,]*,|,|$)" grep -Eh -- "$query" ${gservice}_${gfilename[$3]}.setup } # ---------------------------------------------------------------------------- item_searchN() # ---------------------------------------------------------------------------- # Purpose: Look for text in N columns and display the result. # columns must be in numeric sort order. # Arguments: $1 - space separated columns, $2 - FILE_XXX, # $3 - comma separated text # matching empty fields) # Returns: Outputs text { local query i ncols=${gfile_ncols[$2]} declare -a colnos=($1) IFS=, read -a txt <<<"$3" [[ ${colnos[0]} -eq 1 ]] && { query="^" } # Build a precise query for i in `seq 1 $((${colnos[0]}-1))`; do query="$query.*,"; done query="$query${txt[0]}" for j in `seq 2 ${#colnos[*]}`; do for i in `seq $((${colnos[$j-2]}+1)) $((${colnos[$j-1]}-1))`; do query="$query,.*" done query="$query,${txt[$j-1]}" done for i in `seq ${colnos[${#colnos[*]}-1]} $(($ncols-1))`; do query="$query,.*" done #TeSt: echo "search query is '$query'" grep -h -- "$query" ${gservice}_${gfilename[$2]}.setup } # ---------------------------------------------------------------------------- item_merge() # ---------------------------------------------------------------------------- # Purpose: Merge two lines of csv. A '-' in an oldcsv field nullifies it. # NOTE: $gcsv is modified by this function. # Arguments: $1 - numcolumns, $2 - oldcsv, $3 - newcsv # Returns: Nothing { local i newcsv= comma= IFS=, read -ra oldcols <<<"$2" IFS=, read -ra newcols <<<"$3" for i in `seq 0 $(($1-1))`; do oldcol=${oldcols[i]} newcol=${newcols[i]} if [[ -n $newcol ]]; then [[ $newcol == "-" ]] && newcol= newcsv="$newcsv$comma$newcol" else newcsv="$newcsv$comma$oldcol" fi comma="," done gcsv="$newcsv" } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # ACTIONS # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # --------------------------------------------------------------------------- action_show() # --------------------------------------------------------------------------- # Purpose: Show csv lines. # Arguments: None # Returns: Nothing { local column filter check_setup_dir cd $SETUP_DIR eval "$gcsv" 2>/dev/null [[ -z "$column" ]] && column=1 [[ -z "$filter" ]] && filter=".*" lockdir item_search $column $gnfile "$filter" unlockdir } # --------------------------------------------------------------------------- action_add() # --------------------------------------------------------------------------- # Purpose: Add new entry. # Arguments: None # Returns: Nothing { check_setup_dir if ! check_num_cols ${gfile_ncols[$gnfile]}; then echo "NAGCTL ERROR: Invalid data. Number of columns mismatch." exit 10 fi if ! check_null_cols $gnfile; then echo "NAGCTL ERROR: Invalid data. A required field is empty." exit 10 fi cd $SETUP_DIR case $gnfile in $FILE_HOSTTEMPLATES) add_hosttemplate_line ;; $FILE_SERVICETEMPLATES) add_servicetemplate_line ;; $FILE_HOSTS) add_host_line ;; $FILE_SERVICES) add_service_line ;; $FILE_HOSTGROUPS) add_hostgroups_line ;; $FILE_SERVICEGROUPS) add_servicegroups_line ;; $FILE_CONTACTS) add_contacts_line ;; $FILE_CONTACTGROUPS) add_contactgroups_line ;; $FILE_SERVICESETS) add_servicesets_line ;; $FILE_TIMEPERIODS) add_timeperiods_line ;; $FILE_COMMANDS) add_commands_line ;; $FILE_SERVICEDEPS) add_servicedeps_line ;; $FILE_HOSTDEPS) add_hostdeps_line ;; $FILE_SERVICEESC) add_serviceesc_line ;; $FILE_HOSTESC) add_hostesc_line ;; $FILE_SERVICEEXTINFO) add_serviceextinfo_line ;; $FILE_HOSTEXTINFO) add_hostextinfo_line ;; esac } # --------------------------------------------------------------------------- action_modify() # --------------------------------------------------------------------------- # Purpose: Modify an existing entry. # Arguments: None # Returns: Nothing { check_setup_dir if ! check_num_cols ${gfile_ncols[$gnfile]}; then echo "NAGCTL ERROR: Invalid data. Number of columns mismatch." exit 10 fi cd $SETUP_DIR case $gnfile in $FILE_HOSTTEMPLATES) modify_hosttemplate_line ;; $FILE_SERVICETEMPLATES) modify_servicetemplate_line ;; $FILE_HOSTS) modify_host_line ;; $FILE_SERVICES) modify_service_line ;; $FILE_HOSTGROUPS) modify_hostgroups_line ;; $FILE_SERVICEGROUPS) modify_servicegroups_line ;; $FILE_CONTACTS) modify_contacts_line ;; $FILE_CONTACTGROUPS) modify_contactgroups_line ;; $FILE_SERVICESETS) modify_servicesets_line ;; $FILE_TIMEPERIODS) modify_timeperiods_line ;; $FILE_COMMANDS) modify_commands_line ;; $FILE_SERVICEDEPS) modify_servicedeps_line ;; $FILE_HOSTDEPS) modify_hostdeps_line ;; $FILE_SERVICEESC) modify_serviceesc_line ;; $FILE_HOSTESC) modify_hostesc_line ;; $FILE_SERVICEEXTINFO) modify_serviceextinfo_line ;; $FILE_HOSTEXTINFO) modify_hostextinfo_line ;; esac } # --------------------------------------------------------------------------- action_delete() # --------------------------------------------------------------------------- # Purpose: Delete an existing entry. # Arguments: None # Returns: Nothing { check_setup_dir if ! check_num_cols ${gfile_ncols[$gnfile]}; then echo "NAGCTL ERROR: Invalid data. Number of columns mismatch." exit 10 fi if ! check_delete_null_cols $gnfile; then echo "NAGCTL ERROR: Invalid data. A required field is empty." exit 10 fi cd $SETUP_DIR case $gnfile in $FILE_HOSTTEMPLATES) delete_hosttemplate_line ;; $FILE_SERVICETEMPLATES) delete_servicetemplate_line ;; $FILE_HOSTS) delete_host_line ;; $FILE_SERVICES) delete_service_line ;; $FILE_HOSTGROUPS) delete_hostgroups_line ;; $FILE_SERVICEGROUPS) delete_servicegroups_line ;; $FILE_CONTACTS) delete_contacts_line ;; $FILE_CONTACTGROUPS) delete_contactgroups_line ;; $FILE_SERVICESETS) delete_servicesets_line ;; $FILE_TIMEPERIODS) delete_timeperiods_line ;; $FILE_COMMANDS) delete_commands_line ;; $FILE_SERVICEDEPS) delete_servicedeps_line ;; $FILE_HOSTDEPS) delete_hostdeps_line ;; $FILE_SERVICEESC) delete_serviceesc_line ;; $FILE_HOSTESC) delete_hostesc_line ;; $FILE_SERVICEEXTINFO) delete_serviceextinfo_line ;; $FILE_HOSTEXTINFO) delete_hostextinfo_line ;; esac } # --------------------------------------------------------------------------- action_pipecmd() # --------------------------------------------------------------------------- # Purpose: Send to Nagios cmd file. # Arguments: None # Returns: Nothing { cd $SETUP_DIR case $gnfile in $PIPECMD_ENABLEHOSTSVCCHECKS) pipecmd_enablehostsvcchecks ;; $PIPECMD_DISABLEHOSTSVCCHECKS) pipecmd_disablehostsvcchecks ;; $PIPECMD_DISABLESVCCHECK) pipecmd_disablesvccheck ;; $PIPECMD_ENABLESVCCHECK) pipecmd_enablesvccheck ;; $PIPECMD_SCHEDULEHOSTDOWNTIME) pipecmd_schedulehostdowntime ;; $PIPECMD_DELHOSTDOWNTIME) pipecmd_delhostdowntime ;; $PIPECMD_SCHEDULEHOSTSVCDOWNTIME) pipecmd_schedulehostsvcdowntime ;; $PIPECMD_DELHOSTSVCDOWNTIME) pipecmd_delhostsvcdowntime ;; $PIPECMD_SCHEDULESVCDOWNTIME) pipecmd_schedulesvcdowntime ;; $PIPECMD_DELSVCDOWNTIME) pipecmd_delsvcdowntime ;; esac } # --------------------------------------------------------------------------- action_propagate() # --------------------------------------------------------------------------- # Purpose: Propagate to master. # Arguments: None # Returns: Nothing { : } # --------------------------------------------------------------------------- action_check() # --------------------------------------------------------------------------- # Purpose: Propagate to master. # Arguments: None # Returns: Nothing { case $gnfile in $CHECK_NAGIOSCONFIG) check_nagiosconfig ;; esac } # --------------------------------------------------------------------------- action_restart() # --------------------------------------------------------------------------- # Purpose: Restart a system service. # Arguments: None # Returns: Nothing { case $gnfile in $RESTART_NAGIOS) restart_nagios ;; esac } # --------------------------------------------------------------------------- action_apply() # --------------------------------------------------------------------------- # Purpose: Restart a system service. # Arguments: None # Returns: Nothing { case $gnfile in $APPLY_NAGIOSCONFIG) apply_nagiosconfig ;; $APPLY_NAGIOSLASTGOODCONFIG) apply_nagioslastgoodconfig ;; esac } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # MODIFY ACTION METHODS # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # ---------------------------------------------------------------------------- modify_hosttemplate_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hosttemplates file. # Arguments: None # Returns: Nothing { local name IFS=, read name x contacts contactgroups x x x notifperiod \ notifopts x checkperiod x check_command x <<<"$gcsv" [[ -n $check_command ]] && check_command=`echo "$command" | \ sed -nr 's/%21/!/g;s/(^[^!]*)(!|%21|$).*/\1/p'` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if ! item_exists 1 $FILE_HOSTTEMPLATES $name; then echo "NAGCTL ERROR: Item not found." unlockdir exit 14 fi [[ -n $check_command ]] && { if ! item_exists 1 $FILE_COMMANDS "$check_command"; then echo -n "NAGCTL ERROR: Command '$check_command' not found." echo " Will not modify host." unlockdir exit 13 fi } [[ -n $notifperiod && $notifperiod != "-" ]] && { if ! item_exists 1 $FILE_TIMEPERIODS $notifperiod; then echo -n "NAGCTL ERROR: Notifperiod '$notifperiod' not found." echo " Will not modify host template." unlockdir exit 14 fi } [[ -n $checkperiod && $checkperiod != "-" ]] && { if ! item_exists 1 $FILE_TIMEPERIODS $checkperiod; then echo -n "NAGCTL ERROR: Checkperiod '$checkperiod' not found." echo "Will not modify host template." unlockdir exit 14 fi } [[ -n $contacts && $contacts != "-" ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not modify host template." unlockdir exit 13 fi done } [[ -n $contactgroups && $contactgroups != "-" ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo -n "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not modify host template." unlockdir exit 13 fi done } [[ -n $notifopts && $notifopts != "-" ]] && { numterms=`echo "$notifopts" | grep -Eo '([^ ]*)?' | wc -l` numvalidterms=`echo "$notifopts" | \ grep -Eo '(\bd( |$)|\bu( |$)|\br( |$)|\bf( |$)|\bs( |$)|\bn( |$))?' | wc -l` [[ $numterms -ne $numvalidterms ]] && { echo -n "NAGCTL ERROR: Notifopts '$notifopts' contains invalid" echo " option(s). Only d, u, r, f, s, or n are allowed." unlockdir exit 14 } } olditem=`item_search 1 $FILE_HOSTTEMPLATES "$name"` item_merge ${gfile_ncols[$FILE_HOSTTEMPLATES]} "$olditem" "$gcsv" item_delete 1 $FILE_HOSTTEMPLATES "$name" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_servicetemplate_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x contacts contactgroups notifopts x x x x notifperiod \ x checkperiod x x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` #contacts=`echo "$gcsv" | cut -d, -f3` #contactgroups=`echo "$gcsv" | cut -d, -f4` #notifperiod=`echo "$gcsv" | cut -d, -f10` #checkperiod=`echo "$gcsv" | cut -d, -f12` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if ! item_exists 1 $FILE_SERVICETEMPLATES "$name"; then echo "NAGCTL ERROR: Item not found." unlockdir exit 14 fi [[ -n $notifperiod && $notifperiod != "-" ]] && { if ! item_exists 1 $FILE_TIMEPERIODS $notifperiod; then echo -n "NAGCTL ERROR: Notifperiod '$notifperiod' unavailable." echo "Will not modify service template." unlockdir exit 14 fi } [[ -n $checkperiod && $checkperiod != "-" ]] && { if ! item_exists 1 $FILE_TIMEPERIODS $checkperiod; then echo -n "NAGCTL ERROR: Checkperiod '$checkperiod' unavailable." echo "Will not modify service template." unlockdir exit 14 fi } [[ -n $contacts && $contacts != "-" ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not modify service template." unlockdir exit 13 fi done } [[ -n $contactgroups && $contactgroups != "-" ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo -n "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not modify service template." unlockdir exit 13 fi done } [[ -n $notifopts && $notifopts != "-" ]] && { numterms=`echo "$notifopts" | grep -Eo '([^ ]*)?' | wc -l` numvalidterms=`echo "$notifopts" | \ grep -Eo '(\bw( |$)|\bu( |$)|\bc( |$)|\br( |$)|\bf( |$)|\bs( |$)|\bn( |$))?' | wc -l` [[ $numterms -ne $numvalidterms ]] && { echo -n "NAGCTL ERROR: Notifopts '$notifopts' contains invalid" echo " option(s). Only w, u, c, r, f, s, or n are allowed." unlockdir exit 14 } } olditem=`item_search 1 $FILE_SERVICETEMPLATES "$name"` item_merge ${gfile_ncols[$FILE_SERVICETEMPLATES]} "$olditem" "$gcsv" item_delete 1 $FILE_SERVICETEMPLATES "$name" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_host_line() # ---------------------------------------------------------------------------- { local name disabled a is_ip IFS=, read name x ipaddress template hostgroups contacts contactgroups \ x servicesets disable x parents command x <<<"$gcsv" # IP check is disabled (Issue #26) #is_ip='^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.)){3}' #is_ip+='(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'1 is_ip=".*" [[ -n $ipaddress ]] && { if ! echo "$ipaddress" | grep -qs -E "$is_ip"; then echo "NAGCTL ERROR: Invalid IP address." exit 13 fi } lockdir if ! item_exists 1 $FILE_HOSTS "$name"; then echo "NAGCTL ERROR: Item not found." unlockdir exit 14 fi [[ -n $command && $command != "-" ]] && { if ! item_exists 1 $FILE_COMMANDS "$command"; then echo -n "NAGCTL ERROR: Command '$command' not found." echo " Will not modify host." unlockdir exit 13 fi } [[ -n $servicesets && $servicesets != "-" ]] && { for i in $servicesets; do if ! item_exists 1 $FILE_SERVICESETS "$i"; then echo -n "NAGCTL ERROR: Service Set '$i' not found." echo " Will not modify host." unlockdir exit 13 fi done } [[ -n $template ]] && { if ! item_exists 1 $FILE_HOSTTEMPLATES "$template"; then echo -n "NAGCTL ERROR: Template '$template' not found." echo " Will not modify host." unlockdir exit 13 fi } [[ -n $hostgroups && $hostgroups != "-" ]] && { for i in $hostgroups; do if ! item_exists 1 $FILE_HOSTGROUPS "$i"; then echo -n "NAGCTL ERROR: Hostgroup '$i' not found." echo " Will not modify host." unlockdir exit 13 fi done } [[ -n $contacts && $contacts != "-" ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not modify host." unlockdir exit 13 fi done } [[ -n $contactgroups && $contactgroups != "-" ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo -n "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not modify host." unlockdir exit 13 fi done } [[ -n $parents && $parents != "-" ]] && { [[ $parents == $name ]] && { echo -n "NAGCTL ERROR: Host cannot be a parent of itself." echo " Will not modify host." unlockdir exit 13 } for i in $parents; do if ! item_exists 1 $FILE_HOSTS "$i"; then echo -n "NAGCTL ERROR: Parent '$i' not found." echo " Will not modify host." unlockdir exit 13 fi done } : $((disable++)) ; : $((disable--)) if [[ $disable -eq 1 ]]; then while read line; do disabled=`echo $line | cut -d, -f11` : $((disabled++)) ; : $((disabled--)) if [[ "$disabled" -ne 1 ]]; then echo -n "NAGCTL ERROR: Host has one or more" echo " enabled services. Will not disable." unlockdir exit 16 fi done <<<"$( item_search 1 $FILE_SERVICES "$name" )" if item_exists "1" $FILE_HOSTEXTINFO "$name" then echo -n "NAGCTL ERROR: Host has" echo " one or more hostextinfo entries. Will not disable." unlockdir exit 16 fi elif [[ $disable -eq 2 ]]; then while read line; do disabled=`echo $line | cut -d, -f11` : $((disabled++)) ; : $((disabled--)) if [[ "$disabled" -eq 0 ]]; then echo -n "NAGCTL ERROR: Host has one or more" echo " enabled services. Will not disable." unlockdir exit 16 fi done <<<"$( item_search 1 $FILE_SERVICES "$name" )" if item_exists "1" $FILE_HOSTEXTINFO "$name" then echo -n "NAGCTL ERROR: Host has" echo " one or more hostextinfo entries. Will not disable." unlockdir exit 16 fi fi olditem=`item_search 1 $FILE_HOSTS "$name"` item_merge ${gfile_ncols[$FILE_HOSTS]} "$olditem" "$gcsv" item_delete 1 $FILE_HOSTS "$name" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_service_line() # ---------------------------------------------------------------------------- { local name svcdesc IFS=, read name template command svcdesc servicegroups \ contacts contactgroups x x x disable x x x x x x x x x \ x x x x x x x x x x x x x x svcopt x <<<"$gcsv" #[[ -n $command ]] && command=`echo "$command" | grep -o '^[-A-Za-z0-9_]*'` [[ -n $command ]] && command=`echo "$command" | \ sed -nr 's/%21/!/g;s/(^[^!]*)(!|%21|$).*/\1/p'` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } [[ $name == *'*'* || $svcdesc == *'*'* ]] && { echo "NAGCTL ERROR: wildcards not allowed" exit 13 } lockdir if ! item_exists2 1 4 $FILE_SERVICES "$name" "$svcdesc"; then echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi # command must exist in commands file [[ -n $command ]] && { if ! item_exists 1 $FILE_COMMANDS "$command"; then echo -n "NAGCTL ERROR: Command '$command' not found. " echo "Cannot modify item." unlockdir exit 13 fi } [[ -n $template ]] && { if ! item_exists 1 $FILE_SERVICETEMPLATES "$template"; then echo -n "NAGCTL ERROR: Template '$template' not found." echo " Will not modify service." unlockdir exit 13 fi } [[ -n $contacts && $contacts != "-" ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not modify service." unlockdir exit 13 fi done } [[ -n $contactgroups && $contactgroups != "-" ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo -n "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not modify service." unlockdir exit 13 fi done } [[ -n $servicegroups && $servicegroups != "-" ]] && { for i in $servicegroups; do if ! item_exists 1 $FILE_SERVICEGROUPS "$i"; then echo -n "NAGCTL ERROR: Servicegroup '$i' not found." echo " Will not modify service." unlockdir exit 13 fi done } [[ $disable == "0" || $disable == "2" ]] && { a=`item_search 1 $FILE_HOSTS "$name"` a_disabled=`echo $a | cut -d, -f10` if [[ "$a_disabled" == "1" ]]; then echo -n "NAGCTL ERROR: Host is disabled. Will not enable." unlockdir exit 16 elif [[ "$disable" == "0" && "$a_disabled" == "2" ]]; then echo -n "NAGCTL ERROR: Host is in Test mode. Will not enable." unlockdir exit 16 fi } [[ -n $svcopt && $svcopt != "-" ]] && { numterms=`echo "$svcopt" | grep -Eo '([^ ]*)?' | wc -l` numvalidterms=`echo "$svcopt" | \ grep -Eo '(\bw( |$)|\bu( |$)|\bc( |$)|\br( |$)|\bf( |$)|\bs( |$)|\bn( |$))?' | wc -l` [[ $numterms -ne $numvalidterms ]] && { echo -n "NAGCTL ERROR: Service notif opts '$svcopt' contains invalid" echo " option(s). Only w, u, c, r, f, s, or n are allowed." unlockdir exit 14 } } olditem=`item_search2 1 4 $FILE_SERVICES "$name" "$svcdesc"` item_merge ${gfile_ncols[$FILE_SERVICES]} "$olditem" "$gcsv" item_delete2 1 4 $FILE_SERVICES "$name" "$svcdesc" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_hostgroups_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if ! item_exists 1 $FILE_HOSTGROUPS "$name"; then echo "NAGCTL ERROR: Item not found." unlockdir exit 14 fi olditem=`item_search 1 $FILE_HOSTGROUPS "$name"` item_merge ${gfile_ncols[$FILE_HOSTGROUPS]} "$olditem" "$gcsv" item_delete 1 $FILE_HOSTGROUPS "$name" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_servicegroups_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if ! item_exists 1 $FILE_SERVICEGROUPS "$name"; then echo "NAGCTL ERROR: Item not found." unlockdir exit 14 fi olditem=`item_search 1 $FILE_SERVICEGROUPS "$name"` item_merge ${gfile_ncols[$FILE_SERVICEGROUPS]} "$olditem" "$gcsv" item_delete 1 $FILE_SERVICEGROUPS "$name" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_contacts_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x x x svcnp svcopt x hstnp hstopt x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` #svcnp=`echo "$gcsv" | cut -d, -f5` #hstnp=`echo "$gcsv" | cut -d, -f8` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if ! item_exists 1 $FILE_CONTACTS "$name"; then echo "NAGCTL ERROR: Item not found." unlockdir exit 14 fi if ! item_exists 1 $FILE_TIMEPERIODS $svcnp; then echo -n "NAGCTL ERROR: Service Notifperiod '$svcnp' unavailable." echo "Will not modify contact." unlockdir exit 14 fi if ! item_exists 1 $FILE_TIMEPERIODS $hstnp; then echo -n "NAGCTL ERROR: Host Notifperiod '$hstnp' unavailable." echo "Will not modify contact." unlockdir exit 14 fi [[ -n $hstopt && $hstopt != "-" ]] && { numterms=`echo "$hstopt" | grep -Eo '([^ ]*)?' | wc -l` numvalidterms=`echo "$hstopt" | \ grep -Eo '(\bd( |$)|\bu( |$)|\br( |$)|\bf( |$)|\bs( |$)|\bn( |$))?' | wc -l` [[ $numterms -ne $numvalidterms ]] && { echo -n "NAGCTL ERROR: Host notif opts '$hstopt' contains invalid" echo " option(s). Only d, u, r, f, s, or n are allowed." unlockdir exit 14 } } [[ -n $svcopt && $svcopt != "-" ]] && { numterms=`echo "$svcopt" | grep -Eo '([^ ]*)?' | wc -l` numvalidterms=`echo "$svcopt" | \ grep -Eo '(\bw( |$)|\bu( |$)|\bc( |$)|\br( |$)|\bf( |$)|\bs( |$)|\bn( |$))?' | wc -l` [[ $numterms -ne $numvalidterms ]] && { echo -n "NAGCTL ERROR: Service notif opts '$svcopt' contains invalid" echo " option(s). Only w, u, c, r, f, s, or n are allowed." unlockdir exit 14 } } olditem=`item_search 1 $FILE_CONTACTS "$name"` item_merge ${gfile_ncols[$FILE_CONTACTS]} "$olditem" "$gcsv" item_delete 1 $FILE_CONTACTS "$name" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_contactgroups_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x memberlist x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` #memberlist=`echo "$gcsv" | cut -d, -f3` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if ! item_exists 1 $FILE_CONTACTGROUPS "$name"; then echo "NAGCTL ERROR: Item not found." unlockdir exit 14 fi if [[ -n $memberlist && $memberlist != "-" ]]; then for i in $memberlist; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo "NAGCTL ERROR: Member '$i' not found. Cannot add item." unlockdir exit 13 fi done fi olditem=`item_search 1 $FILE_CONTACTGROUPS "$name"` item_merge ${gfile_ncols[$FILE_CONTACTGROUPS]} "$olditem" "$gcsv" item_delete 1 $FILE_CONTACTGROUPS "$name" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_servicesets_line() # ---------------------------------------------------------------------------- { local name svcdesc IFS=, read name template command svcdesc servicegroups contacts \ contactgroups x <<<"$gcsv" [[ -n $command ]] && command=`echo "$command" | \ sed -nr 's/%21/!/g;s/(^[^!]*)(!|%21|$).*/\1/p'` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if ! item_exists2 1 4 $FILE_SERVICESETS "$name" "$svcdesc"; then echo "NAGCTL ERROR: Item not found." unlockdir exit 14 fi # command must exist in commands file [[ -n $command ]] && { if ! item_exists 1 $FILE_COMMANDS "$command"; then echo "NAGCTL ERROR: Command '$command' unavailable. Cannot add item." unlockdir exit 13 fi } [[ -n $template ]] && { if ! item_exists 1 $FILE_SERVICETEMPLATES "$template"; then echo -n "NAGCTL ERROR: Template '$template' not found." echo " Will not modify service set." unlockdir exit 13 fi } [[ -n $contacts && $contacts != "-" ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not modify service set." unlockdir exit 13 fi done } [[ -n $contactgroups && $contactgroups != "-" ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo -n "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not modify service set." unlockdir exit 13 fi done } [[ -n $servicegroups && $servicegroups != "-" ]] && { for i in $servicegroups; do if ! item_exists 1 $FILE_SERVICEGROUPS "$i"; then echo -n "NAGCTL ERROR: Servicegroup '$i' not found." echo " Will not modify service set." unlockdir exit 13 fi done } olditem=`item_search2 1 4 $FILE_SERVICESETS "$name" "$svcdesc"` item_merge ${gfile_ncols[$FILE_SERVICESETS]} "$olditem" "$gcsv" item_delete2 1 4 $FILE_SERVICESETS "$name" "$svcdesc" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_timeperiods_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x x exclude x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` #exclude=`echo "$gcsv" | cut -d, -f4` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if ! item_exists 1 $FILE_TIMEPERIODS "$name"; then echo "NAGCTL ERROR: Item not found." unlockdir exit 14 fi [[ -n $exclude && $exclude != "-" ]] && { for i in $exclude; do if ! item_exists 1 $FILE_TIMEPERIODS "$i"; then echo -n "NAGCTL ERROR: Excluded timeperiod '$i' not found." echo " Cannot modify item." unlockdir exit 13 fi done } olditem=`item_search 1 $FILE_TIMEPERIODS "$name"` item_merge ${gfile_ncols[$FILE_TIMEPERIODS]} "$olditem" "$gcsv" item_delete 1 $FILE_TIMEPERIODS "$name" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_commands_line() # ---------------------------------------------------------------------------- { local name IFS=, read -r name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if ! item_exists 1 $FILE_COMMANDS "$name"; then echo "NAGCTL ERROR: Item not found." unlockdir exit 14 fi olditem=`item_search 1 $FILE_COMMANDS "$name"` item_merge ${gfile_ncols[$FILE_COMMANDS]} "$olditem" "$gcsv" item_delete 1 $FILE_COMMANDS "$name" echo -E "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_servicedeps_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the servicedeps file. # Arguments: None # Returns: Nothing { IFS=, read dephostname dephostgroupname depsvcdesc \ hostname hostgroupname svcdesc inheritsparent \ execfailcriteria notiffailcriteria period disable x \ <<<"$gcsv" [[ $dephostname == *" "* || $dephostgroupname == *" "* || \ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir longline="$dephostname,$dephostgroupname,$depsvcdesc" longline="$longline,$hostname,$hostgroupname,$svcdesc" if ! item_existsN "1 2 3 4 5 6" $FILE_SERVICEDEPS "$longline" then echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi olditem=`item_searchN "1 2 3 4 5 6" $FILE_SERVICEDEPS "$longline"` item_merge ${gfile_ncols[$FILE_SERVICEDEPS]} "$olditem" "$gcsv" item_deleteN "1 2 3 4 5 6" $FILE_SERVICEDEPS "$longline" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_hostdeps_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { IFS=, read dephostname dephostgroupname \ hostname hostgroupname inheritsparent \ execfailcriteria notiffailcriteria period disable x \ <<<"$gcsv" [[ $dephostname == *" "* || $dephostgroupname == *" "* || \ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir longline="$dephostname,$dephostgroupname" longline="$longline,$hostname,$hostgroupname" if ! item_existsN "1 2 3 4" $FILE_HOSTDEPS "$longline" then echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi olditem=`item_searchN "1 2 3 4" $FILE_HOSTDEPS "$longline"` item_merge ${gfile_ncols[$FILE_HOSTDEPS]} "$olditem" "$gcsv" item_deleteN "1 2 3 4" $FILE_HOSTDEPS "$longline" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_serviceesc_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { IFS=, read hostname hostgroupname svcdesc contacts contactgroups \ firstnotif lastnotif notifinterval period escopts disable x \ <<<"$gcsv" [[ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } longline="$hostname,$hostgroupname,$svcdesc" lockdir if ! item_existsN "1 2 3" $FILE_SERVICEESC "$longline" then echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi [[ -n $contacts && $contacts != "-" ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not modify item." unlockdir exit 13 fi done } [[ -n $contactgroups && $contactgroups != "-" ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo -n "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not modify item." unlockdir exit 13 fi done } [[ -n $period && $period != "-" ]] && { if ! item_exists 1 $FILE_TIMEPERIODS "$period"; then echo -n "NAGCTL ERROR: Escalation period not found." echo " Will not modify item." unlockdir exit 14 fi } olditem=`item_searchN "1 2 3" $FILE_SERVICEESC "$longline"` item_merge ${gfile_ncols[$FILE_SERVICEESC]} "$olditem" "$gcsv" # Check for contact[group]s IFS=, read x x x contacts contactgroups x <<<"$gcsv" [[ -z $contacts && -z $contactgroups ]] && { echo -n "NAGCTL ERROR: Must have a contact or contactgroup." echo " Will not modify item." unlockdir exit 13 } item_deleteN "1 2 3" $FILE_SERVICEESC "$longline" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_hostesc_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { IFS=, read hostname hostgroupname contacts contactgroups \ firstnotif lastnotif notifinterval period escopts disable x \ <<<"$gcsv" [[ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir if ! item_exists2 1 2 $FILE_HOSTESC "$hostname" "$hostgroupname" then echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi [[ -n $contacts && $contacts != "-" ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not modify item." unlockdir exit 13 fi done } [[ -n $contactgroups && $contactgroups != "-" ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo -n "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not modify item." unlockdir exit 13 fi done } [[ -n $period && $period != "-" ]] && { if ! item_exists 1 $FILE_TIMEPERIODS "$period"; then echo -n "NAGCTL ERROR: Escalation period not found." echo " Will not modify item." unlockdir exit 14 fi } olditem=`item_search2 1 2 $FILE_HOSTESC "$hostname" "$hostgroupname"` item_merge ${gfile_ncols[$FILE_HOSTESC]} "$olditem" "$gcsv" # Check for contact[group]s IFS=, read x x contacts contactgroups x <<<"$gcsv" [[ -z $contacts && -z $contactgroups ]] && { echo -n "NAGCTL ERROR: Must have a contact or contactgroup." echo " Will not modify item." unlockdir exit 13 } item_delete2 1 2 $FILE_HOSTESC "$hostname" "$hostgroupname" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_serviceextinfo_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { IFS=, read hostname svcdesc notes notes_url action_url \ icon_image icon_image_alt disable x \ <<<"$gcsv" [[ $hostname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir if ! item_exists2 1 2 $FILE_SERVICEEXTINFO "$hostname" "$svcdesc" then echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi olditem=`item_search2 1 2 $FILE_SERVICEEXTINFO "$hostname" "$svcdesc"` item_merge ${gfile_ncols[$FILE_SERVICEEXTINFO]} "$olditem" "$gcsv" item_delete2 1 2 $FILE_SERVICEEXTINFO "$hostname" "$svcdesc" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- modify_hostextinfo_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { IFS=, read hostname notes notes_url action_url \ icon_image icon_image_alt vrml_image statusmap_image \ coords2d coords3d disable x \ <<<"$gcsv" [[ $hostname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir if ! item_exists "1" $FILE_HOSTEXTINFO "$hostname" then echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi olditem=`item_search "1" $FILE_HOSTEXTINFO "$hostname"` item_merge ${gfile_ncols[$FILE_HOSTEXTINFO]} "$olditem" "$gcsv" item_delete "1" $FILE_HOSTEXTINFO "$hostname" echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # ADD ACTION METHODS # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # ---------------------------------------------------------------------------- add_hosttemplate_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hosttemplates file. # Arguments: None # Returns: Nothing { local name IFS=, read name x contacts contactgroups x x x notifperiod \ notifopts x checkperiod x check_command x <<<"$gcsv" [[ -n $check_command ]] && check_command=`echo "$command" | \ sed -nr 's/%21/!/g;s/(^[^!]*)(!|%21|$).*/\1/p'` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 1 $FILE_HOSTTEMPLATES "$name"; then echo "NAGCTL ERROR: Host template name is a duplicate. Cannot add item." unlockdir exit 13 fi [[ -n $check_command ]] && { if ! item_exists 1 $FILE_COMMANDS "$check_command"; then echo -n "NAGCTL ERROR: Command '$check_command' not found." echo " Will not modify host." unlockdir exit 13 fi } [[ -n $notifopts && $notifopts != "-" ]] && { numterms=`echo "$notifopts" | grep -Eo '([^ ]*)?' | wc -l` numvalidterms=`echo "$notifopts" | \ grep -Eo '(\bd( |$)|\bu( |$)|\br( |$)|\bf( |$)|\bs( |$)|\bn( |$))?' | wc -l` [[ $numterms -ne $numvalidterms ]] && { echo -n "NAGCTL ERROR: Notifopts '$notifopts' contains invalid" echo " option(s). Only d, u, r, f, s, or n are allowed." unlockdir exit 14 } } if ! item_exists 1 $FILE_TIMEPERIODS $notifperiod; then echo -n "NAGCTL ERROR: Notifperiod '$notifperiod' unavailable." echo "Will not add host template." unlockdir exit 14 fi if ! item_exists 1 $FILE_TIMEPERIODS $checkperiod; then echo -n "NAGCTL ERROR: Checkperiod '$checkperiod' unavailable." echo "Will not add host template." unlockdir exit 14 fi [[ -n $contacts && $contacts != "-" ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not add host template." unlockdir exit 13 fi done } [[ -n $contactgroups && $contactgroups != "-" ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo -n "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not add host template." unlockdir exit 13 fi done } echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_servicetemplate_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x contacts contactgroups notifopts x x x x notifperiod \ x checkperiod x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` #contacts=`echo "$gcsv" | cut -d, -f3` #contactgroups=`echo "$gcsv" | cut -d, -f4` #notifperiod=`echo "$gcsv" | cut -d, -f10` #checkperiod=`echo "$gcsv" | cut -d, -f12` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 1 $FILE_SERVICETEMPLATES "$name"; then echo "NAGCTL ERROR: Service template name is a duplicate. Cannot add item." unlockdir exit 13 fi if ! item_exists 1 $FILE_TIMEPERIODS $notifperiod; then echo -n "NAGCTL ERROR: Notifperiod '$notifperiod' unavailable." echo "Will not add service template." unlockdir exit 14 fi if ! item_exists 1 $FILE_TIMEPERIODS $checkperiod; then echo -n "NAGCTL ERROR: Checkperiod '$checkperiod' unavailable." echo "Will not add service template." unlockdir exit 14 fi [[ -n $contacts ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not add service template." unlockdir exit 13 fi done } [[ -n $contactgroups ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not add service template." unlockdir exit 13 fi done } [[ -n $notifopts && $notifopts != "-" ]] && { numterms=`echo "$notifopts" | grep -Eo '([^ ]*)?' | wc -l` numvalidterms=`echo "$notifopts" | \ grep -Eo '(\bw( |$)|\bu( |$)|\bc( |$)|\br( |$)|\bf( |$)|\bs( |$)|\bn( |$))?' | wc -l` [[ $numterms -ne $numvalidterms ]] && { echo -n "NAGCTL ERROR: Notifopts '$notifopts' contains invalid" echo " option(s). Only w, u, c, r, f, s, or n are allowed." unlockdir exit 14 } } echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_host_line() # ---------------------------------------------------------------------------- { local name i s IFS=, read name x ipaddress template hostgroups contacts contactgroups \ x servicesets disable x <<<"$gcsv" [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } # IP check is disabled (Issue #26) #is_ip='^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.)){3}' #is_ip+='(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'1 is_ip=".*" if ! echo "$ipaddress" | grep -qs -E "$is_ip"; then echo "NAGCTL ERROR: Invalid IP address." exit 13 fi lockdir if item_exists 1 $FILE_HOSTS "$name"; then echo "NAGCTL ERROR: Host name is a duplicate. Cannot add item." unlockdir exit 13 fi # For now don't allow creation in disabled state. # User can create enabled then modify to disable. [[ $disable -gt 0 ]] && { echo -n "NAGCTL ERROR: Host cannot be created in a disabled state." echo " Will not add host." unlockdir exit 13 } [[ -n $servicesets ]] && { for i in $servicesets; do if ! item_exists 1 $FILE_SERVICESETS "$i"; then echo -n "NAGCTL ERROR: Service Set '$i' not found." echo " Will not add host." unlockdir exit 13 fi done } [[ -n $template ]] && { if ! item_exists 1 $FILE_HOSTTEMPLATES "$template"; then echo -n "NAGCTL ERROR: Template '$template' not found." echo " Will not add host." unlockdir exit 13 fi } [[ -n $hostgroups ]] && { for i in $hostgroups; do if ! item_exists 1 $FILE_HOSTGROUPS "$i"; then echo -n "NAGCTL ERROR: Hostgroup '$i' not found." echo " Will not add host." unlockdir exit 13 fi done } [[ -n $contacts ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not add host." unlockdir exit 13 fi done } [[ -n $contactgroups ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not add host." unlockdir exit 13 fi done } echo "$gcsv" >>$SETUP_FILE [[ -n $servicesets ]] && { file="${gservice}_${gfilename[$FILE_SERVICES]}.setup" tfile="/var/tmp/$file" :>$tfile # Apply # Reverse servicesets for i in $servicesets; do s="$i$spc$s"; spc=" "; done # Apply for i in $s; do item_search 1 $FILE_SERVICESETS "$i" | \ sed -- "s/^[^,]*\(,.*\)/$name\1/" \ >>$tfile done # Filter out duplicate services n=`wc -l $tfile | cut -d" " -f 1` for i in `seq 1 $n`;do svcdesc=`sed -n "$i{p}" $tfile | cut -d, -f4` grep -m1 "^[^,]*,[^,]*,[^,]*,$svcdesc," $tfile done | sort -u >>$file rm -f $tfile } unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_service_line() # ---------------------------------------------------------------------------- { local name svcdesc IFS=, read name template command svcdesc servicegroups \ contacts contactgroups x <<<"$gcsv" #command=`echo "$command" | grep -o '[-A-Za-z0-9_]*'` command=`echo "$command" | sed -nr 's/%21/!/g;s/(^[^!]*)(!|%21|$).*/\1/p'` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists2 1 4 $FILE_SERVICES "$name" "$svcdesc"; then echo "NAGCTL ERROR: Service is a duplicate. Cannot add item." unlockdir exit 13 fi # name must exist in hosts file if ! item_exists 1 $FILE_HOSTS "$name"; then echo "NAGCTL ERROR: No host entry for this service. Cannot add item." unlockdir exit 13 fi # command must exist in commands file if ! item_exists 1 $FILE_COMMANDS "$command"; then echo "NAGCTL ERROR: Command '$command' unavailable. Cannot add item." unlockdir exit 13 fi [[ -n $template ]] && { if ! item_exists 1 $FILE_SERVICETEMPLATES "$template"; then echo -n "NAGCTL ERROR: Template '$template' not found." echo " Will not add service." unlockdir exit 13 fi } [[ -n $contacts ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not add service." unlockdir exit 13 fi done } [[ -n $contactgroups ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not add service." unlockdir exit 13 fi done } [[ -n $servicegroups ]] && { for i in $servicegroups; do if ! item_exists 1 $FILE_SERVICEGROUPS "$i"; then echo -n "NAGCTL ERROR: Servicegroup '$i' not found." echo " Will not add service." unlockdir exit 13 fi done } echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_hostgroups_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 1 $FILE_HOSTGROUPS "$name"; then echo "NAGCTL ERROR: Hostgroup name is a duplicate. Cannot add item." unlockdir exit 13 fi echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_servicegroups_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 1 $FILE_SERVICEGROUPS "$name"; then echo "NAGCTL ERROR: Servicegroup name is a duplicate. Cannot add item." unlockdir exit 13 fi echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_contacts_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x x x svcnp svcopt x hstnp hstopt x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` #svcnp=`echo "$gcsv" | cut -d, -f5` #hstnp=`echo "$gcsv" | cut -d, -f8` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 1 $FILE_CONTACTS "$name"; then echo "NAGCTL ERROR: Contact name is a duplicate. Cannot add item." unlockdir exit 13 fi if ! item_exists 1 $FILE_TIMEPERIODS $svcnp; then echo -n "NAGCTL ERROR: Service Notifperiod '$svcnp' unavailable." echo "Will not add contact." unlockdir exit 14 fi if ! item_exists 1 $FILE_TIMEPERIODS $hstnp; then echo -n "NAGCTL ERROR: Host Notifperiod '$hstnp' unavailable." echo "Will not add contact." unlockdir exit 14 fi [[ -n $hstopt && $hstopt != "-" ]] && { numterms=`echo "$hstopt" | grep -Eo '([^ ]*)?' | wc -l` numvalidterms=`echo "$hstopt" | \ grep -Eo '(\bd( |$)|\bu( |$)|\br( |$)|\bf( |$)|\bs( |$)|\bn( |$))?' | wc -l` [[ $numterms -ne $numvalidterms ]] && { echo -n "NAGCTL ERROR: Host notif opts '$hstopt' contains invalid" echo " option(s). Only d, u, r, f, s, or n are allowed." unlockdir exit 14 } } [[ -n $svcopt && $svcopt != "-" ]] && { numterms=`echo "$svcopt" | grep -Eo '([^ ]*)?' | wc -l` numvalidterms=`echo "$svcopt" | \ grep -Eo '(\bw( |$)|\bu( |$)|\bc( |$)|\br( |$)|\bf( |$)|\bs( |$)|\bn( |$))?' | wc -l` [[ $numterms -ne $numvalidterms ]] && { echo -n "NAGCTL ERROR: Service notif opts '$svcopt' contains invalid" echo " option(s). Only w, u, c, r, f, s, or n are allowed." unlockdir exit 14 } } echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_contactgroups_line() # ---------------------------------------------------------------------------- { local name i IFS=, read name x memberlist x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` #memberlist=`echo "$gcsv" | cut -d, -f3` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 1 $FILE_CONTACTGROUPS "$name"; then echo "NAGCTL ERROR: Contactgroup name is a duplicate. Cannot add item." unlockdir exit 13 fi for i in $memberlist; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo "NAGCTL ERROR: Member '$i' not found. Cannot add item." unlockdir exit 13 fi done echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_servicesets_line() # ---------------------------------------------------------------------------- { local name svcdesc IFS=, read name template command svcdesc servicegroups contacts \ contactgroups x <<<"$gcsv" #command=`echo "$command" | grep -o '[-A-Za-z0-9_]*'` command=`echo "$command" | sed -nr 's/%21/!/g;s/(^[^!]*)(!|%21|$).*/\1/p'` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists2 1 4 $FILE_SERVICESETS "$name" "$svcdesc"; then echo "NAGCTL ERROR: Serviceset item is a duplicate. Cannot add item." unlockdir exit 13 fi # command must exist in commands file if ! item_exists 1 $FILE_COMMANDS "$command"; then echo "NAGCTL ERROR: Command '$command' unavailable. Cannot add item." unlockdir exit 13 fi [[ -n $template ]] && { if ! item_exists 1 $FILE_SERVICETEMPLATES "$template"; then echo -n "NAGCTL ERROR: Template '$template' not found." echo " Will not add service set." unlockdir exit 13 fi } [[ -n $contacts ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Will not add service set." unlockdir exit 13 fi done } [[ -n $contactgroups ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo "NAGCTL ERROR: Contact Group '$i' not found." echo " Will not add service set." unlockdir exit 13 fi done } [[ -n $servicegroups ]] && { for i in $servicegroups; do if ! item_exists 1 $FILE_SERVICEGROUPS "$i"; then echo -n "NAGCTL ERROR: Servicegroup '$i' not found." echo " Will not add service set." unlockdir exit 13 fi done } echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_timeperiods_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x x exclude x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` #exclude=`echo "$gcsv" | cut -d, -f4` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 1 $FILE_TIMEPERIODS "$name"; then echo "NAGCTL ERROR: Timeperiod item is a duplicate. Cannot add item." unlockdir exit 13 fi for i in $exclude; do if ! item_exists 1 $FILE_TIMEPERIODS "$i"; then echo -n "NAGCTL ERROR: Excluded timeperiod '$i' not found." echo " Cannot add item." unlockdir exit 13 fi done echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- add_commands_line() # ---------------------------------------------------------------------------- { local name IFS=, read -r name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 1 $FILE_COMMANDS "$name"; then echo "NAGCTL ERROR: Command name is a duplicate. Cannot add item." unlockdir exit 13 fi echo -E "$gcsv" >>$SETUP_FILE unlockdir echo "Success" exit 0 } # ---------------------------------------------------------------------------- add_servicedeps_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the servicedeps file. # Arguments: None # Returns: Nothing { local name longline IFS=, read dephostname dephostgroupname depsvcdesc \ hostname hostgroupname svcdesc inheritsparent \ execfailcriteria notiffailcriteria period disable x \ <<<"$gcsv" [[ $dephostname == *" "* || $dephostgroupname == *" "* || \ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir longline="$dephostname,$dephostgroupname,$depsvcdesc" longline="$longline,$hostname,$hostgroupname,$svcdesc" if item_existsN "1 2 3 4 5 6" $FILE_SERVICEDEPS "$longline" then echo -n "NAGCTL ERROR: Service dependency is a duplicate. " echo "Cannot add item." unlockdir exit 13 fi [[ -n $dephostname && -n $hostname && \ ( "$svcdesc" == "$depsvcdesc" ) ]] && { echo "NAGCTL ERROR: Circular dependency. Cannot add item." unlockdir exit 13 } [[ -n $dephostname ]] && { if ! item_exists2 1 4 $FILE_SERVICES "$dephostname" "$depsvcdesc"; then echo "NAGCTL ERROR: Dependent service not found. Cannot add item." unlockdir exit 13 fi } [[ -n $dephostgroupname ]] && { # Too heavy to check for a valid service, maybe... TODO if ! item_exists 1 $FILE_HOSTGROUPS "$dephostgroupname"; then echo -n "NAGCTL ERROR: Dependent hostgroup name not found. " echo "Cannot add item." unlockdir exit 13 fi } [[ -n $hostname ]] && { if ! item_exists2 1 4 $FILE_SERVICES "$hostname" "$svcdesc"; then echo "NAGCTL ERROR: Service not found. Cannot add item." unlockdir exit 13 fi } [[ -n $hostgroupname ]] && { # Too heavy to check for a valid service, maybe... TODO if ! item_exists 1 $FILE_HOSTGROUPS "$hostgroupname"; then echo -n "NAGCTL ERROR: Dependent hostgroup name not found. " echo "Cannot add item." unlockdir exit 13 fi } echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_hostdeps_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { local name longline IFS=, read dephostname dephostgroupname \ hostname hostgroupname inheritsparent \ execfailcriteria notiffailcriteria period disable x \ <<<"$gcsv" [[ $dephostname == *" "* || $dephostgroupname == *" "* || \ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir longline="$dephostname,$dephostgroupname" longline="$longline,$hostname,$hostgroupname" if item_existsN "1 2 3 4" $FILE_HOSTDEPS "$longline" then echo -n "NAGCTL ERROR: Host dependency is a duplicate. " echo "Cannot add item." unlockdir exit 13 fi [[ -n $dephostname ]] && { if ! item_exists 1 $FILE_HOSTS "$dephostname"; then echo "NAGCTL ERROR: Dependent hostname not found. Cannot add item." unlockdir exit 13 fi } [[ -n $dephostgroupname ]] && { # Too heavy to check for a valid service, maybe... TODO if ! item_exists 1 $FILE_HOSTGROUPS "$dephostgroupname"; then echo -n "NAGCTL ERROR: Dependent hostgroup name not found. " echo "Cannot add item." unlockdir exit 13 fi } [[ -n $hostname ]] && { if ! item_exists 1 $FILE_HOSTS "$hostname"; then echo "NAGCTL ERROR: Host name not found. Cannot add item." unlockdir exit 13 fi } [[ -n $hostgroupname ]] && { # Too heavy to check for a valid service, maybe... TODO if ! item_exists 1 $FILE_HOSTGROUPS "$hostgroupname"; then echo -n "NAGCTL ERROR: Hostgroup name not found. " echo "Cannot add item." unlockdir exit 13 fi } [[ -n $period ]] && { if ! item_exists 1 $FILE_TIMEPERIODS "$period"; then echo -n "NAGCTL ERROR: Period not found." echo " Cannot add item." unlockdir exit 14 fi } echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_serviceesc_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { local name IFS=, read hostname hostgroupname svcdesc contacts contactgroups \ firstnotif lastnotif notifinterval period escopts disable x \ <<<"$gcsv" [[ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir if item_existsN "1 2 3" $FILE_SERVICEESC \ "$hostname,$hostgroupname,$svcdesc" then echo -n "NAGCTL ERROR: Service escalation is a duplicate. " echo "Cannot add item." unlockdir exit 13 fi [[ -n $hostname ]] && { if ! item_exists2 1 4 $FILE_SERVICES "$hostname" "$svcdesc" then echo -n "NAGCTL ERROR: Service not found for hostname. " echo "Cannot add item." unlockdir exit 13 fi } [[ -n $hostgroupname ]] && { # Too heavy to check for a valid service, maybe... TODO if ! item_exists 1 $FILE_HOSTGROUPS "$hostgroupname"; then echo -n "NAGCTL ERROR: Hostgroup not found. " echo "Cannot add item." unlockdir exit 13 fi } [[ -n $contacts ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Cannot add item." unlockdir exit 13 fi done } [[ -n $contactgroups ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo -n "NAGCTL ERROR: Contact Group '$i' not found." echo " Cannot add item." unlockdir exit 13 fi done } [[ -n $period ]] && { if ! item_exists 1 $FILE_TIMEPERIODS "$period"; then echo -n "NAGCTL ERROR: Escalation period not found." echo " Cannot add item." unlockdir exit 14 fi } echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_hostesc_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { local name IFS=, read hostname hostgroupname contacts contactgroups \ firstnotif lastnotif notifinterval period escopts disable x \ <<<"$gcsv" [[ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir if item_exists2 1 2 $FILE_HOSTESC \ "$hostname" "$hostgroupname" then echo -n "NAGCTL ERROR: Host escalation is a duplicate. " echo "Cannot add item." unlockdir exit 13 fi [[ -n $hostname ]] && { if ! item_exists "1" $FILE_HOSTS "$hostname" then echo -n "NAGCTL ERROR: Hostname not found. " echo "Cannot add item." unlockdir exit 13 fi } [[ -n $hostgroupname ]] && { # Too heavy to check for a valid service, maybe... TODO if ! item_exists 1 $FILE_HOSTGROUPS "$hostgroupname"; then echo -n "NAGCTL ERROR: Hostgroup not found. " echo "Cannot add item." unlockdir exit 13 fi } [[ -n $contacts ]] && { for i in $contacts; do if ! item_exists 1 $FILE_CONTACTS "$i"; then echo -n "NAGCTL ERROR: Contact '$i' not found." echo " Cannot add item." unlockdir exit 13 fi done } [[ -n $contactgroups ]] && { for i in $contactgroups; do if ! item_exists 1 $FILE_CONTACTGROUPS "$i"; then echo -n "NAGCTL ERROR: Contact Group '$i' not found." echo " Cannot add item." unlockdir exit 13 fi done } [[ -n $period ]] && { if ! item_exists 1 $FILE_TIMEPERIODS "$period"; then echo -n "NAGCTL ERROR: Escalation period not found." echo " Cannot add item." unlockdir exit 14 fi } echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_serviceextinfo_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { local name IFS=, read hostname svcdesc notes notes_url action_url \ icon_image icon_image_alt disable x \ <<<"$gcsv" [[ $hostname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir if item_exists2 1 2 $FILE_SERVICEEXTINFO "$hostname" "$svcdesc" then echo -n "NAGCTL ERROR: Service extinfo is a duplicate. " echo "Cannot add item." unlockdir exit 13 fi if ! item_exists2 1 4 $FILE_SERVICES "$hostname" "$svcdesc" then echo -n "NAGCTL ERROR: Service not found. " echo "Cannot add item." unlockdir exit 13 fi echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # ---------------------------------------------------------------------------- add_hostextinfo_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { local name IFS=, read hostname notes notes_url action_url \ icon_image icon_image_alt vrml_image statusmap_image \ coords2d coords3d disable x \ <<<"$gcsv" [[ $hostname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir if item_exists "1" $FILE_HOSTEXTINFO "$hostname" then echo -n "NAGCTL ERROR: Host extinfo is a duplicate. " echo "Cannot add item." unlockdir exit 13 fi if ! item_exists "1" $FILE_HOSTS "$hostname" then echo -n "NAGCTL ERROR: Hostname not found. " echo "Cannot add item." unlockdir exit 13 fi echo "$gcsv" >>$SETUP_FILE unlockdir echo "Success" } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # APPLY ACTION METHODS # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # --------------------------------------------------------------------------- apply_nagiosconfig() # --------------------------------------------------------------------------- # Purpose: Apply a nagios configuration. # Arguments: None # Returns: Nothing { local a b c eval "$gcsv" 2>/dev/null if [[ $verbose -ne 1 ]]; then cd $NAG_OBJ_DIR/$gservice lockdir sudo $CSV2NAG -y all &>/dev/null if [[ $? -gt 0 ]]; then echo "Fail" else echo "Success" fi unlockdir else cd $NAG_OBJ_DIR/$gservice lockdir sudo $CSV2NAG -y all unlockdir fi exit 0 } # --------------------------------------------------------------------------- apply_nagioslastgoodconfig() # --------------------------------------------------------------------------- # Purpose: Request a nagios restart. # Arguments: None # Returns: Nothing { rm -f $SETUP_DIR/* 2>/dev/null cp -a $SETUP_DIR.known_good/* $SETUP_DIR/ apply_nagiosconfig exit 0 } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # RESTART ACTION METHODS # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # --------------------------------------------------------------------------- restart_nagios() # --------------------------------------------------------------------------- # Purpose: Request a nagios restart. # Arguments: None # Returns: Nothing { sudo $NAGIOSBIN -v $NAG_CONFIG &>/dev/null if [[ $? -eq 0 ]]; then touch /tmp/nagios_restart_request rm -f $SETUP_DIR.known_good/* 2>/dev/null mkdir -p $SETUP_DIR.known_good cp -a $SETUP_DIR/* $SETUP_DIR.known_good 2>/tmp/nagctl_err.$$ if [[ $? -ne 0 ]]; then echo -n "NAGCTL ERROR: Config could not be saved. Nagios restart" echo -n " request not sent. Error was " head -1 /tmp/nagctl_err.$$ exit 1 fi rm -f /tmp/nagctl_err.$$ echo -n "Config saved and Nagios restart request sent." else echo "NAGCTL ERROR: Config error. Nagios restart request not sent." exit 1 fi exit 0 } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # CHECK ACTION METHODS # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # --------------------------------------------------------------------------- check_nagiosconfig() # --------------------------------------------------------------------------- # Purpose: Run nagios -v to check it. # Edit sudoers file: Comment out 'Defaults requiretty' line # and add '%nagios ALL = NOPASSWD: /usr/sbin/nagios -v *' # Arguments: None # Returns: Nothing { local a b c tmp="/var/tmp/nagctl.output.tmp" eval "$gcsv" 2>/dev/null if [[ $verbose -ne 1 ]]; then while read a; do [[ $a == "Total Warnings"* ]] && b=$a [[ $a == "Total Errors"* ]] && c=$a done <<<"$( sudo $NAGIOSBIN -v $NAG_CONFIG )" else sudo $NAGIOSBIN -v $NAG_CONFIG | tee $tmp if grep -qs "Total Warnings" $tmp; then b=`grep "Total Warnings" $tmp` fi if grep -qs "Total Errors" $tmp; then c=`grep "Total Errors" $tmp` fi echo fi if [[ -n $b && -n $c ]]; then echo "$b, $c" | sed 's/ */ /g' else echo "CONFIG ERROR" fi exit 0 } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # DELETE ACTION METHODS # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # ---------------------------------------------------------------------------- delete_hosttemplate_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hosttemplates file. # Arguments: None # Returns: Nothing { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 4 $FILE_HOSTS "$name"; then echo -n "NAGCTL ERROR: Hosttemplate is referenced by one or more hosts." echo " Will not delete." RETVAL=15 else if item_exists 1 $FILE_HOSTTEMPLATES "$name"; then item_delete 1 $FILE_HOSTTEMPLATES "$name" echo "Success" else echo "NAGCTL ERROR: Hosttemplate not found." RETVAL=15 fi fi unlockdir } # ---------------------------------------------------------------------------- delete_servicetemplate_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 2 $FILE_SERVICES "$name"; then echo -n "NAGCTL ERROR: Servicetemplate is referenced by one or more services." echo " Will not delete." RETVAL=15 elif item_exists 2 $FILE_SERVICESETS "$name"; then echo -n "NAGCTL ERROR: Servicetemplate is referenced by one or more service sets." echo " Will not delete." RETVAL=15 else if item_exists 1 $FILE_SERVICETEMPLATES "$name"; then item_delete 1 $FILE_SERVICETEMPLATES "$name" echo "Success" else echo "NAGCTL ERROR: Servicetemplate not found." RETVAL=15 fi fi unlockdir } # ---------------------------------------------------------------------------- delete_host_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 1 $FILE_SERVICES "$name"; then echo -n "NAGCTL ERROR: Host is referenced by one or more services." echo " Will not delete." RETVAL=15 elif [[ $name != '.*' && `item_exists 12 $FILE_HOSTS "$name"` ]]; then echo -n "NAGCTL ERROR: Host is a parent to one or more hosts." echo " Will not delete." RETVAL=15 else if item_exists 1 $FILE_HOSTS "$name"; then item_delete 1 $FILE_HOSTS "$name" echo "Success" else echo "NAGCTL ERROR: Host not found." RETVAL=15 fi fi unlockdir } # ---------------------------------------------------------------------------- delete_service_line() # ---------------------------------------------------------------------------- { local name svcdesc IFS=, read name x x svcdesc x <<<"$gcsv" [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists2 1 3 $FILE_SERVICEDEPS "$name" "$svcdesc"; then echo -n "NAGCTL ERROR: Service dependency exists for this service." echo " Will not delete." RETVAL=15 elif item_exists2 4 6 $FILE_SERVICEDEPS "$name" "$svcdesc"; then echo -n "NAGCTL ERROR: One or more services depend on this service." echo " Will not delete." RETVAL=15 else if item_exists2 1 4 $FILE_SERVICES "$name" "$svcdesc"; then item_delete2 1 4 $FILE_SERVICES "$name" "$svcdesc" echo "Success" else echo "NAGCTL ERROR: Service not found." RETVAL=15 fi fi unlockdir } # ---------------------------------------------------------------------------- delete_hostgroups_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 5 $FILE_HOSTS "$name"; then echo -n "NAGCTL ERROR: Hostgroup is referenced by one or more hosts." echo " Will not delete." RETVAL=15 else if item_exists 1 $FILE_HOSTGROUPS "$name"; then item_delete 1 $FILE_HOSTGROUPS "$name" echo "Success" else echo "NAGCTL ERROR: Hostgroup not found." RETVAL=15 fi fi unlockdir } # ---------------------------------------------------------------------------- delete_servicegroups_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 5 $FILE_SERVICES "$name"; then echo -n "NAGCTL ERROR: Servicegroup is referenced by one or more" echo " hosts. Will not delete." RETVAL=15 else if item_exists 1 $FILE_SERVICEGROUPS "$name"; then item_delete 1 $FILE_SERVICEGROUPS "$name" echo "Success" else echo "NAGCTL ERROR: Servicegroup not found." RETVAL=15 fi fi unlockdir } # ---------------------------------------------------------------------------- delete_contacts_line() # ---------------------------------------------------------------------------- { local name t IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 3 $FILE_CONTACTGROUPS "$name"; then t=`item_search "3" $FILE_CONTACTGROUPS ".*$name\>" | head -1` IFS=, read nname x <<<"$t" echo -n "NAGCTL ERROR: Contact '$name' is referenced by one or more" echo " Contact Groups ($nname ...). Will not delete." RETVAL=15 elif item_exists 3 $FILE_SERVICETEMPLATES "$name"; then t=`item_search "3" $FILE_SERVICETEMPLATES ".*$name\>" | head -1` IFS=, read nname x <<<"$t" echo -n "NAGCTL ERROR: Contact '$name' is referenced by one" echo " or more service templates ($nname ...). Will not delete." RETVAL=15 elif item_exists 3 $FILE_HOSTTEMPLATES "$name"; then t=`item_search "3" $FILE_HOSTTEMPLATES "$name" | head -1` IFS=, read nname x <<<"$t" echo -n "NAGCTL ERROR: Contact '$name' is referenced by one" echo " or more host templates ($nname ...). Will not delete." RETVAL=15 elif item_exists 6 $FILE_HOSTS "$name"; then t=`item_search "6" $FILE_HOSTS "$name" | head -1` IFS=, read nname x <<<"$t" echo -n "NAGCTL ERROR: Contact '$name' is referenced by one" echo " or more hosts ($nname ...). Will not delete." RETVAL=15 elif item_exists 6 $FILE_SERVICES "$name"; then t=`item_search "6" $FILE_SERVICES "$name" | head -1` IFS=, read nname x x svcdesc x <<<"$t" echo -n "NAGCTL ERROR: Contact '$name' is referenced by one" echo " or more services ($nname:$svcdesc ...). Will not delete." RETVAL=15 elif item_exists 6 $FILE_SERVICESETS "$name"; then t=`item_search "6" $FILE_SERVICESETS "$name" | head -1` IFS=, read nname x x svcdesc x <<<"$t" echo -n "NAGCTL ERROR: Contact '$name' is referenced by one" echo " or more service sets ($nname:$svcdesc ...). Will not delete." RETVAL=15 else if item_exists 1 $FILE_CONTACTS "$name"; then item_delete 1 $FILE_CONTACTS "$name" echo "Success" else echo "NAGCTL ERROR: Contact not found." RETVAL=15 fi fi unlockdir } # ---------------------------------------------------------------------------- delete_contactgroups_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 4 $FILE_SERVICETEMPLATES "$name"; then echo -n "NAGCTL ERROR: Contact Group '$name' is referenced by one" echo " or more service templates. Will not delete." RETVAL=15 elif item_exists 4 $FILE_HOSTTEMPLATES "$name"; then echo -n "NAGCTL ERROR: Contact Group '$name' is referenced by one" echo " or more host templates. Will not delete." RETVAL=15 elif item_exists 7 $FILE_HOSTS "$name"; then echo -n "NAGCTL ERROR: Contact Group '$name' is referenced by one" echo " or more hosts. Will not delete." RETVAL=15 elif item_exists 7 $FILE_SERVICES "$name"; then echo -n "NAGCTL ERROR: Contact Group '$name' is referenced by one" echo " or more services. Will not delete." RETVAL=15 elif item_exists 7 $FILE_SERVICESETS "$name"; then echo -n "NAGCTL ERROR: Contact Group '$name' is referenced by one" echo " or more service sets. Will not delete." RETVAL=15 else if item_exists 1 $FILE_CONTACTGROUPS "$name"; then item_delete 1 $FILE_CONTACTGROUPS "$name" echo "Success" else echo "NAGCTL ERROR: Contact Group not found." RETVAL=15 fi fi unlockdir } # ---------------------------------------------------------------------------- delete_servicesets_line() # ---------------------------------------------------------------------------- { local name svcdesc numitems=0 wildcard=0 IFS=, read name x x svcdesc x <<<"$gcsv" [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } [[ "$svcdesc" == ".*" ]] && wildcard=1 lockdir # Don't allow deletion if this is the last service in the serviceset # and it's referenced by a HOST. numitems=`item_search 1 $FILE_SERVICESETS "$name" | wc -l` if [[ $numitems -eq 1 || $wildcard -eq 1 ]]; then if item_exists 9 $FILE_HOSTS "$name"; then t=`item_search "9" $FILE_HOSTS "$name" | head -1` IFS=, read nname x <<<"$t" echo -n "NAGCTL ERROR: Service set '$name' is referenced by one" echo " or more hosts ($nname ...). Will not delete." unlockdir RETVAL=15 return fi fi if item_exists2 1 4 $FILE_SERVICESETS "$name" "$svcdesc"; then item_delete2 1 4 $FILE_SERVICESETS "$name" "$svcdesc" echo "Success" else echo "NAGCTL ERROR: Serviceset not found." RETVAL=15 fi unlockdir } # ---------------------------------------------------------------------------- delete_timeperiods_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 8 $FILE_HOSTTEMPLATES "$name\>"; then echo -n "NAGCTL ERROR: Timeperiod '$name' is referenced by" echo " one or more host templates. Will not delete." RETVAL=15 elif item_exists 11 $FILE_HOSTTEMPLATES "$name\>"; then echo -n "NAGCTL ERROR: Timeperiod '$name' is referenced by" echo " one or more host templates. Will not delete." RETVAL=15 elif item_exists 10 $FILE_SERVICETEMPLATES "$name\>"; then echo -n "NAGCTL ERROR: Timeperiod '$name' is referenced by" echo " one or more service templates. Will not delete." RETVAL=15 elif item_exists 12 $FILE_SERVICETEMPLATES "$name\>"; then echo -n "NAGCTL ERROR: Timeperiod '$name' is referenced by" echo " one or more service templates. Will not delete." RETVAL=15 elif item_exists 5 $FILE_CONTACTS "$name\>"; then echo -n "NAGCTL ERROR: Timeperiod '$name' is referenced by" echo " one or more contacts. Will not delete." RETVAL=15 elif item_exists 8 $FILE_CONTACTS "$name\>"; then echo -n "NAGCTL ERROR: Timeperiod '$name' is referenced by" echo " one or more contacts. Will not delete." RETVAL=15 elif item_exists 4 $FILE_TIMEPERIODS "$name\>"; then echo -n "NAGCTL ERROR: Timeperiod '$name' is referenced by one" echo " or more timeperiods. Will not delete." RETVAL=15 else if item_exists 1 $FILE_TIMEPERIODS "$name"; then item_delete 1 $FILE_TIMEPERIODS "$name" echo "Success" else echo "NAGCTL ERROR: Timeperiod not found." RETVAL=15 fi fi unlockdir } # ---------------------------------------------------------------------------- delete_commands_line() # ---------------------------------------------------------------------------- { local name IFS=, read name x <<<"$gcsv" #name=`echo "$gcsv" | cut -d, -f1` [[ $name == *" "* ]] && { echo "NAGCTL ERROR: 'name' field cannot contain spaces" exit 13 } lockdir if item_exists 3 $FILE_SERVICES "$name\>.*"; then echo -n "NAGCTL ERROR: Command '$name' is referenced by one" echo " or more services. Will not delete." RETVAL=15 elif item_exists 3 $FILE_SERVICESETS "$name\>.*"; then echo -n "NAGCTL ERROR: Command '$name' is referenced by one" echo " or more servicesets. Will not delete." RETVAL=15 elif item_exists 13 $FILE_HOSTTEMPLATES "$name\>.*"; then echo -n "NAGCTL ERROR: Command '$name' is referenced by one" echo " or more host templates. Will not delete." RETVAL=15 elif item_exists 7 $FILE_CONTACTS "$name\>.*"; then echo -n "NAGCTL ERROR: Command '$name' is referenced by one" echo " or more contacts. Will not delete." RETVAL=15 elif item_exists 10 $FILE_CONTACTS "$name\>.*"; then echo -n "NAGCTL ERROR: Command '$name' is referenced by one" echo " or more contacts. Will not delete." RETVAL=15 else if item_exists 1 $FILE_COMMANDS "$name"; then item_delete 1 $FILE_COMMANDS "$name" echo "Success" else echo "NAGCTL ERROR: Command not found." RETVAL=15 fi fi unlockdir } # ---------------------------------------------------------------------------- delete_servicedeps_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the servicedeps file. # Arguments: None # Returns: Nothing { IFS=, read dephostname dephostgroupname depsvcdesc \ hostname hostgroupname svcdesc inheritsparent \ execfailcriteria notiffailcriteria period disable x \ <<<"$gcsv" [[ $dephostname == *" "* || $dephostgroupname == *" "* || \ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir longline="$dephostname,$dephostgroupname,$depsvcdesc" longline="$longline,$hostname,$hostgroupname,$svcdesc" if item_existsN "1 2 3 4 5 6" $FILE_SERVICEDEPS "$longline" then item_deleteN "1 2 3 4 5 6" $FILE_SERVICEDEPS "$longline" echo "Success" else echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi unlockdir } # ---------------------------------------------------------------------------- delete_hostdeps_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { IFS=, read dephostname dephostgroupname \ hostname hostgroupname inheritsparent \ execfailcriteria notiffailcriteria period disable x \ <<<"$gcsv" [[ $dephostname == *" "* || $dephostgroupname == *" "* || \ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir longline="$dephostname,$dephostgroupname" longline="$longline,$hostname,$hostgroupname" if item_existsN "1 2 3 4" $FILE_HOSTDEPS "$longline" then item_deleteN "1 2 3 4" $FILE_HOSTDEPS "$longline" echo "Success" else echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi unlockdir } # ---------------------------------------------------------------------------- delete_serviceesc_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { IFS=, read hostname hostgroupname svcdesc contacts contactgroups \ firstnotif lastnotif notifinterval period escopts disable x \ <<<"$gcsv" [[ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } longline="$hostname,$hostgroupname,$svcdesc" lockdir if item_existsN "1 2 3" $FILE_SERVICEESC "$longline" then item_deleteN "1 2 3" $FILE_SERVICEESC "$longline" echo "Success" else echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi unlockdir } # ---------------------------------------------------------------------------- delete_hostesc_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { IFS=, read hostname hostgroupname contacts contactgroups \ firstnotif lastnotif notifinterval period escopts disable x \ <<<"$gcsv" [[ $hostname == *" "* || $hostgroupname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir if item_exists2 1 2 $FILE_HOSTESC "$hostname" "$hostgroupname" then item_delete2 1 2 $FILE_HOSTESC "$hostname" "$hostgroupname" echo "Success" else echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi unlockdir } # ---------------------------------------------------------------------------- delete_serviceextinfo_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { IFS=, read hostname svcdesc notes notes_url action_url \ icon_image icon_image_alt disable x \ <<<"$gcsv" [[ $hostname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir if item_exists2 1 2 $FILE_SERVICEEXTINFO "$hostname" "$svcdesc" then item_delete2 1 2 $FILE_SERVICEEXTINFO "$hostname" "$svcdesc" echo "Success" else echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi unlockdir } # ---------------------------------------------------------------------------- delete_hostextinfo_line() # ---------------------------------------------------------------------------- # Purpose: Add a line to the hostdeps file. # Arguments: None # Returns: Nothing { IFS=, read hostname notes notes_url action_url \ icon_image icon_image_alt vrml_image statusmap_image \ coords2d coords3d disable x \ <<<"$gcsv" [[ $hostname == *" "* ]] && { echo "NAGCTL ERROR: Field cannot contain spaces." exit 13 } lockdir if item_exists "1" $FILE_HOSTEXTINFO "$hostname" then item_delete "1" $FILE_HOSTEXTINFO "$hostname" echo "Success" else echo "NAGCTL ERROR: Item not found." unlockdir exit 13 fi unlockdir } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # PIPECMD ACTION METHODS # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # SEE: # http://old.nagios.org/developerinfo/externalcommands/commandlist.php # # ---------------------------------------------------------------------------- pipecmd_enablehostsvcchecks() # ---------------------------------------------------------------------------- { local comment name item freshness check_file_exists_and_is_writeable "COMMANDFILE" eval "$gcsv" 2>/dev/null [[ -z "$name" ]] && { echo "NAGCTL ERROR: Host 'name' variable not supplied." exit 0 } lockdir if ! item_exists 1 $FILE_HOSTS "$name"; then echo "NAGCTL ERROR: Host '$name' not found." unlockdir exit 14 else now=`date +%s` printf "[%lu] ENABLE_HOST_CHECK;$name\n" $now > $COMMANDFILE sleep 1 now=`date +%s` while read a; do desc=`echo $a | cut -d, -f4` printf "[%lu] ENABLE_PASSIVE_SVC_CHECKS;$name;$desc\n" \ $now > $COMMANDFILE done <<<"$( item_search 1 $FILE_SERVICES "$name" )" sleep 1 now=`date +%s` while read a; do desc=`echo $a | cut -d, -f4` item=`item_search2 1 4 $FILE_SERVICES "$name" "$desc"` freshness=`echo $item | cut -d, -f8` [[ -z $freshness ]] && { printf "[%lu] ENABLE_SVC_CHECK;$name;$desc\n" $now \ > $COMMANDFILE } done <<<"$( item_search 1 $FILE_SERVICES "$name" )" fi unlockdir echo "Success" } # ---------------------------------------------------------------------------- pipecmd_disablehostsvcchecks() # ---------------------------------------------------------------------------- { local comment name descr check_file_exists_and_is_writeable "COMMANDFILE" eval "$gcsv" 2>/dev/null [[ -z "$comment" ]] && comment="Disabled via REST interface." [[ -z "$name" ]] && { echo "NAGCTL ERROR: Host 'name' not supplied." exit 14 } lockdir if ! item_exists 1 $FILE_HOSTS "$name"; then echo "NAGCTL ERROR: Host '$name' not found." unlockdir exit 14 else now=`date +%s` printf "[%lu] DISABLE_HOST_CHECK;$name\n" $now > $COMMANDFILE printf "[%lu] DISABLE_HOST_SVC_CHECKS;$name\n" $now > $COMMANDFILE sleep 1 now=`date +%s` while read a; do desc=`echo $a | cut -d, -f4` printf \ "[%lu] PROCESS_SERVICE_CHECK_RESULT;$name;$desc;0;$comment\n" \ $now >$COMMANDFILE done <<<"$( item_search 1 $FILE_SERVICES "$name" )" # TODO: Nagios does not seem to process its external command queue in # the order in which items are queued, hence these silly sleeps. # As load grows will 10 seconds be enough? # For check_multi, passive checks must be disabled otherwise the # freshness check will kick in and alert, which is not desired. # Will need to: # * check nagios source to see if there's a workaround # * write a script that checks to see if status is updated. If so, # /then/ it will disable the service. # Let someone else do something while we're sleeping unlockdir sleep 10 lockdir now=`date +%s` while read a; do desc=`echo $a | cut -d, -f4` printf "[%lu] DISABLE_PASSIVE_SVC_CHECKS;$name;$desc\n" \ $now > $COMMANDFILE done <<<"$( item_search 1 $FILE_SERVICES "$name" )" fi unlockdir echo "Success" } # ---------------------------------------------------------------------------- pipecmd_enablesvccheck() # ---------------------------------------------------------------------------- { local comment name svcdesc item freshness check_file_exists_and_is_writeable "COMMANDFILE" eval "$gcsv" 2>/dev/null [[ -z "$svcdesc" ]] && { echo "NAGCTL ERROR: 'name' and 'svcdesc' are required fields." exit 14 } [[ -z "$comment" ]] && comment="Un-disabled via REST. Check scheduled." [[ -z "$name" ]] && { echo "NAGCTL ERROR: Host 'name' not supplied." exit 14 } lockdir if ! item_exists2 1 4 $FILE_SERVICES "$name" "$svcdesc"; then echo "NAGCTL ERROR: Service '$svcdesc' not found." unlockdir exit 14 else now=`date +%s` printf "[%lu] ENABLE_PASSIVE_SVC_CHECKS;$name;$svcdesc\n" \ $now > $COMMANDFILE sleep 1 now=`date +%s` item=`item_search2 1 4 $FILE_SERVICES "$name" "$svcdesc"` freshness=`echo $item | cut -d, -f8` [[ -z $freshness ]] && { printf "[%lu] ENABLE_SVC_CHECK;$name;$svcdesc\n" $now \ > $COMMANDFILE } sleep 1 now=`date +%s` printf \ "[%lu] PROCESS_SERVICE_CHECK_RESULT;$name;$svcdesc;0;$comment\n" \ $now >$COMMANDFILE fi unlockdir echo "Success" } # ---------------------------------------------------------------------------- pipecmd_disablesvccheck() # ---------------------------------------------------------------------------- { local comment name svcdesc a check_file_exists_and_is_writeable "COMMANDFILE" eval "$gcsv" 2>/dev/null [[ -z "$svcdesc" ]] && { echo "NAGCTL ERROR: 'name' and 'svcdesc' are required fields." exit 14 } [[ -z "$comment" ]] && comment="Disabled via REST interface." [[ -z "$name" ]] && { echo "NAGCTL ERROR: Host 'name' not supplied." exit 14 } lockdir if ! item_exists2 1 4 $FILE_SERVICES "$name" "$svcdesc"; then echo "NAGCTL ERROR: Service '$svcdesc' not found." unlockdir exit 14 else now=`date +%s` printf "[%lu] DISABLE_SVC_CHECK;$name;$svcdesc\n" $now > $COMMANDFILE sleep 1 printf \ "[%lu] PROCESS_SERVICE_CHECK_RESULT;$name;$svcdesc;0;$comment\n" \ $now >$COMMANDFILE if [[ -w $LIVESTATUSCOMMANDFILE && -x $LIVESTATUSUNIXCAT ]]; then # Wait for the state to change - PROCESS_SERVICE_CHECK_RESULT # changes the state to OK. a="GET services\n" a="${a}WaitObject: $name $svcdesc\n" a="${a}WaitTrigger: all\n" a="${a}WaitTimeout: 15000\n" a="${a}WaitCondition: plugin_output = $comment\n" a="${a}Filter: host_name = $name\n" a="${a}Filter: description = $svcdesc\n" a="${a}Columns: plugin_output\n" echo -en "$a" | $LIVESTATUSUNIXCAT $LIVESTATUSCOMMANDFILE else sleep 1 fi printf "[%lu] DISABLE_PASSIVE_SVC_CHECKS;$name;$svcdesc\n" \ $now > $COMMANDFILE fi unlockdir echo "Success" } # ---------------------------------------------------------------------------- pipecmd_schedulehostdowntime() # ---------------------------------------------------------------------------- { local a comment name fixed flexible starttime endtime duration flexible check_file_exists_and_is_writeable "COMMANDFILE" eval "$gcsv" 2>/dev/null [[ -z "$name" ]] && { echo "NAGCTL ERROR: Host name is a required field." exit 14 } [[ -z "$comment" ]] && comment="Scheduled via the REST interface." [[ -z "$author" ]] && author="nagrestconf" if [[ -z "$flexible" || $flexible -eq 0 ]]; then fixed=1 else fixed=0 fi # Force duration to a number : $((duration++)) ; : $((duration--)) lockdir if ! item_exists 1 $FILE_HOSTS "$name" ; then echo "NAGCTL ERROR: Host '$name' not found." unlockdir exit 14 else now=`date +%s` a="[%lu] SCHEDULE_HOST_DOWNTIME;$name;$starttime;$endtime" a="${a};$fixed;0;$duration;$author;$comment" printf "$a" $now > $COMMANDFILE fi unlockdir echo "Success" } # ---------------------------------------------------------------------------- pipecmd_delhostdowntime() # ---------------------------------------------------------------------------- { local a name check_file_exists_and_is_writeable "COMMANDFILE" check_file_exists_and_is_readable "STATUSFILE" eval "$gcsv" 2>/dev/null [[ -z "$name" ]] && { echo "NAGCTL ERROR: Host name is a required field." exit 14 } lockdir if ! item_exists 1 $FILE_HOSTS "$name" ; then echo "NAGCTL ERROR: Host '$name' not found." unlockdir exit 14 else now=`date +%s` while read id; do echo "[$now] DEL_HOST_DOWNTIME;$id" >$COMMANDFILE done <<<"$( sed -n "/hostdowntime/,/downtime_id/ { /host_name=$name/,/downtime_id/ { s/[[:space:]]*downtime_id=//p} }" $STATUSFILE )" fi unlockdir echo "Success" } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # MAIN # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # --------------------------------------------------------------------------- main() # --------------------------------------------------------------------------- # Purpose: Program execution starts here. # Arguments: None # Returns: Nothing { parse_options "$@" init case $gnaction in $ACT_ADD) action_add ;; $ACT_MODIFY) action_modify ;; $ACT_DELETE) action_delete ;; $ACT_SHOW) action_show ;; $ACT_PIPECMD) action_pipecmd ;; $ACT_PROPAGATE) action_propagate ;; $ACT_CHECK) action_check ;; $ACT_RESTART) action_restart ;; $ACT_APPLY) action_apply ;; esac exit $RETVAL } # start... main "$@" # vim:ts=4:et:sw=4:tw=76