文章目录
一、Linux监控服务端口脚本
脚本是基于telnet命令进行监控端口服务,所以运行脚本前务必检查telnet工具是否安装!
#!/bin/bash
prot_status(){
temp_file=`mktemp prot_status.XXX`
# 1、判断telnet命令是否存在
[ ! -x /usr/bin/telnet ] && echo "telnet: not found comand" && exit 1
# 2、测试端口 $1:IP地址 $2:端口号
( telnet $1 $2 <<EOF
quit
EOF
) &> $temp_file
# 3、分析文件中内容 判断结果
if egrep "\^]" $temp_file &>/dev/null;then
echo "$1 $2 open"
else
echo "$1 $2 down"
fi
rm -f $temp_file
}
prot_status $1 $2
二、Linux编译安装Nginx脚本
#!/bin/bash
clear
echo "Preparations before installation..."
##variabled
NGINX_VERSION="nginx-1.16.0"
NGINX_INSTALL_DOC="/usr/local/nginx"
NGINX_USER="nginx"
NGINX_GROUP="nginx"
NGINX_CONFIGURE="--prefix=${NGINX_INSTALL_DOC} --user=${NGINX_USER} --group=${NGINX_GROUP} --with-http_ssl_module --with-http_stub_status_module"
##function
nginx_check(){
# 1、监测当前用户 要求为root
if [ "$USER" != 'root' ];then
echo "need to be root so that"
exit 5
fi
# 2、检查wget命令
WGET_CHECK=$(rpm -q wget)
if [ $? -ne 0 ];then
yum -y install wget &> /dev/null
fi
}
nginx_install_pre(){
# 1、安装依赖
if ! (yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre-devel openssl openssl-devel elinks 1>/dev/null);then
echo "ERROR:YUM install error"
exit 5
fi
# 2、下载nginx源码包
if (wget http://nginx.org/download/${NGINX_VERSION}.tar.gz &>/dev/null);then
tar zxf ${NGINX_VERSION}.tar.gz
if [ ! -d ${NGINX_VERSION} ];then
echo "ERROR:not found ${NGINX_VERSION}";exit 5
fi
else
echo "ERROR:wget download file ${NGINX_VERSION}.tar.gz fail"
fi
}
nginx_install_make(){
(groupadd ${NGINX_GROUP} ;useradd -s /sbin/nologin -r -M -g ${NGINX_GROUP} ${NGINX_USER}) &>/dev/null
cd ${NGINX_VERSION}
echo "nginx configure..."
if ./configure ${NGINX_CONFIGURE} 1>/dev/null;then
echo "nginx make ..."
if make 1>/dev/null;then
echo "nginx make install ..."
if make install 1>/dev/null;then
echo "nginx install success"
else
echo "ERROR: nginx install tail!";exit 5
fi
else
echo "ERROR: nginx make tail!";exit 5
fi
else
echo"ERROR: nginx configure tail!";exit 5
fi
}
# 配置nginx开机自启,使用systemctl 管理nginx服务
nginx_enable(){
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=${NGINX_INSTALL_DOC}/sbin/nginx
ExecReload=${NGINX_INSTALL_DOC}/sbin/nginx -s reload
ExecStop=${NGINX_INSTALL_DOC}/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl enable nginx.service 1>/dev/null
}
nginx_start(){
TEMP_NGINX=$(mktemp nginx.XXX)
if systemctl start nginx.service;then
echo "nginx start SUCCESS!"
clear
elinks http://localhost -dump >${TEMP_NGINX}
head -n +11 ${TEMP_NGINX}
rm -f ${TEMP_NGINX}
echo -e "\e[1;36m Manager Nginx:\e[0m \e[0;32m systemctl start|stop|status|restart| nginx.service \e[0m"
else
echo "nginx stop FAIL"
fi
}
nginx_check
nginx_install_pre
nginx_install_make
nginx_enable
nginx_start
脚本运行成功效果图:
可改变量:
6 NGINX_VERSION="nginx-1.16.0" #定义安装nginx版本
7 NGINX_INSTALL_DOC="/usr/local/nginx" #定义安装目录
8 NGINX_USER="nginx" #定义nginx用户
9 NGINX_GROUP="nginx" #定义nginx组
10 NGINX_CONFIGURE="...省略" #定义nginx编译需要模块及参数!
# 注意:在定义 NGINX_CONFIGURE变量模块时 需要安装此模块需要的依赖(如:with-http_ssl_module模块就需要依赖openssl 否则就会报错!)
三、Linux监控一个主机状态脚本
[root@shell jao]# cat monitor_host.sh
#!/bin/bash
for ((i=1;i<4;i++));do
if ping -c1 $1 &>/dev/null;then
export ping_count"$i"=1
else
export ping_count"$i"=0
fi
# 时间间隔
sleep 0.5
done
# 3次ping 失败报警
if [ $ping_count1 -eq $ping_count2 ] && [ $ping_count2 -eq $ping_count3 ] && [ $ping_count1 -eq 0 ]
then
echo "$1 is down"
else
echo "$1 is up"
fi
# 取消上面定义的变量
unset ping_count1
unset ping_count2
unset ping_count3
运行脚本
语法: bash monitor_host.sh 监控IP地址
[root@shell jao]# bash monitor_host.sh 192.168.1.68
192.168.1.68 is up
[root@shell jao]# bash monitor_host.sh 192.168.1.69
192.168.1.69 is down
四、Linux统计内存、CPU使用前十进程脚本
#!/bin/bash
memory(){
#1、收集任务管理器进程信息
temp_file=$(mktemp memory.XXX)
top -b -n1 > $temp_file
# -b 显示所有top内容
# -n1 动态显示1次后退出
#2、按进程统计内存使用大小
tail -n +8 $temp_file | awk '{array[$NF]+=$6}END{for (i in array) print array[i],i}'| sort -k 1 -n -r | head -10
rm -f $temp_file
# -n +8 从第八行开始输出内容
# sort -k 表示从第几列排序 -r 倒序 -n 以数字类型排序
}
cpu(){
#1、收集任务管理器进程信息
temp_file=$(mktemp cpu.XXX)
top -b -n1 > $temp_file
#2、按进程统计内存使用大小
tail -n +8 $temp_file | awk '{array[$NF]+=$9}END{for (i in array) print array[i],i}'| sort -k 1 -n -r | head -10
rm -f $temp_file
}
echo '----------memory----------'
memory
echo '----------cpu----------'
cpu
五、Linux 磁盘I/O列长度监控脚本
#!/bin/bash
# 判断系统是否安装iostat scipt 基于iostat命令
[ ! -x /usr/bin/iostat ] && echo "iostat:command not found"&& exit 1
io(){
#匹配以sd开头的磁盘 如磁盘是vd,或其他开头 这里直接改 Device_name变量即可。
Device_name=sd
Device_length=`iostat -d -x | egrep "^$Device_name[[:alpha:]]|^dm" |wc -l`
# tail -n +K 表示从第K个开始输出
iostat -x 1 3 | egrep "^sd[[:alpha:]]|^dm" |tail -n +$((Device_length+1)) | awk '{io_long[$1]+=$9}END{for (i in io_long)print io_long[i],i}'
echo "-----"
}
for ((i=1;i<6;i++));do
io
done
六、Linux计算内存使用率占比
#!/bin/bash
MEM_TOTAL=`free -h |awk 'NR==2{print $2}'`
MEM_USE=`free -h |awk 'NR==2{print $3}'`
MEM_USED=`free | grep -i mem |awk '{print $3/$2*100"%"}'`
MEM_FREE=`free -h |awk 'NR==2{print $4}'`
MEM_FREED=`free | grep -i mem |awk '{print $4/$2*100"%"}'`
MEM_BUFF_CACHE=`free -h |awk 'NR==2{print $6}'`
MEM_BUFF_CACHED=`free | grep -i mem |awk '{print $6/$2*100 "%"}'`
MEM_SHARE=`free -h |awk 'NR==2{print $5}'`
MEM_SHARED=`free | grep -i mem |awk '{print $5/$2*100 "%"}'`
MEM_ZONE=`head -3 /proc/meminfo | awk 'NR==1{t=$2}NR==2{f=$2;print(t-f)*100/t"%"}'`
echo "内存总量: $MEM_TOTAL"
echo "内存总量减剩余内存(所有已使用)占比: $MEM_ZONE"
echo "已使用: $MEM_USE 占比: $MEM_USED"
echo "剩 余: $MEM_FREE 占比: $MEM_FREED"
echo "共 享: $MEM_SHARE 占比: $MEM_SHARED"
echo "缓冲区: $MEM_BUFF_CACHE 占比: $MEM_BUFF_CACHED"
七、一键部署wordpress博客平台
#!/bin/bash
clear
## variabled
WORDPRESS_VERSION='5.5.6'
DB_ROOT_PWD='123.com'
DB_USER='lisi'
DB_USER_PWD='123.com'
DB_NAME='blog'
## function
echo "Please wait a few minutes..."
word_yum(){
(firewall-cmd --add-port=80/tcp --permanent) &>/dev/null
(firewall-cmd --add-service=mysql --permanent;firewall-cmd --add-port=9000/tcp --permanent) &>/dev/null
firewall-cmd --reload &>/dev/null
(sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config;setenforce 0) &>/dev/null
TIME_CURRENT=$(date +%X | awk '{print $1}')
mkdir /etc/yum.repos.d/${TIME_CURRENT}.bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/${TIME_CURRENT}.bak &>/dev/null
[ ! -x /usr/bin/wget ] && echo "wget: command not found" && exit 5
if ! (wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null);then
echo "ERROR: wget CentOS-Base.repo fail"
exit 5
fi
if ! (wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &>/dev/null);then
echo "ERROR: wget epel.repo fail"
exit 5
fi
}
word_nginx_install(){
cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/\$basearch/
gpgcheck=0
enabled=1
EOF
[ ! -f /etc/yum.repos.d/nginx.repo ] && echo "ERROR: nginx.repo fail" && exit 5
yum clean all &>/dev/null
if (yum install nginx -y &>/dev/null);then
echo "nginx install success..."
if systemctl start nginx &>/dev/null;then
echo "nginx start sucess..."
if systemctl enable nginx &>/dev/null;then
echo "nginx enable success..."
else
echo "nginx enable fail"
exit 5
fi
else
echo "nginx start fail"
exit 5
fi
else
echo "nginx install fail"
exit 5
fi
}
word_php_install(){
if ! (rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm &>/dev/null);then
echo "ERROR: epel-release-latest-7.noarch.rpm fail"
fi
if ! (rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm &>/dev/null);then
echo "ERROR: webtatic-release.rpm fail"
fi
yum clean all &>/dev/null
if (yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache &>/dev/null);then
if systemctl start php-fpm &>/dev/null;then
echo "php-fpm start success..."
if systemctl enable php-fpm &>/dev/null;then
echo "php-fpm enable success..."
else
echo "ERROR: php-fpm enable fail"
exit 5
fi
else
echo "ERROR: php-fpm start fail"
exit 5
fi
else
echo "ERROR: install php fail"
exit 5
fi
}
word_mysql_install(){
if ! (rpm -ivh http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm &>/dev/null);then
echo "ERROR: repo.mysql.com fail"
fi
if (yum install mysql-community-server -y &>/dev/null);then
echo "install mysql success..."
if systemctl start mysqld &>/dev/null;then
echo "start mysql success..."
if systemctl enable mysqld &>/dev/null;then
echo "enable mysql success..."
else
echo "ERROR: enable mysqld fail"
exit 5
fi
else
echo "ERROR: start mysqld fail"
exit 5
fi
else
echo "ERROR: install mysqld fail"
exit 5
fi
mysql <<EOF
set password for root@localhost=password('$DB_ROOT_PWD')
EOF
mysql -uroot -p$DB_ROOT_PWD -e "create database $DB_NAME" &>/dev/null
mysql -uroot -p$DB_ROOT_PWD -e "grant all on $DB_NAME.* to $DB_USER@localhost identified by '$DB_USER_PWD'" &>/dev/null
}
word_install(){
if ! wget https://cn.wordpress.org/wordpress-$WORDPRESS_VERSION-zh_CN.tar.gz &>/dev/null;then
echo "ERROR: install wordpress fail"
exit 5
fi
tar zxf wordpress-$WORDPRESS_VERSION-zh_CN.tar.gz -C / &>/dev/null
chmod -R 777 /wordpress/ &>/dev/null
}
nginx_end(){
cat > /etc/nginx/conf.d/blog.conf <<EOF
server {
listen 80;
server_name blog.benet.com;
root /wordpress;
index index.php index.html;
location ~ \.php$ {
root /wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
systemctl reload nginx &>/dev/null
}
echo_t1(){
clear
echo -e "\e[1;36m 数据库root用户密码:${DB_ROOT_PWD}\e[0m"
echo -e "\e[1;36m 博客管理员:${DB_USER} 密码:${DB_USER_PWD}\e[0m"
echo -e "\e[1;36m 博客数据库名字:${DB_NAME}\e[0m"
echo
echo -e "\e[1;36m http://本地IP地址 进行访问博客首页\e[0m"
}
word_yum
word_nginx_install
word_php_install
word_mysql_install
word_install
nginx_end
echo_t1