Skip to content

Instantly share code, notes, and snippets.

@Kurobyte
Created October 26, 2015 21:44
Show Gist options
  • Save Kurobyte/155cd5b293d3df090cac to your computer and use it in GitHub Desktop.
Save Kurobyte/155cd5b293d3df090cac to your computer and use it in GitHub Desktop.
Python script that runs an http server that creates an API to fetch system information for Orange PI machine(Could work in others, I didn't test it)
#!/usr/bin/env python
import os, json
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
from subprocess import *
def callCommand(comm):
return check_output(comm+"; exit 0", stderr=STDOUT, shell=True)
def getCpuInfo(req):
cpuInfo = str(callCommand("cat /proc/cpuinfo")).split('\n')
cpuData = {'processor':[]}
core = 0;
for section in cpuInfo:
if ":" in section:#search key for processors
section = str(section).split(':')
if 'processor' in section[0]:
cpuData['processor'].append({'core' : section[1].strip(), 'BogoMIPS' : ""})
elif 'BogoMIPS' in section[0]:
cpuData['processor'][core]['BogoMIPS'] = section[1].strip()
core = core + 1
else:
cpuData[section[0].strip()] = section[1].strip()
return cpuData;
def getMemoryInfo(req):
memInfo = str(callCommand("cat /proc/meminfo")).split('\n')
memData = {}
for section in memInfo:
if ":" in section:
section = str(section).split(':')
memData[section[0].strip()] = section[1].strip()
return memData;
def getSystemInfo(req):
sysData = {
'Kernel_version':str(callCommand("uname -mrs")),
'System':str(callCommand("uname -a")),
'System_Date':str(callCommand("date")),
'Distribution':{}
}
lsbData = str(callCommand("lsb_release -a")).split('\n')
for section in lsbData:
if ":" in section:
section = str(section).split(':')
sysData['Distribution'][section[0].strip()] = section[1].strip()
return sysData
def getSystemTemp(req):
nSensors = str(callCommand('ls /sys/class/hwmon/ | egrep ^"hwmon"')).split('\n')
tempData = []
for sensor in nSensors:
if sensor != "":
tempData.append({
'sensor_name': str(callCommand('cat /sys/class/hwmon/'+sensor+'/name')),
'temp': str(callCommand('cat /sys/class/hwmon/'+sensor+'/temp1_input'))
})
return tempData
class InfoHandler(SimpleHTTPRequestHandler):
def do_GET(req):
req.send_response(200)
req.send_header("Content-type", "application/json")
req.end_headers()
response = {}
if req.path == '/allinfo':
response = {
'systemp': getSystemTemp(req),
'cpuinfo': getCpuInfo(req),
'meminfo': getMemoryInfo(req),
'systeminfo': getSystemInfo(req)
}
elif req.path == '/cpuinfo':
response = {'cpuinfo': getCpuInfo(req)}
elif req.path == '/meminfo':
response = {'meminfo': getMemoryInfo(req)}
elif req.path == '/systeminfo':
response = {'systeminfo': getSystemInfo(req)}
elif req.path == '/systemp':
response = {'systemp': getSystemTemp(req)}
else:
response = {'error': {'message': 'route inexistent: "'+str(req.path)+'"'}}
req.wfile.write(json.dumps(response))
if __name__ == '__main__':
httpd = HTTPServer(('192.168.1.12', 8000), InfoHandler)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment