a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 162|回复: 0

[考试辅导] 几个常用的Linux监控脚本

[复制链接]
发表于 2012-8-4 14:06:19 | 显示全部楼层 |阅读模式
最近时不时有互联网的朋友问我关于服务器监控方面的问题,问常用的服务器监控除了用开源软件,比如:cacti,nagios监控外是否可以自己写shell脚本呢?根据自己的需求写出的shell脚本更能满足需求,更能细化主机监控的全面性。$ N- _; d! ~: O5 H
下面是我常用的几个主机监控的脚本,大家可以根据自己的情况再进行修改,希望能给大家一点帮助。. R* g" M0 Y/ I1 t2 D4 Y: {4 J
1、查看主机网卡流量' j4 A. N3 S# b' D
#!/bin/bash4 p; F; D8 S: b2 {. \5 ?
#network9 K& o, g& c7 m8 q9 r4 d/ C7 n3 [' ^
#Mike.Xu+ N- `( K" Z, \  y
while : ; do% K# @2 T7 s" ~" v
time=‘date +%m“-”%d“ ”%k“:”%M’. A+ \9 b/ Y4 S8 L
day=‘date +%m“-”%d’* H" M) A- n' o2 p7 `
rx_before=‘ifconfig eth0|sed -n “8”p|awk ’{print $2}‘|cut -c7-’% W# o/ K  @' B
tx_before=‘ifconfig eth0|sed -n “8”p|awk ’{print $6}‘|cut -c7-’
9 X2 H8 b9 _4 k  \sleep 2
: `& \* R* n0 q5 x, U. j" M9 ^3 j; arx_after=‘ifconfig eth0|sed -n “8”p|awk ’{print $2}‘|cut -c7-’
- [8 e: [% j& v( g+ jtx_after=‘ifconfig eth0|sed -n “8”p|awk ’{print $6}‘|cut -c7-’/ j* E" i; t' [6 X0 h
rx_result=$[(rx_after-rx_before)/256]! q- y: @* j8 r* N  F
tx_result=$[(tx_after-tx_before)/256]
& ~0 n8 I1 j( s* e: I$ ^# V% pecho “$time Now_In_Speed: ”$rx_result“kbps Now_OUt_Speed: ”$tx_result“kbps”, t4 l  w8 |. `( x
sleep 2
( B/ h  A6 X7 \' U! y3 n' Qdone
" m% t3 e, k: ^! k4 F# N2、系统状况监控
8 P& d8 T' R5 {4 c4 s#!/bin/sh! ~) `% l9 i/ E5 q8 h0 m* m) J
#systemstat.sh! z: }4 k2 z5 L: y
#Mike.Xu
) j5 m6 b7 `+ z4 w5 |IP=192.168.1.227
  r* P8 r! t9 [' ^top -n 2| grep “Cpu” 》》。/temp/cpu.txt1 r( d( P8 n& v  z
free -m | grep “Mem” 》》 。/temp/mem.txt
/ a  D& B. y( Wdf -k | grep “sda1” 》》 。/temp/drive_sda1.txt' Z2 S; W& w) e4 V. v
#df -k | grep sda2 》》 。/temp/drive_sda2.txt
4 k) d' Y! X" I3 N9 pdf -k | grep “/mnt/storage_0” 》》 。/temp/mnt_storage_0.txt
4 `+ x3 |: y6 W# P" adf -k | grep “/mnt/storage_pic” 》》 。/temp/mnt_storage_pic.txt+ l) ^; V8 Y. R' m( ?
time=`date +%m“。”%d“ ”%k“:”%M`
* `, T1 N7 d" |" zconnect=`netstat -na | grep “219.238.148.30:80” | wc -l`. L/ G# a% P$ t# V
echo “$time $connect” 》》 。/temp/connect_count.txt
( A! A1 W6 I' ]) ?1 O0 r+ L1 [3、监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告#!/bin/bash8 j2 w$ V! a( G$ }" i
#monitor available disk space( h. E* g7 c! d+ A* R6 a9 B
SPACE=‘df | sed -n ’/ \ / $ / p‘ | gawk ’{print $5}‘ | sed ’s/%//‘
- l: h7 g- i% {if [ $SPACE -ge 90 ]# N; g" s$ \$ C9 L" Q% M, j* F
then% T: q' ~4 V* m
xxx@xxx.com2 y. J: p! C/ \& h4 g5 D: H8 P: t
fi
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-7 22:54 , Processed in 0.168893 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表