# Experimental Case One,
**
Writing sysmon.sh Scripts
(1) Use the DF command to extract the disk occupancy of the root partition and assign a value to the variable dug.
(2) Use the Mpstat command to extract the CPU usage (Install the SYSSTAT package) and assign a value to the variable CUG.
(3) Use the free command to extract memory usage and assign a value to the variable mug.
(4) To determine whether the above monitoring items exceed the standard, will need to alarm information to save to the/tmp/alert.txt file.
(5) Determine whether the/tmp/alert.txt file exists, if any, as a warning message sent.
[Email protected] ~]# vi/root/sysmon.sh
#!/bin/bash# Extract performance monitoring metrics (disk footprint, CPU usage, memory usage) dug=$ (df-ht | grep "/$" | awk ' {print $6} ' |awk-f% ' {print $} ') cug=$ (expr 100-$ ( Mpstat | Tail-1 | awk ' {print $} ' | Awk-f. ' {print $} ') mug=$ (expr $ (free | grep "mem" | awk ' {print $} ') \* +/$ (free | grep "MEM:" | awk ' {print $} ') #设置告警日志文件, Alarm mailbox alog= "/tmp/alert.txt" amail= "root" #判断是否记录告警if [$DUG-gt 1]then echo "disk occupancy: $DUG%" ;> $ALOGfiif [$CUG-gt 2]then echo "CPU usage: $CUG%" >> $ALOG fiif [$MUG-gt 3]then echo "Memory utilization: $MUG%" ;> $ALOGfi # Off whether to send an alarm message, and finally delete the alarm log file if [-F $ALOG]then cat $ALOG | Mail-s "Host Alert" $AMAIL rm-rf $ALOGfi [[[email protected] ~]# chmod +x/root/sysmon.sh*** Test sysmon.sh Script Execution * * * (1 Confirm that there are available mail servers (such as Postfix, SendMail, and so on), then turn down the monitoring thresholds and execute the sysmon.sh script to test. [[email protected] ~]#/root/sysmon.sh (2) Check the [email protected] mailbox, confirm the alarm message content. [[email protected] ~]$ cat/var/spool/mail/rootx-original-to: [email protected] ... From: [email protected] (Root) Memory utilization: 91%** Set crontab Scheduled Task * * (1)System service Crond is already running. [[email protected] ~]# systemctl status Crondcrond (PID 5839) is running ... (2) Add crontab Scheduled task configuration and call sysmon.sh script every half hour. [[email protected] ~]# crontab-e08 * * */root/sysmon.sh** experimental Case II: Determine if the kernel version is greater than 3.4**[[email protected] ~]# VI chkv ersion.sh
#!/bin/bash
mnum=$ (uname-r | awk-f. ' {print $} ')
snum=$ (uname-r | awk-f. ' {print $} ')
If [$Mnum-GT 3]
Then
echo "kernel version: $Mnum. $Snum"
elif [$Mnum-eq 3] && [$Snum-GT 4]
Then
echo "kernel version: $Mnum. $Snum"
Else
echo "The kernel version is too low to continue!" "
Fi
[Email protected] ~]# chmod +x chkversion.sh
[Email protected] ~]#./chkversion.sh
The kernel version is: 3.10
Test case three, determine whether RPM package Sysstat installed, if not installed automatically installed
[Email protected] ~]# VI chkinstall.sh
#!/bin/bashrpm -q sysstat &> /dev/nullif [ $? -eq 0 ]then echo "已安装 ‘rpm -q sysstat‘"else echo "未安装,尝试自动安装" RHEL_DIR="/media/Packages/" if [ -d $RHEL_DIR ] then cd $RHEL_DIR rpm -ivh sysstat-*.rpm && echo "安装完成。" else echo "错误:无法访问光盘目录:$RHEL_DIR" fifi[[email protected] ~]# chmod +x chkinstall.sh[[email protected] ~]# ./chkinstall.sh //未安装时的情况未安装,尝试自动安装Preparing... ########################################### [100%] 1:sysstat ########################################### [100%]安装完成。[[email protected] ~]# ./chkinstall.sh //已安装时的情况已安装 sysstat-7.0.2-3.el5
Shell Script 3