summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfangshunjian <[email protected]>2022-07-27 18:43:08 +0800
committerfangshunjian <[email protected]>2022-07-27 18:43:08 +0800
commit6a08491c1dcaad065b4930eaa675411cf834538a (patch)
tree4e7c4fbb4a38e9c887b8acd4f829f1fbee024a25
parentb83fcd03c833ea4cb0333854281c53811f9cc557 (diff)
build: 更新rpm构建脚本rel-22.07.01
1、安装完成后重启所有组件
-rw-r--r--tools/afterinstall.sh222
-rw-r--r--tools/afterremove.sh4
-rw-r--r--tools/beforeinstall.sh32
3 files changed, 160 insertions, 98 deletions
diff --git a/tools/afterinstall.sh b/tools/afterinstall.sh
index 0bdf7b3..cf45b8e 100644
--- a/tools/afterinstall.sh
+++ b/tools/afterinstall.sh
@@ -1,21 +1,7 @@
-#!/bin/sh
+#!/bin/bash
-# Find Java
-if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
- JAVA_EXE="$JAVA_HOME/bin/java"
-elif type -p java > /dev/null 2>&1; then
- JAVA_EXE=$(type -p java)
-elif [[ -x "/usr/bin/java" ]]; then
- JAVA_EXE="/usr/bin/java"
-else
- echo "Unable to find Java"
- JAVA_EXE="java"
-# exit 1
-fi
-
-# install
-if [ 1 -eq $1 ];then
+fn_init_nz_agent_service(){
cat > /usr/lib/systemd/system/nz-agent.service <<EOF
[Unit]
Description=nz-agent
@@ -28,8 +14,10 @@ Restart=always
[Install]
WantedBy=multi-user.target
EOF
+}
- cat > /usr/lib/systemd/system/prometheus.service <<"EOF"
+fn_init_prometheus_service(){
+ cat > /usr/lib/systemd/system/prometheus.service <<EOF
[Unit]
Description=prometheus
After=network.target
@@ -43,10 +31,10 @@ LimitNOFILE=524288
[Install]
WantedBy=multi-user.target
EOF
- cat > /opt/nezha/nz-agent/prometheus/config.conf <<EOF
-OPTION=" --config.file='/opt/nezha/nz-agent/prometheus/prometheus.yml' --web.listen-address='127.0.0.1:10091' --storage.tsdb.path='/opt/nezha/data' --web.enable-admin-api --web.enable-lifecycle --storage.tsdb.retention.time=15d "
-EOF
- cat > /usr/lib/systemd/system/snmp-exporter.service <<"EOF"
+}
+
+fn_init_snmp_exporter_service(){
+ cat > /usr/lib/systemd/system/snmp-exporter.service <<EOF
[Unit]
Description=snmp-exporter
After=network.target
@@ -59,12 +47,10 @@ LimitNOFILE=524288
[Install]
WantedBy=multi-user.target
EOF
- cat >/opt/nezha/nz-agent/snmp_exporter/config.conf <<EOF
-OPTION="--web.listen-address='127.0.0.1:19116' --config.file='/opt/nezha/nz-agent/snmp_exporter/snmp.yml' "
-EOF
-
+}
- cat > /usr/lib/systemd/system/blackbox-exporter.service <<"EOF"
+fn_init_blackbox_exporter_service(){
+ cat > /usr/lib/systemd/system/blackbox-exporter.service <<EOF
[Unit]
Description=blackbox_exporter
After=network.target
@@ -78,12 +64,10 @@ LimitNOFILE=524288
[Install]
WantedBy=multi-user.target
EOF
- cat >/opt/nezha/nz-agent/blackbox_exporter/config.conf <<EOF
-OPTION="--web.listen-address='127.0.0.1:19115' --config.file='/opt/nezha/nz-agent/blackbox_exporter/blackbox.yml' "
-EOF
+}
- chmod 755 /opt/nezha/nz-agent/cortex/cortex-linux-amd64
- cat > /usr/lib/systemd/system/cortex.service <<"EOF"
+fn_init_cortex_service(){
+ cat > /usr/lib/systemd/system/cortex.service <<EOF
[Unit]
Description=cortex
After=network.target
@@ -97,10 +81,9 @@ LimitNOFILE=524288
[Install]
WantedBy=multi-user.target
EOF
- cat >/opt/nezha/nz-agent/cortex/config.conf <<EOF
-OPTION=" -config.file=/opt/nezha/nz-agent/cortex/cortex.yml"
-EOF
- chmod 755 /opt/nezha/nz-agent/loki/loki-linux-amd64
+}
+
+fn_init_loki_service(){
cat > /usr/lib/systemd/system/loki.service <<"EOF"
[Unit]
Description=loki
@@ -115,76 +98,149 @@ LimitNOFILE=524288
[Install]
WantedBy=multi-user.target
EOF
- cat >/opt/nezha/nz-agent/loki/config.conf <<EOF
-OPTION=" -config.file=/opt/nezha/nz-agent/loki/loki.yml"
-EOF
- systemctl daemon-reload
- systemctl enable nz-agent && systemctl restart nz-agent
- systemctl enable prometheus && systemctl restart prometheus
- systemctl enable snmp-exporter && systemctl restart snmp-exporter
- systemctl enable blackbox-exporter && systemctl restart blackbox-exporter
- systemctl enable cortex && systemctl restart cortex
- systemctl enable loki && systemctl restart loki
- echo 'install nz-agent success !'
-fi
-compareMD5()
-{
- if [ ! -f $1 ] || [ ! -f $2 ];then
- echo 1
- return 1
- fi
-
- local f1MD5=`md5sum $1|awk '{print $1}'`
- local f2MD5=`md5sum $2|awk '{print $1}'`
-
- if [ ${f1MD5} = ${f2MD5} ];then
- echo 0
- return 0
- else
- echo 1
- return 1
- fi
}
-# update
-if [ 2 -eq $1 ];then
+fn_init_config(){
+ # nz-agent component
+ chmod +x /opt/nezha/nz-agent/xjar
+
+ # prometheus component
cat > /opt/nezha/nz-agent/prometheus/config.conf <<EOF
OPTION=" --config.file='/opt/nezha/nz-agent/prometheus/prometheus.yml' --web.listen-address='127.0.0.1:10091' --storage.tsdb.path='/opt/nezha/data' --web.enable-admin-api --web.enable-lifecycle --storage.tsdb.retention.time=15d "
EOF
+ # snmp-exporter component
cat >/opt/nezha/nz-agent/snmp_exporter/config.conf <<EOF
OPTION="--web.listen-address='127.0.0.1:19116' --config.file='/opt/nezha/nz-agent/snmp_exporter/snmp.yml' "
EOF
+ # blackbox-exporter component
cat >/opt/nezha/nz-agent/blackbox_exporter/config.conf <<EOF
OPTION="--web.listen-address='127.0.0.1:19115' --config.file='/opt/nezha/nz-agent/blackbox_exporter/blackbox.yml' "
EOF
- chmod 755 /opt/nezha/nz-agent/cortex/cortex-linux-amd64
+ # cortex component
+ chmod +x /opt/nezha/nz-agent/cortex/cortex-linux-amd64
cat >/opt/nezha/nz-agent/cortex/config.conf <<EOF
OPTION=" -config.file=/opt/nezha/nz-agent/cortex/cortex.yml"
EOF
- chmod 755 /opt/nezha/nz-agent/loki/loki-linux-amd64
+ # loki component
+ chmod +x /opt/nezha/nz-agent/loki/loki-linux-amd64
cat >/opt/nezha/nz-agent/loki/config.conf <<EOF
OPTION=" -config.file=/opt/nezha/nz-agent/loki/loki.yml"
EOF
+}
+compareMD5(){
+ if [ ! -f $1 ] || [ ! -f $2 ];then
+ echo 1
+ return 1
+ fi
+
+ local f1MD5=`md5sum $1|awk '{print $1}'`
+ local f2MD5=`md5sum $2|awk '{print $1}'`
+
+ if [ ${f1MD5} = ${f2MD5} ];then
+ echo 0
+ return 0
+ else
+ echo 1
+ return 1
+ fi
+}
+
+# Find Java
+if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
+ JAVA_EXE="$JAVA_HOME/bin/java"
+elif type -p java > /dev/null 2>&1; then
+ JAVA_EXE=$(type -p java)
+elif [[ -x "/usr/bin/java" ]]; then
+ JAVA_EXE="/usr/bin/java"
+else
+ echo "Unable to find Java"
+ JAVA_EXE="java"
+# exit 1
+fi
+
+# install
+if [ 1 -eq $1 ];then
+ fn_init_config
+ fn_init_nz_agent_service
+ fn_init_prometheus_service
+ fn_init_snmp_exporter_service
+ fn_init_blackbox_exporter_service
+ fn_init_cortex_service
+ fn_init_loki_service
+
+ systemctl daemon-reload
+ systemctl enable nz-agent && systemctl restart nz-agent
+ systemctl enable prometheus && systemctl restart prometheus
+ systemctl enable snmp-exporter && systemctl restart snmp-exporter
+ systemctl enable blackbox-exporter && systemctl restart blackbox-exporter
+ systemctl enable cortex && systemctl restart cortex
+ systemctl enable loki && systemctl restart loki
+ echo 'install nz-agent success !'
+fi
+
+# update
+if [ 2 -eq $1 ];then
+ fn_init_config
+ # nz-agent component
+ if [ ! -f "/usr/lib/systemd/system/nz-agent.service" ];then
+ fn_init_nz_agent_service
+ fi
+
+ # prometheus component
+ if [ ! -f "/usr/lib/systemd/system/prometheus.service" ];then
+ fn_init_prometheus_service
+ fi
+
+ # snmp-exporter component
+ if [ ! -f "/usr/lib/systemd/system/snmp-exporter.service" ];then
+ fn_init_snmp_exporter_service
+ fi
+
+ # blackbox-exporter component
+ if [ ! -f "/usr/lib/systemd/system/blackbox-exporter.service" ];then
+ fn_init_blackbox_exporter_service
+ fi
+
+ # cortex component
+ if [ ! -f "/usr/lib/systemd/system/cortex.service" ];then
+ fn_init_cortex_service
+ fi
+
+ # cortex component
+ if [ ! -f "/usr/lib/systemd/system/loki.service" ];then
+ fn_init_loki_service
+ fi
+
+ # restore nz-agent config
+ AGENT_PATH=/opt/nezha/nz-agent
+ TMP_PATH=/tmp/nezha/nz-agent
+ echo 'move backup config file...'
+ if [ -d $TMP_PATH ];then
+ for i in $(ls $AGENT_PATH/config);do
+ if [ 1 -eq `compareMD5 $AGENT_PATH/config/$i $TMP_PATH/config/$i` ];then
+ echo 'return config file '${i}
+ #mv -f $AGENT_PATH/config/$i $AGENT_PATH/config/$i
+ cp -f $TMP_PATH/config/$i $AGENT_PATH/config/$i
+ #cat $AGENT_PATH/config/token.auth'.oldconfig' > $AGENT_PATH/config/token.auth && rm -f $AGENT_PATH/config/token.auth'.oldconfig'
+ fi
+ done
+ fi
- AGENT_PATH=/opt/nezha/nz-agent
-
- TMP_PATH=/tmp/nezha/nz-agent
- echo 'move backup config file...'
- if [ -d $TMP_PATH ];then
- for i in $(ls $AGENT_PATH/config);do
- if [ 1 -eq `compareMD5 $AGENT_PATH/config/$i $TMP_PATH/config/$i` ];then
- echo 'return config file '${i}
- #mv -f $AGENT_PATH/config/$i $AGENT_PATH/config/$i
- cp -f $TMP_PATH/config/$i $AGENT_PATH/config/$i
- #cat $AGENT_PATH/config/token.auth'.oldconfig' > $AGENT_PATH/config/token.auth && rm -f $AGENT_PATH/config/token.auth'.oldconfig'
- fi
- done
- fi
- systemctl restart nz-agent
+ # restart all components
+ systemctl daemon-reload
+ systemctl enable nz-agent && systemctl restart nz-agent
+ systemctl enable prometheus && systemctl restart prometheus
+ systemctl enable snmp-exporter && systemctl restart snmp-exporter
+ systemctl enable blackbox-exporter && systemctl restart blackbox-exporter
+ systemctl enable cortex && systemctl restart cortex
+ systemctl enable loki && systemctl restart loki
+ echo 'update nz-agent success !'
fi
+
+
diff --git a/tools/afterremove.sh b/tools/afterremove.sh
index b725742..1eaa14c 100644
--- a/tools/afterremove.sh
+++ b/tools/afterremove.sh
@@ -1,7 +1,7 @@
-#!/bin/sh
+#!/bin/bash
if [ 0 -eq $1 ];then
- echo 'start remove nz-agent module from disk...'
+ echo 'start remove nz-agent from disk...'
systemctl stop nz-agent.service
systemctl stop prometheus.service
diff --git a/tools/beforeinstall.sh b/tools/beforeinstall.sh
index cd6a80f..08d9795 100644
--- a/tools/beforeinstall.sh
+++ b/tools/beforeinstall.sh
@@ -1,24 +1,30 @@
#!/bin/bash
+# 安装目录
AGENT_PATH=/opt/nezha/nz-agent
+# 临时目录
+TMP_PATH=/tmp/nezha/nz-agent
-if [ 2 -eq $1 ];then
-
- if [ ! -d ${AGENT_PATH}/config ];then
- exit 0
- fi
- systemctl stop nz-agent
- TMP_PATH=/tmp/nezha/nz-agent
+# 备份配置
+if [ -d $AGENT_PATH/config ];then
rm -rf $TMP_PATH
-
mkdir -p $TMP_PATH
cp -rf $AGENT_PATH/config $TMP_PATH
echo 'backup config file from '${AGENT_PATH}
-
- if [ 0 -lt $(rpm -aq nz-agent|wc -w) ];then
- echo 'clean before install...'
- rm -rf $AGENT_PATH
- fi
fi
+# 停止服务并卸载
+if [ 0 -lt $(rpm -aq nz-agent|wc -w) ];then
+ echo "stop nz-agent service"
+ systemctl stop nz-agent
+ systemctl stop prometheus
+ systemctl stop snmp-exporter
+ systemctl stop blackbox-exporter
+ systemctl stop cortex
+ systemctl stop loki
+ echo 'clean before install...'
+ rm -rf $AGENT_PATH
+fi
+
+