Skip to content

Commit

Permalink
[refactor] Added main, help functions to agent #33
Browse files Browse the repository at this point in the history
Closes #33
  • Loading branch information
devkapilbansal authored Sep 19, 2021
1 parent 7be1724 commit bac6d54
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 79 deletions.
185 changes: 107 additions & 78 deletions openwrt-openwisp-monitoring/files/monitoring.agent
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,35 @@
#
# OpenWISP Monitoring Daemon

VERSION=0 # default value of version
# parse options
while [ -n "$1" ]; do
case "$1" in
--version | -v)
export VERSION=1
break
;;
--url)
export BASE_URL="$2"
shift
;;
--uuid)
export UUID="$2"
shift
;;
--key)
export KEY="$2"
shift
;;
--verify_ssl)
export VERIFY_SSL="$2"
shift
;;
--interval)
export INTERVAL="$2"
shift
;;
--monitored_interfaces)
export MONITORED_INTERFACES="$2"
shift
;;
--verbose_mode)
export VERBOSE_MODE="$2"
shift
;;
--required_memory)
export REQUIRED_PERCENT="$2"
shift
;;
--mode)
export MODE="$2"
shift
;;
--max_retries)
export MAX_RETRY="$2"
shift
;;
-*)
echo "Invalid option: $1"
exit 1
;;
*) break ;;
esac
shift
done

if [ "$VERSION" -eq "1" ]; then
VERSION=$(cat /etc/openwisp/monitoring/VERSION)
echo "openwisp-monitoring $VERSION"
show_help() {
printf "Usage:\n"
printf " openwisp_monitoring [OPTIONS...]\n"
printf "\n"
printf "Runs OpenWISP Monitoring Agent for collecting and sending data.\n"
printf "\n"
printf "General options:\n"
printf " --help\t\t\t\t: Show this help text\n"
printf " -v, --version\t\t\t\t: Shows version of the agent.\n"
printf " --mode <send, collect>\t\t: Mode for running Monitoring Agent.\n"
printf "OpenWISP Config options:\n"
printf " --url <server-url>\t\t\t: URL of the OpenWISP server.\n"
printf " --uuid <uuid>\t\t\t\t: UUID of the device.\n"
printf " --key <key>\t\t\t\t: Key for the device.\n"
printf " --verify_ssl <0, 1>\t\t\t: Whether SSL Authentication should be enabled or not.\n"
printf "OpenWISP Monitoring config options:\n"
printf " --interval <time>\t\t\t: Time in seconds after which data should be sent/collected.\n"
printf " --monitored-interfaces <interfaces>\t: Interfaces that needs to be monitored.\n"
printf " --verbose-mode <0, 1>\t\t\t: Run agent in verbose mode.\n"
printf " --required-memory <0-1>\t\t: Fraction of total memory that should be available to collect data.\n"
printf " --max-retries <max-retries-count>\t: No. of time agent should retry to send data.\n"
exit 0
fi
}

INTERVAL=${INTERVAL:-300}
VERBOSE_MODE=${VERBOSE_MODE:-0}
TMP_DIR="/tmp/openwisp/monitoring"
show_version() {
VERSION=$(cat /etc/openwisp/monitoring/VERSION)
echo "$(basename "$0") $VERSION"
exit 0
}

echoerr() { echo "$@" 1>&2 && exit 1; }

Expand Down Expand Up @@ -219,17 +182,83 @@ send_data() {
done
}

[ -z "$MODE" ] && echoerr "missing required --mode option"

if [ "$MODE" = "collect" ]; then
MONITORED_INTERFACES=${MONITORED_INTERFACES:-*}
# remove double quotes from interfaces
MONITORED_INTERFACES=$(echo "$MONITORED_INTERFACES" | tr -d '"')
save_data
elif [ "$MODE" = "send" ]; then
VERIFY_SSL=${VERIFY_SSL:-0}
RESPONSE_FILE="$TMP_DIR"/response.txt
set_url_and_curl && send_data
else
echoerr "The supplied mode is invalid. Only send and collect are allowed"
fi
main() {
# parse options
while [ -n "$1" ]; do
case "$1" in
--version | -v)
show_version
;;
--help | -h)
show_help
;;
--url)
export BASE_URL="$2"
shift
;;
--uuid)
export UUID="$2"
shift
;;
--key)
export KEY="$2"
shift
;;
--verify_ssl)
export VERIFY_SSL="$2"
shift
;;
--interval)
export INTERVAL="$2"
shift
;;
--monitored_interfaces)
export MONITORED_INTERFACES="$2"
shift
;;
--verbose_mode)
export VERBOSE_MODE="$2"
shift
;;
--required_memory)
export REQUIRED_PERCENT="$2"
shift
;;
--mode)
export MODE="$2"
shift
;;
--max_retries)
export MAX_RETRY="$2"
shift
;;
-*)
echo "Invalid option: $1"
exit 1
;;
*) break ;;
esac
shift
done

INTERVAL=${INTERVAL:-300}
VERBOSE_MODE=${VERBOSE_MODE:-0}
TMP_DIR="/tmp/openwisp/monitoring"

[ -z "$MODE" ] && echoerr "missing required --mode option"

if [ "$MODE" = "collect" ]; then
MONITORED_INTERFACES=${MONITORED_INTERFACES:-*}
# remove double quotes from interfaces
MONITORED_INTERFACES=$(echo "$MONITORED_INTERFACES" | tr -d '"')
save_data
elif [ "$MODE" = "send" ]; then
VERIFY_SSL=${VERIFY_SSL:-0}
RESPONSE_FILE="$TMP_DIR"/response.txt
set_url_and_curl && send_data
else
echoerr "The supplied mode is invalid. Only send and collect are allowed"
fi
}

main "$@"
1 change: 0 additions & 1 deletion openwrt-openwisp-monitoring/files/monitoring.init
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ start_service() {
exit 1
fi
interval="--interval $interval"
monitored_interfaces="--monitored_interfaces \"$monitored_interfaces\""
verbose="--verbose_mode ${verbose_mode:-0}"
set -- --monitored_interfaces \""$monitored_interfaces"\"
monitored_interfaces="$*"
Expand Down

0 comments on commit bac6d54

Please sign in to comment.