前段時間看到一篇用腳本一鍵檢查服務器的優秀博文,寫的真棒,美中不足的是在實際應用中發現一個小問題:他的腳本好是好,可是因爲環境和服務不同,會檢查沒有的服務項,要去掉這些檢查,還要一行行註釋掉,非常麻煩,因而就優化腳本內容,把全部的檢查項都定義爲一個個的函數,最後在定義主函數的時候,能夠自主的選擇要使用的函數,不想使用的函數能夠用#註釋掉,比原來省時省事很多;完成腳本優化後,聯想到系統優化,本身也按這種思路進行自主選擇,下面的腳本內容和你們一塊分享:
php
腳本1爲初始優化系統:
mysql
#!/bin/bashlinux
##################################################nginx
#Name: centos6.8_init.sh #web
#Version: v1.0 #sql
#Function: initialization system #centos
#Author: Andy #bash
#organization: https://blog.51cto.com/13162375 #服務器
#Create Date: 2018-06-08 #app
#Description: initialization #
##################################################
####### make dir ########
function packages(){
/bin/mkdir -p /application/packages
}
###### SELINUX status ######
function SELINUX(){
/bin/sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config
}
###### iptables status ######
function iptables(){
/sbin/service iptables stop
/sbin/chkconfig iptables off
}
###### yum is aliyun ########
function yum(){
/bin/mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
/usr/bin/wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
}
###### epel is aliyun #######
function epel(){
/usr/bin/wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
}
########## base rpm ##########
function baserpm(){
/usr/bin/yum install lrzsz nmap tree dos2unix nc -y
}
######## del issue and release ######
function delissue(){
/bin/cp /etc/issue /etc/issue.backup
/bin/cp /etc/issue.net /etc/issue.net.backup
/bin/cp /etc/redhat-release /etc/redhat-release.back
>/etc/issue
>/etc/issue.net
>/etc/redhat-release
}
####### ntp server ##########
function ntp(){
/usr/bin/yum install -y ntp
/sbin/service ntpd start
/sbin/chkconfig ntpd on
/sbin/chkconfig ntpdate on
}
######### crontab date ########
function crontabdate(){
/bin/echo '#### ntp server ###' >>/var/spool/cron/root
/bin/echo '*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' >>/var/spool/cron/root
}
####### chrony server ##########
function chrony(){
/usr/bin/yum install -y chrony
/bin/echo 'server ntp1.aliyun.com iburst' >>/etc/chrony.conf
/sbin/service chronyd start
/sbin/chkconfig chronyd on
}
main(){
packages
SELINUX
iptables
yum
epel
baserpm
delissue
#ntp ##因爲已經安裝ntpd,全部就註釋掉了
crontabdate
#chrony ##有ntpd,就不須要chrony了,也註釋掉
}
main $*
腳本2能夠檢查系統(因爲檢查項較多,能夠把不須要的檢查註釋掉):
#!/bin/bash
##################################################
# Name: centos6.8_check.sh #
# Version: v1.0 #
# Function: checking system #
# Author: Andy #
# organization: https://blog.51cto.com/13162375 #
# Create Date: 2018-06-09 #
# Description: checking system items #
##################################################
yd=`date -d "1 day ago" +%Y%m%d`
Ip=`ifconfig eth1|grep "inet addr:"|awk -F '[: ]+' '{print $4}'`
echo -e "\033[35;32m Subject:$Ip \033[0m"
iplist(){
echo -e "\033[35;34m###################\033[0m"
echo 'show ip'
/sbin/ifconfig
}
crontablist(){
echo '##############'
echo 'crontab list'
/usr/bin/crontab -l
echo ''
}
cpulist(){
echo '##############'
echo 'status of CPU'
/usr/bin/top -b -n 1 |head -n 15
}
cputop10(){
echo '##############'
echo 'cpu-top10'
/bin/ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head
}
memory(){
echo '##############'
echo 'status of memory'
/usr/bin/free -mh
}
memorytop10(){
echo '##############'
echo 'mem-top10'
/bin/ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head
}
showdisk(){
echo '##############'
echo 'status of disk'
/bin/df -hT
}
port(){
echo '##############'
echo 'the port of service'
/bin/netstat -lntup
}
datetime(){
echo '##############'
echo 'system time'
/bin/date
}
pingnet(){
echo '##############'
echo 'ping wwwbaidu.com'
/bin/ping -c 4 www.baidu.com
}
tcpstatus(){
echo '##############'
echo '80port tcp status'
/bin/netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}'
}
last5user(){
echo '##############'
echo 'last 5 users'
/usr/bin/tail -n 5 /etc/passwd
}
last5login(){
echo '##############'
echo 'last 5 tty login'
/usr/bin/last -5
}
log(){
echo '##############'
echo 'ydIPshu'
/bin/grep -v HEAD /opt/logs/*${yd}.log | awk '{print $1}' |sort | uniq -c |wc -l
}
messageslog(){
echo '##############'
echo 'message log'
/usr/bin/tail -n 20 /var/log/messages
}
nginx-error(){
echo '##############'
echo 'nginx-error'
/usr/bin/tail -n 10 /opt/logs/error.log
}
php-error(){
echo '##############'
echo 'php-error'
/usr/bin/tail -n 5 /var/log/php-fpm/error.log
}
inotify-log(){
echo '##############'
echo 'inotify-log size'
/bin/ls -l /opt/inotify.log
}
inotifytail(){
echo '##############'
echo 'inotify-tailf-10'
/usr/bin/tail -n 10 /opt/inotify.log
}
showdata(){
echo '##############'
echo 'show databases'
mysql -uroot -pmima -e 'show databases'
}
mysql-max-connection(){
echo '##############'
echo 'mysql-max-connection'
mysql -uroot -pmima -e "show global status like 'Max_used_connections';"
}
myqlslave(){
echo '##############'
echo 'mysql-master-slave'
mysql -uroot -pmima -e 'show slave status\G;'
}
mysqlslow(){
echo '##############'
echo 'mysql-slowlog'
/usr/bin/tail -n 20 /usr/local/mysql/logs/mysqlslow.log
}
main(){
iplist
crontablist
cpulist
cputop10
memory
memorytop10
showdisk
port
datetime
pingnet
tcpstatus
last5user
last5login
#log
#messageslog
#nginx-error
#php-error
#inotify-log
#inotifytail
#showdata
#mysql-max-connection
#myqlslave
#mysqlslow
}
main $*
注:無論可否解決你遇到的問題,歡迎相互交流,共同提升!