Skip to content

Commit

Permalink
[refactor] Improvement in check available memory
Browse files Browse the repository at this point in the history
  • Loading branch information
devkapilbansal committed Oct 12, 2021
1 parent d990215 commit 9742f36
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions openwrt-openwisp-monitoring/files/monitoring.agent
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,18 @@ check_available_memory() {
required=$(echo - | awk -v percent="$REQUIRED_PERCENT" -v total="$total" '{printf("%.f",percent*total)}')

if [ "$available" -ge "$required" ]; then
echo "0"
break
return 0
else
# enough memory not available, deleting old data
# shellcheck disable=SC2012
file="$TMP_DIR/$(ls -1t "$TMP_DIR" | tail -1)"
if [ -f "$file" ]; then
rm "$file"
else
[ "$VERBOSE_MODE" -eq "1" ] && logger -s "Not enough memory available, skipping collect data." \
-p daemon.warn
echo "1"
break
[ "$VERBOSE_MODE" -eq "1" ] \
&& logger -s "Not enough memory available, skipping collect data." \
-p daemon.warn
return 1
fi
fi
done
Expand All @@ -67,8 +66,7 @@ collect_data() {
/usr/sbin/netjson-monitoring --dump "$MONITORED_INTERFACES" && break

if [ "$n" -eq 5 ]; then
[ "$VERBOSE_MODE" -eq "1" ] && logger -s "Collecting data failed!" \
-p daemon.err
[ "$VERBOSE_MODE" -eq "1" ] && logger -s "Collecting data failed!" -p daemon.err
fi
n=$((n + 1))
sleep 5
Expand All @@ -95,8 +93,7 @@ set_url_and_curl() {

save_data() {
while true; do
memory_available="$(check_available_memory)"
if [ "$memory_available" -eq "0" ]; then
if check_available_memory; then
data="$(collect_data)"
filename="$(date -u +'%d-%m-%Y_%H:%M:%S')"
# make directory
Expand Down

0 comments on commit 9742f36

Please sign in to comment.