apkipa 2 mēneši atpakaļ
vecāks
revīzija
dd77c40c81

+ 3 - 0
.gitignore

@@ -1,2 +1,5 @@
 log
 data
+release
+watch-daemon/watch-daemon
+watch-daemon/config

+ 37 - 0
package.sh

@@ -0,0 +1,37 @@
+#!/bin/bash
+
+set -e
+
+cur_date="`date +%Y-%m-%d`"
+ROOT=$(cd `dirname $0`; pwd)
+
+mkdir -p $ROOT/tmp
+rm -rf $ROOT/tmp/*
+
+# ----- Start build -----
+go build -o $ROOT/watch-daemon/watch-daemon $ROOT/
+
+cp -rf $ROOT/config $ROOT/watch-daemon/
+# ----- End build -----
+
+cp -rf $ROOT/watch-daemon $ROOT/tmp/
+cp -rf $ROOT/watch-daemon-installer $ROOT/tmp/
+
+mkdir -p $ROOT/tmp/watch-daemon/lib
+mkdir -p $ROOT/tmp/watch-daemon/log
+
+cd $ROOT/tmp
+tar -zcf $ROOT/tmp/watch-daemon-installer/watch-daemon.tar.gz watch-daemon
+
+cd $ROOT
+if [ ! -d "release" ]; then
+        mkdir -p release
+fi
+
+cd $ROOT/tmp
+tar -zcf $ROOT/release/watch-daemon-installer-v${cur_date}.tar.gz watch-daemon-installer
+
+rm -rf $ROOT/tmp/*
+rmdir $ROOT/tmp
+
+echo "打包成功"

+ 26 - 0
watch-daemon-installer/install.sh

@@ -0,0 +1,26 @@
+#! /bin/bash
+if [ $UID -ne 0 ]; then
+    echo "请用root用户运行当前脚本 (su root)" && exit 1
+fi
+if [ -r /opt/watch-daemon/watch-daemon  ];then
+      echo "/opt/watch-daemon/watch-daemon文件夹已存在,请重命名旧版本文件后再尝试"
+      exit
+fi
+root_dir=/opt/watch-daemon
+
+if [ ! -d "$root_dir" ]; then
+        mkdir -p $root_dir
+fi
+
+tar zxf ./watch-daemon.tar.gz -C $root_dir
+cp -f watch-daemon.service /usr/lib/systemd/system/
+if [ ! -r /opt/watch-daemon/watch-daemon  ];then
+      echo "安装失败!"
+      exit
+ else
+      echo "安装成功!"
+  fi
+chmod +x /opt/watch-daemon/watch-daemon/*.sh
+chmod +x /opt/watch-daemon/watch-daemon/lib/*
+
+systemctl daemon-reload

+ 24 - 0
watch-daemon-installer/uninstall.sh

@@ -0,0 +1,24 @@
+#!/bin/bash
+if [ $UID -ne 0 ]; then
+    echo "请用root用户运行当前脚本 (su root)" && exit 1
+fi
+root_dir=/opt/watch-daemon
+
+if [ ! -d "$root_dir" ]; then
+        mkdir -p $root_dir
+fi
+
+# Don't delete `/`
+if [[ $root_dir == "/" ]]; then
+    echo "root_dir is /, exit"
+    exit 1
+fi
+
+systemctl stop watch-daemon
+
+rm -rf $root_dir
+rm -f /usr/lib/systemd/system/watch-daemon.service
+
+echo "卸载成功!"
+
+systemctl daemon-reload

+ 15 - 0
watch-daemon-installer/watch-daemon.service

@@ -0,0 +1,15 @@
+[Unit]
+Description=watch-daemon
+After=watch-daemon.service
+  
+[Service]
+Type=forking
+User=root
+Group=root
+ExecStart=/opt/watch-daemon/watch-daemon/run.sh
+ExecReload=/opt/watch-daemon/watch-daemon/restart.sh
+ExecStop=/opt/watch-daemon/watch-daemon/stop.sh
+PrivateTmp=true
+  
+[Install]
+WantedBy=multi-user.target

+ 13 - 0
watch-daemon-installer/安装说明.txt

@@ -0,0 +1,13 @@
+version: v1.0.0
+install.sh脚本用于自动将watch-daemon时序数据库安装在/opt/watch-daemon/watch-daemon下
+如存在旧版本文件需要重新命名
+(以下操作均需通过root用户执行)
+1. chmod +x *.sh给脚本可执行权限
+2. 执行./install.sh安装watch-daemon
+3. 进入/opt/watch-daemon/watch-daemon路径下,完成授权操作
+4. 至此watch-daemon可作为watch-daemon服务运行
+启动:systemctl start watch-daemon
+停止:systemctl stop watch-daemon
+重启: systemctl restart watch-daemon
+查看状态:systemctl status watch-daemon
+开机自启:systemctl enable watch-daemon

+ 42 - 0
watch-daemon/restart.sh

@@ -0,0 +1,42 @@
+#!/usr/bin/env bash
+
+cat << 'LOGO'
+###############################################################
+   
+                           重启脚本
+
+                运行此脚本时可附加参数(不加参数则视为全部重启)
+                        -1 storage
+                        -2 insert
+                        -3 query
+
+###############################################################                                             
+LOGO
+base_dir=$(
+    cd "$(dirname "$0")"
+    pwd
+)
+cd $base_dir
+# extra function
+## func of log
+function log_warn() {
+    local date=$(date "+%Y-%m-%d-%H:%M:%S")
+    local para=$1
+    echo "[WARN] $date $1"
+
+}
+
+function log_info() {
+    local date=$(date "+%Y-%m-%d-%H:%M:%S")
+    local para=$1
+    echo "[INFO] $date $1"
+}
+
+function log_err() {
+    local date=$(date "+%Y-%m-%d-%H:%M:%S")
+    local para=$1
+    echo -e "[ERROR] $date $1"
+}
+
+./stop.sh no_logo "$@" && ./run.sh no_logo
+log_info "服务重启结束"

+ 82 - 0
watch-daemon/run.sh

@@ -0,0 +1,82 @@
+#!/usr/bin/env bash
+set +u
+set -e
+# set -x
+base_dir=$(
+    cd "$(dirname "$0")"
+    pwd
+)
+cd $base_dir
+echo $base_dir
+# ./auto_del_log.sh
+function log_warn() {
+    local date=$(date "+%Y-%m-%d-%H:%M:%S")
+    local para=$1
+    echo "[WARN] $date $1" | tee -a "./log/run_log"
+
+}
+
+function log_info() {
+    local date=$(date "+%Y-%m-%d-%H:%M:%S")
+    local para=$1
+    echo "[INFO] $date $1" | tee -a "./log/run_log"
+}
+
+function log_err() {
+    local date=$(date "+%Y-%m-%d-%H:%M:%S")
+    local para=$1
+    echo "[ERROR] $date $1" | tee -a "./log/run_log"
+}
+
+ulimit -n 100000
+if [[ $1 != "no_logo" ]]; then
+    cat << 'LOGO'
+###############################################################
+
+                        配置文件:
+                        ./config/
+
+###############################################################
+LOGO
+else
+    shift 1
+fi
+
+# parameter set
+log_path="./log/watch-daemon/"
+
+# extra function
+if [[ ! -d "$base_dir/log" ]]; then
+    echo "log文件夹不存在,创建log文件夹" && mkdir "./log"
+fi
+
+# preparation check
+if [[ ! -d "$base_dir/lib" ]]; then
+    log_err "缺少lib文件夹,请查证后重新运行此脚本" && exit 1
+fi
+
+if [[ ! -f "$base_dir/watch-daemon" ]]; then
+    log_err "文件夹中缺少watch-daemon(主程序),无法运行" && exit 1
+fi
+
+if [[ ! -x "$base_dir/watch-daemon" ]]; then
+    log_err "watch-daemon无可执行权限,无法运行。请使用chmod" && exit 1
+fi
+
+if [[ ! -f "$base_dir/license/license_key.json" ]]; then
+    log_warn "您可能未开始、或未完成授权过程.现在生成key,请之后完成授权过程再启动此脚本"
+    # exit 1
+fi
+
+
+###################################################################################################
+if ps -auxc | grep "watch-daemon" | grep -v grep > /dev/null; then
+    log_info "watch-daemon已在运行"
+else
+    if [[ ! -d $log_path ]]; then
+        mkdir "$log_path"
+    fi
+    log_file="watch-daemon_$(date "+%Y-%m-%d-%H:%M:%S").log"
+    ./watch-daemon server --config-file=$base_dir/config/config.xml &> "${log_path}${log_file}" &
+    ./status.sh
+fi

+ 25 - 0
watch-daemon/status.sh

@@ -0,0 +1,25 @@
+#!/usr/bin/env bash
+set -e
+cat << 'LOGO'
+----------------------------------
+LOGO
+base_dir=$(
+    cd "$(dirname "$0")"
+    pwd
+)
+cd $base_dir
+server_pid=$(ps -auxc | grep watch-daemon | grep -vE "grep|-Xmx700m" | awk '{print $2}')
+if [[ $server_pid == "" ]]; then
+    echo "watch-daemon 未在运行"
+else
+    running_time=$(ps -o etime= -p "$server_pid")
+    ports=$(ss -tnlp | grep watch-daemon | awk '{print $4}')
+
+    echo "watch-daemon 当前PID: $server_pid"
+    echo "当前运行时间: $running_time" | tr -s " "
+    echo "当前运行端口:"
+    for i in "$ports"; do
+        echo "$i"
+	break
+    done
+fi

+ 53 - 0
watch-daemon/stop.sh

@@ -0,0 +1,53 @@
+#!/usr/bin/env bash
+set +u
+set -e
+base_dir=$(
+    cd "$(dirname "$0")"
+    pwd
+)
+cd $base_dir
+if [[ $1 != "no_logo" ]]; then
+    cat << 'LOGO'
+###############################################################
+                         关闭服务脚本
+###############################################################
+LOGO
+else
+    shift 1
+fi
+# extra function
+## func of log
+function log_warn() {
+    local date=$(date "+%Y-%m-%d-%H:%M:%S")
+    local para=$1
+    echo "[WARN] $date $1" | tee -a "./log/stop_log"
+
+}
+
+function log_info() {
+    local date=$(date "+%Y-%m-%d-%H:%M:%S")
+    local para=$1
+    echo "[INFO] $date $1" | tee -a "./log/stop_log"
+}
+
+function log_err() {
+    local date=$(date "+%Y-%m-%d-%H:%M:%S")
+    local para=$1
+    echo "[ERROR] $date $1" | tee -a "./log/stop_log"
+}
+function stop_process() {
+    ID=$(ps -auxc | grep "$1" | grep -v "$0" | grep -v "grep" | awk '{print $2}')
+    if [[ "$ID" ]]; then
+        log_info "$1 运行中,准备关闭"
+        for id in $ID; do
+            kill -15 "$id"
+            echo "stop $1 process:(PID=${id})"
+        done
+        echo "---------------"
+    fi
+}
+
+if [[ -z $1 ]]; then
+    stop_process "watch-daemon"
+    log_info "服务停止脚本运行完成"
+fi