Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active February 19, 2023 02:17
Show Gist options
  • Save gilangvperdana/6043de0807d9fa3b36969c7cda0adc14 to your computer and use it in GitHub Desktop.
Save gilangvperdana/6043de0807d9fa3b36969c7cda0adc14 to your computer and use it in GitHub Desktop.
Screenshoot Grafana Dashboard with Chrome Headless

Goals

  • Can automaticly screenshoot Grafana Dashboard with Google Chrome Headless on Ubuntu Server then send to Telegram Group with Telegram BOT

Prerequisites

  • Google Chrome
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
sudo apt-get update
sudo apt-get install google-chrome-stable

Script

#!/bin/bash

# URL website yang ingin di-screenshot
URL="https://grafana.example.org/d/DASHBOARD_ID/DASHBOARD_NAME?orgId=1&refresh=10s"

# Timeout Script
TIMEOUT=60

# Path untuk menyimpan hasil screenshot
FILEPATH="/root/dll/screenshot.png"

# ID bot telegram yang akan digunakan untuk mengirim hasil screenshot
BOTID="BOT_ID_TOKEN"
CHATID="CHAT_ID_TOKEN"

# Take the screenshot using Chrome Headless
timeout $TIMEOUT /usr/bin/google-chrome --headless --disable-gpu --no-sandbox --screenshot="$FILEPATH" --user-data-dir=/root/.config/google-chrome/ --window-size=1920,1080 --virtual-time-budget=1000 --run-all-compositor-stages-before-draw "$URL"


# Kirim hasil screenshot ke bot Telegram
curl -s -X POST "https://api.telegram.org/bot$BOTID/sendPhoto" \
    -F chat_id="$CHATID" \
    -F photo="@$FILEPATH" \
    -F caption="Server Utilization Live Report on $URL"

# Hapus hasil screenshot setelah dikirim
rm "$FILEPATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment