Linux記錄-shell實現腳本監控服務器及web應用


1.apache web 服務器

1
2
3
4
5
6
7
8
9
10
!/bin/bash
# 表示請求鏈接3秒鍾,不要返回的測試數據
nc -3 localhost 80 &>/dev/null
if [ $? -eq 0 ];then
        str="apache web status Running!"
else
        str="apache web status Shuting!"
fi
# 發送的主題,郵件地址
echo str|mail -'apache web server' admin@lampym.com

2.監控mysql

1
2
3
4
5
6
7
8
9
10
!/bin/bash
# 表示請求鏈接3秒鍾,不要返回的測試數據
nc -3 localhost 3306 &>/dev/null
if [ $? -eq 0 ];then
        str="mysql server status Running!"
else
        str="mysql server status Shuting!"
fi
# 發送的主題,郵件地址
echo str|mail -'mysql server status' admin@lampym.com

3.監控服務器disk

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
:<<!
NR表示行數,$5表示第5列,具體的自己根據實際調整
!
ds=`df |awk '{if(NR==4){print int($5)}}'`
# 這里45根據實際需要更改
if [ $ds -lt 45 ];then
    str="disk space is less then!!!"
else
    str="disk space is greate than 45%!!!"
fi
echo $str|mailx -'linux server disk space' admin@lampym.com

4.監控服務器monery 

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
:<<!
具體的自己根據實際調整
!
mery=`df |awk '{if(NR==2){print int($3*100/$2)}}'`
if [ $mery -lt 50 ];then
    str="mery space is less then 50%!!!"
else
    str="mery space is greate than 50%!!!"
fi
echo $str|mailx -'linux server mery space' admin@lampym.com

整合一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
# 功能:監控資源
# 名稱:cont.sh
# 作者:楓客浪人
# 版本:0.1
# 聯系方式:xxxx
# apache 應用服務
apache_web(){
    nc -3 localhost 80 &>/dev/null
  if [ $? -eq 0 ];then
        str="apache web status Running!"
  else
        str="apache web status Shuting!"
  fi
    # 發送的主題,郵件地址
  echo str|mail -'apache web server' admin@lampym.com
}
# mysql 服務
mysql_db(){
    nc -3 localhost 3306 &>/dev/null
    if [ $? -eq 0 ];then
        str="mysql server status Running!"
    else
        str="mysql server status Shuting!"
    fi
        # 發送的主題,郵件地址
    echo str|mail -'mysql server status' admin@lampym.com
}
# 磁盤使用情況
disk_mnt(){
    ds=`df |awk '{if(NR==4){print int($5)}}'`
    # 這里45根據實際需要更改
    if [ $ds -lt 45 ];then
        str="disk space is less then!!!"
    else
    str="disk space is greate than 45%!!!"
    fi
    echo $str|mailx -'linux server disk space' admin@lampym.com
}
# 內存使用情況
meny_mnt(){
    mery=`df |awk '{if(NR==2){print int($3*100/$2)}}'`
    if [ $mery -lt 50 ];then
    str="mery space is less then 50%!!!"
    else
    str="mery space is greate than 50%!!!"
    fi
    echo $str|mailx -'linux server mery space' admin@lampym.com
}
min(){
apache_web()
mysql_db()
disk_mnt()
meny_mnt()
}
crontab -e

每天13:10分執行代碼發送一份郵件

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM