Skip to content

Commit

Permalink
Merge pull request #3 from vahid-dan/main
Browse files Browse the repository at this point in the history
Bug fixes, improvements and new modules and features
  • Loading branch information
vahid-dan authored Aug 25, 2023
2 parents f5c466c + 643db8f commit d1662db
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 19 deletions.
File renamed without changes.
96 changes: 96 additions & 0 deletions gateways/base/module-toggler.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/bash

# Network Interface Monitor Module
# Executd from the Gateways
# Monitors network activity on specific interface(s)
# Usage: Run when needed

########## HEADER ##########

module_name=general # status_update is a part of general

# Load utility functions and configurations for gateways
source /home/ubuntu/miscellaneous/gateways/base/utils.sh

# Redirect all output of this module to log_to_file function
exec > >(while IFS= read -r line; do log_to_file "$module_name" "$line"; echo "$line"; done) 2>&1

echo "########## START ##########"

########## BODY ##########

# Function to display modules and their status
display_modules_status() {
echo "Modules and their current status:"
modules=($(yq eval 'keys | .[]' $config_file))
for idx in "${!modules[@]}"; do
if [ "${modules[$idx]}" == "general" ]; then
status=$(yq e ".general.gateway_maintenance_mode" $config_file)
echo "$((idx+1)). ${modules[$idx]} (maintenance_mode): $status"
else
status=$(yq e ".${modules[$idx]}.enabled" $config_file)
echo "$((idx+1)). ${modules[$idx]}: $status"
fi
done
}

# Function to toggle the status of a module
toggle_module_status() {
local opt="$1"
local current_status

if [ "$opt" == "general" ]; then
current_status=$(get_config_value "general" "gateway_maintenance_mode")
if [ "$current_status" == "true" ]; then
yq eval ".${opt}.gateway_maintenance_mode = false" -i $config_file
echo "Set ${opt}'s gateway_maintenance_mode to false."
else
yq eval ".${opt}.gateway_maintenance_mode = true" -i $config_file
echo "Set ${opt}'s gateway_maintenance_mode to true."
fi
else
current_status=$(get_config_value "$opt" "enabled")
if [ "$current_status" == "true" ]; then
yq eval ".${opt}.enabled = false" -i $config_file
echo "Set ${opt} to false."
else
yq eval ".${opt}.enabled = true" -i $config_file
echo "Set ${opt} to true."
fi
fi
}

# Display modules and their status
display_modules_status

# Get user selection
echo "Select modules to toggle its status (e.g., 1 3 6) or enter 'q' to exit without changes:"
read -a selections

for selected in "${selections[@]}"; do
# Exit if user chooses the exit option
if [ "$selected" == "q" ]; then
echo "Exiting without changes."

# Validate user input
elif [ "$selected" -lt 1 ] || [ "$selected" -gt "${#modules[@]}" ]; then
echo "Invalid selection: $selected. Exiting without changes."

else
idx=$((selected-1))
toggle_module_status "${modules[$idx]}"
fi
done

# After toggling the status, display and log the final status
echo "Final Status:"
display_modules_status 2>&1 | tee $general_data_dir/$general_git_logs_branch/$general_module_toggler_log_file

########## FOOTER ##########

echo "########## END ##########"

# Close stdout and stderr
exec >&- 2>&-
# Wait for all background processes to complete
wait
24 changes: 24 additions & 0 deletions gateways/base/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set -e
config_file=/home/ubuntu/miscellaneous/gateways/config-files/config.yml

# General
export general_log_file=$(yq e '.general.log_file' $config_file)
export general_gateway_name=$(yq e '.general.gateway_name' $config_file)
export general_gateway_location=$(yq e '.general.gateway_location' $config_file)
export general_gateway_power_mode=$(yq e '.general.gateway_power_mode' $config_file)
Expand All @@ -17,6 +18,7 @@ export general_datalogger_data_dir=$(yq e '.general.datalogger_data_dir' $config
export general_git_repo=$(yq e '.general.git_repo' $config_file)
export general_git_data_branch=$(yq e '.general.git_data_branch' $config_file)
export general_git_logs_branch=$(yq e '.general.git_logs_branch' $config_file)
export general_module_toggler_log_file=$(yq e '.general.module_toggler_log_file' $config_file)

# Scheduler
export scheduler_enabled=$(yq e '.scheduler.enabled' $config_file)
Expand Down Expand Up @@ -80,6 +82,28 @@ export network_interface_monitor_interfaces=$(yq e '.network_interface_monitor.i
export led_monitor_enabled=$(yq e '.led_monitor.enabled' $config_file)
export led_monitor_log_file=$(yq e '.led_monitor.log_file' $config_file)

# LoRa
export lora_enabled=$(yq e '.lora.enabled' $config_file)
export lora_log_file=$(yq e '.lora.log_file' $config_file)
export lora_mode=$(yq e '.lora.mode' $config_file)
export lora_serial_interface=$(yq e '.lora.serial_interface' $config_file)
export lora_lora_interface=$(yq e '.lora.lora_interface' $config_file)
export lora_evio_interface=$(yq e '.lora.evio_interface' $config_file)
export lora_switch_interface=$(yq e '.lora.switch_interface' $config_file)
export lora_node_ip=$(yq e '.lora.node_ip' $config_file)
export lora_switch_ip=$(yq e '.lora.switch_ip' $config_file)
export lora_baud_rate=$(yq e '.lora.baud_rate' $config_file)
export lora_mtu=$(yq e '.lora.mtu' $config_file)
export lora_rate=$(yq e '.lora.rate' $config_file)
export lora_burst=$(yq e '.lora.burst' $config_file)
export lora_latency=$(yq e '.lora.latency' $config_file)
export lora_ingress_policing_rate=$(yq e '.lora.ingress_policing_rate' $config_file)
export lora_ingress_policing_burst=$(yq e '.lora.ingress_policing_burst' $config_file)

# Nebula
export nebula_enabled=$(yq e '.nebula.enabled' $config_file)
export nebula_log_file=$(yq e '.nebula.log_file' $config_file)

########## DEFINE FUNCTIONS ##########

# Check if module is enabled
Expand Down
16 changes: 6 additions & 10 deletions gateways/cron-jobs/non-root
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

# Captures and logs the status of the system
@reboot sleep 60 && /home/ubuntu/miscellaneous/gateways/system-monitors/status-monitor.sh
19 */6 * * * /home/ubuntu/miscellaneous/gateways/system-monitors/status-monitor.sh
25 00,08,14,20 * * * /home/ubuntu/miscellaneous/gateways/system-monitors/status-monitor.sh

# Pushes the new additions to the remote repo and runs Git garbage collection afterwards
@reboot sleep 120 && /home/ubuntu/miscellaneous/gateways/git-maintenance/git-push.sh; /home/ubuntu/miscellaneous/gateways/git-maintenance/git-garbage-collector.sh; /home/ubuntu/miscellaneous/gateways/git-maintenance/git-push.sh
20 * * * * /home/ubuntu/miscellaneous/gateways/git-maintenance/git-push.sh; /home/ubuntu/miscellaneous/gateways/git-maintenance/git-garbage-collector.sh; /home/ubuntu/miscellaneous/gateways/git-maintenance/git-push.sh
30 00,08,14,20 * * * /home/ubuntu/miscellaneous/gateways/git-maintenance/git-push.sh; /home/ubuntu/miscellaneous/gateways/git-maintenance/git-garbage-collector.sh; /home/ubuntu/miscellaneous/gateways/git-maintenance/git-push.sh

# Sends awake ping signals to healthchecks.io
* * * * * /home/ubuntu/miscellaneous/gateways/system-monitors/health-checks-io.sh
Expand All @@ -25,13 +25,9 @@
@reboot /home/ubuntu/miscellaneous/gateways/system-monitors/network-interface-monitor.sh

# Runs Nebula VPN
@reboot sleep 60 && sudo /usr/local/bin/restart_nebula.sh
00 * * * * sudo /usr/local/bin/restart_nebula.sh
@reboot sleep 90 && sudo /home/ubuntu/miscellaneous/gateways/remote-access/nebula.sh
00 * * * * sudo /home/ubuntu/miscellaneous/gateways/remote-access/nebula.sh

# Runs LoRa Radio
# On pendant node
@reboot sudo /usr/local/bin/restart_lora_at_pendant.sh 10.10.101.2/24 10.10.101.1/24
00 * * * * sudo /usr/local/bin/restart_lora_at_pendant.sh 10.10.101.2/24 10.10.101.1/24
# On switch node
@reboot sudo /usr/local/bin/restart_lora_at_noevio_gateway.sh 10.10.101.1/24
00 * * * * sudo /usr/local/bin/restart_lora_at_noevio_gateway.sh 10.10.101.1/24
@reboot sleep 90 && sudo /home/ubuntu/miscellaneous/gateways/remote-access/lora.sh
00 * * * * sudo /home/ubuntu/miscellaneous/gateways/remote-access/lora.sh
2 changes: 1 addition & 1 deletion gateways/git-maintenance/git-garbage-collector.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ for dir in "${dir_array[@]}"; do
df -h | grep $general_data_dir

cd $dir || continue
echo -e "Working on: $(pwd)"
echo -e "Processing: $(pwd)"
git gc --prune || continue

echo -e "After:"
Expand Down
6 changes: 1 addition & 5 deletions gateways/git-maintenance/git-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ readarray -t dir_array <<< "$git_push_directories"

for dir in "${dir_array[@]}"; do
timestamp=$(date +"%D %T %Z %z")
echo "Processing directory: $dir"

echo "Processing: $dir"
cd "$dir" || continue

git add .

git commit -m "$timestamp" || continue

for commit in $(git log --reverse --format="%H" --branches --not --remotes); do
git push --force origin $commit:refs/heads/$(git rev-parse --abbrev-ref HEAD) || continue
done
Expand Down
82 changes: 82 additions & 0 deletions gateways/remote-access/lora.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash

# LoRa Module
# This module sets up and configures the LoRa interface, applies traffic control, configures IP layer as NAT in "noevio" mode, and configures IP layer to route through the gateway it connects to in "pendant" mode.

# mode: evio (if the fitlet2 serves as an EdgeVPN (evio) switch)
# When fitlet2 gateway is connected to a LoRa radio but not a cell link (e.g. at the FCR Weir)
# It configures the LoRa tnc0 interface, applies traffic control, and configures IP layer to route through the gateway it connects to
# this assumes the evio docker container is already running
# docker run -d -v /home/$USER/.evio/config.json:/etc/opt/evio/config.json -v /var/log/evio/:/var/log/evio/ --restart always --privileged --name evio-node --network host edgevpnio/evio-node:latest

# mode: noevio (if the fitlet2 is not running as an EdgeVPN (evio) switch to the other node)
# When fitlet2 gateway is connected to a LoRa radio and a cell link (e.g. at the FCR Catwalk)

# mode: pendant
# When fitlet2 gateway is connected to a LoRa radio but not a cell link (e.g. at the FCR Weir)

# Usage: Run after reboot and periodically, every hour, for instance.

########## HEADER ##########

module_name=lora

# Load utility functions and configurations for gateways
source /home/ubuntu/miscellaneous/gateways/base/utils.sh

# Check if the module is enabled
check_if_enabled "$module_name"

# Redirect all output of this module to log_to_file function
exec > >(while IFS= read -r line; do log_to_file "$module_name" "$line"; echo "$line"; done) 2>&1

echo "########## START ##########"

########## BODY ##########

# Bring lora interface down and up
/usr/bin/killall tncattach || true

sleep 5

case $lora_mode in
"pendant")
/usr/local/bin/tncattach /dev/$lora_serial_interface $lora_baud_rate -d -e -n -m $lora_mtu -i $lora_node_ip
/usr/sbin/tc qdisc add dev $lora_lora_interface root tbf rate "$lora_rate"kbit burst "$lora_burst"kbit latency "$lora_latency"ms
/usr/sbin/ip route delete default
/usr/sbin/ip route add default via $lora_switch_ip
;;

"noevio")
/usr/local/bin/tncattach /dev/$lora_serial_interface $lora_baud_rate -d -e -n -m $lora_mtu -i $lora_node_ip
/usr/sbin/tc qdisc add dev $lora_lora_interface root tbf rate "$lora_rate"kbit burst "$lora_burst"kbit latency "$lora_latency"ms
echo 1 > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -t nat -A POSTROUTING -o $lora_switch_interface -j MASQUERADE
/sbin/iptables -A FORWARD -i $lora_switch_interface -o $lora_lora_interface -m state --state RELATED,ESTABLISHED -j ACCEPT
/sbin/iptables -A FORWARD -i $lora_lora_interface -o $lora_switch_interface -j ACCEPT
;;

"evio")
/usr/local/bin/tncattach /dev/$lora_serial_interface $lora_baud_rate -d -e -n -m $lora_mtu
/usr/bin/docker exec -it evio-node ovs-vsctl add-port $lora_evio_interface $lora_lora_interface
/usr/bin/docker exec -it evio-node ovs-vsctl set interface $lora_lora_interface ingress_policing_rate=$lora_ingress_policing_rate
/usr/bin/docker exec -it evio-node ovs-vsctl set interface $lora_lora_interface ingress_policing_burst=$lora_ingress_policing_burst
/usr/sbin/tc qdisc add dev $lora_lora_interface root tbf rate "$lora_rate"kbit burst "$lora_burst"kbit latency "$lora_latency"ms
/usr/sbin/sysctl -w net.ipv4.ip_forward=1
/usr/sbin/iptables -t nat -A POSTROUTING -s $lora_node_ip -j MASQUERADE
;;

*)
echo "Invalid mode: $lora_mode. Exiting."
exit 1
;;
esac

########## FOOTER ##########

echo "########## END ##########"

# Close stdout and stderr
exec >&- 2>&-
# Wait for all background processes to complete
wait
38 changes: 38 additions & 0 deletions gateways/remote-access/nebula.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Nebula Module
# This module manages the Nebula service by ensuring the service is restarted and logs are captured.
# Usage: Run after reboot and periodically, every hour, for instance.

########## HEADER ##########

module_name=nebula

# Load utility functions and configurations for gateways
source /home/ubuntu/miscellaneous/gateways/base/utils.sh

# Check if the module is enabled
check_if_enabled "$module_name"

# Redirect all output of this module to log_to_file function
exec > >(while IFS= read -r line; do log_to_file "$module_name" "$line"; echo "$line"; done) 2>&1

echo "########## START ##########"

########## BODY ##########

# Killing any running instance of nebula
/usr/bin/killall nebula || true

# Start nebula with configuration
nohup /etc/nebula/nebula -config /etc/nebula/config.yaml &

########## FOOTER ##########

echo "########## END ##########"

# Close stdout and stderr
exec >&- 2>&-
# Wait for all background processes to complete
wait
cront
7 changes: 4 additions & 3 deletions gateways/system-monitors/network-interface-monitor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ trap "exit" SIGINT SIGTERM
while read -r line; do
interface_name=$(echo "$line" | yq -r '.name')
read -r log_line
log_file=$(echo "$log_line" | yq -r '.log_file')
log_file_path=$general_data_dir/$general_git_logs_branch/$log_file
log_file_directory=$(echo "$log_line" | yq -r '.log_file_directory')
log_file_directory_path=$general_data_dir/$general_git_logs_branch/$log_file_directory
mkdir -p $general_data_dir/$general_git_logs_branch/$log_file_directory

# Continuously monitor interface
(
Expand All @@ -44,7 +45,7 @@ while read -r line; do
done

echo "Starting tcpdump for $interface_name..."
sudo tcpdump -i $interface_name -G $network_interface_monitor_log_rotation_interval -w "$log_file_path"_%Y-%m-%d_%H:%M:%S.pcap &
nohup sudo tcpdump -i $interface_name -G $network_interface_monitor_log_rotation_interval -w $log_file_directory_path/%Y-%m-%d_%H:%M:%S.pcap > /dev/null 2>&1 &
pid=$!

while kill -0 $pid 2>/dev/null && ip link show $interface_name up &>/dev/null; do
Expand Down

0 comments on commit d1662db

Please sign in to comment.