在Debian上監控Node.js應用程序的性能和資源使用情況有多種方法。以下是一些常用的工具和策略:
安裝PM2:
sudo npm install pm2 -g
啟動應用程序:
pm2 start app.js --name "my-app"
監控應用程序:
pm2 list
pm2 logs my-app
pm2 monit
pm2 top
安裝Prometheus:
wget https://github.com/prometheus/prometheus/releases/download/v2.34.0/prometheus_2.34.0.linux-amd64.deb
sudo dpkg -i prometheus_2.34.0.linux-amd64.deb
配置Prometheus抓取Node.js應用的指標:
編輯 /etc/prometheus/prometheus.yml
文件,添加抓取配置:
scrape_configs:
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
這里假設Node.js應用暴露了 /metrics
端點,可以使用 node_exporter
來抓取這些指標。
安裝Grafana:
wget https://dl.grafana.com/oss/release/grafana-8.2.0.linux-amd64.tar.gz
tar -zxvf grafana-8.2.0.linux-amd64.tar.gz
sudo mv grafana-8.2.0 /opt/grafana
配置Grafana:
編輯 /opt/grafana/conf/grafana.ini
文件,配置數據源為Prometheus。
啟動Grafana:
sudo grafana-server
在應用程序中添加代碼以創建一個健康檢查端點,例如:
const http = require('http');
http.createServer((req, res) => {
if (req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('OK');
} else {
// Your app's normal routes go here
}
}).listen(3000);
然后,你可以使用 curl
或其他HTTP客戶端來檢查應用程序的健康狀態。
通過上述方法,可以有效地監控Node.js應用在Debian上的運行狀態和性能。根據你的具體需求選擇合適的工具進行監控。