From d4f5108ee11fda4c6393f204f20957faced24a96 Mon Sep 17 00:00:00 2001 From: xuel Date: Wed, 9 Jan 2019 18:53:29 +0800 Subject: [PATCH] add some scripts file --- 脚本实用工具/Autoinstall_ELK_V1.3.sh | 164 +++++++++++++++ 脚本实用工具/Cpu_Limit.sh | 53 +++++ 脚本实用工具/Custom_Rm.sh | 26 +++ 脚本实用工具/Sys_Check.sh | 225 +++++++++++++++++++++ 4 files changed, 468 insertions(+) create mode 100644 脚本实用工具/Autoinstall_ELK_V1.3.sh create mode 100644 脚本实用工具/Cpu_Limit.sh create mode 100644 脚本实用工具/Custom_Rm.sh create mode 100644 脚本实用工具/Sys_Check.sh diff --git a/脚本实用工具/Autoinstall_ELK_V1.3.sh b/脚本实用工具/Autoinstall_ELK_V1.3.sh new file mode 100644 index 0000000..4d1f583 --- /dev/null +++ b/脚本实用工具/Autoinstall_ELK_V1.3.sh @@ -0,0 +1,164 @@ +#!/bin/bash +#mail:xuel@anchnet.com +#data:2017/9/7 +#AutoInstall ELK scripts +#Software:elasticsearch-5.4.1/logstash-5.4.1/filebeat-5.4.1/kibana-5.4.1 +clear +echo "##########################################" +echo "# Auto Install ELK. ##" +echo "# Press Ctrl + C to cancel ##" +echo "# Any key to continue ##" +echo "##########################################" +read -p +software_dir="/usr/local/software" +elasticsearch_url="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz" +kibana_url="https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz" +logstash_url="https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz" +filebeat_url="https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz" +sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1` +IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1` +jvm_conf="/usr/local/elasticsearch/config/jvm.options" +sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1` + +#wget software +wget_fun() { +if [ ! -d ${software_dir} ];then + mkdir -p ${software_dir} && cd ${software_dir} +else + cd ${software_dir} +fi +for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url +do + wget -c $software +done +clear +} +#initial system:install java wget;set hostname;disable firewalld +init_sys() { +[ -f /etc/init.d/functions ] && . /etc/init.d/functions +[ "${sys_version}" != "7" ] && echo "Error:This Scripts Support Centos7.xx" && exit 1 +[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1 +sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config +setenforce 0 +yum install -y java-1.8.0-openjdk wget net-tools +hostnamectl set-hostname elk-server +systemctl stop firewalld +cat >>/etc/security/limits.conf<>/etc/sysctl.conf && sysctl -p +if [ ${sys_mem} -eq 0 ];then + sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf} + sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf} +else + sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf} + sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf} +fi +cat >>/usr/local/elasticsearch/config/elasticsearch.yml</usr/local/logstash/config/01-syslog.conf< "5044" + } + } +output { + elasticsearch { + hosts => "127.0.0.1:9200" + } + stdout { codec => rubydebug } +} +EOF +nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/config/01-syslog.conf & >/dev/null +} + +#install filebeat +install_filebeat() { +cd $software_dir +tar -zxf filebeat-5.4.1-linux-x86_64.tar.gz +mv filebeat-5.4.1-linux-x86_64 /usr/local/filebeat +cat >/usr/local/filebeat/filebeat.yml</dev/null +} + +#install kibana +install_kibana() { +cd $software_dir +tar -zxf kibana-5.4.1-linux-x86_64.tar.gz +mv kibana-5.4.1-linux-x86_64 /usr/local/kibana +cat >> /usr/local/kibana/config/kibana.yml </dev/null +} + +check() { +port=$1 +program=$2 +check_port=`netstat -lntup|grep ${port}|wc -l` +check_program=`ps -ef|grep ${program}|grep -v grep|wc -l` +if [ $check_port -gt 0 ] && [ $check_program -gt 0 ];then + action "${program} run is ok!" /bin/true +else + action "${program} run is error!" /bin/false +fi +} + +main() { +init_sys +wget_fun +install_elasticsearch +install_filebeat +install_logstash +install_kibana +echo -e "\033[32m Checking Elasticsearch...\033[0m" +sleep 20 +check :9200 "elasticsearch" +echo -e "\033[32m Checking Logstash...\033[0m" +sleep 2 +check ":9600" "logstash" +echo -e "\033[32m Checking Kibana...\033[0m" +sleep 2 +check ":5601" "kibana" +action "ELK install is success!" /bin/true +echo "url:http://$IP:5601" +} +main diff --git a/脚本实用工具/Cpu_Limit.sh b/脚本实用工具/Cpu_Limit.sh new file mode 100644 index 0000000..1d959da --- /dev/null +++ b/脚本实用工具/Cpu_Limit.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# auth:kaliarch +# func:sys info check +# version:v1.0 +# sys:centos6.x/7.x + +set -e +[ $(id -u) -gt 0 ] && exit 1 + +# cpu使用超过百分之多少进行限制 +PEC_CPU=80 + +# 限制进程使用百分之多少,如果程序为多线程,单个cpu限制为85,如果为多核心,就需要按照比例写,例如cpu为2c,像限制多线程占比80%,就写170 +LIMIT_CPU=85 +# 日志 +LOG_DIR=/var/log/cpulimit/ + +# 超过阀值进程pid +PIDARG=$(ps -aux |awk -v CPU=${PEC_CPU} '{if($3 > CPU) print $2}') +CPULIMITCMD=$(which cpulimit) + +install_cpulimit() { + [ ! -d /tmp ] && mkdir /tmp || cd /tmp + wget -c https://github.com/opsengine/cpulimit/archive/v0.2.tar.gz + tar -zxf v0.2.tar.gz + cd cpulimit-0.2 && make + [ $? -eq 0 ] && cp src/cpulimit /usr/bin/ +} + + +do_cpulimit() { +[ ! -d ${LOG_DIR} ] && mkdir -p ${LOG_DIR} +for i in ${PIDARG}; +do + MSG=$(ps -aux |awk -v pid=$i '{if($2 == pid) print $0}') + echo ${MSG} + [ ! -d /tmp ] && mkdir /tmp || cd /tmp + nohup ${CPULIMITCMD} -p $i -l ${LIMIT_CPU} & + echo "$(date) -- ${MSG}" >> ${LOG_DIR}$(date +%F).log +done +} + +main() { + + hash cpulimit + if [ $? -eq 0 ];then + do_cpulimit + else + install_cpulimit && do_cpulimit + fi +} + +main diff --git a/脚本实用工具/Custom_Rm.sh b/脚本实用工具/Custom_Rm.sh new file mode 100644 index 0000000..75a06d3 --- /dev/null +++ b/脚本实用工具/Custom_Rm.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# function:自定义rm命令,每天晚上定时清理 + +CMD_SCRIPTS=$HOME/.rm_scripts.sh +TRASH_DIR=$HOME/.TRASH_DIR +CRON_FILE=/var/spool/cron/root +BASHRC=$HOME/.bashrc + +[ ! -d ${TRASH_DIR} ] && mkdir -p ${TRASH_DIR} +cat > $CMD_SCRIPTS <> $CRON_FILE +echo "删除目录:$TRASH_DIR" +echo "删除脚本:$CMD_SCRIPTS" +echo "请执行:source $BASHRC 来加载文件或退出当前shell重新登录" diff --git a/脚本实用工具/Sys_Check.sh b/脚本实用工具/Sys_Check.sh new file mode 100644 index 0000000..d94b3eb --- /dev/null +++ b/脚本实用工具/Sys_Check.sh @@ -0,0 +1,225 @@ +#!/bin/bash +# auth:kaliarch +# func:sys info check +# version:v1.0 + +[ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1 +sysversion=$(rpm -q centos-release|cut -d- -f3) +line="-------------------------------------------------" + + +[ -d logs ] || mkdir logs + +sys_check_file="logs/$(ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}')-`date +%Y%m%d`.txt" + +# 获取系统cpu信息 +function get_cpu_info() { + Physical_CPUs=$(grep "physical id" /proc/cpuinfo| sort | uniq | wc -l) + Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l) + CPU_Kernels=$(grep "cores" /proc/cpuinfo|uniq| awk -F ': ' '{print $2}') + CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq) + CPU_Arch=$(uname -m) +cat </dev/null) + if [ ${sysversion} -gt 6 ];then + service_config=$(systemctl list-unit-files --type=service --state=enabled|grep "enabled") + run_service=$(systemctl list-units --type=service --state=running |grep ".service") + else + service_config=$(/sbin/chkconfig | grep -E ":on|:启用" |column -t) + run_service=$(/sbin/service --status-all|grep -E "running") + fi +cat </dev/null|cut -d/ -f5;egrep -v "^$|^#" ${cronuser} 2>/dev/null;echo "";done) +cat < ${sys_check_file}