声明
作者:昨夜星辰
博客:http://yestreenstars.blog.51cto.com/
本文由本人创作,如需转载,请注明出处,谢谢合作!
目的
一键创建nagios监控主机脚本。
环境
OS: CentOS 6.6 64
配置
#!/bin/bash # 脚本名称:一键创建nagios监控主机脚本 # 脚本描述:该脚本是用来自动创建nagios监控主机的,目前只适用于创建系统类型为Linux的监控主机,并只支持少量常用服务。 # 创建时间:2015-04-07 # 修改时间:2015-04-07 echo ‘请选择目标监控主机的操作系统类型:‘ select OS in Linux Windows do case $OS in Linux) host_template=‘linux-server‘ break ;; Windows) host_template=‘windows-server‘ break ;; *) echo ‘无此类型!请重新输入!‘ ;; esac done while read -p ‘请输入目标监控主机的主机名:‘ host_name do if [ -n "$host_name" ]; then break fi echo ‘主机名不能为空!请重新输入!‘ done read -p ‘请输入该主机的别名(为空则与主机名相同):‘ alias if [ -z $alias ]; then alias=$host_name fi while read -p ‘请输入目标监控主机的IP地址:‘ address do if [ -n "$address" ]; then break fi echo ‘IP地址不能为空!请重新输入!‘ done echo "cfg_file=/usr/local/nagios/etc/objects/$address.cfg" >> /usr/local/nagios/etc/nagios.cfg echo "define host{ use $host_template host_name $host_name alias $alias address $address }" > /usr/local/nagios/etc/objects/$address.cfg head=" define service{ use generic-service host_name $host_name" tail="}" services[1]=" service_description PING check_command check_ping!100.0,20%!500.0,60% " services[2]=" service_description Root Partition check_command check_local_disk!20%!10%!/ " services[3]=" service_description Current Users check_command check_local_users!20!50 " services[4]=" service_description Total Processes check_command check_local_procs!250!400!RSZDT " services[5]=" service_description Current Load check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0 " services[6]=" service_description Swap Usage check_command check_local_swap!20!10 " services[7]=" service_description SSH check_command check_ssh " services[8]=" service_description HTTP check_command check_http " echo ‘1)PING 2)Root Partition 3)Current Users 4)Total Processes 5)Current Load 6)Swap Usage 7)SSH 8)HTTP‘ echo ‘请根据实际需求选择一种方式输入: 一、如果你只需监控一种服务,那么你只需输入该服务对应的序号即可; 二、如果你需要监控的服务比较多,那么你可以用逗号将它们分隔(比如1,3,5); 三、如果你需要监控的服务比较多,并且它们的序号是连续的,那么你可以用破折号连接起始序号和终止序号(比如1-3); 四、如果你需要监控的服务比较多,并且有些服务的序号是连续的,有些是不连续的,那么你可以混合使用逗号和破折号(比如1,3,5-7,9)。‘ while read -p ‘请选择需要监控的服务:‘ choose do if [ -n "$choose" ]; then choose=$(awk -F, ‘{for(i=0;i++<NF;){if($i~/-/){split($i,a,/-/);for(j=a[1];j<=a[2];j++)print j;continue}print $i}}‘ <<< $choose) for no in ${choose[@]} do echo "$head${services[$no]}$tail" >> /usr/local/nagios/etc/objects/$address.cfg done break fi echo ‘你没有输入服务的序号,请重新输入!‘ done
本文出自 “昨夜星辰” 博客,请务必保留此出处http://yestreenstars.blog.51cto.com/1836303/1629452
原文地址:http://yestreenstars.blog.51cto.com/1836303/1629452