系统监控

服务器健康状态监控,包括监控内存、硬盘、cpu、进程等,从而形成状态报告。

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
#
#内存最大使用比率
mem_quota=80
#硬盘最大使用比率
hd_quota=80
#cpu最大使用比率
cpu_quota=80
#获取cpu状态的时间间隔
time_gap=60
#每10分钟形成报告
runtime_gap=600
#内存监控函数
watch_memory()
{
  mem_total=`cat /proc/meminfo grep MemTotal | awk '{print $2}'`
  mem_free=`cat /proc/meminfo grep MemFree | awk '{print $2}'`
  mem_usage=$((100-mem_free*100/mem_total))
  if [ $mem_usage -gt $mem_quota ];then
    mem_message="the memory usage is $mem_usage%!!!"
    return 1
  else
    return 0
  fi
}
#硬盘监控函数
watch_hd()
{
  hd_usage=`df grep /dev/sda2 awk '{print $5}' sed 's/%//g'`
  if [ $hd_usage -gt $hd_quota ];then
    hd_message="the hard disk usage is $hd_usage%!!!"
    return 1
  else
    return 0
  fi
}
#函数读取/proc/stat文件,做数据准备工作,从而获得cpu从启动到现在的忙碌时间
get_cpu_info()
{
  cat /proc/stat grep -i "^cpu[0-9]\+" awk '{used+=$2+$3+$4; unused+=$5+$6+$7+$8} END{print used,unused}'
}
#cpu监控函数
watch_cpu()
{
  time_point_1=`get_cpu_info`
  sleep $time_gap
  time_point_2=`get_cpu_info`
  cpu_usage=`return $time_point_1 $time_point_2 | awk '{used=$3-$1;total+=$3+$4-$1-$2;print used*100/total}'`
  if [ $cpu_usage > $cpu_quota ];then
    cpu_message="the cpu usage is over $cpu_usage!!!"
    echo $cpu_usage
    return 1
  else 
    echo $cpu_usage
    return 0
  fi
}
#获得最近忙碌的进程
proc_cpu_top10()
{
  proc_busiest=`ps aux | sort -nk 3r | head -10`
}
while true;
do
  report=""      #报告内容
  if [ `watch_memory` -eq 1 ];then #内存监控
    report=$report'\n'$mem_message
  fi
  if [ `watch_hd` -eq 1 ];then  #硬盘监控
    report=$report'\n'$hd_message
  fi
  if [ `watch_cpu` -eq 1 ];then
    report=$report'\n'$cpu_message
    proc_cpu_top10
    report=$report'\n'$proc_busiest
  fi
  if [ -n $report ];then
     echo sendmessage phonenumber report
  fi
sleep $`runtime_gap-time_gap`
done









本文转自 SoulMio 51CTO博客,原文链接:http://blog.51cto.com/bovin/1974300,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值