-
Notifications
You must be signed in to change notification settings - Fork 1
/
print_map.sh.j2
142 lines (118 loc) · 4.19 KB
/
print_map.sh.j2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
#Print out local connection data for map creation
memory_usage()
{
meminfo=$(cat /proc/meminfo)
free=$(echo "$meminfo" | awk /^MemFree:/'{print($2)}')
buffers=$(echo "$meminfo" | awk /^Buffers:/'{print($2)}')
cached=$(echo "$meminfo" | awk /^Cached:/'{print($2)}')
total=$(echo "$meminfo" | awk /^MemTotal:/'{print($2)}')
echo $free $buffers $cached $total | awk '{ printf("%f", 1 - ($1 + $2 + $3) / $4)}'
}
rootfs_usage()
{
df / | awk 'BEGIN {val=100} NR==2 {val=$5} END { printf("%.2f", val/100) }'
}
print_basic() {
local community="{{ alfred_community }}"
local version="{{ alfred_router_version }}"
local name="{{ alfred_router_name }}"
local longitude="{{ alfred_router_longitude }}"
local latitude="{{ alfred_router_latitude }}"
local contact="{{ alfred_router_contact }}"
local autoupdater_enabled="{{ alfred_router_autoupdater_enabled }}"
local autoupdater_branch="{{ alfred_router_autoupdater_branch }}"
local manufacturer=$(/usr/sbin/dmidecode -s system-manufacturer)
local product=$(/usr/sbin/dmidecode -s system-product-name)
local hw_version=$(/usr/sbin/dmidecode -s baseboard-version)
local os_desc=$(/usr/bin/lsb_release -ds)
[ -n "$contact" ] && echo -n "\"contact\" : \"$contact\", "
[ -n "$name" ] && echo -n "\"name\" : \"$name\", "
[ -n "$version" ] && echo -n "\"firmware\" : \"ffbsee-$version\", "
[ -n "$community" ] && echo -n "\"community\" : \"$community\", "
if [ "$autoupdater_enabled" = "1" ]; then
echo -n "\"autoupdater\" : \"$autoupdater_branch\", "
fi
if [ -n "$longitude" -a -n "$latitude" ]; then
echo -n "\"longitude\" : $longitude, "
echo -n "\"latitude\" : $latitude, "
fi
manufacturer=$(/usr/sbin/dmidecode -t system | grep -Po '^[\t ]+Manufacturer:[\t ]+\K.*')
product=$(/usr/sbin/dmidecode -t system | grep -Po '^[\t ]+Product Name:[\t ]+\K.*')
if [ -z "$product" ]; then
echo -n "\"model\" : \"RaspberryPI - $os_desc\", "
else
echo -n "\"model\" : \"$manufacturer - $product - $hw_version\", "
fi
mygwname=$(nc -U /var/run/fastd.ffbsee.sock | jq '.peers[] | select(.connection != null) | .name' | sed -e 's/^"//' -e 's/"$//')
echo -n "\"using_gateway\" : \"$mygwname\", "
echo -n "\"links\" : ["
printLink() { echo -n "{ \"smac\" : \"$(cat /sys/class/net/$3/address)\", \"dmac\" : \"$1\", \"qual\" : $2 }"; }
IFS="
"
nd=0
for entry in $(/usr/sbin/batctl meshif bat-ffbsee neighbors -H 2> /dev/null | awk -F '[][)( \t]+' '/^[a-f0-9]/{ print($1, $3, $4) }'); do
[ $nd -eq 0 ] && nd=1 || echo -n ", "
IFS=" "
printLink $entry
done
echo -n '], '
macs=$(cat /sys/class/net/*/address)
echo -n "$(/usr/sbin/batctl meshif bat-ffbsee translocal -H 2 2> /dev/null | grep -v "P" | awk -v macs="$macs" 'BEGIN{c=0} {if (index(macs, $1) == 0){ c+=1 }} END {printf("\"clientcount\" : %d", c)}')"
}
print_more() {
echo -n "\"loadavg\" : $(uptime | awk '{print($NF)}'), "
echo -n "\"uptime\" : $(awk '{print(int($1))}' /proc/uptime), "
print_basic
}
print_all() {
echo -n "\"rootfs_usage\" : $(rootfs_usage), "
echo -n "\"memory_usage\" : $(memory_usage), "
echo -n "\"addresses\" : ["
listv6=$(ip -6 address show dev {{ alfred_interface_br }} 2> /dev/null | grep -v "{{ alfred_v6prefix }}::/64" | tr '/' ' ' | awk 'BEGIN{i=0} /inet/ { if($2 !~ /^fe80/) { printf("%s\"%s\"", (i ? ", " : ""), $2); i=1; }}')
if [ -n "$listv6" ]; then
echo -n $listv6
echo -n ", "
fi
listv4=$(ip -4 address show dev br-ffbsee 2> /dev/null | tr '/' ' ' | awk 'BEGIN{i=0} /inet/ { if($2 ~ /^10\.15\./) { printf("%s\"%s\"", (i ? ", " : ""), $2); i=1; }}')
if [ -n "$listv4" ]; then
echo -n $listv4
fi
echo -n "], "
print_more
}
print() {
echo -n "{"
case $1 in
"basic")
print_basic
;;
"more")
print_more
;;
"all")
print_all
;;
*)
;;
esac
echo -n '}'
}
map_level="{{ alfred_maplevel }}"
if [ "$1" = "-p" ]; then
[ $map_level = "none" ] && exit 0
content="$(print $map_level)"
if [ -n "$content" ]; then
#make sure alfred is running
if ! systemctl is-active alfred-ffbsee ; then
systemctl start alfred-ffbsee
fi
#publish content via alfred
echo "$content" | gzip -c - | /usr/local/sbin/alfred -s 64 -u /var/run/alfred-ffbsee.sock
echo "map published"
else
echo "nothing published"
fi
else
print $map_level
fi