Skip to content

Instantly share code, notes, and snippets.

@mtongnz
Last active January 5, 2025 04:03
Show Gist options
  • Select an option

  • Save mtongnz/a7d88f65a78699ca6c427058d376ae02 to your computer and use it in GitHub Desktop.

Select an option

Save mtongnz/a7d88f65a78699ca6c427058d376ae02 to your computer and use it in GitHub Desktop.
This is a User Script for unRaid to test containers have an internet connection via a container:vpn network. If the vpn is down (i.e. no internet) then run commands to restart it.
#!/usr/bin/php
<?php
#description=Container VPN Tester
$container_label = "label_here=value";
$restart_commands = [
"php /boot/config/plugins/user.scripts/scripts/restartContainerVPN/script"
];
echo "\n\nChecking VPN connections...\n";
exec("docker ps --filter 'label={$container_label}' --format='{{.Names}}'", $containers);
foreach($containers as $container) {
$status_output = [];
exec("docker inspect --format='{{json .State.Health.Status}}' $container", $status_output);
$status = str_replace(['"',"'"], "", $status_output[0]);
if ($status == "unhealthy") {
echo " - {$container} is UNHEALTHY!\n\n";
echo "Restarting stacks...\n\n";
foreach($restart_commands as $restart_command) {
$restart_output = [];
exec($restart_command, $restart_output);
foreach ($restart_output as &$line)
echo "{$line}\n";
}
break;
} else {
echo " - {$container} is {$status}\n";
}
}
?>
@mtongnz
Copy link
Author

mtongnz commented Jan 5, 2025

Usage

This is an unRaid User Script. Install it from CA Apps.

Script Setup

- $container_label

The label you assign to the containers to check. Note that this can be set to any label you'd like.
If this contains '=' then the label must equal the value supplied. If not, then any container with the label regardless of value will be checked.

- $restart_commands

The command(s) to run. Separate each command by a comma.
The example provided will run a User Script called restartContainerVPN.

- Cron Schedule

Set the script to run every 15 minutes (or similar). On the main User Scripts page, set the schedule to Custom and enter the following cron: */15 * * * *

Docker Setup

Add your label to each container that you'd like to be checked.
Ensure each container has a health check that tests internet connectivity. After adding this, you should see the health status (in brackets) next to your uptime.

- Healthcheck via unRaid GUI:

Add this to extra parameters for each container. (Note - I haven't tested this)
--health-cmd='curl --fail http://google.com/ || exit 1' --health-interval=10s --health-retries=5 --health-timeout=10s --health-start-period=5m

- Healthcheck via Docker Compose:

Add this to each container:

    healthcheck:
      test: curl --fail http://google.com || exit 1
      start_period: 5m
      interval: 30s
      timeout: 10s
      retries: 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment