2525
2626use OCA \ServerInfo \Resources \Disk ;
2727use OCA \ServerInfo \Resources \Memory ;
28+ use OCA \ServerInfo \Resources \NetInterface ;
29+ use RuntimeException ;
2830
2931class DefaultOs implements IOperatingSystem {
32+ private const AF_INET = 2 ;
33+ private const AF_INET6 = 10 ;
34+
3035 public function supported (): bool {
3136 return true ;
3237 }
@@ -36,7 +41,7 @@ public function getMemory(): Memory {
3641
3742 try {
3843 $ meminfo = $ this ->readContent ('/proc/meminfo ' );
39- } catch (\ RuntimeException $ e ) {
44+ } catch (RuntimeException $ e ) {
4045 return $ data ;
4146 }
4247
@@ -79,7 +84,7 @@ public function getCpuName(): string {
7984
8085 try {
8186 $ cpuinfo = $ this ->readContent ('/proc/cpuinfo ' );
82- } catch (\ RuntimeException $ e ) {
87+ } catch (RuntimeException $ e ) {
8388 return $ data ;
8489 }
8590
@@ -100,7 +105,7 @@ public function getCpuName(): string {
100105
101106 $ pattern = '/processor\s+:\s(.+)/ ' ;
102107
103- $ result = preg_match_all ($ pattern , $ cpuinfo , $ matches );
108+ preg_match_all ($ pattern , $ cpuinfo , $ matches );
104109 $ cores = count ($ matches [1 ]);
105110
106111 if ($ cores === 1 ) {
@@ -121,7 +126,7 @@ public function getUptime(): int {
121126
122127 try {
123128 $ uptime = $ this ->readContent ('/proc/uptime ' );
124- } catch (\ RuntimeException $ e ) {
129+ } catch (RuntimeException $ e ) {
125130 return $ data ;
126131 }
127132
@@ -141,50 +146,55 @@ public function getNetworkInfo(): array {
141146 }
142147
143148 public function getNetworkInterfaces (): array {
144- $ interfaces = glob ('/sys/class/net/* ' ) ?: [];
145- $ result = [];
149+ $ data = [];
146150
147- foreach ($ interfaces as $ interface ) {
148- $ iface = [];
149- $ iface ['interface ' ] = basename ($ interface );
150- $ iface ['mac ' ] = shell_exec ('ip addr show dev ' . $ iface ['interface ' ] . ' | grep "link/ether " | cut -d \' \' -f 6 | cut -f 1 -d \'/ \'' );
151- $ iface ['ipv4 ' ] = shell_exec ('ip addr show dev ' . $ iface ['interface ' ] . ' | grep "inet " | cut -d \' \' -f 6 | cut -f 1 -d \'/ \'' );
152- $ iface ['ipv6 ' ] = shell_exec ('ip -o -6 addr show ' . $ iface ['interface ' ] . ' | sed -e \'s/^.*inet6 \([^ ]\+\).*/\1/ \'' );
153- if ($ iface ['interface ' ] !== 'lo ' ) {
154- $ iface ['status ' ] = shell_exec ('cat /sys/class/net/ ' . $ iface ['interface ' ] . '/operstate ' );
155- $ iface ['speed ' ] = (int )shell_exec ('cat /sys/class/net/ ' . $ iface ['interface ' ] . '/speed ' );
156- if (isset ($ iface ['speed ' ]) && $ iface ['speed ' ] > 0 ) {
157- if ($ iface ['speed ' ] >= 1000 ) {
158- $ iface ['speed ' ] = $ iface ['speed ' ] / 1000 . ' Gbps ' ;
159- } else {
160- $ iface ['speed ' ] = $ iface ['speed ' ] . ' Mbps ' ;
161- }
162- } else {
163- $ iface ['speed ' ] = 'unknown ' ;
151+ foreach ($ this ->getNetInterfaces () as $ interfaceName => $ interface ) {
152+ $ netInterface = new NetInterface ();
153+ $ data [] = $ netInterface ;
154+
155+ $ netInterface ->setName ($ interfaceName );
156+ $ netInterface ->setUp ($ interface ['up ' ]);
157+
158+ foreach ($ interface ['unicast ' ] as $ unicast ) {
159+ if ($ unicast ['family ' ] === self ::AF_INET ) {
160+ $ netInterface ->addIPv4 ($ unicast ['address ' ]);
161+ }
162+ if ($ unicast ['family ' ] === self ::AF_INET6 ) {
163+ $ netInterface ->addIPv6 ($ unicast ['address ' ]);
164164 }
165- $ duplex = shell_exec ('cat /sys/class/net/ ' . $ iface ['interface ' ] . '/duplex ' );
166- if (isset ($ duplex ) && $ duplex !== '' ) {
167- $ iface ['duplex ' ] = 'Duplex: ' . $ duplex ;
165+ }
166+
167+ if ($ netInterface ->isLoopback ()) {
168+ continue ;
169+ }
170+
171+ $ interfacePath = '/sys/class/net/ ' . $ interfaceName ;
172+
173+ try {
174+ $ netInterface ->setMAC ($ this ->readContent ($ interfacePath . '/address ' ));
175+
176+ $ speed = (int )$ this ->readContent ($ interfacePath . '/speed ' );
177+ if ($ speed >= 1000 ) {
178+ $ netInterface ->setSpeed ($ speed / 1000 . ' Gbps ' );
168179 } else {
169- $ iface [ ' duplex ' ] = '' ;
180+ $ netInterface -> setSpeed ( $ speed . ' Mbps ' ) ;
170181 }
171- } else {
172- $ iface [ ' status ' ] = ' up ' ;
173- $ iface [ ' speed ' ] = ' unknown ' ;
174- $ iface [ ' duplex ' ] = '' ;
182+
183+ $ netInterface -> setDuplex ( $ this -> readContent ( $ interfacePath . ' /duplex ' )) ;
184+ } catch ( RuntimeException $ e ) {
185+ // unable to read interface data
175186 }
176- $ result [] = $ iface ;
177187 }
178188
179- return $ result ;
189+ return $ data ;
180190 }
181191
182192 public function getDiskInfo (): array {
183193 $ data = [];
184194
185195 try {
186196 $ disks = $ this ->executeCommand ('df -TPk ' );
187- } catch (\ RuntimeException $ e ) {
197+ } catch (RuntimeException $ e ) {
188198 return $ data ;
189199 }
190200
@@ -227,7 +237,7 @@ public function getThermalZones(): array {
227237 $ tzone ['hash ' ] = md5 ($ thermalZone );
228238 $ tzone ['type ' ] = $ this ->readContent ($ thermalZone . '/type ' );
229239 $ tzone ['temp ' ] = (float )((int )($ this ->readContent ($ thermalZone . '/temp ' )) / 1000 );
230- } catch (\ RuntimeException $ e ) {
240+ } catch (RuntimeException $ e ) {
231241 continue ;
232242 }
233243 $ result [] = $ tzone ;
@@ -236,19 +246,35 @@ public function getThermalZones(): array {
236246 return $ result ;
237247 }
238248
249+ /**
250+ * @throws RuntimeException
251+ */
239252 protected function readContent (string $ filename ): string {
240253 $ data = @file_get_contents ($ filename );
241254 if ($ data === false || $ data === '' ) {
242- throw new \ RuntimeException ('Unable to read: " ' . $ filename . '" ' );
255+ throw new RuntimeException ('Unable to read: " ' . $ filename . '" ' );
243256 }
244- return $ data ;
257+ return trim ( $ data) ;
245258 }
246259
247260 protected function executeCommand (string $ command ): string {
248261 $ output = @shell_exec (escapeshellcmd ($ command ));
249262 if ($ output === false || $ output === null || $ output === '' ) {
250- throw new \ RuntimeException ('No output for command: " ' . $ command . '" ' );
263+ throw new RuntimeException ('No output for command: " ' . $ command . '" ' );
251264 }
252265 return $ output ;
253266 }
267+
268+ /**
269+ * Wrapper for net_get_interfaces
270+ *
271+ * @throws RuntimeException
272+ */
273+ protected function getNetInterfaces (): array {
274+ $ data = net_get_interfaces ();
275+ if ($ data === false ) {
276+ throw new RuntimeException ('Unable to get network interfaces ' );
277+ }
278+ return $ data ;
279+ }
254280}
0 commit comments