lirantal / daloradius

daloRADIUS is an advanced RADIUS web management application for managing hotspots and general-purpose ISP deployments. It features user management, graphical reporting, accounting, a billing engine, and integrates with OpenStreetMap for geolocation. The system is based on FreeRADIUS with which it shares access to the backend database.
http://www.daloradius.com
GNU General Public License v2.0
649 stars 333 forks source link

heartbeating #448

Open Lutzion opened 1 year ago

Lutzion commented 1 year ago

A few days ago, i installed daloradius. As a last step, i tried to transfer the heartbeat-data by calling dalo-dashboard-update-openwrt.sh from time to time.

dalo-dashboard-update-openwrt.sh In the file dalo-dashboard-update-openwrt.sh there is a line saying # Do not edit past this point In fact i had to edit, because my wan-device is not eth0.2, but simply wan.

So maybe it would be better to have a section with: WAN_DEV= LAN_DEV= WLAN_DEV= and then work with ... wan_ip=ifconfig $WAN_DEV | awk '/inet addr/{print substr ($2,6)}' ...

The variable firmware_revision is missing, so i added firmware_revision=cat /etc/os-release | awk '/OPENWRT_RELEASE/{print}' | sed "s/\"//g;s/ /%20/g;s/OPENWRT_RELEASE=//" Replacing " and spaces is neccessary to not get error 400.

On the server side i had to edit heartbeat.php and db_open.php.

db_open.php: with my php8.1 if not checked isset($_SESSION) before array_key_exists('location_name', $_SESSION), the function exits.

[Tue May 02 11:33:50.239350 2023] [php:error] [pid 1822951] [client 192.168.64.203:33148] PHP Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, null given in /var/www/html/daloradius/app/common/includes/db_open.php:39\nStack trace:\n#0 /var/www/html/daloradius/app/operators/heartbeat.php(67): require_once()\n#1 {main}\n thrown in /var/www/html/daloradius/app/common/includes/db_open.php on line 39

So change to

$location = (isset($_SESSION) &&
     array_key_exists('location_name', $_SESSION) &&
             isset($_SESSION['location_name']) &&
             $_SESSION['location_name'] != "default")
          ? $configValues['CONFIG_LOCATIONS'][$_SESSION['location_name']]
          : "";

heartbeat.php There are errors [Mon May 01 17:15:36.408716 2023] [php:error] [pid 1572012] [client 192.168.64.217:57020] PHP Parse error: syntax error, unexpected token ";" in /var/www/html/daloradius/app/operators/heartbeat.php on line 29

The lines

$wan_iface = (array_key_exists('wan_iface', $_GET) && !empty(trim($_GET['wan_iface'])) ? trim($_GET['wan_iface']) : "";
...
$cpu = (array_key_exists('cpu', $_GET) && !empty(trim($_GET['cpu'])) ? trim($_GET['cpu']) : "";

are all missing a ) at the and, so change to

$wan_iface = (array_key_exists('wan_iface', $_GET) && !empty(trim($_GET['wan_iface'])) ? trim($_GET['wan_iface']) : "");
...
$cpu = (array_key_exists('cpu', $_GET) && !empty(trim($_GET['cpu'])) ? trim($_GET['cpu']) : "");

Seems that php8.1 complains a bit more about errors than previous versions. Many thanks for your work Lutz

momothefox commented 1 year ago

In fact i had to edit, because my wan-device is not eth0.2, but simply wan.

So maybe it would be better to have a section with: WAN_DEV= LAN_DEV= WLAN_DEV= and then work with ... wan_ip=ifconfig $WAN_DEV | awk '/inet addr/{print substr ($2,6)}' ...

The variable firmware_revision is missing, so i added firmware_revision=cat /etc/os-release | awk '/OPENWRT_RELEASE/{print}' | sed "s/\"//g;s/ /%20/g;s/OPENWRT_RELEASE=//" Replacing " and spaces is necessary to not get error 400.

Yes you are right Thank you for your contribution i will check your code and update dalo-dashboard-update-openwrt.sh

momothefox commented 1 year ago

@Lutzion Please test this out

#!/bin/sh
#
# daloRADIUS Heartbeat agent
# @version 1.1
# @author Liran Tal <liran.tal@gmail.com>
# @author Muhammed Al-Qadhy <witradius@gmail.com>

# ----------------------------------------------------------------------------
# Configuration --------------------------------------------------------------
# ----------------------------------------------------------------------------
# edit the settings below to apply configuration for your own deployment

# Set to the URL of daloradius's heartbeat script location
DALO_HEARTBEAT_ADDR="http://daloradius.com/heartbeat.php"

# This is Auto Set NAS MAC to the MAC address of LAN connected openwrt node
# MAC address format, according to how the NAS sends this information. For example: 00-aa-bb or 00:aa:bb
# Extracting NAS MAC from uci chilli config.
NAS_MAC=`uci get chilli.@chilli[0].radiusnasid`

# Set to a unique, hard-to-figure-out key across all of your NASes.
# This key is saved in daloRADIUS's configuration and so should also
# be configured in daloRADIUS as well.
SECRET_KEY="sillykey"

# Do not edit past this point
# ----------------------------------------------------------------------------
# Configuration --------------------------------------------------------------
# ----------------------------------------------------------------------------

wan_iface=`uci get network.wan.device`
wan_ip=`ifconfig $wan_iface | awk '/inet addr/{print substr ($2,6)}'`
wan_mac=`ifconfig $wan_iface | awk '/HWaddr/{print substr ($5,0)}'`
wan_gateway=`ifconfig $wan_iface | awk '/inet addr/{print substr ($2,6)}'`
wan_proto=`uci get network.wan.proto`
wifi_iface=`uci get wireless.default_radio0.device  | awk '{ gsub(/ /,""); print }'`
wifi_ip=`uci get chilli.@chilli[0].uamlisten`
wifi_mac=`ifconfig wlan0 | awk '/HWaddr/{print substr ($5,0)}'`
wifi_ssid=`uci get wireless.default_radio0.ssid | awk '{ gsub(/ /,""); print }'`
#wifi_key=`uci get wireless.default_radio0.key`
wifi_channel=`uci get wireless.radio0.channel`
lan_iface=`uci get network.lan.device`
lan_ip=`ifconfig $lan_iface | awk '/inet addr/{print substr ($2,6)}'`
lan_mac=`ifconfig $lan_iface | awk '/HWaddr/{print substr ($5,1)}'`
lan_proto=`uci get network.lan.proto`
ip=$wan_ip
mac=$lan_mac
uptime=`cat /proc/uptime | awk '{print ($1)}'`
memfree=`cat /proc/meminfo | awk '/MemFree/{print substr($2,$3)}'`
wan_bdown=`ifconfig eth0.2 | awk '/RX bytes/{print substr($2,7)}'`
wan_bup=`ifconfig eth0.2 | awk '/TX bytes/{print substr($6,7)}'`
bdown=`cat /proc/net/dev | awk '/$wan_iface/{print $2}'` #in byte
bup=`cat /proc/net/dev | awk '/$wan_iface/{print $10}'` #in bytes, need to turn to kilobytes
kbdown=$((bdown/1024))
kbup=$((bup/1024))
firmware=`cat /etc/os-release | awk '/ID="openwrt"/{print substr ($0,5,7)}'`
firmware_revision=`cat /etc/os-release | awk '/VERSION_ID/{print substr ($0,13)}' | sed "s/\"//g;s/ /%20/g;s/VERSION_ID=//"`

# Snippet to get CPU % --------------------------------------------------------------
# adopted from Paul Colby (http://colby.id.au)
PREV_TOTAL=0
PREV_IDLE=0
#repeat period
x=5
#counter
i=1
while [ $i -le $x ]
do
  IDLE=`cat /proc/stat | grep '^cpu ' | awk '{print $5}'`       # get cpu idle time
  TOTAL=`cat /proc/stat | grep '^cpu ' | awk '{print $1+$2+$3+$4+$5+$6+$7+$8+$9+$10+$11}'` #get total cpu time

  # Calculate the CPU usage since we last checked.
  let "DIFF_IDLE=$IDLE-$PREV_IDLE"
  let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
  let "DIFF_USAGE=1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL"
  let "DIFF_USAGE_UNITS=$DIFF_USAGE/10"
  let "DIFF_USAGE_DECIMAL=$DIFF_USAGE%10"
#  echo -en "\rCPU: $DIFF_USAGE_UNITS.$DIFF_USAGE_DECIMAL%    \b\b\b\b"

# No decemical  
  #let "DIFF_IDLE=$IDLE-$PREV_IDLE"
  #let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
  #let "DIFF_USAGE=1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL"
  #let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"
  #echo -en "\rCPU: $DIFF_USAGE%  \b\b"

  # Remember the total and idle CPU times for the next check.
  PREV_TOTAL="$TOTAL"
  PREV_IDLE="$IDLE"

  # Wait before checking again.
  sleep 1
  i=$(( $i + 1 ))
done
cpu=$DIFF_USAGE_UNITS.$DIFF_USAGE_DECIMAL%
# --------------------------------------------------------------------------------------

if [ "$DEBUG_MODE" = "1" ]
then
    echo "Collected the following information..."
    echo "-------------------------------------------------------"
    echo "wan_iface $wan_iface"
    echo "wan_ip $wan_ip"
    echo "wan_mac $wan_mac"
    echo "wan_gateway $wan_gateway"
    echo "wifi_mac $wifi_mac"
    echo "wifi_ip $wifi_ip"
    echo "wifi_iface $wifi_iface"

    echo "lan_mac $lan_mac"
    echo "lan_ip $lan_ip"
    echo "lan_iface $lan_iface"

    echo "ip $ip"
    echo "mac $mac"
    echo "uptime $uptime"
    echo "memfree $memfree"
    echo "wan_bdown $wan_bdown"
    echo "wan_bup $wan_bup"
    echo "wifi_ssid $wifi_ssid"
    echo "wifi_key $wifi_key"
    echo "wifi_channel $wifi_channel"
    echo "firmware $firmware"
    echo "firmware_revision $firmware_revision"
    echo "$cpu"
    echo "-------------------------------------------------------"

fi

wget -O /tmp/heartbeat.txt "$DALO_HEARTBEAT_ADDR?secret_key=$SECRET_KEY&nas_mac=$NAS_MAC&firmware=$firmware&firmware_revision=$firmware_revision&wan_iface=$wan_iface&wan_ip=$wan_ip&wan_mac=$wan_mac&wifi_mac=$wifi_mac&wan_gateway=$wan_gateway&wifi_iface=$wifi_iface&wifi_ip=$wifi_ip&wifi_mac=$wifi_mac&wifi_ssid=$wifi_ssid&wifi_key=$wifi_key&wifi_channel=$wifi_channel&lan_iface=$lan_iface&lan_ip=$lan_ip&lan_mac=$lan_mac&uptime=$uptime&memfree=$memfree&wan_bup=$wan_bup&wan_bdown=$wan_bdown&cpu=$cpu"

if [ "$DEBUG_MODE" = "1" ]
then
    echo "-------------------------------------------------------"
    echo "daloRADIUS server returned: \n"
    echo "-------------------------------------------------------"
    cat /tmp/heartbeat.txt
    echo "-------------------------------------------------------"
fi

Also i found out another bug in rep-hb-dashboard.php Line 175 . sprintf($format, t('all','WanGateway'), $row['wan_ip']); Should be . sprintf($format, t('all','WanGateway'), $row['wan_gateway']);