Shell script implements batch generation Nagios configuration file _linux Shell

Source: Internet
Author: User

If you manage more sites and servers, every time you modify a configuration file, it's pretty painful. The idea is to use a shell script to generate bulk configuration files and configuration data. The following script is a shell script for batch generation of Nagios monitoring configuration files. The principle is to define a shell script template beforehand, and then each site that needs to be monitored copies a template to replace the variables in the template file.

1, prepare the template file Webcheck.template

More webcheck.template 

################## #WEBURL define start###################
define service{
    use               Generic-service     ; Name of service template to use
    host_name            webcheck
    service_description       weburl
    Check_command      check_webpage!-h weburl-u INDEX
    is_volatile 0
    max_check_attempts 3
    check_interval 1
    retry _interval 1
    check_period 24x7
    notification_interval 5
    notification_period 24x7
    notification_ Options w,u,r,c
    contact_groups Admins
    }
################## #WEBURL define end###################

Variables are Weburl and index

2, site list file Weblist.txt

Copy Code code as follows:

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 is the URL of the site. If the first domain name is www.aaa.com/

3, Batch generation script file create.sh

[Root@bogon webcheckes]# more create.sh #!/bin/bash path=/bin: $PATH: $HOME/bin:/sbin:/usr/bin:/usr/sbin export PATH #e Cho $PATH usage () {echo-en "Usage: $ [Web list] or $ [Template] [Web list]\nfor Example: $ host.template host. 
 
List (Field: [Web URL] [INDEX web PAGE] \ n "1>&2 exit 1} If [$#-gt 2];then usage Exit 1 fi 
    Case "$#" in 2) template=$1 host_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>&2 Exit 1 fi if [! f ' ${host_list} '];then echo ' host list: ${host_list} not exist! ' 1>&2 Exit 1 fi #echo $PWD/${host_list} webtemp= "Wcalltemp.txt rm $PWD/${webtemp} #cat $PWD/${host_list}/ 
Bin/cat $PWD/${host_list}| While read Weburl the index do #eCho "${ip}" |grep-op ' ^\d{1,3} (\.\d{1,3}) {3}$ ' >/dev/null 2>&1 | | field= ' not IP ' #if [' ${field} ' = ' Don't IP '];then # echo ' ${ip} not ip! '  1>&2 # exit 1 #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} do Ne/bin/cat Webcheck_org.template>webcheck_${host_list}.cfg/bin/cat ${webtemp}>>webcheck_${host_list}.

 CFG rm $PWD/${webtemp}/usr/local/nagios/bin/nagios-v/usr/local/nagios/etc/nagios.cfg service Nagios restart

The main function is this sentence, sed-i "s/weburl/${weburl}/g;s/index/${index}/g" ${tmppage}, after all, the SED command of the credit. Replace the Weburl and index variables in the template with the contents of the Weblist.txt.

4. Calling mode

Copy Code code as follows:

Sh./create.sh webcheck.template weblist.txt

Or
Copy Code code as follows:

Sh./create.sh weblist.txt

Consider this approach if you have a large number of situations where you need to manually modify the configuration files, or if you are generating some similar files in batches.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.