码迷,mamicode.com
首页 > 其他好文 > 详细

用脚本自主初始优化和全面检查服务器

时间:2018-06-09 23:10:59      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:shell脚本   系统优化   系统检查   

      前段时间看到一篇用脚本一键检查服务器的优秀博文,写的真棒,美中不足的是在实际应用中发现一个小问题:他的脚本好是好,但是由于环境和服务不一样,会检查没有的服务项,要去掉这些检查,还要一行行注释掉,很是麻烦,于是就优化脚本内容,把所有的检查项都定义为一个个的函数,最后在定义主函数的时候,可以自主的选择要使用的函数,不想使用的函数可以用#注释掉,比原来省时省事不少;完成脚本优化后,联想到系统优化,自己也按这种思路进行自主选择,下面的脚本内容和大家一块分享:


脚本1为初始优化系统:

#!/bin/bash

 

##################################################

#Name:       centos6.8_init.sh         #

#Version:     v1.0                 #

#Function:    initialization system      #

#Author:      Andy                  #

#organization:  http://blog.51cto.com/13162375 #

#Create Date:  2018-06-08             #

#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:  http://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 $*

注:不管能否解决你遇到的问题,欢迎相互交流,共同提高!

用脚本自主初始优化和全面检查服务器

标签:shell脚本   系统优化   系统检查   

原文地址:http://blog.51cto.com/13162375/2126822

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!