天天看點

配置告警系統主腳本main.sh mon.sh load.sh 502.sh disk.sh

需求:使用shell定制各種個性化告警工具,但需要統一化管理、規範化管理。

思路:指定一個腳本包,包含主程式、子程式、配置檔案、郵件引擎、輸出日志等。

主程式:作為整個腳本的入口,是整個系統的命脈。

配置檔案:是一個控制中心,用它來開關各個子程式,指定各個相關聯的日志檔案。

子程式:這個才是真正的監控腳本,用來監控各個名額。

郵件引擎:是由一個python程式來實作,它可以定義發郵件的伺服器、發郵件人以及發件人密碼

輸出日志:整個監控系統要有日志輸出。

在/usr/local/sbin目錄下建立個mon目錄

在mon目錄下建立四個檔案夾

[root@aminglinux-001 mon]# ls

bin conf log mail shares

1、在mon/bin下建立main.sh

編輯告警系統 main.sh

[root@aminglinux-001 bin]# vim 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

2、在conf下建立mon.sh

編輯告警系統mon.sh

#to config the options if to monitor

#定義mysql的伺服器位址、端口以及user、password

to_mon_cdb=0 ##0 or 1, default 0,0 not monitor, 1 monitor

db_ip=10.20.3.13

db_port=3315

db_user=username

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

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

#request_count 定義日志路徑以及域名

to_mon_request_count=0

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

domainname=www.discuz.net

3.在shares建立load.sh

3.1編輯監控項目load.sh

3.2在shares再建立一個502.sh

502.sh内容

3.3建立一個shell項目-告警系統disk.sh

disk.sh内容

繼續閱讀