一、告警系统主脚本

要求:我们的机器角色多种多样,但是所有机器上都要部署同样的监控系统,也就说所有机器不管什么角色,整个程序框架都是一致的,不同的地方在于根据不同的角色,定制不同的配置文件。 程序架构:

bin下是主程序

conf下是配置文件

shares下是各个监控脚本

mail下是邮件引擎

log下是日志

告警系统主脚本:

main.sh内容

#!/bin/bash

#Written by aming.

# 是否发送邮件的开关

export send=1

# 过滤ip地址

export addr=`/sbin/ifconfig |grep -A1 "ens33: "|awk '/inet/ {print $2}'`

dir=`pwd`

# 只需要最后一级目录名

last_dir=`echo $dir|awk -F'/' '{print $NF}'`

# 下面的判断目的是,保证执行脚本的时候,我们在bin目录里,不然监控脚本、邮件和日志很有可能找不到

if [ $last_dir == "bin" ] || [ $last_dir == "bin/" ]; then

conf_file="../conf/mon.conf"

else

echo "you shoud cd bin dir"

exit

fi exec 1>>../log/mon.log 2>>../log/err.log

echo "`date +"%F %T"` load average"

/bin/bash ../shares/load.sh

#先检查配置文件中是否需要监控502

if grep -q 'to_mon_502=1' $conf_file; then

export log=`grep 'logfile=' $conf_file |awk -F '=' '{print $2}' |sed 's/ //g'`

/bin/bash  ../shares/502.sh

fi

命令分解

读取ip

export send=1    # 是否发送邮件的开关,1代表需要发送邮件

查看网卡ip  可以自己定义网卡ens33:

判断是否在bin目录下,dir这个名称自己定义

dir=`pwd`

我们只需要找到bin目录就可以了,# 只需要最后一级目录名,last_dir自己定义

last_dir=`echo $dir|awk -F'/' '{print $NF}'`

主脚本路径

/usr/local/sbin/mon/bin/main.sh

判断如果last_dir就是bin,那么就去配置文件遍历,如果不是dir则提示需要进入bin dir,然后退出脚本

if [ $last_dir == "bin" ] || [ $last_dir == "bin/" ]; then

conf_file="../conf/mon.conf"

else

echo "you shoud cd bin dir"

exit

把正确log重定向到../log/mon.log 2,错误日志重定向到../log/err.log

fi exec 1>>../log/mon.log 2>>../log/err.log

在这里做一个时间标记,系统负载

echo "`date +"%F %T"` load average"

执行负载监控监本

/bin/bash ../shares/load.sh

#先检查配置文件中是否需要监控502

if grep -q 'to_mon_502=1' $conf_file; then        使用正则表达式过滤出是否监控502,to_mon_502=1这个在配置文件mon.conf里边自己定义

export log=`grep 'logfile=' $conf_file |awk -F '=' '{print $2}' |sed 's/ //g'`  使用正则表达式找出log=

/bin/bash  ../shares/502.sh   如果找到了,就去执行502脚本

fi

主脚本

二、告警系统配置文件

路径/usr/local/sbin/mon/conf/mon.conf

mon.conf内容,里边的内容可以自己定义,就是一些开关,日志、port,进入mysql的账号

## to config the options if to monitor

## 定义mysql的服务器地址、端口以及user、password

to_mon_cdb=0   ##0 or 1, default 0,0 not monitor, 1 monitor,为1就执行主脚本,0就不执行主脚本,主脚本继续往下走

db_ip=10.20.3.13

db_port=3315

db_user=username     mysql用户

db_pass=passwd

## httpd   如果是1则监控,为0不监控

to_mon_httpd=0 ## php 如果是1则监控,为0不监控

to_mon_php_socket=0

## http_code_502  需要定义访问日志的路径

to_mon_502=1 指向主脚本if grep -q 'to_mon_502=1' $conf_file; then

logfile=/data/log/xxx.xxx.com/access.log

## request_count

定义日志路径以及域名

to_mon_request_count=0  监控请求数,及log涉及到的域名

req_log=/data/log/www.discuz.net/access.log

domainname=www.discuz.net

三、告警系统监控项目  三个:日志、磁盘、负载

路径在shares下边

路径/usr/local/sbin/mon/shares/load.sh

load.sh内容

#! /bin/bash

##Writen by aming##

load=`uptime |awk -F 'average:' '{print $2}'|cut -d',' -f1|sed 's/ //g' |cut -d. -f1`

if [ $load -gt 10 ] && [ $send -eq "1" ]

then

echo "$addr `date +%T` load is $load" >../log/load.tmp

/bin/bash ../mail/mail.sh aming_test@163.com "$addr\_load:$load"

`cat ../log/load.tmp`

fi

echo "`date +%T` load is $load"

指令分解

load=`uptime |awk -F 'average:' '{print $2}'|cut -d',' -f1|sed 's/ //g' |cut -d. -f1`

使用awk,以average:为分隔,打印第二个字段,也就是average:前边一大串为一个字段,average:后边一大串为一个字段

0前边一直有一个空格

用sed替换把空格去掉

sed 's/ //g'

在加入以.为分隔截取第一段就达到最终目的

cut -d. -f1

if [ $load -gt 10 ] && [ $send -eq "1" ]

判断如果负载load大于10,那么就去去主脚本把发邮件开关打开,# 是否发送邮件的开关export send=1

then

echo "$addr `date +%T` load is $load" >../log/load.tmp     并给负载输送一以本机IP命名的日志放到../log/load.tmp

/bin/bash ../mail/mail.sh aming_test@163.com "$addr\_load:$load" `cat ../log/load.tmp`   执行给我的邮箱发送一个邮件,告诉我负载很高了

邮件的内容为查看日志

echo "`date +%T` load is $load"   在主脚本main.sh里边已经定义fi exec 1>>../log/mon.log 2>>../log/err.log

load.sh主脚本

/usr/local/sbin/mon/shares/load.sh

四、告警系统502日志脚本

路径同样在shares下边

/usr/local/sbin/mon/shares/502.sh

502.sh内容

#! /bin/bash

d=`date -d "-1 min" +%H:%M`

c_502=`grep :$d:  $log  |grep ' 502 '|wc -l`

if [ $c_502 -gt 10 ] && [ $send == 1 ]; then

echo "$addr $d 502 count is $c_502">../log/502.tmp

/bin/bash ../mail/mail.sh $addr\_502 $c_502  ../log/502.tmp `cat ../log/load.tmp`

fi

echo "`date +%T` 502 $c_502"

指令解析

d=`date -d "-1 min" +%H:%M`  每一分钟监控一次,查看一分钟以前的日志

以时间命名在log目录里边查找502log

c_502=`grep :$d:  $log  |grep ' 502 '|wc -l`

如果502log大于10条,那么就去主脚本打开发送邮件开关,把502log发送到我的邮箱,邮箱内容是502log

if [ $c_502 -gt 10 ] && [ $send == 1 ]; then

echo "$addr $d 502 count is $c_502">../log/502.tmp

/bin/bash ../mail/mail.sh $addr\_502 $c_502  ../log/502.tmp `cat ../log/502.tmp`

fi

502日志脚本

vim /usr/local/sbin/mon/shares/502.sh

 五、告警系统disk监控脚本

路径同样在shares下边

/usr/local/sbin/mon/shares/dish.sh

disk.sh内容,思路;把所有分区都检查看一遍

#! /bin/bash

##Writen by aming##

rm -f ../log/disk.tmp

LANG=en 后边就不会出现中文了

for r in `df -h |awk -F '[ %]+' '{print $5}'|grep -v Use`

do

if [ $r -gt 90 ] && [ $send -eq "1" ]

then

echo "$addr `date +%T` disk useage is $r" >>../log/disk.tmp

fi

if [ -f ../log/disk.tmp ]

then

df -h >> ../log/disk.tmp

/bin/bash ../mail/mail.sh $addr\_disk $r ../log/disk.tmp

echo "`date +%T` disk useage is nook"

else

echo "`date +%T` disk useage is ok"

fi

指令详解

df -h |awk -F '[ %]+' '{print $5}'|grep -v Use

df -h |awk -F '[ %]+' '{print $5}'    分隔符 [  %]+ 以空格或多个空格、%,      +号表示多个

这个分隔符的例子:

[root@davery shares]# echo "12:sdf#gg3:dd:#22" |awk -F '[:#]' '{print $2}'  被分割成6段

if [ $r -gt 90 ] && [ $send -eq "1" ]

then

echo "$addr `date +%T` disk useage is $r" >>../log/disk.tmp  判断是否大于90,就把磁盘使用情况日志发送到log

f [ -f ../log/disk.tmp ]  判断log文件是否存在-f

then

df -h >> ../log/disk.tmp      如果存在就把磁盘信息输送到log

/bin/bash ../mail/mail.sh $addr\_disk $r ../log/disk.tmp  执行邮件脚本发送log信息

echo "`date +%T` disk useage is nook"  并打上磁盘使用不ok标签

else

echo "`date +%T` disk useage is ok"   如果日志文件不存在就发送邮件告诉我磁盘ok

fi

磁盘监控监本

/usr/local/sbin/mon/shares/dish.sh

六、告警系统邮件引擎

其中mail.py内容到这里下载https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D22Z/mail.py

编辑mail.py  配置文件

路径:/usr/local/sbin/mon/mail/mail.py

内容这里下载https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D22Z/mail.py

vim mail.py 是python编写直接使用即可,就是mail的配置文件了

编辑mail.sh邮件发送脚本

路径:/usr/local/sbin/mon/mail/mail.sh

log=$1

t_s=`date +%s`

t_s2=`date -d "2 hours ago" +%s`

if [ ! -f /tmp/$log ]

then

echo $t_s2 > /tmp/$log

fi

t_s2=`tail -1 /tmp/$log|awk '{print $1}'`

echo $t_s>>/tmp/$log

v=$[$t_s-$t_s2]

echo $v

if [ $v -gt 3600 ]

then

./mail.py  $1  $2  $3

echo "0" > /tmp/$log.txt

else

if [ ! -f /tmp/$log.txt ]

then

echo "0" > /tmp/$log.txt

fi

nu=`cat /tmp/$log.txt`

nu2=$[$nu+1]

echo $nu2>/tmp/$log.txt

if [ $nu2 -gt 10 ]

then

./mail.py  $1 "trouble continue 10 min $2" "$3"

echo "0" > /tmp/$log.txt

fi

fi

指令解析

 log=$1  是一个参数,每一次发邮件都要找一个根,后边\就是 /bin/bash ../mail/mail.sh $addr\_disk $r ../log/disk.tmp        $addr\_502 $c_502

date +%s 时间戳秒

date -d "2 hours ago" +%s两个小时前的时间戳

if [ ! -f /tmp/$log ]  这个日志其实就是 $addr\_disk项目,如果这个日志不存在,就执行下边一行

echo $t_s2 > /tmp/$log   这个日志就是两个小时之前的时间戳

t_s2=`tail -1 /tmp/$log|awk '{print $1}'`  把时间戳截取出来,找到日志最后一行

echo $t_s>>/tmp/$log  把当前时间戳写到这个日志

v=$[$t_s-$t_s2]  做比较这两个时间戳差

echo $v 输出v

if [ $v -gt 3600 ]  如果差值大于3600秒

./mail.py  $1  $2  $3  发邮件告警 间隔1小时2小时3小时

echo "0" > /tmp/$log.txt 生成新的日志,用来计数,每告警一次一次数

else

if [ ! -f /tmp/$log.txt ]

then

echo "0" > /tmp/$log.txt  如果log为空也记一次数

fi

nu=`cat /tmp/$log.txt` 查看计数器里边数字

nu2=$[$nu+1]         循环加1,每一次都加1

echo $nu2>/tmp/$log.txt

if [ $nu2 -gt 10 ]     新的计数器

then

./mail.py  $1 "trouble continue 10 min $2" "$3" 如果计数累加大于10就发邮件,持续十分钟

echo "0" > /tmp/$log.txt 重新计数

清空计数器1个小时作为一个周期

Linux centosVMware 告警系统主脚本、告警系统配置文件、告警系统负载脚本、告警系统502日志脚本、告警系统disk监控脚本、告警系统邮件引擎的更多相关文章

  1. Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件

    一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...

  2. Linux(7)- Nginx.conf主配置文件、Nginx虚拟主机/访问日志/限制访问IP/错误页面优化、Nginx反向代理、Nginx负载均衡

    一.Nginx.conf主配置文件 Nginx主配置文件conf/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的.一般,每个区块以一对大括号{}来表示开始与结束. 核心模 ...

  3. Linux centosVMware shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量

    一. shell脚本介绍 shell是一种脚本语言 aming_linux blog.lishiming.net 可以使用逻辑判断.循环等语法 可以自定义函数 shell是系统命令的集合 shell脚 ...

  4. Linux centosVMware shell中的函数、shell中的数组、

    一.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可. 格式: function _name() { command ...

  5. linux -小记(1) 问题:"linux ifconfig查看网卡名称与配置文件不否" 或 启动网卡提示“ eth0 似乎不存在, 初始化操作将被延迟”。

    "linux ifconfig查看网卡名称与配置文件不否" 或 启动网卡提示" eth0 似乎不存在, 初始化操作将被延迟" . 问题 1. service n ...

  6. linux系统CPU,内存,磁盘,网络流量监控脚本

    前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat ...

  7. Linux 服务器系统监控脚本 Shell【转】

    转自: Linux 服务器系统监控脚本 Shell - 今日头条(www.toutiao.com)http://www.toutiao.com/i6373134402163048961/ 本程序在Ce ...

  8. nginx+keepalived主辅切换(监控脚本在keepalived.conf中执行)

    以前写过一篇,nginx+keepalived 双机互备的文章,写那篇文章的时候没有想过如果apache或者nginx 挂了,而 keepalived 或者 机器没有死,那么主辅是不会切换的,今天就研 ...

  9. 按时按登录IP记录Linux所有用户操作日志的方法(附脚本)

    PS:Linux用户操作记录一般通过命令history来查看历史记录,但是如果因为某人误操作了删除了重要的数据,这种情况下history命令就不会有什么作用了.以下方法可以实现通过记录登陆IP地址和所 ...

随机推荐

  1. javascipt——jQuery

    1.首先需要导入jQuery1.12.4.js文件,把文件和当前目录放同一目录下.在<body>中写上<script scr = 'jQuery1.12.4.js'><s ...

  2. Cosmetic Airless Bottles To Meet Practical Requirements

    Today, people use cosmetic bottles, many of which are in cosmetic airless bottles. We can use them, ...

  3. opencv:图像的读取,显示,写入文件

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  4. Mybatis 元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id*,result*,association*,collection*,discriminat

    <resultMap id="BaseResultMap" type="com.youotech.tl_cons_credit_rating.entity.TL_C ...

  5. 《实战Java高并发程序设计》读书笔记四

    第四章 锁的优化及注意事项 1.锁性能的几点建议 减小锁持有时间: 系统持有锁时间越长锁竞争程度就越激烈,只对需要同步的方法加锁,可以减小锁持有时间进而提高锁性能. 减少锁的持有时间有助于降低锁冲突的 ...

  6. yii2.0 验证码

    首先我们在控制器里创建一个actions方法,用于使用yii\captcha\CaptchaAction <?php namespace app\controllers; use YII; us ...

  7. ABC156 F - Modularness

    题目链接 题意还是比较清楚的,给你q个询问,对每组询问的模数和初始值不同,求满足条件\(a_j~\textrm{mod}~m_i < a_{j + 1}~\textrm{mod}~m_i,(0 ...

  8. 6.Python字符串

    #header { display: none !important; } } #header-spacer { width: 100%; visibility: hidden; } @media p ...

  9. 5_2 木块问题(UVa101)<vector的使用>

    [背景] 在计算机科学中的很多地方都会使用简单,抽象的方法来做分析和实验验究.比如在早期的规划学和机器人学的人工智能研究就利用一个积木世界,让机械臂执行操作积木的任务. 在这个问题中,你将在确定的规则 ...

  10. Pycharm 分屏

    有图由真相 效果自在眼前