Pi66

pino-rs
Rust notification daemon.
git clone https://git.pi66.xyz/pino-rs

← back to log

Add some usefull monitoring scripts

author: pixel
date: 2025-04-15 21:54
hash: 3e3b73cdd2727aa8c7e7826de2a2a8a65155e1de

Diffstat:

A

scripts/battery-monitor.sh
40 ++++++++++++++++++++++++++++++

A

scripts/cpu-monitor.sh
22 ++++++++++++++++

A

scripts/memory-monitor.sh
19 ++++++++++++++
 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
diff --git a/scripts/battery-monitor.sh b/scripts/battery-monitor.sh
new file mode 100755
index 0000000..c642220
--- /dev/null
+++ b/scripts/battery-monitor.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+# Battery Monitor - Fixed and Optimized
+UP_THRESHOLD=90
+DOWN_THRESHOLD=20
+CHECK_INTERVAL=0.3
+
+# Find battery path (more reliable method)
+BATTERY_PATH=$(find /sys/class/power_supply/ -name "BAT*" | head -1)
+AC_PATH=$(find /sys/class/power_supply/ -name "AC*" | head -1)
+[ -z "$BATTERY_PATH" ] && echo "No battery found" && exit 1
+
+last_level=<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>a</mi><mi>t</mi><mi>"</mi></mrow></math>BATTERY_PATH/capacity")
+last_ac_status=<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>a</mi><mi>t</mi><mi>"</mi></mrow></math>AC_PATH/online" 2>/dev/null || echo "0")
+
+while true; do
+    level=<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>a</mi><mi>t</mi><mi>"</mi></mrow></math>BATTERY_PATH/capacity")
+    ac_status=<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>a</mi><mi>t</mi><mi>"</mi></mrow></math>AC_PATH/online" 2>/dev/null || echo "0")
+    
+    # 1. Plug detection (AC status changed from 0 to 1)
+    if [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>a</mi><msub><mi>c</mi><mi>s</mi></msub><mi>t</mi><mi>a</mi><mi>t</mi><mi>u</mi><mi>s</mi><mi>"</mi><mo>&#x02212;</mo><mi>e</mi><mi>q</mi><mn>1</mn><mo stretchy="false">]</mo><mi>&</mi><mi>&</mi><mo stretchy="false">[</mo><mi>"</mi></mrow></math>last_ac_status" -eq 0 ]; then
+        pino -t "Plugged In" -m "Now charging ($level%)" -d 5
+    
+    # 2. Unplug detection (AC status changed from 1 to 0)
+    elif [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>a</mi><msub><mi>c</mi><mi>s</mi></msub><mi>t</mi><mi>a</mi><mi>t</mi><mi>u</mi><mi>s</mi><mi>"</mi><mo>&#x02212;</mo><mi>e</mi><mi>q</mi><mn>0</mn><mo stretchy="false">]</mo><mi>&</mi><mi>&</mi><mo stretchy="false">[</mo><mi>"</mi></mrow></math>last_ac_status" -eq 1 ]; then
+        pino -t "Unplugged" -m "On battery ($level%)" -d 5
+    
+    # 3. Crossed above 90% (only when discharging)
+    elif [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>l</mi><mi>e</mi><mi>v</mi><mi>e</mi><mi>l</mi><mi>"</mi><mo>&#x02212;</mo><mi>g</mi><mi>e</mi></mrow></math>UP_THRESHOLD ] && [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>l</mi><mi>a</mi><mi>s</mi><msub><mi>t</mi><mi>l</mi></msub><mi>e</mi><mi>v</mi><mi>e</mi><mi>l</mi><mi>"</mi><mo>&#x02212;</mo><mi>l</mi><mi>t</mi></mrow></math>UP_THRESHOLD ] && [ "$ac_status" -eq 0 ]; then
+        pino -t "Battery High" -m "Reached $level%" -d 5
+    
+    # 4. Crossed below 20% (only when discharging)
+    elif [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>l</mi><mi>e</mi><mi>v</mi><mi>e</mi><mi>l</mi><mi>"</mi><mo>&#x02212;</mo><mi>l</mi><mi>e</mi></mrow></math>DOWN_THRESHOLD ] && [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>l</mi><mi>a</mi><mi>s</mi><msub><mi>t</mi><mi>l</mi></msub><mi>e</mi><mi>v</mi><mi>e</mi><mi>l</mi><mi>"</mi><mo>&#x02212;</mo><mi>g</mi><mi>t</mi></mrow></math>DOWN_THRESHOLD ] && [ "$ac_status" -eq 0 ]; then
+        pino -t "Battery Low" -m "Only $level% left" -d 5
+    fi
+    
+    last_level=$level
+    last_ac_status=$ac_status
+    sleep $CHECK_INTERVAL
+done
diff --git a/scripts/cpu-monitor.sh b/scripts/cpu-monitor.sh
new file mode 100755
index 0000000..af03434
--- /dev/null
+++ b/scripts/cpu-monitor.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+MAX_TEMP=70
+CHECK_INTERVAL=0.3
+THERMAL_ZONE="/sys/class/thermal/thermal_zone3/temp"
+COOLDOWN_TEMP=$((MAX_TEMP - 20))  # 50°C in this case
+
+[ ! -f "$THERMAL_ZONE" ] && pino -t "Error" -m "Thermal zone not found!" -d 10 && exit 1
+
+while true; do
+    temp=<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mo stretchy="false">&#x00028;</mo></mrow></math>(cat "$THERMAL_ZONE") / 1000))
+    
+    if [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>t</mi><mi>e</mi><mi>m</mi><mi>p</mi><mi>"</mi><mo>&#x02212;</mo><mi>g</mi><mi>t</mi><mi>"</mi></mrow></math>MAX_TEMP" ]; then
+        pino -t "CPU Overheating Warning" -m "Current temperature: ${temp}°C" -d 5
+        while [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>t</mi><mi>e</mi><mi>m</mi><mi>p</mi><mi>"</mi><mo>&#x02212;</mo><mi>g</mi><mi>t</mi><mi>"</mi></mrow></math>COOLDOWN_TEMP" ]; do
+            sleep "$CHECK_INTERVAL"
+            temp=<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mo stretchy="false">&#x00028;</mo></mrow></math>(cat "$THERMAL_ZONE") / 1000))
+        done
+    fi
+    
+    sleep "$CHECK_INTERVAL"
+done
diff --git a/scripts/memory-monitor.sh b/scripts/memory-monitor.sh
new file mode 100755
index 0000000..e4ca91b
--- /dev/null
+++ b/scripts/memory-monitor.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+
+MEM_HIGH=85    
+MEM_LOW=65     
+CHECK_INTERVAL=0.5
+
+alert_triggered=false
+
+while true; do
+    mem_used=<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>f</mi><mi>r</mi><mi>e</mi><mi>e</mi><mo stretchy="false">&#x0007C;</mo><mi>a</mi><mi>w</mi><msup><mi>k</mi><mi>&#x02032;</mi></msup><mo>&#x0002F;</mo><mi>M</mi><mi>e</mi><mi>m</mi><mo>&#x0002F;</mo><mrow><mi>u</mi><mi>s</mi><mi>a</mi><mi>g</mi><mi>e</mi><mo>&#x0003D;</mo></mrow></mrow></math>3/$2*100; printf("%.0f", usage)}')
+    if [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>m</mi><mi>e</mi><msub><mi>m</mi><mi>u</mi></msub><mi>s</mi><mi>e</mi><mi>d</mi><mi>"</mi><mo>&#x02212;</mo><mi>g</mi><mi>e</mi><mi>"</mi></mrow></math>MEM_HIGH" ] && [ "$alert_triggered" = false ]; then
+        pino -t "Memory Alert" -m "High RAM usage: ${mem_used}%" -d 5
+        alert_triggered=true
+    elif [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>m</mi><mi>e</mi><msub><mi>m</mi><mi>u</mi></msub><mi>s</mi><mi>e</mi><mi>d</mi><mi>"</mi><mo>&#x02212;</mo><mi>l</mi><mi>t</mi><mi>"</mi></mrow></math>MEM_LOW" ] && [ "$alert_triggered" = true ]; then
+        alert_triggered=false
+    fi
+    
+    sleep "$CHECK_INTERVAL"
+done