Monitoring & Logging
Prometheus & Grafana
Prometheus CLI Commands
Prometheus Configuration (prometheus.yml)
scrape_configs:
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
- job_name: 'kubernetes'
static_configs:
- targets: ['kube-state-metrics:8080']
Prometheus Alert Manager Configuration (alertmanager.yml)
route:
receiver: 'slack'
receivers:
- name: 'slack'
slack_configs:
- channel: '#alerts'
send_resolved: true
api_url: 'https://hooks.slack.com/services/your_webhook_url'
Useful PromQL Queries
Grafana CLI Commands
Grafana API Commands
List dashboards
curl -X GET http://localhost:3000/api/search\?query\=\&type\=dash-db -H "Authorization: Bearer <API_TOKEN>"
Create dashboard
curl -X POST http://localhost:3000/api/dashboards/db -H "Content-Type: application/json" -H "Authorization: Bearer <API_TOKEN>" --data '@dashboard.json'
Add Prometheus data source
curl -X POST http://localhost:3000/api/datasources -H "Content-Type:application/json" -H "Authorization: Bearer <API_TOKEN>" --data '{ "name":"Prometheus", "type": "prometheus", "url": "http://localhost:9090", "access":"proxy" }'
Integrating Prometheus with Grafana
- Add Prometheus Data Source in Grafana
- Grafana → Configuration → Data Sources → Add Prometheus
- Set URL to http://localhost:9090
- Click Save & Test
- Import a Prebuilt Dashboard
- Grafana → Dashboards → Import
- Enter Dashboard ID from Grafana Repository
- Select Prometheus as data source → Import
- Create a New Dashboard
- Grafana → Dashboards → New Dashboard
- Add PromQL queries for visualization
- Choose Panel Type (Graph, Gauge, Table, etc.)
- Click Save Dashboard
Grafana Alerting Setup
Create Alert in Grafana
- Open Dashboard → Edit Panel
- Click Alert → Create Alert
- Set Condition (e.g., CPU usage > 80%)
- Define Evaluation Interval (e.g., Every 1 min)
- Configure Notification Channels (Slack, Email, PagerDuty, etc.)
- Click Save Alert
Configure Slack Alerts in Grafana
- Grafana → Alerting → Notification Channels
- Click Add New Channel
- Select Slack, enter Webhook URL
- Click Save & Test
ELK Stack (Elasticsearch, Logstash, Kibana)
1. Elasticsearch Commands
Elasticsearch CLI Commands
Index Management
Insert a document
curl -X PUT "http://localhost:9200/index\_name/\_doc/1" -H "Content-Type: application/json" -d '{"name": "DevOps"}'
Elasticsearch Query Examples
Query using JSON
curl -X GET "http://localhost:9200/index\_name/\_search" -H "Content-Type: application/json" -d '{ "query": { "match": { "name": "DevOps" } } }'
Get all documents
curl -X GET "http://localhost:9200/index\_name/\_search?pretty" -H "Content-Type: application/json" -d '{ "size": 10, "query": { "match_all": {} } }'
2. Logstash Commands
Logstash CLI Commands
Sample Logstash Configuration (logstash.conf)
input {
file {
path => "/var/log/syslog"
start_position => "beginning"
}
}
filter {
grok {
match => {
"message" => "%{SYSLOGTIMESTAMP:timestamp} %{HOSTNAME:host} %{DATA:process}: %{GREEDYDATA:log_message}"
}
}
}
output {
elasticsearch {
hosts => ["http://localhost:9200"]
index => "logstash-logs"
}
stdout { codec => rubydebug }
}
3. Kibana Commands
Kibana CLI Commands
Kibana API Commands
Kibana Dashboard Import Example
curl -X POST "http://localhost:5601/api/saved\_objects/\_import" -H "kbn-xsrf: true" --form file=@dashboard.ndjson
4. Integrating ELK Stack
1. Configure Elasticsearch in Kibana
- Go to Kibana → Management → Stack Management → Data Views
- Click Create Data View
- Set Index Pattern as logstash-*
- Click Save
2. Configuring Logstash to Send Logs to Elasticsearch
- Open
/etc/logstash/logstash.conf
- Ensure the output points to Elasticsearch:
5. Visualizing Logs in Kibana
- Go to Kibana → Discover
- Select logstash-* Data View
- Apply Filters & View Logs
6. ELK Stack Monitoring
Monitor Elasticsearch Cluster Health
Monitor Logstash Logs
Monitor Kibana Logs
Datadog
Datadog Agent Installation (Linux)
DD_API_KEY=your_api_key bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script.sh)"
Enable Log Monitoring
Datadog Agent CLI Commands
Metric Queries
Datadog API Commands
Submit a custom metric
curl -X POST "https://api.datadoghq.com/api/v1/series" \
-H "DD-API-KEY: <API_KEY>" \
-H "Content-Type: application/json" \
--data '{ "series": [{ "metric": "custom.metric", "points": [[1633000000, 10]],"type": "gauge", "tags": ["env:prod"] }] }'
Datadog Configuration
/etc/datadog-agent/datadog.yaml
api_key: "<YOUR_API_KEY>"
site: "datadoghq.com"
apm_config:
enabled: true
Datadog Log Collection Setup
Monitor Logs in Datadog UI
- Go to Datadog → Logs → Live Tail
- Filter logs by service, environment, or host
Datadog Monitoring Commands
Datadog Kubernetes Agent Installation
kubectl apply -f https://raw.githubusercontent.com/DataDog/datadog-agent/main/Dockerfiles/manifests/agent.yaml
Datadog Kubernetes Monitoring
Datadog Integrations for DevOps
Datadog Log Collection for Docker
docker run -d --name datadog-agent \
-e DD_API_KEY=<YOUR_API_KEY> \
-e DD_LOGS_ENABLED=true \
-e DD_CONTAINER_EXCLUDE="name:datadog-agent" \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
-v /proc/:/host/proc/:ro \
-v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro \
datadog/agent
Datadog APM (Application Performance Monitoring)
Datadog CI/CD Monitoring
Track CI/CD pipeline duration
curl -X POST "https://api.datadoghq.com/api/v1/series" \
-H "DD-API-KEY: <API_KEY>" \
-H "Content-Type: application/json" \
--data '{ "series": [{ "metric": "ci.pipeline.duration", "points": [[1633000000, 30]], "type": "gauge", "tags": ["pipeline:deploy"] }] }'
Datadog Synthetic Monitoring (API Tests)
curl -X POST "https://api.datadoghq.com/api/v1/synthetics/tests" \
-H "DD-API-KEY: <API_KEY>" \
-H "Content-Type: application/json" \
--data '{
"config": { "request": { "method": "GET", "url": "https://example.com" },
"assertions": [{ "operator": "is", "type": "statusCode", "target": 200 }] },
"locations": ["aws:us-east-1"],
"message": "Website should be reachable",
"name": "Website Availability Test",
"options": { "monitor_options": { "renotify_interval": 0 } },
"tags": ["env:prod"],
"type": "api"
}'
Datadog Dashboard & Alerts
Create a new dashboard
curl -X POST "https://api.datadoghq.com/api/v1/dashboard" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: <API_KEY>" \
--data '{
"title": "DevOps Dashboard",
"widgets": [
{
"definition": {
"type": "timeseries",
"requests": [
{ "q": "avg:system.cpu.user{*}" }
]
}
}
]
}'
Create an alert
curl -X POST "https://api.datadoghq.com/api/v1/monitor" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: <API_KEY>" \
--data '{
"name": "High CPU Usage",
"type": "query alert",
"query": "avg(last_5m):avg:system.cpu.user{*} > 80",
"message": "CPU usage is too high!",
"tags": ["env:prod"]
}'
Datadog Incident Management
curl -X POST "https://api.datadoghq.com/api/v1/incidents" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: <API_KEY>" \
--data '{
"data": {
"type": "incidents",
"attributes": {
"title": "Production Outage",
"customer_impact_scope": "global",
"customer_impact_duration": 30,
"severity": "critical",
"state": "active",
"commander": "DevOps Team"
}
}
}'
New Relic
Install New Relic Agent
For Linux Servers
curl -Ls https://download.newrelic.com/install/newrelic-cli/scripts/install.sh | newrelic install
Query Logs & Metrics
NRQL Queries (New Relic Query Language)
New Relic Agent CLI Commands
New Relic API Commands
List all applications
curl -X GET "https://api.newrelic.com/v2/applications.json" -H "X-Api-Key:<API_KEY>" -H "Content-Type: application/json"
List monitored servers
curl -X GET "https://api.newrelic.com/v2/servers.json" -H "X-Api-Key:<API_KEY>"
Record a deployment
curl -X POST "https://api.newrelic.com/v2/applications/<APP_ID>/deployments.json" -H "X-Api-Key:<API_KEY>" -H "Content-Type: application/json" -d '{ "deployment": { "revision": "1.0.1", "description": "New deployment", "user": "DevOps Team" } }'
New Relic Configuration
/etc/newrelic-infra.yml
license_key: "<YOUR_LICENSE_KEY>"
log_file: /var/log/newrelic-infra.log
custom_attributes:
environment: production
New Relic Log Monitoring Setup
Enable Log Forwarding
Edit: /etc/newrelic-infra.yml:
logs:
enabled: true
include:
- /var/log/syslog
- /var/log/nginx/access.log
View Logs in New Relic UI
- Go to New Relic → Logs
- Filter logs by application, environment, or tags