a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 176|回复: 0

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

[复制链接]
发表于 2012-8-4 14:06:19 | 显示全部楼层 |阅读模式
最近时不时有互联网的朋友问我关于服务器监控方面的问题,问常用的服务器监控除了用开源软件,比如:cacti,nagios监控外是否可以自己写shell脚本呢?根据自己的需求写出的shell脚本更能满足需求,更能细化主机监控的全面性。
0 B: o+ Z! F: \0 q2 A! K下面是我常用的几个主机监控的脚本,大家可以根据自己的情况再进行修改,希望能给大家一点帮助。! S) b% V4 B" \( H+ o& M
1、查看主机网卡流量! O. F1 G! `4 P7 `
#!/bin/bash) B1 b+ i0 Q' d* C
#network$ F- O# ]: s4 W2 I8 I6 k/ v9 H: m: @' I
#Mike.Xu; A  G8 _& C1 D  t
while : ; do
# v# D. m# C: A9 t! J" L* w" c, Itime=‘date +%m“-”%d“ ”%k“:”%M’
& ^; N' V' ~( s, Fday=‘date +%m“-”%d’" `3 K5 P5 I  u" [0 L( |" W$ \
rx_before=‘ifconfig eth0|sed -n “8”p|awk ’{print $2}‘|cut -c7-’
$ Z4 C0 S' `& T$ g) `tx_before=‘ifconfig eth0|sed -n “8”p|awk ’{print $6}‘|cut -c7-’
$ `6 B' }8 s0 usleep 2# T; q& E# J4 m
rx_after=‘ifconfig eth0|sed -n “8”p|awk ’{print $2}‘|cut -c7-’
6 m4 |- T# y* S- }tx_after=‘ifconfig eth0|sed -n “8”p|awk ’{print $6}‘|cut -c7-’
6 g: o0 b' M  a3 Lrx_result=$[(rx_after-rx_before)/256]# i1 u. f( a% V) l
tx_result=$[(tx_after-tx_before)/256]
+ o0 z8 A* @) s$ uecho “$time Now_In_Speed: ”$rx_result“kbps Now_OUt_Speed: ”$tx_result“kbps”
! e4 a3 X! T( S; R2 H- ssleep 2
: U9 k9 s, F7 C7 e$ Z7 odone
8 i# `  ?- {8 F2、系统状况监控4 p8 L  ~% Z$ y6 x
#!/bin/sh
* g( Q3 l( l+ r: q, d. e$ }#systemstat.sh
3 m+ p% t! i! j- Z#Mike.Xu5 y: f9 d6 Q7 j; t
IP=192.168.1.227
3 p) \1 A8 [6 B4 \top -n 2| grep “Cpu” 》》。/temp/cpu.txt% U( M3 q% }; n' |
free -m | grep “Mem” 》》 。/temp/mem.txt! m. q8 U. `& o4 {+ w
df -k | grep “sda1” 》》 。/temp/drive_sda1.txt
, l1 P  R) w/ M7 f- k8 J' |#df -k | grep sda2 》》 。/temp/drive_sda2.txt) P+ i9 ]) w6 b1 o2 L# G
df -k | grep “/mnt/storage_0” 》》 。/temp/mnt_storage_0.txt$ F8 ?) w3 h+ l: W- C
df -k | grep “/mnt/storage_pic” 》》 。/temp/mnt_storage_pic.txt: T2 N$ @, A7 U, p- ^8 p
time=`date +%m“。”%d“ ”%k“:”%M`
& E& }* K: _7 {% X7 N) A- r: ?: s5 a5 Oconnect=`netstat -na | grep “219.238.148.30:80” | wc -l`
! g5 ]" `% c1 V  I- I$ T: K; Aecho “$time $connect” 》》 。/temp/connect_count.txt. a6 x$ g* }: c: F+ o
3、监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告#!/bin/bash# t, s2 _% p+ d2 t  c
#monitor available disk space
0 k  ?. g5 [. K: JSPACE=‘df | sed -n ’/ \ / $ / p‘ | gawk ’{print $5}‘ | sed ’s/%//‘
% h0 |- ~) T) R) T% e. q( |# Dif [ $SPACE -ge 90 ]
1 h7 f3 Z- L7 E7 a4 [* I* Jthen
! w9 e: W+ Q  x; @: Pxxx@xxx.com1 R2 \! |9 m3 E4 h
fi
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-28 06:29 , Processed in 0.162028 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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