[Mirror] The Firmware for FFBSee Community
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

print_map.sh 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/sh
  2. #Print out local connection data for map creation
  3. memory_usage()
  4. {
  5. meminfo=$(cat /proc/meminfo)
  6. free=$(echo "$meminfo" | awk /^MemFree:/'{print($2)}')
  7. buffers=$(echo "$meminfo" | awk /^Buffers:/'{print($2)}')
  8. cached=$(echo "$meminfo" | awk /^Cached:/'{print($2)}')
  9. total=$(echo "$meminfo" | awk /^MemTotal:/'{print($2)}')
  10. echo $free $buffers $cached $total | awk '{ printf("%f", 1 - ($1 + $2 + $3) / $4)}'
  11. }
  12. rootfs_usage()
  13. {
  14. df / | awk 'BEGIN {val=100} NR==2 {val=$5} END { printf("%.2f", val/100) }'
  15. }
  16. print_basic() {
  17. local community="$(uci -q get freifunk.@settings[0].community 2> /dev/null)"
  18. local version="$(uci -q get freifunk.@settings[0].version 2> /dev/null)"
  19. local name="$(uci -q get freifunk.@settings[0].name 2> /dev/null)"
  20. local longitude="$(uci -q get freifunk.@settings[0].longitude 2> /dev/null)"
  21. local latitude="$(uci -q get freifunk.@settings[0].latitude 2> /dev/null)"
  22. local contact="$(uci -q get freifunk.@settings[0].contact 2> /dev/null)"
  23. local autoupdater_enabled="$(uci -q get autoupdater.settings.enabled 2> /dev/null)"
  24. local autoupdater_branch="$(uci -q get autoupdater.settings.branch 2> /dev/null)"
  25. [ -n "$contact" ] && echo -n "\"contact\" : \"$contact\", "
  26. [ -n "$name" ] && echo -n "\"name\" : \"$name\", "
  27. [ -n "$version" ] && echo -n "\"firmware\" : \"ffulm-$version\", "
  28. [ -n "$community" ] && echo -n "\"community\" : \"$community\", "
  29. if [ "$autoupdater_enabled" = "1" ]; then
  30. echo -n "\"autoupdater\" : \"$autoupdater_branch\", "
  31. fi
  32. if [ -n "$longitude" -a -n "$latitude" ]; then
  33. echo -n "\"longitude\" : $longitude, "
  34. echo -n "\"latitude\" : $latitude, "
  35. fi
  36. echo -n "\"model\" : \"$(cat /tmp/sysinfo/model)\", "
  37. echo -n "\"links\" : ["
  38. # Calculate bandwidth as percent value
  39. calcQual() { echo "$1" "40" | awk '{ printf("%.1f", ($1 > $2) ? 100 : (100 * $1 / $2)) }'; }
  40. printLink() { echo -n "{ \"smac\" : \"$(cat /sys/class/net/$3/address)\", \"dmac\" : \"$1\", \"qual\" : $(calcQual $2) }"; }
  41. IFS="
  42. "
  43. nd=0
  44. for entry in $(batctl neighbors 2> /dev/null | awk -F '[][)( \t]+' '/^[a-f0-9]/{ print($1, $3, $4) }'); do
  45. [ $nd -eq 0 ] && nd=1 || echo -n ", "
  46. IFS=" "
  47. printLink $entry
  48. done
  49. echo -n '], '
  50. mac=$(uci -q get network.freifunk.macaddr)
  51. batctl translocal 2> /dev/null | tr '\t/[]()' ' ' | awk -v mac=$mac 'BEGIN{ c=0; } { if($1 == "*" && $2 != mac && $4 ~ /^[.NW]+$/ && $5 < 300) c++;} END{ printf("\"clientcount\" : %d", c);}'
  52. }
  53. print_more() {
  54. echo -n "\"loadavg\" : $(uptime | awk '{print($NF)}'), "
  55. echo -n "\"uptime\" : $(awk '{print(int($1))}' /proc/uptime), "
  56. print_basic
  57. }
  58. print_all() {
  59. local prefix="$(uci -q get network.globals.ula_prefix)"
  60. echo -n "\"rootfs_usage\" : $(rootfs_usage), "
  61. echo -n "\"memory_usage\" : $(memory_usage), "
  62. echo -n "\"addresses\" : ["
  63. ip -6 address show dev br-freifunk 2> /dev/null | grep -v "$prefix" | tr '/' ' ' | awk 'BEGIN{i=0} /inet/ { if($2 !~ /^fe80/) { printf("%s\"%s\"", (i ? ", " : ""), $2); i=1; }}'
  64. echo -n "], "
  65. print_more
  66. }
  67. print() {
  68. echo -n "{"
  69. case $1 in
  70. "basic")
  71. print_basic
  72. ;;
  73. "more")
  74. print_more
  75. ;;
  76. "all")
  77. print_all
  78. ;;
  79. *)
  80. ;;
  81. esac
  82. echo -n '}'
  83. }
  84. map_level="$(uci -q get freifunk.@settings[0].publish_map 2> /dev/null)"
  85. if [ "$1" = "-p" ]; then
  86. [ $map_level = "none" ] && exit 0
  87. content="$(print $map_level)"
  88. if [ -n "$content" ]; then
  89. #make sure alfred is running
  90. pidof alfred > /dev/null || /etc/init.d/alfred start
  91. #publish content via alfred
  92. echo "$content" | alfred -s 64
  93. echo "map published"
  94. else
  95. echo "nothing published"
  96. fi
  97. else
  98. print $map_level
  99. fi