Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 193d8fc

Browse files
committed
update linux
1 parent e11c4d7 commit 193d8fc

File tree

1 file changed

+30
-250
lines changed

1 file changed

+30
-250
lines changed

Linux学习之路.md

Lines changed: 30 additions & 250 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,197 +2843,6 @@ crontab /root/etcbakcron
28432843

28442844
然后执行 crontab file 使生效。
28452845

2846-
#五个常用的Linux监控脚本代码
2847-
2848-
2849-
2850-
为大家提供五个常用linux监控脚本(查看主机网卡流量、系统状况监控、监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告、监控CPU和内存的使用情况、全方位监控主机),有需要的朋友不妨看看哦
2851-
2852-
2853-
2854-
2855-
2856-
2857-
2858-
1、查看主机网卡流量
2859-
2860-
2861-
2862-
#!/bin/bash
2863-
2864-
#network
2865-
2866-
#Mike.Xu
2867-
2868-
while : ; do
2869-
2870-
time='date +%m"-"%d" "%k":"%M'
2871-
2872-
day='date +%m"-"%d'
2873-
2874-
rx_before='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'
2875-
2876-
tx_before='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'
2877-
2878-
sleep 2
2879-
2880-
rx_after='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'
2881-
2882-
tx_after='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'
2883-
2884-
rx_result=$[(rx_after-rx_before)/256]
2885-
2886-
tx_result=$[(tx_after-tx_before)/256]
2887-
2888-
echo "$time Now_In_Speed: "$rx_result"kbps Now_OUt_Speed: "$tx_result"kbps"
2889-
2890-
sleep 2
2891-
2892-
done
2893-
2894-
2、系统状况监控
2895-
2896-
2897-
2898-
#!/bin/sh
2899-
2900-
#systemstat.sh
2901-
2902-
#Mike.Xu
2903-
2904-
IP=192.168.1.227
2905-
2906-
top -n 2| grep "Cpu" >>./temp/cpu.txt
2907-
2908-
free -m | grep "Mem" >> ./temp/mem.txt
2909-
2910-
df -k | grep "sda1" >> ./temp/drive_sda1.txt
2911-
2912-
#df -k | grep sda2 >> ./temp/drive_sda2.txt
2913-
2914-
df -k | grep "/mnt/storage_0" >> ./temp/mnt_storage_0.txt
2915-
2916-
df -k | grep "/mnt/storage_pic" >> ./temp/mnt_storage_pic.txt
2917-
2918-
time=`date +%m"."%d" "%k":"%M`
2919-
2920-
connect=`netstat -na | grep "219.238.148.30:80" | wc -l`
2921-
2922-
echo "$time $connect" >> ./temp/connect_count.txt
2923-
2924-
3、监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告
2925-
2926-
2927-
2928-
#!/bin/bash
2929-
2930-
#monitor available disk space
2931-
2932-
SPACE='df | sed -n '/ \ / $ / p' | gawk '{print $5}' | sed 's/%//'
2933-
2934-
if [ $SPACE -ge 90 ]
2935-
2936-
then
2937-
2938-
2939-
2940-
fi
2941-
2942-
4、 监控CPU和内存的使用情况
2943-
2944-
2945-
2946-
#!/bin/bash
2947-
2948-
#script to capture system statistics
2949-
2950-
OUTFILE=/home/xu/capstats.csv
2951-
2952-
DATE='date +%m/%d/%Y'
2953-
2954-
TIME='date +%k:%m:%s'
2955-
2956-
TIMEOUT='uptime'
2957-
2958-
VMOUT='vmstat 1 2'
2959-
2960-
USERS='echo $TIMEOUT | gawk '{print $4}' '
2961-
2962-
LOAD='echo $TIMEOUT | gawk '{print $9}' | sed "s/,//' '
2963-
2964-
FREE='echo $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $4} ' '
2965-
2966-
IDLE='echo $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' |gawk '{print $15}' '
2967-
2968-
echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" >> $OUTFILE
2969-
2970-
5、全方位监控主机
2971-
2972-
2973-
2974-
#!/bin/bash
2975-
2976-
# check_xu.sh
2977-
2978-
# 0 * * * * /home/check_xu.sh
2979-
2980-
2981-
2982-
DAT="`date +%Y%m%d`"
2983-
2984-
HOUR="`date +%H`"
2985-
2986-
DIR="/home/oslog/host_${DAT}/${HOUR}"
2987-
2988-
DELAY=60
2989-
2990-
COUNT=60
2991-
2992-
# whether the responsible directory exist
2993-
2994-
if ! test -d ${DIR}
2995-
2996-
then
2997-
2998-
/bin/mkdir -p ${DIR}
2999-
3000-
fi
3001-
3002-
# general check
3003-
3004-
export TERM=linux
3005-
3006-
/usr/bin/top -b -d ${DELAY} -n ${COUNT} > ${DIR}/top_${DAT}.log 2>&1 &
3007-
3008-
# cpu check
3009-
3010-
/usr/bin/sar -u ${DELAY} ${COUNT} > ${DIR}/cpu_${DAT}.log 2>&1 &
3011-
3012-
#/usr/bin/mpstat -P 0 ${DELAY} ${COUNT} > ${DIR}/cpu_0_${DAT}.log 2>&1 &
3013-
3014-
#/usr/bin/mpstat -P 1 ${DELAY} ${COUNT} > ${DIR}/cpu_1_${DAT}.log 2>&1 &
3015-
3016-
# memory check
3017-
3018-
/usr/bin/vmstat ${DELAY} ${COUNT} > ${DIR}/vmstat_${DAT}.log 2>&1 &
3019-
3020-
# I/O check
3021-
3022-
/usr/bin/iostat ${DELAY} ${COUNT} > ${DIR}/iostat_${DAT}.log 2>&1 &
3023-
3024-
# network check
3025-
3026-
/usr/bin/sar -n DEV ${DELAY} ${COUNT} > ${DIR}/net_${DAT}.log 2>&1 &
3027-
3028-
#/usr/bin/sar -n EDEV ${DELAY} ${COUNT} > ${DIR}/net_edev_${DAT}.log 2>&1 &
3029-
3030-
放在crontab里每小时自动执行:
3031-
3032-
0 * * * * /home/check_xu.sh
3033-
3034-
这样便会在/home/oslog/host_yyyymmdd/hh目录下生成各小时cpu、内存、网络,IO的统计数据。
3035-
3036-
如果某个时间段产生问题了,就可以去看对应的日志信息,看看当时的主机性能如何。
30372846

30382847
# shell “syntax error:unexpected end of file”
30392848

@@ -3087,65 +2896,36 @@ echo $ip
30872896

30882897
# shell 判断字符串是否存在包含关系
30892898

3090-
#! /bin/bash
3091-
3092-
3093-
3094-
var1="hello"
3095-
3096-
var2="he"
3097-
3098-
3099-
3100-
#方法1
3101-
3102-
if [ ${var1:0:2} = $var2 ]
3103-
3104-
then
3105-
3106-
echo "1:include"
3107-
3108-
fi
3109-
3110-
3111-
3112-
#方法2
3113-
3114-
echo "$var1" |grep -q "$var2"
3115-
3116-
if [ $? -eq 0 ]
3117-
3118-
then
3119-
3120-
echo "2:include"
3121-
3122-
fi
3123-
3124-
3125-
3126-
#方法3
3127-
3128-
echo "$var1" |grep -q "$var2" && echo "include" ||echo "not"
3129-
3130-
3131-
3132-
#方法4
3133-
3134-
[[ "${var1/$var2/}" != "$var2" ]] && echo "include" || echo "not"
3135-
3136-
3137-
3138-
其他方法:
3139-
3140-
3141-
3142-
expr或awk的index函数
3143-
3144-
${var#...}
3145-
3146-
${var%...}
3147-
3148-
${var/.../...}
2899+
```
2900+
#! /bin/bash
2901+
2902+
var1="hello"
2903+
var2="he"
2904+
2905+
# 方法1
2906+
if [ ${var1:0:2} = $var2 ]
2907+
then
2908+
echo "1:include"
2909+
fi
2910+
2911+
# 方法2
2912+
echo "$var1" |grep -q "$var2"
2913+
if [ $? -eq 0 ]
2914+
then
2915+
echo "2:include"
2916+
fi
2917+
2918+
# 方法3
2919+
echo "$var1" |grep -q "$var2" && echo "include" ||echo "not"
2920+
2921+
# 方法4
2922+
[[ "${var1/$var2/}" != "$var2" ]] && echo "include" || echo "not"
2923+
2924+
# 其他方法,expr或awk的index函数
2925+
${var#...}
2926+
${var%...}
2927+
${var/.../...}
2928+
```
31492929

31502930

31512931
## C语言调试手段:锁定错误的实现方法

0 commit comments

Comments
 (0)