2525
2626use OCA \ServerInfo \Resources \Disk ;
2727use OCA \ServerInfo \Resources \Memory ;
28+ use OCA \ServerInfo \Resources \NetInterface ;
29+ use RuntimeException ;
2830
2931class FreeBSD implements IOperatingSystem {
32+ private const AF_INET = 2 ;
33+ private const AF_INET6 = 28 ;
34+
3035 public function supported (): bool {
3136 return false ;
3237 }
@@ -36,7 +41,7 @@ public function getMemory(): Memory {
3641
3742 try {
3843 $ swapinfo = $ this ->executeCommand ('/usr/sbin/swapinfo -k ' );
39- } catch (\ RuntimeException $ e ) {
44+ } catch (RuntimeException $ e ) {
4045 $ swapinfo = '' ;
4146 }
4247
@@ -53,7 +58,7 @@ public function getMemory(): Memory {
5358
5459 try {
5560 $ meminfo = $ this ->executeCommand ('/sbin/sysctl -n hw.realmem hw.pagesize vm.stats.vm.v_inactive_count vm.stats.vm.v_cache_count vm.stats.vm.v_free_count ' );
56- } catch (\ RuntimeException $ e ) {
61+ } catch (RuntimeException $ e ) {
5762 $ meminfo = '' ;
5863 }
5964
@@ -80,7 +85,7 @@ public function getCpuName(): string {
8085 } else {
8186 $ data = $ model . ' ( ' . $ cores . ' cores) ' ;
8287 }
83- } catch (\ RuntimeException $ e ) {
88+ } catch (RuntimeException $ e ) {
8489 return $ data ;
8590 }
8691 return $ data ;
@@ -89,7 +94,7 @@ public function getCpuName(): string {
8994 public function getTime (): string {
9095 try {
9196 return $ this ->executeCommand ('date ' );
92- } catch (\ RuntimeException $ e ) {
97+ } catch (RuntimeException $ e ) {
9398 return '' ;
9499 }
95100 }
@@ -102,7 +107,7 @@ public function getUptime(): int {
102107 preg_match ("/[\d]+/ " , $ shell_boot , $ boottime );
103108 $ time = $ this ->executeCommand ('date +%s ' );
104109 $ uptime = (int )$ time - (int )$ boottime [0 ];
105- } catch (\ RuntimeException $ e ) {
110+ } catch (RuntimeException $ e ) {
106111 return $ uptime ;
107112 }
108113 return $ uptime ;
@@ -122,91 +127,69 @@ public function getNetworkInfo(): array {
122127 if (count ($ gw [0 ]) > 0 ) {
123128 $ result ['gateway ' ] = implode (", " , array_map ("trim " , $ gw [0 ]));
124129 }
125- } catch (\ RuntimeException $ e ) {
130+ } catch (RuntimeException $ e ) {
126131 return $ result ;
127132 }
128133 return $ result ;
129134 }
130135
131136 public function getNetworkInterfaces (): array {
132- $ result = [];
137+ $ data = [];
133138
134- try {
135- $ ifconfig = $ this ->executeCommand ('/sbin/ifconfig -a ' );
136- } catch (\RuntimeException $ e ) {
137- return $ result ;
138- }
139+ foreach ($ this ->getNetInterfaces () as $ interfaceName => $ interface ) {
140+ $ netInterface = new NetInterface ($ interfaceName , $ interface ['up ' ]);
141+ $ data [] = $ netInterface ;
139142
140- preg_match_all ("/^(?<=(?! \t)).*(?=:)/m " , $ ifconfig , $ interfaces );
143+ foreach ($ interface ['unicast ' ] as $ unicast ) {
144+ if ($ unicast ['family ' ] === self ::AF_INET ) {
145+ $ netInterface ->addIPv4 ($ unicast ['address ' ]);
146+ }
147+ if ($ unicast ['family ' ] === self ::AF_INET6 ) {
148+ $ netInterface ->addIPv6 ($ unicast ['address ' ]);
149+ }
150+ }
141151
142- foreach ($ interfaces [ 0 ] as $ interface ) {
143- $ iface = [] ;
144- $ iface [ ' interface ' ] = $ interface ;
152+ if ($ netInterface -> isLoopback () ) {
153+ continue ;
154+ }
145155
146156 try {
147- $ intface = $ this ->executeCommand ('/sbin/ifconfig ' . $ iface [ ' interface ' ] );
148- } catch (\ RuntimeException $ e ) {
157+ $ details = $ this ->executeCommand ('/sbin/ifconfig ' . $ interfaceName );
158+ } catch (RuntimeException $ e ) {
149159 continue ;
150160 }
151161
152- preg_match_all ("/(?<=inet ).\S*/m " , $ intface , $ ipv4 );
153- preg_match_all ("/(?<=inet6 )((.*(?=%))|(.\S*))/m " , $ intface , $ ipv6 );
154- $ iface ['ipv4 ' ] = implode (' ' , $ ipv4 [0 ]);
155- $ iface ['ipv6 ' ] = implode (' ' , $ ipv6 [0 ]);
156-
157- if ($ iface ['interface ' ] !== 'lo0 ' ) {
158- preg_match_all ("/(?<=ether ).*/m " , $ intface , $ mac );
159- preg_match ("/(?<=status: ).*/m " , $ intface , $ status );
160- preg_match ("/\b[0-9].*?(?=base)/m " , $ intface , $ speed );
161- preg_match ("/(?<=\<).*(?=-)/m " , $ intface , $ duplex );
162-
163- if (isset ($ mac [0 ])) {
164- $ iface ['mac ' ] = implode (' ' , $ mac [0 ]);
165- }
166-
167- if (isset ($ speed [0 ])) {
168- $ iface ['speed ' ] = $ speed [0 ];
169- }
170-
171- if (isset ($ status [0 ])) {
172- $ iface ['status ' ] = $ status [0 ];
173- } else {
174- $ iface ['status ' ] = 'active ' ;
175- }
162+ preg_match ("/(?<=ether ).*/m " , $ details , $ mac );
163+ if (isset ($ mac [0 ])) {
164+ $ netInterface ->setMAC ($ mac [0 ]);
165+ }
176166
177- if (isset ($ iface ['speed ' ])) {
178- if (strpos ($ iface ['speed ' ], 'G ' )) {
179- $ iface ['speed ' ] = rtrim ($ iface ['speed ' ], 'G ' );
180- $ iface ['speed ' ] = $ iface ['speed ' ] . ' Gbps ' ;
181- } else {
182- $ iface ['speed ' ] = $ iface ['speed ' ] . ' Mbps ' ;
183- }
167+ preg_match ("/\b[0-9].*?(?=base)/m " , $ details , $ speed );
168+ if (isset ($ speed [0 ])) {
169+ if (substr ($ speed [0 ], -1 ) === 'G ' ) {
170+ $ netInterface ->setSpeed (rtrim ($ speed [0 ], 'G ' ) . ' Gbps ' );
184171 } else {
185- $ iface [ ' speed ' ] = ' unknown ' ;
172+ $ netInterface -> setSpeed ( $ speed[ 0 ] . ' Mbps ' ) ;
186173 }
174+ }
187175
188- if (isset ($ duplex [0 ])) {
189- $ iface ['duplex ' ] = 'Duplex: ' . $ duplex [0 ];
190- } else {
191- $ iface ['duplex ' ] = '' ;
192- }
193- } else {
194- $ iface ['status ' ] = 'active ' ;
195- $ iface ['speed ' ] = 'unknown ' ;
196- $ iface ['duplex ' ] = '' ;
176+ preg_match ("/(?<=\<).*(?=-)/m " , $ details , $ duplex );
177+ if (isset ($ duplex [0 ])) {
178+ $ netInterface ->setDuplex ($ duplex [0 ]);
197179 }
198- $ result [] = $ iface ;
180+
181+ unset($ mac , $ speed , $ duplex );
199182 }
200183
201- return $ result ;
184+ return $ data ;
202185 }
203186
204187 public function getDiskInfo (): array {
205188 $ data = [];
206189
207190 try {
208191 $ disks = $ this ->executeCommand ('df -TPk ' );
209- } catch (\ RuntimeException $ e ) {
192+ } catch (RuntimeException $ e ) {
210193 return $ data ;
211194 }
212195
@@ -245,8 +228,21 @@ public function getThermalZones(): array {
245228 protected function executeCommand (string $ command ): string {
246229 $ output = @shell_exec (escapeshellcmd ($ command ));
247230 if ($ output === null || $ output === '' || $ output === false ) {
248- throw new \ RuntimeException ('No output for command: " ' . $ command . '" ' );
231+ throw new RuntimeException ('No output for command: " ' . $ command . '" ' );
249232 }
250233 return $ output ;
251234 }
235+
236+ /**
237+ * Wrapper for net_get_interfaces
238+ *
239+ * @throws RuntimeException
240+ */
241+ protected function getNetInterfaces (): array {
242+ $ data = net_get_interfaces ();
243+ if ($ data === false ) {
244+ throw new RuntimeException ('Unable to get network interfaces ' );
245+ }
246+ return $ data ;
247+ }
252248}
0 commit comments