[luci-app-bmx7] Add bmx7-info script to obtain JSON-formatted input for the Status...
[feed/routing.git] / luci-app-bmx7 / files / www / cgi-bin / bmx7-info
1 #!/bin/sh
2 # Copyright © 2011 Pau Escrich
3 # Contributors Jo-Philipp Wich <xm@subsignal.org>
4 # Roger Pueyo Centelles <roger.pueyo@guifi.net>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #
20 # The full GNU General Public License is included in this distribution in
21 # the file called "COPYING".
22 #
23 # This script gives information about bmx7
24 # Can be executed from a linux shell: ./bmx7-info -s links
25 # Or from web interfae (with cgi enabled): http://host/cgi-bin/bmx7-info?links
26 # If you ask for a directory you wil get the directory contents in JSON forman
27
28 BMX7_DIR="$(uci get bmx7.general.runtimeDir 2>/dev/null)" || BMX7_DIR="/var/run/bmx7/json"
29
30 #Checking if shell mode or cgi-bin mode
31 if [ "$1" == "-s" ]; then
32 QUERY="$2"
33 else
34 QUERY="${QUERY_STRING%%=*}"
35 echo "Content-type: application/json"
36 echo ""
37
38 fi
39
40 check_path() {
41 [ -d "$1" ] && path=$(cd $1; pwd)
42 [ -f "$1" ] && path=$(cd $1/..; pwd)
43 [ $(echo "$path" | grep -c "^$BMX7_DIR") -ne 1 ] && exit 1
44 }
45
46 print_mem() {
47 echo -n '{ "memory": { "bmx7": "'
48 cat /proc/$(cat /var/run/bmx7/pid)/status |grep -i VmSize | tr -s " " | cut -d " " -f 2,3 | tr -d "\n"
49 echo '"}}'
50 }
51
52 print_query() {
53 # If the query is a directory
54 [ -d "$BMX7_DIR/$1" ] &&
55 {
56 # If /all has not been specified
57 [ -z "$QALL" ] &&
58 {
59 total=$(ls $BMX7_DIR/$1 | wc -w)
60 i=1
61 echo -n "{ \"$1\": [ "
62 for f in $(ls $BMX7_DIR/$1); do
63 echo -n "{ \"name\": \"$f\" }"
64 [ $i -lt $total ] && echo -n ','
65 i=$(( $i + 1 ))
66 done
67 echo -n " ] }"
68
69 # If /all has been specified, printing all the files together
70 } || {
71 comma=""
72 echo -n "[ "
73 for entry in "$BMX7_DIR/$1/"*; do
74 [ -f "$entry" ] &&
75 {
76 ${comma:+echo "$comma"}
77 tr -d '\n' < "$entry"
78 comma=","
79 }
80 done
81 echo -n " ]"
82 }
83 }
84
85 # If the query is a file, just printing the file
86 [ -f "$BMX7_DIR/$1" ] && cat "$BMX7_DIR/$1";
87 }
88
89 if [ "${QUERY##*/}" == "all" ]; then
90 QUERY="${QUERY%/all}"
91 QALL=1
92 fi
93
94 if [ "$QUERY" == '$info' ]; then
95 echo '{ "info": [ '
96 print_query status
97 echo -n ","
98 print_query interfaces
99 echo -n ","
100 print_query links
101 echo -n ","
102 print_mem
103 echo "] }"
104 fi
105
106 if [ "$QUERY" == '$neighbours' ]; then
107 QALL=1
108 echo '{ "neighbours": [ '
109 echo '{ "originators": '
110 print_query originators
111 echo '}, '
112 echo '{ "descriptions": '
113 print_query descriptions
114 echo "} ] }"
115 exit 0
116
117 else if [ "$QUERY" == '$tunnels' ]; then
118 bmx7 -c --jshow tunnels /r=0
119 exit 0
120
121 else if [ "$QUERY" == '$originators' ]; then
122 bmx7 -c --jshow originators /r=0
123 exit 0
124
125 else
126 check_path "$BMX7_DIR/$QUERY"
127 print_query $QUERY
128 exit 0
129 fi
130 fi
131 fi
132 fi
133
134 ls -1F "$BMX7_DIR"
135 exit 0
136