-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.sh
More file actions
executable file
·119 lines (106 loc) · 3.38 KB
/
telemetry.sh
File metadata and controls
executable file
·119 lines (106 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
###############################################################################
# Name: telemetry.sh
# Description:
# This script monitors hardware metrics, including CPU load, temperature,
# RAM, and SWAP (ZRAM) usage, with optional support for NVIDIA GPUs
# usage. Data is temporarily stored and shared privately through a
# Dockerized Telegram API call written in Python.
#
# Usage:
# ./telemetry.sh [OPTIONS]
# -t, --timestep <seconds> Set the timestep (default: 30 seconds)
# --nogpu Disable GPU monitoring
#
# Dependencies:
# bash, grep, awk
# nvidia-smi (for NVIDIA GPUs usage)
# Docker (rootless) or Podman
#
# Configuration:
# .config Hardware specifications
# Line 1: CPU thermal zone index number
# secrets.json Telegram API and Chat ID
# Template: {
# "chatID": "-12345",
# "token": "12345:ABCDE"
# }
#
# Author: Simone Roncallo
# Date: 2025-12-26
###############################################################################
set -e
# Get options
OPTS=$(getopt -o t: --long timestep:,nogpu -n 'telemetry.sh' -- "$@")
if [ $? -ne 0 ]; then
echo "Error"
exit 1
fi
eval set -- "$OPTS"
timestep=30 # Sampling period (seconds)
readgpu=true # Get GPU usage with nvidia-smi
while true; do
case "$1" in
--nogpu)
readgpu=false
shift 1
;;
-t | --timestep)
timestep="$2"
shift 2
;;
--) # End sequence
shift
break
;;
*) # Match errors
echo "Error"
exit 1
;;
esac
done
function sharedata() {
# Send data using Telegram API (running in Docker)
printf "\r\033[KSample #$1 -> Interrupt\n"
echo "Opening channel..."
docker run --rm --cap-drop=ALL --security-opt=no-new-privileges:true \
--memory=1024m --cpus=2 \
--user=puppet -v $2:/home/puppet/work/data:ro \
-v ./secrets.json:/home/puppet/work/secrets.json:ro \
telegram-bot:latest # Run Docker
rm -rf $2 # Remove temporary data
echo "Completed"
}
mapfile -t config < .config # Read configuration file
tzone=${config[0]} # CPU thermal zone number
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
TMP="./${TIMESTAMP}.tmp" # Output temporary directory
mkdir -p ./$TMP
> ./$TMP/memFree.txt # Free RAM (kB)
> ./$TMP/swpFree.txt # Free SWAP/ZRAM (kB)
> ./$TMP/cpuLoad.txt # Average CPU load (1 minute)
> ./$TMP/cpuTemp.txt # Temperature (mC)
if $readgpu; then
> ./$TMP/gpuUsed.txt # Used VRAM (MB)
> ./$TMP/gpuTotal.txt # Total VRAM (MB)
fi
counter=1
hostnamectl | grep "Operating System:" | awk '{print $3, $4, $5}' > ./$TMP/distroName.txt
hostnamectl | grep "Static hostname:" | awk '{print $3}' > ./$TMP/hostName.txt
nproc > ./$TMP/numCores.txt
grep MemTotal: /proc/meminfo | awk '{print $2}' > ./$TMP/memTotal.txt
grep SwapTotal: /proc/meminfo | awk '{print $2}' > ./$TMP/swpTotal.txt
while true; do
printf "\r\033[KSample #${counter}"
grep MemFree: /proc/meminfo | awk '{print $2}' >> ./$TMP/memFree.txt
grep SwapFree: /proc/meminfo | awk '{print $2}' >> ./$TMP/swpFree.txt
cat /proc/loadavg | awk '{print $1}' >> ./$TMP/cpuLoad.txt
cat /sys/class/thermal/thermal_zone${tzone}/temp >> ./$TMP/cpuTemp.txt
if $readgpu; then # Read NVIDIA GPU metrics
nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits >> ./$TMP/gpuUsed.txt
nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits >> ./$TMP/gpuTotal.txt
fi
counter=$(($counter + 1))
sleep $timestep
trap "sharedata $counter $TMP" SIGINT
done