加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux记录-shell实现脚本监控服务器及web应用

发布时间:2020-12-14 01:59:48 所属栏目:Linux 来源:网络整理
导读:1.apache web 服务器 1 2 3 4 5 6 7 8 9 10 ! / bin / bash # 表示请求链接3秒钟,不要返回的测试数据 nc? - w? 3 ? localhost? 80 ? / dev / null if ? [ $?? - eq? 0 ? ];then ???????? str = "apache web status Running!" else ???????? str = "apach

1.apache web 服务器

1
2
3
4
5
6
7
8
9
10
!/bin/bash
# 表示请求链接3秒钟,不要返回的测试数据
nc?-w?3?localhost?80?&>/dev/null
if?[ $??-eq?0?];then
????????str="apache web status Running!"
else
????????str="apache web status Shuting!"
fi
# 发送的主题,邮件地址
echo?str|mail?-s?‘apache web server‘?[email?protected]

2.监控mysql

1
2
3
4
5
6
7
8
9
10
!/bin/bash
# 表示请求链接3秒钟,不要返回的测试数据
nc?-w?3?localhost?3306?&>/dev/null
if?[ $??-eq?0?];then
????????str="mysql server status Running!"
else
????????str="mysql server status Shuting!"
fi
# 发送的主题,邮件地址
echo?str|mail?-s?‘mysql server status‘?[email?protected]

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?-s?‘linux server disk space‘ [email?protected]

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?-s?‘linux server mery space‘ [email?protected]

整合一下

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?-w?3?localhost?80?&>/dev/null
  if?[ $??-eq?0?];then
????????str="apache web status Running!"
  else
????????str="apache web status Shuting!"
  fi
    # 发送的主题,邮件地址
  echo?str|mail?-s?‘apache web server‘?[email?protected]
}
# mysql 服务
mysql_db(){
????nc?-w?3?localhost?3306?&>/dev/null
????if?[ $??-eq?0?];then
????????str="mysql server status Running!"
????else
????????str="mysql server status Shuting!"
????fi
????????# 发送的主题,邮件地址
????echo?str|mail?-s?‘mysql server status‘?[email?protected]
}
# 磁盘使用情况
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?-s?‘linux server disk space‘?[email?protected]
}
# 内存使用情况
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?-s?‘linux server mery space‘?[email?protected]
}
min(){
apache_web()
mysql_db()
disk_mnt()
meny_mnt()
}
crontab -e

每天13:10分执行代码发送一份邮件

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读