mirror of https://git.48k.eu/ogclient
#995 Add link speed in probe responses
Separates probe method into separate ogclient modes (virtual, vdi) so future supported OS can easily have a tailored probe responses. Link speed is retrieved using a minimal ethtool command sent using fcntl module from python.more_events
parent
8b959c8be9
commit
bd98dd1da0
|
@ -9,7 +9,9 @@
|
|||
import os
|
||||
import json
|
||||
import subprocess
|
||||
import fcntl, socket, array, struct
|
||||
from src.ogClient import ogClient
|
||||
from src.ogRest import ThreadState
|
||||
|
||||
OG_SHELL = '/bin/bash'
|
||||
|
||||
|
@ -276,3 +278,30 @@ class OgLiveOperations:
|
|||
self._restartBrowser(self._url)
|
||||
|
||||
return self.parseGetConf(output.decode('utf-8'))
|
||||
|
||||
def probe(self, ogRest):
|
||||
def ethtool(interface):
|
||||
try:
|
||||
ETHTOOL_GSET = 0x00000001
|
||||
SIOCETHTOOL = 0x8946
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sockfd = sock.fileno()
|
||||
ecmd = array.array(
|
||||
"B", struct.pack("I39s", ETHTOOL_GSET, b"\x00" * 39)
|
||||
)
|
||||
interface = interface.encode("utf-8")
|
||||
ifreq = struct.pack("16sP", interface, ecmd.buffer_info()[0])
|
||||
fcntl.ioctl(sockfd, SIOCETHTOOL, ifreq)
|
||||
res = ecmd.tobytes()
|
||||
speed = struct.unpack("12xH29x", res)[0]
|
||||
except IOError:
|
||||
speed = 0
|
||||
finally:
|
||||
sock.close()
|
||||
return speed
|
||||
|
||||
interface = os.environ['DEVICE']
|
||||
speed = ethtool(interface)
|
||||
|
||||
return {'status': 'OPG' if ogRest.state != ThreadState.BUSY else 'BSY',
|
||||
'speed': speed}
|
||||
|
|
|
@ -373,16 +373,20 @@ class ogRest():
|
|||
threading.Thread(target=ogThread.poweroff, args=(self,)).start()
|
||||
|
||||
def process_probe(self, client):
|
||||
try:
|
||||
status = self.operations.probe(self)
|
||||
except:
|
||||
response = restResponse(ogResponses.INTERNAL_ERR)
|
||||
client.send(response.get())
|
||||
return
|
||||
|
||||
json_body = jsonBody()
|
||||
for k, v in status.items():
|
||||
json_body.add_element(k, v)
|
||||
|
||||
if self.state != ThreadState.BUSY:
|
||||
if self.mode == 'live':
|
||||
json_body.add_element('status', 'OPG')
|
||||
elif self.mode == 'virtual':
|
||||
json_body.add_element('status', 'VDI')
|
||||
response = restResponse(ogResponses.OK, json_body)
|
||||
else:
|
||||
json_body.add_element('status', 'BSY')
|
||||
response = restResponse(ogResponses.IN_PROGRESS, json_body)
|
||||
|
||||
client.send(response.get())
|
||||
|
|
|
@ -600,3 +600,6 @@ class OgVirtualOperations:
|
|||
for k, v in device_names.items():
|
||||
f.write(f'{k}={v}\n')
|
||||
f.truncate()
|
||||
|
||||
def probe(self, ogRest):
|
||||
return {'status': 'VDI' if ogRest.state != ThreadState.BUSY else 'BSY'}
|
||||
|
|
Loading…
Reference in New Issue