This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nezha-nz-talon/tools/afterinstall.sh

254 lines
6.2 KiB
Bash

#!/bin/sh
WORK_DIR=/opt/nezha/nz-talon
service_exists(){
local n=$1
if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | sed 's/^\s*//g' | cut -f1 -d' ') == $n.service ]]; then
return 0
else
return 1
fi
}
# 初始化 rsyslog & logrotate 配置
fn_init_syslog_logrotate_config(){
# promtail
cat > /etc/rsyslog.d/promtail.conf << "EOF"
if $programname == 'promtail' then {
/var/log/nezha/promtail/promtail.log
stop
}
EOF
cat > /etc/logrotate.d/promtail << "EOF"
/var/log/nezha/promtail/*.log {
daily
missingok
maxsize 100M
rotate 7
copytruncate
compress
}
EOF
# telegraf
cat > /etc/rsyslog.d/telegraf.conf << "EOF"
if $programname == 'telegraf' then {
/var/log/nezha/telegraf/telegraf.log
stop
}
EOF
cat > /etc/logrotate.d/telegraf << "EOF"
/var/log/nezha/telegraf/*.log {
daily
missingok
maxsize 100M
rotate 7
copytruncate
compress
}
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
}
# 还原配置文件
# $1 需要还原的文件目录 /opt/nezha/nz-talon/config /opt/nezha/nz-talon/promtail ...
# $2 备份文件目录 /tmp/nezha/nz-talon/config /tmp/nezha/nz-talon/promtail ...
restoreComponentConfig(){
if [ ! -d $1 ] || [ ! -d $2 ];then
echo "Directory does not exist"
return 1
fi
for i in $(ls $2);do
if [ 1 -eq `compareMD5 $1/$i $2/$i` ];then
echo 'return config file '$1/${i}
cp -f $2/$i $1/$i
fi
done
}
# Find Java
if [[ -x "$WORK_DIR/jdk/bin/java" ]]; then
JAVA_EXE="$WORK_DIR/jdk/bin/java"
elif [[ -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
cat > /usr/lib/systemd/system/nz-talon.service <<EOF
[Unit]
Description=nz-talon
After=network.target
[Service]
WorkingDirectory=/opt/nezha/nz-talon
EnvironmentFile=-/opt/nezha/nz-talon/config/config.conf
ExecStart=/opt/nezha/nz-talon/xjar ${JAVA_EXE} --add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/jdk.internal.loader=ALL-UNNAMED -Dnz-agent.dir=/opt/nezha/nz-talon -jar /opt/nezha/nz-talon/nz-talon.xjar
RestartSec=5s
Restart=always
[Install]
WantedBy=multi-user.target
EOF
cat > /usr/lib/systemd/system/nz-promtail.service <<"EOF"
[Unit]
Description=nz-talon
After=network.target
[Service]
WorkingDirectory=/opt/nezha/promtail
EnvironmentFile=/opt/nezha/promtail/config.conf
ExecStart=/opt/nezha/promtail/promtail $OPTION
RestartSec=5s
Restart=always
[Install]
WantedBy=multi-user.target
EOF
cat > /usr/lib/systemd/system/nz-telegraf.service <<"EOF"
[Unit]
Description=Telegraf
Documentation=https://github.com/influxdata/telegraf
After=network-online.target
Wants=network-online.target
[Service]
WorkingDirectory=/opt/nezha/telegraf
Type=notify
EnvironmentFile=/opt/nezha/telegraf/config.conf
ExecStart=/opt/nezha/telegraf/telegraf $OPTION
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartForceExitStatus=SIGPIPE
KillMode=control-group
LimitMEMLOCK=8M:8M
[Install]
WantedBy=multi-user.target
EOF
fn_init_syslog_logrotate_config
systemctl daemon-reload
systemctl enable nz-talon && systemctl restart nz-talon
systemctl enable nz-promtail && systemctl restart nz-promtail
systemctl enable nz-telegraf && systemctl restart nz-telegraf
systemctl enable crond && systemctl restart crond
systemctl enable rsyslog && systemctl restart rsyslog
echo 'install nz-talon success !'
fi
# update
if [ 2 -eq $1 ];then
TALON_PATH=/opt/nezha/nz-talon
TMP_PATH=/tmp/nezha/nz-talon
PROMTAIL_PATH=/opt/nezha/promtail
TELEGRAF_PATH=/opt/nezha/telegraf
echo 'move backup config file...'
if [ -d $TMP_PATH ];then
# nz-talon config
restoreComponentConfig $TALON_PATH/config $TMP_PATH/config
# promtail.service 名称改为 nz-promtail.service
# 这里重新替换下 application-prod.yml 中的 systemctl start|stop promtail 命令
sed -i 's/systemctl start promtail/systemctl start nz-promtail/g' $TALON_PATH/config/application-prod.yml
sed -i 's/systemctl stop promtail/systemctl stop nz-promtail/g' $TALON_PATH/config/application-prod.yml
# promtail config
restoreComponentConfig $PROMTAIL_PATH $TMP_PATH/promtail
# telegraf config
if [ -d $TELEGRAF_PATH ];then
restoreComponentConfig $TELEGRAF_PATH $TMP_PATH/telegraf
restoreComponentConfig $TELEGRAF_PATH/telegraf.d $TMP_PATH/telegraf/telegraf.d
fi
fi
fn_init_syslog_logrotate_config
systemctl daemon-reload
systemctl restart nz-talon
systemctl enable crond && systemctl restart crond
systemctl enable rsyslog && systemctl restart rsyslog
# nz-promtail.service
if service_exists nz-promtail; then
systemctl restart nz-promtail
else
cat > /usr/lib/systemd/system/nz-promtail.service <<"EOF"
[Unit]
Description=nz-talon
After=network.target
[Service]
WorkingDirectory=/opt/nezha/promtail
EnvironmentFile=/opt/nezha/promtail/config.conf
ExecStart=/opt/nezha/promtail/promtail $OPTION
RestartSec=5s
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable nz-promtail && systemctl restart nz-promtail
fi
# nz-telegraf.service
if service_exists nz-telegraf; then
systemctl restart nz-telegraf
else
cat > /usr/lib/systemd/system/nz-telegraf.service <<"EOF"
[Unit]
Description=Telegraf
Documentation=https://github.com/influxdata/telegraf
After=network-online.target
Wants=network-online.target
[Service]
WorkingDirectory=/opt/nezha/telegraf
Type=notify
EnvironmentFile=/opt/nezha/telegraf/config.conf
ExecStart=/opt/nezha/telegraf/telegraf $OPTION
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartForceExitStatus=SIGPIPE
KillMode=control-group
LimitMEMLOCK=8M:8M
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable nz-telegraf && systemctl restart nz-telegraf
fi
fi