Created
August 8, 2024 13:57
-
-
Save danpawlik/cbec3d9ae1fbf4652a86c6cafc7bd652 to your computer and use it in GitHub Desktop.
Start Prometheus and Grafana localy and configure OpenWRT exporter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
MAIN_DIR=~/monitoring | |
ROUTER_NODE_EXPORTER_ADDRESS="192.168.88.1:9100" | |
if ! [ -d "$MAIN_DIR" ]; then | |
mkdir -p $MAIN_DIR | |
fi | |
# NOTE: https://grafana.com/blog/2021/02/09/how-i-monitor-my-openwrt-router-with-grafana-cloud-and-prometheus/ | |
# Remember to run on router: | |
# opkg update && \ | |
# opkg install prometheus-node-exporter-lua \ | |
# prometheus-node-exporter-lua-nat_traffic \ | |
# prometheus-node-exporter-lua-netstat \ | |
# prometheus-node-exporter-lua-openwrt \ | |
# prometheus-node-exporter-lua-wifi \ | |
# prometheus-node-exporter-lua-wifi_stations | |
# | |
# cat << EOF > /etc/config/prometheus-node-exporter-lua | |
# config prometheus-node-exporter-lua 'main' | |
# option listen_ipv6 '0' | |
# option listen_port '9100' | |
# option listen_interface 'lan' | |
# EOF | |
# | |
# /etc/init.d/prometheus-node-exporter-lua restart | |
################ | |
## Prometheus ## | |
################ | |
mkdir -p $MAIN_DIR/prometheus/etc | |
mkdir -p $MAIN_DIR/prometheus/var | |
chmod 0777 -R $MAIN_DIR/prometheus/etc $MAIN_DIR/prometheus/var/ | |
cat << EOF > $MAIN_DIR/prometheus/etc/prometheus.yaml | |
global: | |
evaluation_interval: "1m" | |
scrape_interval: "1m" | |
scrape_timeout: "10s" | |
scrape_configs: | |
- job_name: node | |
static_configs: | |
- targets: | |
- $ROUTER_NODE_EXPORTER_ADDRESS | |
EOF | |
podman create --name prometheus \ | |
--network host \ | |
-v $MAIN_DIR/prometheus/etc:/etc/prometheus/:Z \ | |
-v $MAIN_DIR/prometheus/var:/prometheus:Z \ | |
quay.io/prometheus/prometheus:latest \ | |
--config.file=/etc/prometheus/prometheus.yaml | |
podman start prometheus | |
############# | |
## Grafana ## | |
############# | |
mkdir -p $MAIN_DIR/grafana/etc/{secrets,provisioning} | |
mkdir -p $MAIN_DIR/grafana/etc/provisioning/{datasources,dashboards} | |
mkdir -p $MAIN_DIR/grafana/var/grafana/plugins | |
chmod 0777 -R $MAIN_DIR/grafana/etc $MAIN_DIR/grafana/var/ | |
cat << EOF > $MAIN_DIR/grafana/etc/secrets/admin_password | |
admin | |
EOF | |
cat << EOF > $MAIN_DIR/grafana/etc/secrets/admin_user | |
admin | |
EOF | |
# Create simply config | |
cat << EOF > $MAIN_DIR/grafana/etc/grafana.ini | |
[paths] | |
[server] | |
root_url = http://localhost:3000/ | |
EOF | |
# Create datasource for Prometheus | |
cat << EOF > $MAIN_DIR/grafana/etc/provisioning/datasources/prometheus.yaml | |
--- | |
apiVersion: 1 | |
datasources: | |
- id: 1 | |
uid: edu7l5b3oledca | |
name: prometheus | |
type: prometheus | |
typeName: Prometheus | |
access: proxy | |
url: http://localhost:9090 | |
basicAuth: false | |
isDefault: true | |
readOnly: false | |
version: 1 | |
EOF | |
# Create dashboard config file | |
cat << EOF > $MAIN_DIR/grafana/etc/provisioning/dashboards/dashboards.yaml | |
--- | |
apiVersion: 1 | |
providers: | |
- name: 'default' | |
orgId: 1 | |
type: file | |
disableDeletion: true | |
editable: false | |
updateIntervalSeconds: 10 | |
allowUiUpdates: false | |
options: | |
path: /etc/grafana/provisioning/dashboards | |
foldersFromFilesStructure: true | |
EOF | |
# Download dashboard - https://grafana.com/grafana/dashboards/18153-openwrt/ | |
# curl -SL https://grafana.com/api/dashboards/18153/revisions/3/download > $MAIN_DIR/grafana/etc/provisioning/dashboards/18153.json | |
podman create --name grafana \ | |
--network host \ | |
-v $MAIN_DIR/grafana/etc:/etc/grafana:Z \ | |
-v $MAIN_DIR/grafana/var:/var/lib/grafana/:z \ | |
-e GF_SECURITY_ADMIN_PASSWORD__FILE="/etc/grafana/secrets/admin_password" \ | |
-e GF_SECURITY_ADMIN_USER__FILE="/etc/grafana/secrets/admin_user" \ | |
-e GF_AUTH_ANONYMOUS_ENABLED='true' \ | |
-e GF_USERS_ALLOW_SIGN_UP='true' \ | |
grafana/grafana:10.4.6 | |
podman start grafana |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment