Skip to content

Commit 732733f

Browse files
authored
Merge pull request fengyuhetao#4 from redhatxl/master
add some scripts file
2 parents a63a457 + d4f5108 commit 732733f

File tree

4 files changed

+468
-0
lines changed

4 files changed

+468
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/bin/bash
2+
3+
#data:2017/9/7
4+
#AutoInstall ELK scripts
5+
#Software:elasticsearch-5.4.1/logstash-5.4.1/filebeat-5.4.1/kibana-5.4.1
6+
clear
7+
echo "##########################################"
8+
echo "# Auto Install ELK. ##"
9+
echo "# Press Ctrl + C to cancel ##"
10+
echo "# Any key to continue ##"
11+
echo "##########################################"
12+
read -p
13+
software_dir="/usr/local/software"
14+
elasticsearch_url="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz"
15+
kibana_url="https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz"
16+
logstash_url="https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz"
17+
filebeat_url="https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz"
18+
sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1`
19+
IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1`
20+
jvm_conf="/usr/local/elasticsearch/config/jvm.options"
21+
sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1`
22+
23+
#wget software
24+
wget_fun() {
25+
if [ ! -d ${software_dir} ];then
26+
mkdir -p ${software_dir} && cd ${software_dir}
27+
else
28+
cd ${software_dir}
29+
fi
30+
for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url
31+
do
32+
wget -c $software
33+
done
34+
clear
35+
}
36+
#initial system:install java wget;set hostname;disable firewalld
37+
init_sys() {
38+
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
39+
[ "${sys_version}" != "7" ] && echo "Error:This Scripts Support Centos7.xx" && exit 1
40+
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
41+
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
42+
setenforce 0
43+
yum install -y java-1.8.0-openjdk wget net-tools
44+
hostnamectl set-hostname elk-server
45+
systemctl stop firewalld
46+
cat >>/etc/security/limits.conf<<EOF
47+
* soft nofile 65536
48+
* hard nofile 65536
49+
* soft nGproc 65536
50+
* hard nproc 65536
51+
EOF
52+
}
53+
54+
#install elasticsearch
55+
install_elasticsearch() {
56+
cd $software_dir
57+
tar zxf elasticsearch-5.4.1.tar.gz
58+
mv elasticsearch-5.4.1 /usr/local/elasticsearch
59+
mkdir -p /usr/local/elasticsearch/data /usr/local/elasticsearch/logs
60+
useradd elasticsearch
61+
chown -R elasticsearch:elasticsearch /usr/local/elasticsearch
62+
echo "vm.max_map_count = 655360" >>/etc/sysctl.conf && sysctl -p
63+
if [ ${sys_mem} -eq 0 ];then
64+
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf}
65+
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf}
66+
else
67+
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf}
68+
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf}
69+
fi
70+
cat >>/usr/local/elasticsearch/config/elasticsearch.yml<<EOF
71+
cluster.name: my-application
72+
node.name: elk-server
73+
path.data: /usr/local/elasticsearch/data
74+
path.logs: /usr/local/elasticsearch/logs
75+
network.host: 127.0.0.1
76+
http.port: 9200
77+
discovery.zen.ping.unicast.hosts: ["elk-server"]
78+
EOF
79+
su - elasticsearch -c "nohup /usr/local/elasticsearch/bin/elasticsearch &"
80+
}
81+
82+
#install logstash
83+
install_logstash() {
84+
cd $software_dir
85+
tar -zxf logstash-5.4.1.tar.gz
86+
mv logstash-5.4.1 /usr/local/logstash
87+
cat>/usr/local/logstash/config/01-syslog.conf<<EOF
88+
input {
89+
beats {
90+
port => "5044"
91+
}
92+
}
93+
output {
94+
elasticsearch {
95+
hosts => "127.0.0.1:9200"
96+
}
97+
stdout { codec => rubydebug }
98+
}
99+
EOF
100+
nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/config/01-syslog.conf & >/dev/null
101+
}
102+
103+
#install filebeat
104+
install_filebeat() {
105+
cd $software_dir
106+
tar -zxf filebeat-5.4.1-linux-x86_64.tar.gz
107+
mv filebeat-5.4.1-linux-x86_64 /usr/local/filebeat
108+
cat >/usr/local/filebeat/filebeat.yml<<EOF
109+
filebeat.prospectors:
110+
- input_type: log
111+
paths:
112+
- /var/log/*.log
113+
output.logstash:
114+
hosts: ["127.0.0.1:5044"]
115+
EOF
116+
cd /usr/local/filebeat/
117+
nohup /usr/local/filebeat/filebeat & >/dev/null
118+
}
119+
120+
#install kibana
121+
install_kibana() {
122+
cd $software_dir
123+
tar -zxf kibana-5.4.1-linux-x86_64.tar.gz
124+
mv kibana-5.4.1-linux-x86_64 /usr/local/kibana
125+
cat >> /usr/local/kibana/config/kibana.yml <<EOF
126+
server.port: 5601
127+
server.host: "0.0.0.0"
128+
elasticsearch.url: "http://127.0.0.1:9200"
129+
EOF
130+
nohup /usr/local/kibana/bin/kibana & >/dev/null
131+
}
132+
133+
check() {
134+
port=$1
135+
program=$2
136+
check_port=`netstat -lntup|grep ${port}|wc -l`
137+
check_program=`ps -ef|grep ${program}|grep -v grep|wc -l`
138+
if [ $check_port -gt 0 ] && [ $check_program -gt 0 ];then
139+
action "${program} run is ok!" /bin/true
140+
else
141+
action "${program} run is error!" /bin/false
142+
fi
143+
}
144+
145+
main() {
146+
init_sys
147+
wget_fun
148+
install_elasticsearch
149+
install_filebeat
150+
install_logstash
151+
install_kibana
152+
echo -e "\033[32m Checking Elasticsearch...\033[0m"
153+
sleep 20
154+
check :9200 "elasticsearch"
155+
echo -e "\033[32m Checking Logstash...\033[0m"
156+
sleep 2
157+
check ":9600" "logstash"
158+
echo -e "\033[32m Checking Kibana...\033[0m"
159+
sleep 2
160+
check ":5601" "kibana"
161+
action "ELK install is success!" /bin/true
162+
echo "url:http://$IP:5601"
163+
}
164+
main

脚本实用工具/Cpu_Limit.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
# auth:kaliarch
3+
# func:sys info check
4+
# version:v1.0
5+
# sys:centos6.x/7.x
6+
7+
set -e
8+
[ $(id -u) -gt 0 ] && exit 1
9+
10+
# cpu使用超过百分之多少进行限制
11+
PEC_CPU=80
12+
13+
# 限制进程使用百分之多少,如果程序为多线程,单个cpu限制为85,如果为多核心,就需要按照比例写,例如cpu为2c,像限制多线程占比80%,就写170
14+
LIMIT_CPU=85
15+
# 日志
16+
LOG_DIR=/var/log/cpulimit/
17+
18+
# 超过阀值进程pid
19+
PIDARG=$(ps -aux |awk -v CPU=${PEC_CPU} '{if($3 > CPU) print $2}')
20+
CPULIMITCMD=$(which cpulimit)
21+
22+
install_cpulimit() {
23+
[ ! -d /tmp ] && mkdir /tmp || cd /tmp
24+
wget -c https://github.com/opsengine/cpulimit/archive/v0.2.tar.gz
25+
tar -zxf v0.2.tar.gz
26+
cd cpulimit-0.2 && make
27+
[ $? -eq 0 ] && cp src/cpulimit /usr/bin/
28+
}
29+
30+
31+
do_cpulimit() {
32+
[ ! -d ${LOG_DIR} ] && mkdir -p ${LOG_DIR}
33+
for i in ${PIDARG};
34+
do
35+
MSG=$(ps -aux |awk -v pid=$i '{if($2 == pid) print $0}')
36+
echo ${MSG}
37+
[ ! -d /tmp ] && mkdir /tmp || cd /tmp
38+
nohup ${CPULIMITCMD} -p $i -l ${LIMIT_CPU} &
39+
echo "$(date) -- ${MSG}" >> ${LOG_DIR}$(date +%F).log
40+
done
41+
}
42+
43+
main() {
44+
45+
hash cpulimit
46+
if [ $? -eq 0 ];then
47+
do_cpulimit
48+
else
49+
install_cpulimit && do_cpulimit
50+
fi
51+
}
52+
53+
main

脚本实用工具/Custom_Rm.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
# function:自定义rm命令,每天晚上定时清理
3+
4+
CMD_SCRIPTS=$HOME/.rm_scripts.sh
5+
TRASH_DIR=$HOME/.TRASH_DIR
6+
CRON_FILE=/var/spool/cron/root
7+
BASHRC=$HOME/.bashrc
8+
9+
[ ! -d ${TRASH_DIR} ] && mkdir -p ${TRASH_DIR}
10+
cat > $CMD_SCRIPTS <<EOF
11+
PARA_CNT=\$#
12+
TRASH_DIR=$TRASH_DIR
13+
for i in \$*; do
14+
DATE=\$(date +%F%T)
15+
fileName=\$(basename \$i)
16+
mv \$i \$TRASH_DIR/\$fileName.\$DATE
17+
done
18+
EOF
19+
20+
sed -i "s@$(grep 'alias rm=' $BASHRC)@alias rm='bash ${CMD_SCRIPTS}'@g" $BASHRC
21+
source $HOME/.bashrc
22+
23+
echo "0 0 * * * rm -rf $TRASH_DIR/*" >> $CRON_FILE
24+
echo "删除目录:$TRASH_DIR"
25+
echo "删除脚本:$CMD_SCRIPTS"
26+
echo "请执行:source $BASHRC 来加载文件或退出当前shell重新登录"

0 commit comments

Comments
 (0)