Uptime Kuma scripts

r00igev@@r

Honorary Master
Joined
Dec 14, 2009
Messages
15,639
Reaction score
14,157
Location
Draadloos Bantha poo doo in 4ways
So a while ago the forum script wizards helped me create a script for Uptime Kuma that measured speedtests. Its decent.

st.png

So my next one was to measure loss to cloudflare/quad9 as I was noticing occasional loss. Here is my script. Was wondering if this is the most optimal and if there is a better way of doing it?

#!/bin/bash
packet=$(ping -c 20 $1 | grep "packet loss" | awk -F ',' '{print $3}' | awk '{print $1}')
loss=$(echo $packet | rev | cut -c 2- | rev )
if [[ $loss < "3" ]]; then

curl -k -s "https://uptime.example.com/api/push/key?status=up&msg=OK&ping=$loss"

fi

Just implemented it and it seems fine!

pl.png
 
Your current script is functional, but there are several ways to make it more efficient and robust. Below is an optimized version of your script:


Bash:
#!/bin/bash

# Exit if no arguments were provided
if [ -z "$1" ]; then
    echo "Error: No target specified for ping."
    exit 1
fi

# Perform the ping and extract the packet loss percentage directly
loss=$(ping -c 20 "$1" | awk -F '[ %]+' '/packet loss/ {print $6}')

# Check if the 'loss' variable is a number, indicating we successfully got the packet loss
if ! [[ $loss =~ ^[0-9]+$ ]]; then
    echo "Error: Could not determine packet loss."
    exit 1
fi

# Compare loss as an integer, and send a notification if it's less than 3%
if (( loss < 3 )); then
    # Call the API. Be careful with sensitive information in URLs.
    curl -k -s "https://uptime.example.com/api/push/key?status=up&msg=OK&ping=$loss"
fi


Optimizations and improvements:

1. Argument Checking: The script now checks whether an argument was passed, improving its robustness by handling cases where it's called without arguments.

2. Direct Extraction: Instead of multiple commands (`grep`, multiple `awk`, `echo`, `rev`, `cut`), we use a single `awk` command. This method is more efficient because it spawns fewer subprocesses and avoids the overhead of passing data from one process to another.

3. Error Checking: The script checks if the `loss` variable holds a valid number (the percentage of packet loss). If `ping` fails or produces unexpected output (like in case of a network error), the script doesn't falsely report a 0% loss.

4. Integer Comparison: Instead of comparing strings, we compare the loss as an integer. Bash can perform arithmetic comparisons, which are more appropriate here than string comparisons.

5. Security Note: The `curl` command includes a URL that might be used with sensitive data. Ensure that this practice complies with your security protocols. Using HTTPS and potentially adding authentication headers is advisable for production environments.

This version of the script is more reliable and should provide more accurate results, as well as better handling of edge cases.



GPT4 says this.
 
Any way to get actual Speed Test data into Uptime Kuma?
Yebo!

Code:
#!/bin/bash
acceptable="100"
note=`/usr/bin/speedtest -p no`
URL=$( echo $note | sed 's/.*URL: //' )
download=$( echo $note | sed 's/^.*Download: //; s/ Mbps.*$//' )
upload=$( echo $note | sed 's/^.*Upload: //; s/ (data.*$//' )
latency=$( echo $note | sed 's/^.*Latency: //; s/ (.*$//' )
loss=$( echo $note | sed 's/^.*Loss: //; s/ Result.*$//' )
if [[ "$download" > "$acceptable" ]]; then
   curl -k -s "https://bazooka.amastelek.com/api/push/<key>?status=up&msg=OK&ping=$download"
fi
 
Yebo!

Code:
#!/bin/bash
acceptable="100"
note=`/usr/bin/speedtest -p no`
URL=$( echo $note | sed 's/.*URL: //' )
download=$( echo $note | sed 's/^.*Download: //; s/ Mbps.*$//' )
upload=$( echo $note | sed 's/^.*Upload: //; s/ (data.*$//' )
latency=$( echo $note | sed 's/^.*Latency: //; s/ (.*$//' )
loss=$( echo $note | sed 's/^.*Loss: //; s/ Result.*$//' )
if [[ "$download" > "$acceptable" ]]; then
curl -k -s "https://bazooka.amastelek.com/api/push/?status=up&msg=OK&ping=$download"
fi

So let’s assume I’m a complete idiot where does this go?

And what does it look like on the interface? Same as the rest just different values?
 
So let’s assume I’m a complete idiot where does this go?

And what does it look like on the interface? Same as the rest just different values?
This goes on a linux box like a pi from the node you are measuring. Its configured as a push notification. Run once an hour from cron. If you at home and have uk running there you can put it on that box.
This is on a 200mbs line with the only thing in the graphs being the its ms were it should be mbs.
speeduk.png
 
Top
Sign up to the MyBroadband newsletter
X