Using the shell to write a script that detects system parameters

Source: Internet
Author: User
Tags disk usage

Script to detect OS

Objective: After learning the shell language, according to the script that has been learned to write a shell to detect some of the system parameters to achieve email alerts,

(I have just learned to write less perfect and practical!) )


[email protected] scripts]# cat os.monitor.sh

#!/bin/bash

###########

#name =linux View System resource Scripts

#auth =CML

#time: 2015

###########


Date= ' Date ' +%f%h:%m:%s "'

Host= ' hostname '


###########


echo "Check: $DATE"


Check_yum () {

Yum Install-y Sysstat

Yum Install-y iftop

Yum Install-y lsof


}


Check_mail () {

Rpm-qa | grep mailx-12.4-8.el6_6.x86_64

Mail= ' echo $? '

If [$mail-eq 0]; Then

echo "Mail is Installed"

Else

Yum Install-y Mailx

Fi

Service Postfix Status

Mail= ' echo $? '

If [$mail-eq 0]; Then

echo "Postfix is running!"

Else

echo "Postfix not Running"

Service Postfix start

echo "Postfix successfully runnig"

Fi

Echo-e "\033[32m=========check_mail is ok========\033[0m"

# #只是为了好看分隔

}


Check_sys () {

# echo "Check host name, System parameters! "

Echo-n "HOSTNAME:"

Echo $HOST

Echo-n "System version:"

Cat/etc/issue

Echo-n "System parameters:"

Cat/proc/version

Echo-e "\033[32m=========check_sys is ok========\033[0m"

}


Check_cpu () {

# echo "Check Cpu,load,wa,idel situation! "

Echo-n "CPU Core:"

cpu= ' Cat/proc/cpuinfo | grep Processor | Wc-l '

Echo $CPU

Echo-n "Load:"

Load1= ' W | awk ' nr==1 {print $} ' | Awk-f '. ' ' {print '} '

Load2=w | awk ' nr==1 {print $} '

Echo $load 2

If [$load 1-lt $cpu]; Then

echo "Send_mail to ******** @qq. com"

echo "Your CPU is normal! cpu= $cpu load balance= $load 2 time: $DATE "| Mail-s "Check your CPU" ******** @qq. com

Else

echo "Your load<cpu load= $load cpu= $cpu $DATE"


Fi

Echo-e "\033[32m=========check_cpu is ok========\033[0m"

}


Check_mem () {

# echo "Check memory usage! "

Free= ' Free-m | awk ' nr==2 {print $4} '

Cache= ' Free-m | awk ' nr==2 {print $7} '

Echo-n "Memory Usage:"

Mem= ' echo $[free+cache] '

Echo $mem

If [$mem-lt 1000]; Then

echo "Send_mail to ******** @qq. com"

echo "Your mem is normal time: $DATE" | Mail-s "Check your mem" ******** @qq. com

Else

echo "Your mem: $mem >1000 is abnormality time: $DATE"

Fi

Echo-e "\033[32m=========check_mem is ok========\033[0m"

}


Check_network () {

# echo "Check the network card eth0 traffic! "

# iftop-t >>/tmp/network

Echo-n "Receive-side traffic:"

#cat/tmp/network |  grep Send | awk ' nr==1 {print $4} '

Ifconfig eth0 | awk ' nr==8 {print $} ' | Awk-f ' (' {print $} '

Echo-n "Send-side traffic:"

#cat/tmp/network | grep Receive | awk ' nr==1 {print $4} '

Ifconfig eth0 | awk ' nr==8 {print $7} ' | Awk-f ' (' {print $} '

Echo-e "\033[32m=========check_network is ok========\033[0m"

}

Check_tcp () {

# echo "Check TCP connection Status! "

Echo-n "TCP Connection Status:"

Netstat-an | awk '/^tcp/{a[$NF]++}end{for (i in a) print i,a[i]} '

Listen= ' Netstat-an | awk '/^tcp/{a[$NF]++}end{for (i in a) print I,a[i]} ' | awk ' nr==2 {print $} '

If [$listen-gt 7]; Then

echo "Send_mail to ******** @qq. com"

echo "Your connection is normal! Time: $DATE "| Mail-s "Check your TCP" ******** @qq. com

Else

echo "Your Now-listen>normal-listen now-listen= $listen $DATE"

Fi

Echo-e "\033[32m=========check_tcp is ok========\033[0m"

}


Check_disk () {

# echo "Check the disk usage! "

echo "/partition percent used:"

root= ' Df-h | awk ' Nr==3{print $4} ' | Awk-f "%" ' {print '} '

Echo $root

echo "Innode Use percent:"

Df-i | awk ' nr==3 {print $4} '

If [$root-lt 80]; Then

echo "Send_mail to ******** @qq. com"

echo "Your disk:/is normal! Time: $DATE "| Mail-s "Check your DISK" ******** @qq. com

Else

echo "Your disk:/greater than 80%/: $root time: $DATE"

Fi

Echo-e "\033[32m=========check_disk is ok========\033[0m"

}




Monitor2 () {

Check_sys

Check_cpu

Check_mem

Check_network

Check_tcp

Check_disk

}


Monitor2 >>/tmp/$ (hostname). txt


This article is from the "Road to be Operational" blog, please be sure to keep this source http://legehappy.blog.51cto.com/13251607/1962803

Using the shell to write a script that detects system parameters

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.