安裝 Grafana
需要使用 Grafana,請按照以下步驟安裝。此安裝指南主要針對 Ubuntu 系統,其他系統安裝包請參考 Grafana 下載頁面。
目錄結構
確保目錄包含以下安裝檔案:
grafana_installation/
├── grafana_10.1.0_amd64.deb
├── certificate.pem
├── private.pem
├── grafana.ini
├── plugins/
│ ├── briangann-datatable-panel-1.0.4.zip
│ └── briangann-datatable-panel-bimap/
└── install_grafana.sh
1. 下載安裝包
將所有安裝包下載並放置在 grafana_installation
目錄下。
2. 解壓安裝包並執行安裝腳本
在 grafana_installation
目錄下,創建 install_grafana.sh
並將以下內容複製進去:
#!/bin/bash
# 變數定義
USERNAME="bimap"
PASSWORD="1qaz2wsx"
INFLUXDB_URL="http://localhost:8086"
# 檢查是否有足夠權限執行腳本
if [ "$EUID" -ne 0 ]; then
echo "請使用 root 權限執行此腳本"
exit 1
fi
# 檢查所需文件是否存在
REQUIRED_FILES=("grafana_10.1.0_amd64.deb" "keys/certificate.pem" "keys/private.pem" "grafana.ini" "plugins/briangann-datatable-panel-1.0.4.zip" "plugins/briangann-datatable-panel-bimap")
echo "檢查所需文件是否存在"
for file in "${REQUIRED_FILES[@]}"; do
if ! ls $file 1> /dev/null 2>&1; then
echo "錯誤:找不到文件 $file"
exit 1
fi
done
# 安裝必要的依賴
echo "start => 安裝必要的依賴"
sudo apt-get update
sudo apt-get install -y adduser libfontconfig1 musl jq &> /dev/null
# 安裝 Grafana
echo "start => 安裝 Grafana"
sudo dpkg -i grafana_10.1.0_amd64.deb
# 配置 SSL 密鑰
echo "start => 配置 SSL 密鑰"
sudo mkdir -p /etc/grafana/keys
sudo cp -r keys/*.pem /etc/grafana/keys
sudo chown -R :grafana /etc/grafana/keys
# 配置 Grafana
echo "start => 配置 Grafana"
sudo cp grafana.ini /etc/grafana/grafana.ini
sudo chown -R :grafana /etc/grafana/grafana.ini
sudo sed -i "s/{{admin_user}}/$USERNAME/" /etc/grafana/grafana.ini
sudo sed -i "s/{{admin_password}}/$PASSWORD/" /etc/grafana/grafana.ini
# 配置 Grafana 插件
echo "start => 配置 Grafana 插件"
sudo mkdir -p /var/lib/grafana/plugins
sudo cp -r plugins /var/lib/grafana
sudo unzip /var/lib/grafana/plugins/briangann-datatable-panel-1.0.4.zip -d /var/lib/grafana/plugins
sudo chown -R :grafana /var/lib/grafana/plugins
# 啟動 Grafana 服務
echo "start => 啟動 Grafana 服務"
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable grafana-server
sudo /bin/systemctl start grafana-server
# 停止 Grafana 服務,重新啟動插件
sudo /bin/systemctl stop grafana-server
sudo cp -r /var/lib/grafana/plugins/briangann-datatable-panel-bimap /var/lib/grafana/plugins/briangann-datatable-panel
sudo /bin/systemctl start grafana-server
echo "Grafana 安裝和配置完成"
echo "Grafana 預設帳號:$USERNAME 預設密碼:$PASSWORD"
# 取得 InfluxDB token
echo "取得 InfluxDB token"
INFLUX_TOKEN=$(influx auth list --json | jq -r '.[0].token')
echo "InfluxDB token: $INFLUX_TOKEN"
# 顯示添加 InfluxDB 作為 Grafana 的數據源的命令,請用戶手動執行
echo "請手動執行以下命令來添加 InfluxDB 作為 Grafana 的數據源:"
echo "------------------------------------------------------------------"
echo "curl -k -s -X POST -H \"Content-Type: application/json\" -d '{"
echo " \"name\":\"influadb-master\","
echo " \"type\":\"influxdb\","
echo " \"url\":\"'$INFLUXDB_URL'\","
echo " \"access\":\"proxy\","
echo " \"basicAuth\":false,"
echo " \"jsonData\": {"
echo " \"defaultBucket\":\"ipoc_rawdata\","
echo " \"organization\":\"master\","
echo " \"version\": \"Flux\""
echo " },"
echo " \"secureJsonData\": {"
echo " \"token\":\"'$INFLUX_TOKEN'\""
echo " }"
echo "}' --user $USERNAME:$PASSWORD https://localhost:3000/api/datasources"
echo "------------------------------------------------------------------"
將腳本保存為 install_grafana.sh
,並按照以下步驟運行:
- 保存腳本為
install_grafana.sh
。 - 打開終端並切換到腳本所在目錄。
- 使用以下命令給腳本添加執行權限並執行腳本:
cd grafana_installation
chmod +x install_grafana.sh
sudo ./install_grafana.sh