优化更新中英文文档-安装Agent

This commit is contained in:
Kris 2024-05-15 04:19:51 +02:00
parent 3b15ae51c2
commit 09282f9563
No known key found for this signature in database
GPG Key ID: 36AFAA97D2910652
2 changed files with 450 additions and 416 deletions

View File

@ -1,331 +1,372 @@
**The service in the monitored server is called Agent. This document will describe how to install the Agent on the monitored server and connect it with Dashboard**
::: tip
Agent binary repository: https://github.com/nezhahq/agent/releases
:::
## Install Agent using one-click script
Nezha Monitoring now supports one-click installation of the Agent on Windows and Linux. Follow the steps in this document and you can easily deploy it on your server
<br/>
---
outline: deep
---
**This document will introduce how to install the Agent on the monitored server and connect it to the Dashboard.**
::: tip
The repository for Agent binaries can be found at: <https://github.com/nezhahq/agent/releases>
:::
## One-Click Installation of the Agent
Nezha Monitoring supports one-click installation of the Agent on both Windows and Linux. By following the steps in this document, you can easily deploy it on your server.
### Preparation
First of all, you need to set up the communication domain name in the settings page of the admin panel, this domain name can not connect to the CDN, here is the sample communication domain name "data.example.com" mentioned earlier for demonstration
Enter the administration panel, go to the "Settings" page, in the item "CDN Bypassed Domain/IP
", fill in the communication domain name, and then click "Save"
<br/>
### One-click installation on Linux (Ubuntu, Debian, CentOS)
* First add a server in the admin panel
* Click on the green Linux icon button next to the newly added server and copy the one-click installation command
* Run the copied one-click installation command on the monitored server, wait for the installation to complete, and then return to the Dashboard home page to see if the server is online.
<br/>
### One-click installation on Windows
* First add a server in the admin panel
* Click on the green Linux icon button next to the newly added server and copy the one-click installation command
* Login to Windows Server, open PowerShell, and run the copied installation command in PowerShell
* If you encounter the prompt "Implement Policy Change" please select Y
* Wait for the installation to complete and return to the Dashboard home page to see if the server is online
<br/>
<br/>
## Other ways to install Agent
<br/>
### Installing Agent on Linux (Ubuntu, Debian, CentOS)
* First add a server in the admin panel
* In the monitored server, run the script:
You need to set up a communication domain in the admin panel in advance, and this domain should not be connected to a CDN. This document uses the example communication domain “data.example.com”.
Go to the settings page in the admin panel, fill in the communication domain in the “Non-CDN Dashboard Server Domain/IP” field, and click "Save".
### One-Click Installation on Linux (Ubuntu, Debian, CentOS)
1. First, add a server in the admin panel.
2. Click the green Linux icon button next to the newly added server and copy the one-click installation command.
3. Run the copied installation command on the monitored server, and wait for the installation to complete. Then, check if the server is online in the Dashboard home page.
### One-Click Installation on Windows
1. First, add a server in the admin panel.
2. Click the green Windows icon button next to the newly added server and copy the one-click installation command.
3. Go to the Windows server, run PowerShell, and execute the copied installation command in PowerShell.
4. If you encounter a prompt to "change execution policy," choose Y.
5. Wait for the installation to complete, then check if the server is online in the Dashboard home page.
::: warning
If you encounter errors when running the one-click installation command in PowerShell, try the **Manual Installation of the Agent on Windows** below.
:::
## Other Ways to Install the Agent
### Installing the Agent on Linux (Ubuntu, Debian, CentOS)
<details>
<summary>Click to expand/collapse</summary>
1. First, add a server in the admin panel.
2. Run the script on the monitored server:
```bash
curl -L https://raw.githubusercontent.com/naiba/nezha/master/script/install_en.sh -o nezha.sh && chmod +x nezha.sh && sudo ./nezha.sh
curl -L https://raw.githubusercontent.com/naiba/nezha/master/script/install.sh -o nezha.sh && chmod +x nezha.sh && sudo ./nezha.sh
```
* Select “Install_agent”
* Input the communication domain name, e.g. "data.example.com"
1. Select “Install monitoring Agent.”
2. Enter the communication domain, such as "data.example.com".
3. Enter the dashboard communication port (gRPC port), default is 5555.
4. Enter the Agent secret, which is generated when you add a server in the admin panel and can be found on the “Servers” page in the admin panel.
5. Wait for the installation to complete, then check if the server is online in the Dashboard home page.
* Input RPC port, default is 5555
</details>
* Input the Agent Secret, which is generated when adding a server in the administration panel and can be found in the " Servers " page of the administration panel
### Installing the Agent on Other Linux Distributions (e.g., Alpine using Openrc)
<details>
<summary>Click to expand/collapse</summary>
* Wait for the installation to complete and return to the Dashboard home page to see if the server is online
<br/>
This section is contributed by [unknown0054](https://github.com/unknwon0054).
### Installing Agent on other Linux (such as alpine use oprec not systemd)
Contributed by [unknown0054](https://github.com/unknwon0054)
* Edit SERVER,SECRET,TLS then run it in Shell
1. Modify SERVER, SECRET, TLS, and execute in the shell:
```shell
cat >/etc/init.d/nezha-agent<< EOF
#!/sbin/openrc-run
SERVER="" #Dashboard address ip:port
SECRET="" #SECRET
TLS="" # Enable tls? yes:"--tls" no:""
SERVER="" # Dashboard domain ip:port
SECRET="" # SECRET
TLS="" # Enable TLS if yes "--tls", leave empty if no
NZ_BASE_PATH="/opt/nezha"
NZ_AGENT_PATH="${NZ_BASE_PATH}/agent"
pidfile="/run/${RC_SVCNAME}.pid"
command="/opt/nezha/agent/nezha-agent"
command_args="-s ${SERVER} -p ${SECRET} ${TLS}"
command_args="-s ${SERVER} -p ${SECRET} ${TLS}"
command_background=true
depend() {
need net
need net
}
checkconfig() {
GITHUB_URL="github.com"
if [ ! -f "${NZ_AGENT_PATH}/nezha-agent" ]; then
if [[ $(uname -m | grep 'x86_64') != "" ]]; then
os_arch="amd64"
elif [[ $(uname -m | grep 'i386\|i686') != "" ]]; then
os_arch="386"
elif [[ $(uname -m | grep 'aarch64\|armv8b\|armv8l') != "" ]]; then
os_arch="arm64"
elif [[ $(uname -m | grep 'arm') != "" ]]; then
os_arch="arm"
elif [[ $(uname -m | grep 's390x') != "" ]]; then
os_arch="s390x"
elif [[ $(uname -m | grep 'riscv64') != "" ]]; then
os_arch="riscv64"
fi
local version=$(curl -m 10 -sL "https://api.github.com/repos/nezhahq/agent/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://fastly.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
fi
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://gcore.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
fi
if [ ! -n "$version" ]; then
echo -e "Failed to get the version number, please check if the network can connect to https://api.github.com/repos/nezhahq/agent/releases/latest"
return 0
else
echo -e "The current latest version is: ${version}"
fi
wget -t 2 -T 10 -O nezha-agent_linux_${os_arch}.zip https://${GITHUB_URL}/nezhahq/agent/releases/download/${version}/nezha-agent_linux_${os_arch}.zip >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "Release download failed, please check if the network can connect to ${GITHUB_URL}${plain}"
return 0
fi
mkdir -p $NZ_AGENT_PATH
chmod 755 -R $NZ_AGENT_PATH
unzip -qo nezha-agent_linux_${os_arch}.zip && mv nezha-agent $NZ_AGENT_PATH && rm -rf nezha-agent_linux_${os_arch}.zip README.md
fi
if [ ! -x "${NZ_AGENT_PATH}/nezha-agent" ]; then
chmod +x ${NZ_AGENT_PATH}/nezha-agent
fi
GITHUB_URL="github.com"
if [ ! -f "${NZ_AGENT_PATH}/nezha-agent" ]; then
if [[ $(uname -m | grep 'x86_64') != "" ]]; then
os_arch="amd64"
elif [[ $(uname -m | grep 'i386\|i686') != "" ]]; then
os_arch="386"
elif [[ $(uname -m | grep 'aarch64\|armv8b\|armv8l') != "" ]]; then
os_arch="arm64"
elif [[ $(uname -m | grep 'arm') != "" ]]; then
os_arch="arm"
elif [[ $(uname -m | grep 's390x') != "" ]]; then
os_arch="s390x"
elif [[ $(uname -m | grep 'riscv64') != "" ]]; then
os_arch="riscv64"
fi
local version=$(curl -m 10 -sL "https://api.github.com/repos/nezhahq/agent/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://fastly.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
fi
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://gcore.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
fi
if [ ! -n "$version" ]; then
echo -e "Failed to get the version number. Please check if the server can connect to https://api.github.com/repos/nezhahq/agent/releases/latest"
return 0
else
echo -e "The latest version is: ${version}"
fi
wget -t 2 -T 10 -O nezha-agent_linux_${os_arch}.zip https://${GITHUB_URL}/nezhahq/agent/releases/download/${version}/nezha-agent_linux_${os_arch}.zip >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "Failed to download Release. Please check if the server can connect to ${GITHUB_URL}"
return 0
fi
mkdir -p $NZ_AGENT_PATH
chmod 755 -R $NZ_AGENT_PATH
unzip -qo nezha-agent_linux_${os_arch}.zip && mv nezha-agent $NZ_AGENT_PATH && rm -rf nezha-agent_linux_${os_arch}.zip README.md
fi
if [ ! -x "${NZ_AGENT_PATH}/nezha-agent" ]; then
chmod +x ${NZ_AGENT_PATH}/nezha-agent
fi
}
start_pre() {
if [ "${RC_CMD}" != "restart" ]; then
checkconfig || return $?
fi
if [ "${RC_CMD}" != "restart" ]; then
checkconfig || return $?
fi
}
EOF
```
* Add execute permission
2. Add execute permissions
```shell
chmod +x /etc/init.d/nezha-agent
```
```shell
chmod +x /etc/init.d/nezha-agent
```
* Run Nezha-Agent
3. Start Nezha-Agent
```shell
rc-service nezha-agent-hy start
```
```shell
rc-service nezha-agent start
```
* Set self-start after boot
4. Add to startup
```shell
rc-update add nezha-agent
```
```shell
rc-update add nezha-agent
```
### Installing Agent on Windows
</details>
- Please refer to the community article:
[哪吒探针 - Windows 客户端安装](https://nyko.me/2020/12/13/nezha-windows-client.html) (Chinese)
<br/>
### Manual Installation of the Agent on Windows
* Refer to the community article:
[Nezha - Windows Client Installation](https://nyko.me/2020/12/13/nezha-windows-client.html) (Chinese)
### Installing Agent on Synology DSM
### Installing the Agent on Synology DSM
<details>
<summary>Click to expand/collapse</summary>
- Please refer to the community article:
[群晖 DSM 7.x 安装 哪吒监控 Agent](https://blog.mitsea.com/3929551d08bd4bb0a8baa453e2d92b0c/) (Chinese)
[哪吒探针——群晖客户端(被控端)安装教程](https://wl.gta5pdx.cn/archives/546/) (Chinese)
* Refer to community articles:
[Installing Nezha Monitoring Agent on Synology DSM 7.x](https://blog.mitsea.com/3929551d08bd4bb0a8baa453e2d92b0c/) (Chinese)
[Nezha - Synology Client (Agent) Installation Tutorial](https://wl.gta5pdx.cn/archives/546/) (Chinese)
* Using Systemd *for DSM7 only*:
- Using Systemd *Only available on DSM7*:
```sh
# Path of the agent binary file.
EXEC="/PATH/TO/nezha-agent"
# Path of the agent's log.
LOG="${EXEC}.log"
# Extend arguments, can be blank.
ARGS="--disable-command-execute"
# The address of nezha server's GRPC.
SERVER="HOST_OR_IP:GRPC_PORT"
# The token of host.
SECRET="APP_SECRET"
# Specify `run_as` user. * Strongly suggest NOT use `root`! *
RUN_USER="nezha"
# Create the service file.
cat << EOF > /usr/lib/systemd/system/nezha.service
[Unit]
Description=Nezha Agent Service
After=network.target
[Service]
Type=simple
ExecStart=/bin/nohup ${EXEC} ${ARGS} -s ${SERVER} -p ${SECRET} &>> ${LOG} &
ExecStop=ps -fe |grep nezha-agent|awk '{print \$2}'|xargs kill
User=${RUN_USER}
Restart=on-abort
[Install]
WantedBy=multi-user.target
EOF
# Reload services
systemctl daemon-reload
# Start the agent service
systemctl start nezha
# Enable auto-start
systemctl enable nezha
```
DO IT WHEN YOU HAVE MODIFIED THE ENVS‼
Using `root` to execute command above, that's all.
```sh
# Agent path
EXEC="/PATH/TO/nezha-agent"
# Log path
LOG="${EXEC}.log"
# Additional execution parameters, can be empty
ARGS=""
# Nezha server gRPC address
SERVER="HOST_OR_IP:gRPC_PORT"
# The secret key obtained in the previous step
SECRET="APP_SECRET"
# User running the service, *strongly recommended to use non-root user*
RUN_USER="nezha"
<br/>
# Write to systemd service file
cat << EOF > /usr/lib/systemd/system/nezha.service
[Unit]
Description=Nezha Agent Service
After=network.target
### Installing Agent on MacOS
***This section is adapted from [Mitsea Blog](https://blog.mitsea.com/e796f93db38d49e4b18df234c6ee75f5), with permission from the original author***
<br/>
[Service]
Type=simple
ExecStart=/bin/nohup ${EXEC} ${ARGS} -s ${SERVER} -p ${SECRET} &>> ${LOG} &
ExecStop=ps -fe |grep nezha-agent|awk '{print \$2}'|xargs kill
User=${RUN_USER}
Restart=on-abort
[Install]
WantedBy=multi-user.target
EOF
# Reload service
systemctl daemon-reload
# Start service
systemctl start nezha
# Enable service startup
systemctl enable nezha
```
‼️ Modify the corresponding information before running the above commands with the `root` account to complete the installation.
</details>
### Installing the Agent on macOS
<details>
<summary>Click to expand/collapse</summary>
***This section is adapted from [Mitsea Blog](https://blog.mitsea.com/e796f93db38d49e4b18df234c6ee75f5) with the author's permission***
::: warning
If you are prompted with "macOS cannot verify this app" during installation, please go to system settings to allow the app to run.
:::
If you are prompted "macOS cannot verify this app" during installation, manually allow the program to run in System Settings.
:::
1. First, add a server in the admin panel.
2. Go to the [Release](https://github.com/nezhahq/agent/releases) page to download the Agent binary file. Choose to download the darwin amd64 or arm64 Agent according to your CPU architecture. Download the amd64 version for Intel CPU, or the arm64 version for Apple Silicon. After downloading, unzip the Agent binary file, such as unzipping it to the Downloads folder.
3. Create a file named `nezha_agent.plist` and save it with the following content:
+ First add a server in the admin panel
+ Go to the [Release](https://github.com/nezhahq/agent/releases) page to download the Agent binary and choose whether to download the darwin amd64 or arm64 Agent depending on the CPU architecture
For example, download the amd64 version for Intel CPU and the arm64 version for Apple Silicon. After downloading, extract the Agent binary file, e.g. to the Download folder
+ Create a new file named `nezha_agent.plist` and save it, edit the contents of the file:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>nezha_agent</string>
<key>Program</key>
<string>Change the path of the Agent binary here, e.g. /Users/123/Downloads/nezha-agent</string>
<key>ProgramArguments</key>
<array>
<string>Change the path of the Agent binary here, e.g. /Users/123/Downloads/nezha-agent</string>
<string>--password</string>
<string>Communication Secret, eg: 529664783eeb23cc25</string>
<string>--server</string>
<string>Communication URL and RPC port, eg:data.example.com:5555</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>nezha_agent</string>
<key>Program</key>
<string>Modify the path to the Agent binary file here, such as: /Users/123/Downloads/nezha-agent</string>
<key>ProgramArguments</key>
<array>
<string>Modify the path to the Agent binary file here, same as above</string>
<string>--password</string>
<string>The communication secret, such as: 529664783eeb23cc25</string>
<string>--server</string>
<string>The communication URL and gRPC port, such as: data.example.com:5555</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
```
+ Use the following command in Terminal to load the plist file into launchd
**Be sure to change the file path**
4. Load the plist file into launchd using the following command in Terminal, **make sure to replace the file path**:
```shell
launchctl load /Users/123/Desktop/nezha_agent.plist
```
+ Start Service
5. Start the process:
```shell
launchctl start nezha_agent
```
+ Check if the service is running
6. Check if the process is running:
```shell
launchctl list | grep nezha_agent
```
+ Stop service and remove
7. Stop the process and remove it:
```shell
launchctl stop nezha_agent
```
```shell
launchctl remove nezha_agent
```
<br/>
Using Homebrew to install the Agent on macOS
</details>
The content of this section is adapted from [🐿️松鼠收集🌰](https://blog.mre.red/archives/install_nezha_monitoring_agent_service_with_homebrew), with the permission of the original author.
### Installing the Agent on macOS Using Homebrew
<details>
<summary>Click to expand/collapse</summary>
::: danger
Please ensure to set environment variables first, and then proceed with the installation of Nezha-agent using Homebrew!
***This section is adapted from [🐿️松鼠收集🌰](https://blog.mre.red/archives/install_nezha_monitoring_agent_service_with_homebrew) with the author's permission***
Due to the creation of service configuration files (PLIST) by Homebrew during software installation, if you install first and then add environment variables, it will fail to start due to missing parameters!
::: warning
Please be sure to add environment variables before installing nezha-agent through Homebrew!
Homebrew creates the service-required plist file during software installation, and if you add the environment variables after installation, it will fail to start due to missing parameters.
:::
* Adding environment variables
1. Add environment variables:
```sh
echo 'export HOMEBREW_NEZHA_AGENT_PASSWORD="your key, obtained from your service page"' >> ~/.zshrc
echo 'export HOMEBREW_NEZHA_AGENT_SERVER="your server and port, in the format your.domain:5555"' >> ~/.zshrc
```shell
echo 'export HOMEBREW_NEZHA_AGENT_PASSWORD="Communication key, obtained from the service page"' >> ~/.zshrc
echo 'export HOMEBREW_NEZHA_AGENT_SERVER="Your server and port, format your.domain:5555 "' >> ~/.zshrc
source ~/.zshrc
```
* Installing the Nezha Agent
2. Install Nezha Agent:
As the package has not yet been submitted to the official Homebrew Core repository, it is currently available in the [third-party Homebrew repository](https://github.com/Brewforge/homebrew-chinese) maintained by the author of the blog mentioned above.
::: danger
Note that this Homebrew repository is maintained by a third party and is unrelated to Nezha Monitoring.
The Nezha project team does not endorse this repository's usability, security, etc. Please evaluate the risks yourself before using!
:::
```sh
Since it has not yet been submitted to the Homebrew Core official library, it is temporarily placed in the [third-party Homebrew repository](https://github.com/Brewforge/homebrew-chinese) maintained by the author of the blog:
```shell
brew install brewforge/chinese/nezha-agent
```
* Starting the Nezha Agent service through Homebrew
3. Start Nezha Agent service through Homebrew:
```sh
```shell
brew services start nezha-agent
```
* Checking the service status
4. Check the service status:
```sh
```shell
brew services info nezha-agent
```
* Stopping the service
5. Stop the service:
```sh
```shell
brew services stop nezha-agent
```
* Uninstalling the Nezha Agent
6. Uninstall Nezha Agent:
```sh
```shell
brew rm nezha-agent
```
* Error handling first check environment variables
7. If there is an error, first check the environment variables:
```sh
```shell
echo $HOMEBREW_NEZHA_AGENT_PASSWORD
echo $HOMEBREW_NEZHA_AGENT_SERVER
```
* If the environment variable configuration is correct, attempt reinstallation
8. If the environment variables are configured correctly, try reinstalling:
```sh
```shell
brew services stop nezha-agent
brew reinstall nezha-agent
brew services start nezha-agent
```
* If the issue persists, please proceed to the [third-party Homebrew repository](https://github.com/Brewforge/homebrew-chinese) and submit a ticket
9. If the issue persists, submit a issue to the [third-party Homebrew repository](https://github.com/Brewforge/homebrew-chinese).
<br/>
</details>
### Installing Agent on OpenWRT
**How to solve various problems during the installation process in one step**
- Please refer to the project:
[NZ-OpenWrt](https://github.com/dysf888/NZ-OpenWrt) (Chinese)
<br/>
### Installing the Agent on OpenWRT
<details>
<summary>Click to expand/collapse</summary>
**How to make the old version of OpenWRT/LEDE self-boot?**
- Please refer to the project:
[哪吒监控 For OpenWRT](https://github.com/Erope/openwrt_nezha) (Chinese)
<br/>
**How to solve installation difficulties and issues in one step?**
**How to make the new version of OpenWRT self-boot? By @艾斯德斯**
* First download the corresponding binary from the release, unzip the zip package and place it in `/root`
* Then run `chmod +x /root/nezha-agent` to give it execute access, create file `/etc/init.d/nezha-service`
* Refer to the project:
[NZ-OpenWrt](https://github.com/dysf888/NZ-OpenWrt)
**How to enable autostart on older OpenWRT/LEDE?**
* Refer to the project:
[Nezha Monitoring for OpenWRT](https://github.com/Erope/openwrt_nezha)
**How to enable autostart on newer OpenWRT? Contributor: @艾斯德斯**
* First, download the corresponding binary from the release, unzip the zip package, and place it in `/root`.
* Run `chmod +x /root/nezha-agent` to grant execution permission, then create `/etc/init.d/nezha-service`:
```shell
#!/bin/sh /etc/rc.common
@ -335,13 +376,13 @@ USE_PROCD=1
start_service() {
procd_open_instance
procd_set_param command /root/nezha-agent -s data.example.com:5555 -p secreat -d
procd_set_param command /root/nezha-agent -s Dashboard communication domain:port -p Key -d
procd_set_param respawn
procd_close_instance
}
stop_service() {
killall nezha-agent
killall nezha-agent
}
restart() {
@ -351,12 +392,13 @@ restart() {
}
```
* Give it permission to execute: `chmod +x /etc/init.d/nezha-service`
* Start the service `/etc/init.d/nezha-service enable && /etc/init.d/nezha-service start`
<br/>
* Run `chmod +x /etc/init.d/nezha-service` to grant execution permission.
* Start the service: `/etc/init.d/nezha-service enable && /etc/init.d/nezha-service start`
## FAQ
### Is there a Docker image for Agent?
**There is currently no Docker image for Agent.**
The Agent is designed to be the opposite of the Dashboard, in that the Dashboard is designed to work without affecting the server as much as possible, while the Agent needs to execute monitoring services and run commands in the server.
Putting the Agent in a container does continue to execute monitoring services, but features such as WebShell do not work, so we do not provide Docker image of the Agent.
</details>
## Does the Agent Have a Docker Image?
**The Agent does not currently have a Docker image.**
The design philosophy of the Agent is opposite to that of the Dashboard. While the Dashboard should minimally impact the server, the Agent needs to execute monitoring services and run commands within the server.
Running the Agent inside a container can still perform monitoring tasks, but features like WebShell will not function properly, so no Docker image is provided.

View File

@ -2,41 +2,37 @@
outline: deep
---
**哪吒监控的被控端服务被称为 Agent本文档将介绍如何在被控端服务器上安装 Agent并与 Dashboard 连接**
**哪吒监控的被控端服务被称为 Agent本文档将介绍如何在被控端服务器上安装 Agent并与 Dashboard 连接**
::: tip
Agent 二进制文件仓库地址为:<https://github.com/nezhahq/agent/releases>
:::
## 一键安装 Agent
目前哪吒监控已支持在 Windows 和 Linux上一键安装 Agent遵循本文档的步骤你可以很轻松的在服务器上部署它
<br/>
哪吒监控支持在 Windows 和 Linux 上一键安装 Agent。遵循本文档的步骤你可以很轻松地在服务器上部署它。
### 准备工作
你需要提前在管理面板中设置好通信域名此域名不可以接入CDN这里以示例通信域名 “data.example.com” 来做演示
进入后台管理面板,转到“设置”页,在“未接入 CDN 的面板服务器域名/IP”项中填入通信域名然后点击"保存"
<br/>
你需要提前在管理面板中设置好通信域名,此域名不可以接入 CDN。本文档以示例通信域名 “data.example.com” 为例。
进入后台管理面板,转到“设置”页,在“未接入 CDN 的面板服务器域名/IP”项中填入通信域名然后点击"保存"。
### 在 Linux 中一键安装 (Ubuntu、Debian、CentOS)
* 首先在管理面板中添加一台服务器
* 点击新添加的服务器旁,绿色的 Linux 图标按钮,复制一键安装命令
* 在被控端服务器中运行复制的一键安装命令,等待安装完成后返回到 Dashboard 主页查看服务器是否上线
<br/>
1. 首先在管理面板中添加一台服务器。
2. 点击新添加的服务器旁的绿色 Linux 图标按钮,复制一键安装命令。
3. 在被控端服务器中运行复制的一键安装命令,等待安装完成后返回到 Dashboard 主页查看服务器是否上线。
### 在 Windows 中一键安装
* 首先在管理面板中添加一台服务器
* 点击新添加的服务器旁,绿色的 Windows 图标按钮,复制一键安装命令
* 进入 Windows 服务器,运行 PowerShell在 PowerShell 中运行复制的安装命令
* 如遇到确认「执行策略变更」请选择 Y
* 等待安装完成后返回 Dashboard 主页查看服务器是否上线
<br/>
:::warning
如果在 Powershell 中运行一键安装命令时遇到错误,请尝试下方的**在 Windows 中手动安装 Agent**
:::
1. 首先在管理面板中添加一台服务器。
2. 点击新添加的服务器旁的绿色 Windows 图标按钮,复制一键安装命令。
3. 进入 Windows 服务器,运行 PowerShell在 PowerShell 中运行复制的安装命令。
4. 如遇到确认「执行策略变更」请选择 Y。
5. 等待安装完成后返回 Dashboard 主页查看服务器是否上线。
<br/>
:::warning
如果在 PowerShell 中运行一键安装命令时遇到错误,请尝试下方的**在 Windows 中手动安装 Agent**。
:::
## 其他方式安装 Agent
@ -44,194 +40,193 @@ Agent 二进制文件仓库地址为:<https://github.com/nezhahq/agent/release
<details>
<summary>点击展开/收起</summary>
* 首先在管理面板中添加一台服务器
* 在被控服务器中,运行脚本(位于中国大陆的服务器请使用镜像):
1. 首先在管理面板中添加一台服务器。
2. 在被控服务器中,运行脚本(位于中国大陆的服务器请使用镜像):
```bash
curl -L https://raw.githubusercontent.com/naiba/nezha/master/script/install.sh -o nezha.sh && chmod +x nezha.sh && sudo ./nezha.sh
curl -L https://raw.githubusercontent.com/naiba/nezha/master/script/install.sh -o nezha.sh && chmod +x nezha.sh && sudo ./nezha.sh
```
如果你的被控服务器位于中国大陆,可以使用镜像:
如果你的被控服务器位于中国大陆,可以使用镜像:
````bash
```bash
curl -L https://gitee.com/naibahq/nezha/raw/master/script/install.sh -o nezha.sh && chmod +x nezha.sh && sudo CN=true ./nezha.sh
````
```
3. 选择“安装监控 Agent”。
4. 输入通信域名”data.example.com“。
5. 输入面板通信端口gRPC 端口),默认为 5555。
6. 输入 Agent 密钥Agent 密钥在管理面板中添加服务器时生成,可以在管理面板中的“服务器”页中找到。
7. 等待安装完成后返回 Dashboard 主页查看服务器是否上线。
* 选择“安装监控 Agent”
* 输入通信域名”data.example.com“
* 输入面板通信端口gRPC 端口),默认为 5555
* 输入 Agent 密钥Agent 密钥在管理面板中添加服务器时生成,可以在管理面板中的“服务器”页中找到
* 等待安装完成后返回 Dashboard 主页查看服务器是否上线
</details>
<br/>
### 在 其他 Linux 如 Alpine 使用 Openrc 的发行版 安装 Agent
### 在其他 Linux 发行版(如 Alpine 使用 Openrc中安装 Agent
<details>
<summary>点击展开/收起</summary>
本节内容由 [unknown0054](https://github.com/unknwon0054) 贡献
* 修改 SERVER、SECRET、TLS 然后在 shell 中执行
本节内容由 [unknown0054](https://github.com/unknwon0054) 贡献。
1. 修改 SERVER、SECRET、TLS然后在 shell 中执行:
```shell
cat >/etc/init.d/nezha-agent<< EOF
#!/sbin/openrc-run
SERVER="" #Dashboard 地址 ip:port
SECRET="" #SECRET
TLS="" # 是否启用 tls 是 "--tls" 否留空
SERVER="" # Dashboard 地址 ip:port
SECRET="" # SECRET
TLS="" # 是否启用 TLS是 "--tls" 否留空
NZ_BASE_PATH="/opt/nezha"
NZ_AGENT_PATH="${NZ_BASE_PATH}/agent"
pidfile="/run/${RC_SVCNAME}.pid"
command="/opt/nezha/agent/nezha-agent"
command_args="-s ${SERVER} -p ${SECRET} ${TLS}"
command_args="-s ${SERVER} -p ${SECRET} ${TLS}"
command_background=true
depend() {
need net
need net
}
checkconfig() {
GITHUB_URL="github.com"
if [ ! -f "${NZ_AGENT_PATH}/nezha-agent" ]; then
if [[ $(uname -m | grep 'x86_64') != "" ]]; then
os_arch="amd64"
elif [[ $(uname -m | grep 'i386\|i686') != "" ]]; then
os_arch="386"
elif [[ $(uname -m | grep 'aarch64\|armv8b\|armv8l') != "" ]]; then
os_arch="arm64"
elif [[ $(uname -m | grep 'arm') != "" ]]; then
os_arch="arm"
elif [[ $(uname -m | grep 's390x') != "" ]]; then
os_arch="s390x"
elif [[ $(uname -m | grep 'riscv64') != "" ]]; then
os_arch="riscv64"
GITHUB_URL="github.com"
if [ ! -f "${NZ_AGENT_PATH}/nezha-agent" ]; then
if [[ $(uname -m | grep 'x86_64') != "" ]]; then
os_arch="amd64"
elif [[ $(uname -m | grep 'i386\|i686') != "" ]]; then
os_arch="386"
elif [[ $(uname -m | grep 'aarch64\|armv8b\|armv8l') != "" ]]; then
os_arch="arm64"
elif [[ $(uname -m | grep 'arm') != "" ]]; then
os_arch="arm"
elif [[ $(uname -m | grep 's390x') != "" ]]; then
os_arch="s390x"
elif [[ $(uname -m | grep 'riscv64') != "" ]]; then
os_arch="riscv64"
fi
local version=$(curl -m 10 -sL "https://api.github.com/repos/nezhahq/agent/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://fastly.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
fi
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://gcore.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
fi
if [ ! -n "$version" ]; then
echo -e "获取版本号失败,请检查本机能否链接 https://api.github.com/repos/nezhahq/agent/releases/latest"
return 0
else
echo -e "当前最新版本为: ${version}"
fi
wget -t 2 -T 10 -O nezha-agent_linux_${os_arch}.zip https://${GITHUB_URL}/nezhahq/agent/releases/download/${version}/nezha-agent_linux_${os_arch}.zip >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "Release 下载失败,请检查本机能否连接 ${GITHUB_URL}${plain}"
return 0
fi
mkdir -p $NZ_AGENT_PATH
chmod 755 -R $NZ_AGENT_PATH
unzip -qo nezha-agent_linux_${os_arch}.zip && mv nezha-agent $NZ_AGENT_PATH && rm -rf nezha-agent_linux_${os_arch}.zip README.md
fi
local version=$(curl -m 10 -sL "https://api.github.com/repos/nezhahq/agent/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://fastly.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
if [ ! -x "${NZ_AGENT_PATH}/nezha-agent" ]; then
chmod +x ${NZ_AGENT_PATH}/nezha-agent
fi
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://gcore.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
fi
if [ ! -n "$version" ]; then
echo -e "获取版本号失败,请检查本机能否链接 https://api.github.com/repos/nezhahq/agent/releases/latest"
return 0
else
echo -e "当前最新版本为: ${version}"
fi
wget -t 2 -T 10 -O nezha-agent_linux_${os_arch}.zip https://${GITHUB_URL}/nezhahq/agent/releases/download/${version}/nezha-agent_linux_${os_arch}.zip >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "Release 下载失败,请检查本机能否连接 ${GITHUB_URL}${plain}"
return 0
fi
mkdir -p $NZ_AGENT_PATH
chmod 755 -R $NZ_AGENT_PATH
unzip -qo nezha-agent_linux_${os_arch}.zip && mv nezha-agent $NZ_AGENT_PATH && rm -rf nezha-agent_linux_${os_arch}.zip README.md
fi
if [ ! -x "${NZ_AGENT_PATH}/nezha-agent" ]; then
chmod +x ${NZ_AGENT_PATH}/nezha-agent
fi
}
start_pre() {
if [ "${RC_CMD}" != "restart" ]; then
checkconfig || return $?
fi
if [ "${RC_CMD}" != "restart" ]; then
checkconfig || return $?
fi
}
EOF
```
* 增加运行权限
2. 增加运行权限
```shell
chmod +x /etc/init.d/nezha-agent
```
```shell
chmod +x /etc/init.d/nezha-agent
```
* 运行 Nezha-Agent
3. 运行 Nezha-Agent
```shell
rc-service nezha-agent start
```
```shell
rc-service nezha-agent start
```
* 添加开机自启动
4. 添加开机自启动
```shell
rc-update add nezha-agent
```
```shell
rc-update add nezha-agent
```
</details>
### 在 Windows 中手动安装 Agent
### 在 Windows 中手动安装 Agent
* 请参考社区文章:
[哪吒探针 - Windows 客户端安装](https://nyko.me/2020/12/13/nezha-windows-client.html)
<br/>
[哪吒探针 - Windows 客户端安装](https://nyko.me/2020/12/13/nezha-windows-client.html)
### 在 群晖DSM 中安装 Agent
### 在群晖 DSM 中安装 Agent
<details>
<summary>点击展开/收起</summary>
* 请参考社区文章:
[群晖 DSM 7.x 安装 哪吒监控 Agent](https://blog.mitsea.com/3929551d08bd4bb0a8baa453e2d92b0c/)
[哪吒探针——群晖客户端(被控端)安装教程](https://wl.gta5pdx.cn/archives/546/)
[哪吒探针——群晖客户端(被控端)安装教程](https://wl.gta5pdx.cn/archives/546/)
* Systemd 实现 *仅适用于 DSM7*:
```sh
# 客户端路径
EXEC="/PATH/TO/nezha-agent"
# 日志路径地址
LOG="${EXEC}.log"
# 额外执行参数, 可留空
ARGS="--disable-command-execute"
# 哪吒服务端gRPC地址
SERVER="HOST_OR_IP:gRPC_PORT"
# 上一步获取的主机密钥
SECRET="APP_SECRET"
# 服务运行用户名, *强烈建议使用非root用户执行*
RUN_USER="nezha"
```sh
# Agent 路径
EXEC="/PATH/TO/nezha-agent"
# 日志路径地址
LOG="${EXEC}.log"
# 额外执行参数, 可留空
ARGS=""
# 哪吒服务端 gRPC 地址
SERVER="HOST_OR_IP:gRPC_PORT"
# 上一步获取的主机密钥
SECRET="APP_SECRET"
# 运行服务的用户名, *强烈建议使用非root用户执行*
RUN_USER="nezha"
# 写入到systemd服务文件
cat << EOF > /usr/lib/systemd/system/nezha.service
[Unit]
Description=Nezha Agent Service
After=network.target
# 写入到 systemd 服务文件
cat << EOF > /usr/lib/systemd/system/nezha.service
[Unit]
Description=Nezha Agent Service
After=network.target
[Service]
Type=simple
ExecStart=/bin/nohup ${EXEC} ${ARGS} -s ${SERVER} -p ${SECRET} &>> ${LOG} &
ExecStop=ps -fe |grep nezha-agent|awk '{print \$2}'|xargs kill
User=${RUN_USER}
Restart=on-abort
[Service]
Type=simple
ExecStart=/bin/nohup ${EXEC} ${ARGS} -s ${SERVER} -p ${SECRET} &>> ${LOG} &
ExecStop=ps -fe |grep nezha-agent|awk '{print \$2}'|xargs kill
User=${RUN_USER
[Install]
WantedBy=multi-user.target
EOF
}
Restart=on-abort
# 重载服务
systemctl daemon-reload
# 启动服务
systemctl start nezha
# 服务自启动
systemctl enable nezha
```
[Install]
WantedBy=multi-user.target
EOF
# 重载服务
systemctl daemon-reload
# 启动服务
systemctl start nezha
# 服务自启动
systemctl enable nezha
```
‼️修改对应信息后‼️
使用 `root` 账号执行上述命令即可安装完成。
‼️修改对应信息后‼️
使用 `root` 账号执行上述命令即可安装完成
</details>
<br/>
### 在 macOS 中安装 Agent
### 在 macOS 中安装 Agent
<details>
<summary>点击展开/收起</summary>
***本节内容改编自 [Mitsea Blog](https://blog.mitsea.com/e796f93db38d49e4b18df234c6ee75f5),改编已获得原作者授权***
<br/>
::: warning
安装过程中如提示“macOS 无法验证此 app“请前往系统设置手动允许程序运行
安装过程中如提示“macOS 无法验证此 app“请前往系统设置手动允许程序运行
:::
* 首先在管理面板中添加一台服务器
* 前往 [Release](https://github.com/nezhahq/agent/releases) 页下载 Agent 二进制文件,根据 CPU 架构选择下载 darwin amd64 还是 arm64 的 Agent
如 Intel CPU 下载 amd64Apple Silicon 下载 arm64 版本。下载完成后解压 Agent 二进制文件,如解压到下载文件夹
* 新建一个名为 `nezha_agent.plist` 的文件并保存,修改文件内容如下:
1. 首先在管理面板中添加一台服务器。
2. 前往 [Release](https://github.com/nezhahq/agent/releases) 页下载 Agent 二进制文件,根据 CPU 架构选择下载 darwin amd64 还是 arm64 的 Agent
如 Intel CPU 下载 amd64Apple Silicon 下载 arm64 版本。下载完成后解压 Agent 二进制文件,如解压到下载文件夹
3. 新建一个名为 `nezha_agent.plist` 的文件并保存,修改文件内容如下:
```xml
<?xml version="1.0" encoding="UTF-8"?>
@ -250,7 +245,7 @@ EOF
<string>--password</string>
<string>通信密钥529664783eeb23cc25</string>
<string>--server</string>
<string>通信网址和gRPC端口如:data.example.com:5555</string>
<string>通信网址和 gRPC 端口,如:data.example.com:5555</string>
</array>
<key>RunAtLoad</key>
<true/>
@ -258,25 +253,25 @@ EOF
</plist>
```
+ 在 Terminal 中使用下面的命令加载 plist 文件到 launchd 里,**注意替换文件路径**
4. 在 Terminal 中使用下面的命令加载 plist 文件到 launchd 里,**注意替换文件路径**
```shell
launchctl load /Users/123/Desktop/nezha_agent.plist
```
+ 启动进程
5. 启动进程:
```shell
launchctl start nezha_agent
```
+ 检查进程是否运行
6. 检查进程是否运行:
```shell
launchctl list | grep nezha_agent
```
+ 停止进程并移除
7. 停止进程并移除:
```shell
launchctl stop nezha_agent
@ -285,8 +280,8 @@ launchctl stop nezha_agent
```shell
launchctl remove nezha_agent
```
</details>
<br/>
### 在 macOS 中使用 Homebrew 安装 Agent
<details>
@ -294,93 +289,92 @@ launchctl remove nezha_agent
***本节内容改编自 [🐿️松鼠收集🌰](https://blog.mre.red/archives/install_nezha_monitoring_agent_service_with_homebrew),改编已获得原作者授权***
::: danger
请务必先添加环境变量,再通过 Homebrew 安装 nezha-agent
因 Homebrew 在软件安装时创建服务所需 plist 文件,若先安装再添加环境变量,会因缺少参数而启动失败
::: warning
请务必先添加环境变量,再通过 Homebrew 安装 nezha-agent
因 Homebrew 在软件安装时创建服务所需 plist 文件,若先安装再添加环境变量,会因缺少参数而启动失败
:::
* 添加环境变量
1. 添加环境变量:
```sh
```shell
echo 'export HOMEBREW_NEZHA_AGENT_PASSWORD="通信密钥,在服务页面获取"' >> ~/.zshrc
echo 'export HOMEBREW_NEZHA_AGENT_SERVER="你的服务器和端口,格式 your.domain:5555 "' >> ~/.zshrc
source ~/.zshrc
```
* 安装 Nezha Agent
2. 安装 Nezha Agent
::: danger
请注意,此 Homebrew 仓库由第三方维护,与哪吒监控无关。
Nezha 项目组不对该仓库的可用性和安全性等方面作出背书。在使用前,请自行评估风险!
Nezha 项目组不对该仓库的可用性和安全性等方面作出背书。在使用前,请自行评估风险!
:::
*由于暂未提交到 Homebrew Core 官方库,暂时放在上述博客作者参与维护的 [第三方 Homebrew 仓库](https://github.com/Brewforge/homebrew-chinese) 中*
由于暂未提交到 Homebrew Core 官方库,暂时放在上述博客作者参与维护的 [第三方 Homebrew 仓库](https://github.com/Brewforge/homebrew-chinese) 中
```sh
```shell
brew install brewforge/chinese/nezha-agent
```
* 通过 Homebrew 启动 Nezha Agent 服务
3. 通过 Homebrew 启动 Nezha Agent 服务:
```sh
```shell
brew services start nezha-agent
```
* 检查服务状态
4. 检查服务状态:
```sh
```shell
brew services info nezha-agent
```
* 停止服务
5. 停止服务:
```sh
```shell
brew services stop nezha-agent
```
* 卸载 Nezha Agent
6. 卸载 Nezha Agent
```sh
```shell
brew rm nezha-agent
```
* 报错时先检查环境变量
7. 报错时先检查环境变量:
```sh
```shell
echo $HOMEBREW_NEZHA_AGENT_PASSWORD
echo $HOMEBREW_NEZHA_AGENT_SERVER
```
* 若环境变量配置正确,再尝试重装
8. 若环境变量配置正确,再尝试重装:
```sh
```shell
brew services stop nezha-agent
brew reinstall nezha-agent
brew services start nezha-agent
```
* 若仍未解决,请前往上述 [第三方 Homebrew 仓库](https://github.com/Brewforge/homebrew-chinese) 提交工单
9. 若仍未解决,请前往上述 [第三方 Homebrew 仓库](https://github.com/Brewforge/homebrew-chinese) 提交 issue。
</details>
<br/>
### 在 OpenWRT 中安装 Agent
### 在 OpenWRT 中安装 Agent
<details>
<summary>点击展开/收起</summary>
**如何 一步到位,解决安装过程疑难杂症**
**如何一步到位,解决安装过程中的疑难杂症?**
* 请参考项目:
[NZ-OpenWrt](https://github.com/dysf888/NZ-OpenWrt)
<br/>
**如何使 旧版 OpenWRT/LEDE 自启动**
**如何使旧版 OpenWRT/LEDE 自启动?**
* 请参考项目:
[哪吒监控 For OpenWRT](https://github.com/Erope/openwrt_nezha)
<br/>
**如何使 新版 OpenWRT 自启动?来自 @艾斯德斯**
**如何使新版 OpenWRT 自启动? 贡献者:@艾斯德斯**
* 首先在 release 下载对应的二进制解压 zip 包后放置到 `/root`
* 首先在 release 下载对应的二进制解压 zip 包后放置到 `/root`
* 运行 `chmod +x /root/nezha-agent` 赋予执行权限,然后创建 `/etc/init.d/nezha-service`
```shell
@ -397,7 +391,7 @@ start_service() {
}
stop_service() {
killall nezha-agent
killall nezha-agent
}
restart() {
@ -407,15 +401,13 @@ restart() {
}
```
* 运行 `chmod +x /etc/init.d/nezha-service` 赋予执行权限
* 启动服务: `/etc/init.d/nezha-service enable && /etc/init.d/nezha-service start`
* 运行 `chmod +x /etc/init.d/nezha-service` 赋予执行权限。
* 启动服务: `/etc/init.d/nezha-service enable && /etc/init.d/nezha-service start`
</details>
<br/>
## 常见问题
### Agent 有 Docker 镜像吗?
## Agent 有 Docker 镜像吗?
**Agent 目前没有推出 Docker 镜像。**
Agent 的设计思路和 Dashboard 相反Dashboard 要尽可能不影响宿主机工作,但 Agent 则需要在宿主机中执行监控服务和运行命令。
将 Agent 放入容器中确实可以继续执行监控任务,但 WebShell 等功能无法正常运行,因此不提供 Docker 镜像。
将 Agent 放入容器中确实可以继续执行监控任务,但 WebShell 等功能无法正常运行,因此不提供 Docker 镜像。