Metrics
Basic System Metrics
Section titled “Basic System Metrics”# CPU usagetophtop
# Memoryfree -h
# Diskdf -h
# Networknetstat -tulpnss -tulpnDocker Stats
Section titled “Docker Stats”# Container resourcesdocker stats
# Specific containerdocker stats snapcode
# Health checkdocker inspect --format='{{.State.Health.Status}}' snapcodeNginx Status
Section titled “Nginx Status”Enable status module in nginx.conf:
location /nginx_status { stub_status on; allow 127.0.0.1; deny all;}curl http://localhost/nginx_statusPrometheus (Advanced)
Section titled “Prometheus (Advanced)”prometheus.yml
Section titled “prometheus.yml”global: scrape_interval: 15s
scrape_configs: - job_name: 'node' static_configs: - targets: ['localhost:9100']
- job_name: 'nginx' static_configs: - targets: ['localhost:9113']Node Exporter
Section titled “Node Exporter”# Installwget https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gztar xvfz node_exporter-*.tar.gz./node_exporter
# Metrics atcurl http://localhost:9100/metricsKey Metrics to Monitor
Section titled “Key Metrics to Monitor”| Metric | Description | Alert Threshold |
|---|---|---|
| CPU Usage | Processor load | > 80% |
| Memory | RAM usage | > 85% |
| Disk | Storage usage | > 90% |
| Response Time | Request latency | > 500ms |
| Error Rate | 5xx responses | > 1% |
| Uptime | Service availability | < 99.9% |
Grafana Dashboard
Section titled “Grafana Dashboard”# Dockerdocker run -d -p 3000:3000 grafana/grafana
# Accesshttp://localhost:3000# Default: admin/adminSimple Health Check
Section titled “Simple Health Check”# Cron job every 5 minutes*/5 * * * * curl -sf http://localhost:8000/health || echo "Service down" | mail -s "Alert" admin@example.comApplication Metrics (Python)
Section titled “Application Metrics (Python)”from prometheus_client import Counter, Histogram, start_http_server
REQUEST_COUNT = Counter('requests_total', 'Total requests')REQUEST_LATENCY = Histogram('request_latency_seconds', 'Request latency')
@REQUEST_LATENCY.time()def handle_request(): REQUEST_COUNT.inc() # ... handle request
start_http_server(9090) # Metrics endpoint