If you manage a large number of sites and servers, it is quite painful to modify the configuration file every time. So I thought of using shell scripts to generate bulk configuration files and configuration data. The following script is a shell script that generates the Nagios monitoring configuration file in bulk. The principle is to define a shell script template beforehand, and then each site that needs to be monitored copies a copy of the template to replace the variables inside the template file.
1. Prepare the template file Webcheck.template
Morewebcheck.template ################## #WEBURL define start################## #define service{use Generic-Service; Name of service template to use host_name webcheck service_description WEB URL Check_command check_webpage!-h WEBURL-u INDEX is_volatile0max_check_attempts3Check_interval1Retry_interval1check_period 24x7 Notification_interval5notification_period 24x7 Notification_optionsW, u,r,c contact_groups admins}################## #WEBURL define end###################
Variables are Weburl and index
2. Site list file Weblist.txt
www.aaa.com \\/bbs.bbb.com \/www.ccc.com \/
Weblist.txt has two field, the first field is the domain name, and the second field corresponds to the URL of the site. If the first domain name is www.aaa.com/
3. Batch generate script file create.sh
[Email protected] webcheckes]# MoreCreate.SH #!/bin/Bash PATH=/bin: $PATH: $HOME/bin:/sbin:/usr/bin:/usr/sbin Export path#Echo$PATHusage () {Echo-en"USAGE: $ [Web list] or $ [Template] [Web list]\nfor Example: $ host.template host.list (Field: [Web URL] [INDEX web PAGE]) \ n" 1>&2Exit1 } if[$#-GT2]; ThenUsage Exit1 fi Case "$#" inch 2) Template=$1host_list=$2 ;; 1) Template='webcheck.template'host_list=$1 ;; 0) # template='webcheck.template'# host_list='host.list'usage;;Esac if[!-F"${template}"]; Then Echo "Template: ${template} not exist!" 1>&2Exit1 fi if[!-F"${host_list}"]; Then Echo "host list: ${host_list} not exist!" 1>&2Exit1 fi #Echo$PWD/${host_list}webtemp="Wcalltemp.txt"RM$PWD/${webtemp}#Cat$PWD/${host_list}/bin/Cat$PWD/${host_list}| whileRead Weburl index Do #Echo "${ip}"|grep-op'^\d{1,3} (\.\d{1,3}) {3}$'>/dev/NULL 2>&1|| Field='Not IP' #if["${field}" = ' not IP '];then # Echo "${ip} not ip!" 1>&2# Exit1 #fi#host_cfg="${hostname}-${ip}.cfg"Tmppage="Webtemp.txt" CP${template} ${tmppage}sed-I."s/weburl/${weburl}/g;s/index/${index}/g"${tmppage}/bin/Cat${tmppage}>>${webtemp} Done/bin/CatWebcheck_org.template>webcheck_${host_list}.cfg/bin/Cat${webtemp}>>webcheck_${host_list}.cfgRM$PWD/${webtemp}/usr/local/nagios/bin/nagios-v/usr/local/nagios/etc/nagios.cfgservice Nagios Restart
The main function of this sentence,sed-i "s/weburl/${weburl}/g;s/index/${index}/g" ${tmppage}, in the final analysis is the credit of the SED command. Replace the contents of the Weblist.txt with the Weburl and index variables in the template.
4. Call mode
sh ./create. sh webcheck.template weblist.txt
Or
sh ./create. SH weblist.txt
This can be considered if there are a large number of situations where you need to manually modify the configuration file, or if you are generating some similar files in bulk.
Shell script Bulk Build configuration file