source: client/php/Routerboard.php

Last change on this file was 79, checked in by chronos, 7 years ago
  • Fixed: Do not include non-existing files.
File size: 8.1 KB
Line 
1<?php
2
3class Routerboard
4{
5 var $SSHPath = '/usr/bin/ssh';
6 var $Timeout = 3;
7 var $HostName;
8 var $UserName;
9 var $Password;
10 var $PrivateKey = 'id_dsa';
11 var $MaxBurstLineCount = 50;
12 var $Debug = false;
13
14 function __construct($HostName = 'localhost', $UserName = 'admin', $Password = '')
15 {
16 $this->HostName = $HostName;
17 $this->UserName = $UserName;
18 $this->Password = $Password;
19 }
20
21 function Execute($Commands)
22 {
23 $Output = array();
24 if(is_array($Commands))
25 {
26 $I = 0;
27 $Batch = array();
28 while($I < count($Commands))
29 {
30 if(($I % $this->MaxBurstLineCount) == 0)
31 {
32 if(count($Batch) > 0)
33 $Output = array_merge($Output, $this->ExecuteBatch(implode(';', $Batch)));
34 $Batch = array();
35 }
36 $Batch[] = $Commands[$I];
37 $I++;
38 }
39 if(count($Batch) > 0)
40 $Output = array_merge($Output, $this->ExecuteBatch(implode(';', $Batch)));
41 } else
42 $Output = array_merge($Output, $this->ExecuteBatch($Commands));
43 return($Output);
44 }
45
46 function ExecuteBatch($Commands)
47 {
48 $Commands = trim($Commands);
49 if($Commands != '')
50 {
51 $Commands = addslashes($Commands);
52 $Commands = str_replace('$', '\$', $Commands);
53 //$Commands = str_replace(' ', '\ ', $Commands);
54 $Command = $this->SSHPath.' -o ConnectTimeout='.$this->Timeout.' -l '.$this->UserName.' -i '.$this->PrivateKey.' '.$this->HostName.' "'.$Commands.'"';
55 if($this->Debug) echo($Command);
56 $Output = array();
57 echo($Command);
58 exec($Command, $Output);
59 } else $Output = '';
60 if($this->Debug) print_r($Output);
61 return($Output);
62 }
63
64 function ItemGet($Path)
65 {
66 $Result = $this->Execute(implode(' ', $Path).' print');
67 array_pop($Result);
68 $List = array();
69 foreach($Result as $ResultLine)
70 {
71 $ResultLineParts = explode(' ', trim($ResultLine));
72 if(count($ResultLineParts) > 1)
73 {
74 if($ResultLineParts[1]{0} == '"') $ResultLineParts[1] = substr($ResultLineParts[1], 1, -1); // Remove quotes
75 $List[substr($ResultLineParts[0], 0, -1)] = $ResultLineParts[1];
76 } else $List[substr($ResultLineParts[0], 0, -1)] = '';
77 }
78 return($List);
79 }
80
81 function ListGet($Path, $Properties, $Conditions = array())
82 {
83 $PropertyList = '"';
84 foreach($Properties as $Index => $Property)
85 {
86 $PropertyList .= $Index.'=".[get $i '.$Property.']." ';
87 }
88 $PropertyList = substr($PropertyList, 0, -3);
89
90 $ConditionList = '';
91 foreach($Conditions as $Index => $Item)
92 {
93 if($Item == 'no') $ConditionList .= $Index.'='.$Item.' ';
94 else $ConditionList .= $Index.'="'.$Item.'" ';
95 }
96 $ConditionList = substr($ConditionList, 0, -1);
97
98 $Result = $this->Execute(implode(' ', $Path).' {:foreach i in=[find '.$ConditionList.'] do={:put ('.$PropertyList.')}}');
99 $List = array();
100 foreach($Result as $ResultLine)
101 {
102 $ResultLineParts = explode(' ', $ResultLine);
103 $ListItem = array();
104 foreach($ResultLineParts as $ResultLinePart)
105 {
106 $Value = explode('=', $ResultLinePart);
107 if(count($Value) > 1) $ListItem[$Properties[$Value[0]]] = $Value[1];
108 else $ListItem[$Properties[$Value[0]]] = '';
109 }
110 $List[] = $ListItem;
111 }
112 return($List);
113 }
114
115 function ListGetPrint($Path, $Properties, $Conditions = array())
116 {
117 $ConditionList = '';
118 foreach($Conditions as $Index => $Item)
119 {
120 $ConditionList .= $Index.'="'.$Item.'" ';
121 }
122 $ConditionList = substr($ConditionList, 0, -1);
123 if(trim($ConditionList) != '')
124 $ConditionList = ' where '.$ConditionList;
125
126 $Result = $this->Execute(implode(' ', $Path).' print terse'.$ConditionList);
127 $List = array();
128 foreach($Result as $ResultLine)
129 {
130 $ResultLineParts = explode(' ', $ResultLine);
131 $ListItem = array();
132 foreach($ResultLineParts as $ResultLinePart)
133 {
134 $Value = explode('=', $ResultLinePart);
135 if(in_array($Value[0], $Properties))
136 {
137 if(count($Value) > 1)
138 {
139 if($Value[1]{0} == '"') $Value[1] = substr($Value[1], 1, -1);
140 //if(strlen($Value[1]) > 0)
141 $ListItem[$Value[0]] = $Value[1];
142 } else $ListItem[$Value[0]] = '';
143 }
144 }
145 if(count($ListItem) > 0) $List[] = $ListItem;
146 }
147 return($List);
148 }
149
150 function ListEraseAll($Path)
151 {
152 $this->Execute(implode(' ', $Path).' { remove [find] }');
153 }
154
155 function ListUpdate($Path, $Properties, $Values, $Condition = array(), $UsePrint = false)
156 {
157 // Get current list from routerboard
158 if($UsePrint == 0)
159 {
160 $List = $this->ListGet($Path, $Properties, $Condition);
161 // Change boolean values yes/no to true/false
162 foreach($List as $Index => $ListItem)
163 {
164 foreach($ListItem as $Index2 => $Item2)
165 {
166 if($Item2 == 'true') $List[$Index][$Index2] = 'yes';
167 if($Item2 == 'false') $List[$Index][$Index2] = 'no';
168 }
169 }
170 } else
171 {
172 $List = $this->ListGetPrint($Path, $Properties, $Condition);
173 }
174 $Commands = array();
175
176 // Add empty properties to values
177 foreach($Values as $Index => $Item)
178 {
179 foreach($Properties as $Property)
180 {
181 if(!array_key_exists($Property, $Item))
182 $Item[$Property] = '';
183 }
184 $Values[$Index] = $Item;
185 }
186 foreach($List as $Index => $Item)
187 {
188 foreach($Properties as $Property)
189 {
190 if(!array_key_exists($Property, $Item))
191 $Item[$Property] = '';
192 }
193 $List[$Index] = $Item;
194 }
195
196 // Sort properties
197 foreach($Values as $Index => $Item)
198 {
199 ksort($Values[$Index]);
200 }
201 foreach($List as $Index => $Item)
202 {
203 ksort($List[$Index]);
204 }
205 if($this->Debug) print_r($List);
206 if($this->Debug) print_r($Values);
207
208 // Erase all items not existed in $Values
209 foreach($List as $Index => $ListItem)
210 {
211 if(!in_array($ListItem, $Values))
212 {
213 $Prop = '';
214 foreach($ListItem as $Index => $Property)
215 {
216 if($Property != '')
217 {
218 if(($Property == 'yes') or ($Property == 'no')) $Prop .= $Index.'='.$Property.' ';
219 else $Prop .= $Index.'="'.$Property.'" ';
220 }
221 }
222 $Prop = substr($Prop, 0, -1);
223 if(trim($Prop) != '')
224 $Commands[] = implode(' ', $Path).' remove [find '.$Prop.']';
225 }
226 }
227
228 // Add new items
229 foreach($Values as $ListItem)
230 {
231 if(!in_array($ListItem, $List))
232 {
233 $Prop = '';
234 foreach($ListItem as $Index => $Property)
235 {
236 if($Property != '') $Prop .= $Index.'="'.$Property.'" ';
237 }
238 $Prop = substr($Prop, 0, -1);
239 $Commands[] = implode(' ', $Path).' add '.$Prop;
240 }
241 }
242 if($this->Debug) print_r($Commands);
243 return($this->Execute($Commands));
244 }
245}
246
247function GetRouterboardNetworkState()
248{
249 global $LastRBNetworkState;
250
251 if(!isset($LastRBNetworkState)) $LastRBNetworkState = array('Time' => 0, 'Down' => 0, 'Up' => 0);
252 $Routerboard = new Routerboard();
253 $Routerboard->UserName = 'admin-ssh';
254 $Routerboard->HostName = 'rt-gateway-1';
255 $FilterList = $Routerboard->ListGet(array('ip', 'firewall', 'filter'), array('comment', 'bytes'));
256 $NetworkState = $LastRBNetworkState;
257 $NetworkState['Time'] = time();
258 foreach($FilterList as $Item)
259 {
260 if($Item['comment'] == 'total-in') $NetworkState['Down'] = $Item['bytes'];
261 if($Item['comment'] == 'total-out') $NetworkState['Up'] = $Item['bytes'];
262 }
263 if(!array_key_exists('Down', $NetworkState) or !array_key_exists('Up', $NetworkState)) print_r($FilterList);
264
265 $NetworkState['DownAverage'] = round(($NetworkState['Down'] - $LastRBNetworkState['Down']) / ($NetworkState['Time'] - $LastRBNetworkState['Time']));
266 $NetworkState['UpAverage'] = round(($NetworkState['Up'] - $LastRBNetworkState['Up']) / ($NetworkState['Time'] - $LastRBNetworkState['Time']));
267 if($NetworkState['DownAverage'] < 0) $NetworkState['DownAverage'] = 0;
268 if($NetworkState['UpAverage'] < 0) $NetworkState['UpAverage'] = 0;
269 $LastRBNetworkState = $NetworkState;
270 unset($Routerboard);
271 return $NetworkState;
272}
Note: See TracBrowser for help on using the repository browser.