一、shell中的函数
二、shell中的数组
三、告警系统需求分析
四、告警系统主脚本
五、告警系统配置文件
六、告警系统监控项目
七、告警系统邮件引擎
八、运行告警系统
一、shell中的函数
函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。定义函数必须要放在最前面。定义好的函数相当于是命令。
语法格式:函数必须放在最前面
//name属性定义了赋予函数的唯一名称。脚本中定义的每个函数都必须有一个唯一的名称。
function name {
commands
}
或者 function可省略
//函数名称后的括号表明正在定义的是一个函数。
name(){
commands
}
调用方式,函数必须在调用之前进行定义。直接像使用命令一样使用函数即可。
案例①:打印函数参数
[root@Linux01 ~]# vim 11.sh
#!/bin/bash
function print_para(){
echo "The first pamameter is $1"
echo "The second pamameter is $2"
echo "The third pamameter is $3"
echo "The script name is $0"
echo "The num pamameter is $#"
}
print_para 1 2 3 #函数名(写函数名就是调用这个函数),函数的第一个参数,第二个参数。。。;也可以跟$1 $2等,表示shell的第一个、第二个参数作为函数的参数
[root@Linux01 ~]# sh 11.sh
The first pamameter is 1
The second pamameter is 2
The third pamameter is 3
The script name is 11.sh //$0就是shell名称
The num pamameter is 3 //$#就是函数参数个数
案例②:求两个数之和的简单脚本
[root@Linux01 ~]# vim 11.sh
#!/bin/bash
function sum() {
sum=$(($1+$2))
echo $sum
}
sum $1 $2
[root@Linux01 ~]# sh 11.sh 1 3 //这里后面跟的两个参数就是shell的参数,由函数调用,然后求和
4
案例③:输入网卡名,然后显示该网卡的ip
[root@Linux01 ~]# vim 11.sh
#!/bin/bash
getip() {
ifconfig $1 | awk -F " " '$1=="inet" {print $2}'
}
read -p "Please enter the eth name: " interface
getip $interface
[root@Linux01 ~]# sh -x 11.sh
+ read -p 'Please enter the eth name: ' interface
Please enter the eth name: ens33
+ getip ens33
+ ifconfig ens33
+ awk -F ' ' '$1 == "inet" {print $2}'
192.168.239.128
更严谨的规范用户输入信息并检查系统中是否存在这个网卡或这个网卡是否存在ip:
参考资料: https://github.com/aminglinux/shell/blob/master/if_ip.txt
二、shell中的数组
数组是一个能保存多个元素的变量,一个元素就是一个值。
1. 定义数组的格式: arr_name=(value1 value2 value3 ...)
调用数组,格式为:${arr_name[@]}
方括号中的 @ 表示所有的元素,也可以用 * 代替 ;调用某一个元素需要使用元素的下标, 下标从0开始,第一个元素下标为0,第二个元素下标为1,依次类推 ; 比如调用第一个元素:${arr_name[0]}
[root@Linux01 ~]# a=(1 2 3 4 5) #定义数组
[root@Linux01 ~]# echo ${a[@]} #遍历数组中所有元素
1 2 3 4 5
[root@Linux01 ~]# echo ${#a[@]} #查看数组中元素个数
5
[root@Linux01 ~]# echo ${a[2]} #获取数组中的某一个元素
3
[root@Linux01 ~]# a[2]=6 #修改元素值
[root@Linux01 ~]# echo ${a[@]}
1 2 6 4 5
[root@Linux01 ~]# a[5]=10 #修改元素值,但指定的下标不存在时,则会将这个值自动添加到数组的最后
[root@Linux01 ~]# echo ${a[@]}
1 2 6 4 5 10
[root@Linux01 ~]# unset a[5] #删除数组的某一个元素
[root@Linux01 ~]# echo ${a[@]}
1 2 6 4 5
[root@Linux01 ~]# unset a[@] #删除整个数组。unset删除数组其实是重新赋值一个空,数组没有删除
[root@Linux01 ~]# echo ${a[@]}
2. 数组的分片
[root@Linux01 ~]# b=(`seq 1 5`) #定义一个数组
[root@Linux01 ~]# echo ${b[@]}
1 2 3 4 5
[root@Linux01 ~]# echo ${b[@]:1:3} #从2个元素开始,向后截取3个
2 3 4
[root@Linux01 ~]# echo ${b[@]:0:3} #从1个元素开始,向后截取3个
1 2 3
[root@Linux01 ~]# echo ${b[@]:0-3:3} #从倒数第三个开始,向后截取3个
3 4 5
3. 数字替换
[root@Linux01 ~]# echo ${b[@]/4/helloworld} #把数组中的4改为helloworld,这只是在输出的时候替换,实际的元素没有改变
1 2 3 helloworld 5
[root@Linux01 ~]# echo ${b[@]}
1 2 3 4 5
[root@Linux01 ~]# b=(${b[@]/4/helloworld}) #真实替换数组中的元素,()不能忘了加
[root@Linux01 ~]# echo ${b[@]}
1 2 3 helloworld 5
三、告警系统需求分析
需求:使用shell定制各种个性化告警工具,但需要统一化管理、规范化管理。
思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等。
主程序:作为整个脚本的入口,是整个系统的命脉。
配置文件:是一个控制中心,用它来开关各个子程序,指定各个相关联的日志文件。
子程序:这个才是真正的监控脚本,用来监控各个指标。
邮件引擎:是由一个python程序来实现,它可以定义发邮件的服务器、发邮件人以及发件人密码
输出日志:整个监控系统要有日志输出
要求:我们的机器角色多种多样,但是所有机器上都要部署同样的监控系统,也就说所有机器不管什么角色,整个程序框架都是一致的,不同的地方在于根据不同的角色,定制不同的配置文件
程序架构:
(主目录 mon)
____________________|_______________________________
| | | | |
bin conf shares mail log
| | | | |
[main.sh] [ mon.conf] [load.sh 502.sh] [mail.py mail.sh] [ mon.log err.log ]
bin下是主程序
conf下是配置文件
shares下是各个监控脚本
mail下是邮件引擎
log下是日志。
四、告警系统主脚本