4d07a1b7b22619686e2eb6646faaa73b549b5703
[juci.git] / plugins / juci-network-netifd / ubus / network
1 #!/bin/sh
2
3 . /usr/share/libubox/jshn.sh
4
5 network_adapters(){
6         # moved to lua. result: went from taking 0.71 sec to 0.49 sec with more details
7         /usr/lib/rpcd/cgi/juci.network.lua adapters
8 }
9
10 network_clients(){
11         # we have moved this one to lua implementation
12         /usr/lib/rpcd/cgi/juci.network.lua clients
13 }
14
15 network_load(){
16         json_init 
17         json_add_int "active_connections" $(cat /proc/sys/net/netfilter/nf_conntrack_count)
18         json_add_int "max_connections" $(cat /proc/sys/net/netfilter/nf_conntrack_max)
19         json_dump
20 }
21
22 network_nat_table(){
23         ESTABLISHED=$(awk '
24                 /ESTABLISHED/ { 
25                         printf "proto=\"" $1 "\";"; 
26                         printf "state=\"" $4 "\";";
27                         printf "remote_ip=\"" substr($5, index($5, "=")+1) "\";";
28                         printf "local_ip=\"" substr($6, index($6, "=")+1) "\";";
29                         printf "remote_port=\"" substr($7, index($7, "=")+1) "\";";
30                         printf "local_port=\"" substr($8, index($8, "=")+1) "\";\n";
31                 }' /proc/net/ip_conntrack); 
32                 
33         IFS=$'\n'; 
34         json_init
35         json_add_array "table"
36         for FIELDS in $ESTABLISHED; do
37                 eval $FIELDS; 
38                 json_add_object ""; 
39                 json_add_string "proto" $proto; 
40                 json_add_string "state" $state; 
41                 json_add_string "local_ip" $local_ip; 
42                 json_add_string "remote_ip" $remote_ip; 
43                 json_add_int "local_port" $local_port;
44                 json_add_int "remote_port" $remote_port; 
45                 json_close_object; 
46         done
47         json_close_array
48         json_dump
49 }
50
51 network_bridges() {
52         $(brctl show | awk -F'\t' '{ # broadcom is stupid (why not write cli utitlities so they are cli friendly?)
53                 if(FNR==1) continue; 
54                 if($1 != "") { 
55                         if(BRNAME != "") { 
56                                 print BRNAME "=" INTERFACES; 
57                         } 
58                         BRNAME=$1; 
59                         INTERFACES=$6; 
60                 } else { 
61                         INTERFACES=INTERFACES "," $8; 
62                 }
63         } 
64         END { 
65                 print BRNAME "=" INTERFACES; 
66         }'); 
67 }
68
69 network_nameservers(){
70         json_init 
71         json_add_array "nameservers"; 
72         for ns in `awk '/nameserver/{print $2}' /var/resolv.conf.auto`; do
73                 json_add_string "" $ns; 
74         done
75         json_close_array
76         json_dump
77 }
78
79 case $1 in 
80         .methods) 
81                 echo "clients,load,adapters,nameservers,nat_table"; 
82                 exit 0
83                 ;;
84         clients) 
85                 network_clients
86                 ;;
87         load)
88                 network_load
89                 ;;
90         nat_table)
91                 network_nat_table
92                 ;;
93         adapters)
94                 network_adapters
95                 ;;
96         nameservers)
97                 network_nameservers
98                 ;;
99 esac