Compare commits
7 Commits
nilmrun-1.
...
nilmrun-1.
Author | SHA1 | Date | |
---|---|---|---|
38c3e67cf9 | |||
7f05a0fb62 | |||
81c2ad07d4 | |||
3588e843ac | |||
9309fd9b57 | |||
21bd1bd050 | |||
cafdfce4f0 |
@@ -7,7 +7,7 @@ Prerequisites:
|
|||||||
sudo apt-get install python2.7 python-setuptools
|
sudo apt-get install python2.7 python-setuptools
|
||||||
|
|
||||||
# Plus nilmdb and its dependencies
|
# Plus nilmdb and its dependencies
|
||||||
nilmdb (1.8.3+)
|
nilmdb (1.9.5+)
|
||||||
|
|
||||||
Install:
|
Install:
|
||||||
|
|
||||||
|
50
scripts/kill.py
Executable file
50
scripts/kill.py
Executable file
@@ -0,0 +1,50 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
from nilmdb.client.httpclient import HTTPClient, ClientError, ServerError
|
||||||
|
from nilmdb.utils.printf import *
|
||||||
|
import nilmrun
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Kill/remove a process from the NilmRun server"""
|
||||||
|
def_url = os.environ.get("NILMRUN_URL", "http://localhost/nilmrun/")
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description = 'Kill/remove a process from the NilmRun server',
|
||||||
|
formatter_class = argparse.ArgumentDefaultsHelpFormatter,
|
||||||
|
version = nilmrun.__version__)
|
||||||
|
group = parser.add_argument_group("Standard options")
|
||||||
|
group.add_argument('-u', '--url',
|
||||||
|
help = 'NilmRun server URL', default = def_url)
|
||||||
|
group.add_argument('-n', '--noverify', action="store_true",
|
||||||
|
help = 'Disable SSL certificate verification')
|
||||||
|
group = parser.add_argument_group("Program")
|
||||||
|
group.add_argument('-q', '--quiet', action="store_true",
|
||||||
|
help = "Don't print out the final log contents")
|
||||||
|
group.add_argument('pid', nargs='+', help="PIDs to kill")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
client = HTTPClient(baseurl = args.url, verify_ssl = not args.noverify)
|
||||||
|
|
||||||
|
# Kill or remove process
|
||||||
|
all_failed = True
|
||||||
|
for pid in args.pid:
|
||||||
|
try:
|
||||||
|
s = client.post("process/remove", { "pid": pid })
|
||||||
|
if not args.quiet:
|
||||||
|
sys.stdout.write(s['log'])
|
||||||
|
all_failed = False
|
||||||
|
except ClientError as e:
|
||||||
|
if "404" in e.status:
|
||||||
|
fprintf(sys.stderr, "no such pid: %s\n", pid)
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
# Return error if we failed to remove any of them
|
||||||
|
if all_failed:
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@@ -10,10 +10,8 @@ def main():
|
|||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description = 'Run the NilmRun server',
|
description = 'Run the NilmRun server',
|
||||||
formatter_class = argparse.ArgumentDefaultsHelpFormatter)
|
formatter_class = argparse.ArgumentDefaultsHelpFormatter,
|
||||||
|
version = nilmrun.__version__)
|
||||||
parser.add_argument("-V", "--version", action="version",
|
|
||||||
version = nilmrun.__version__)
|
|
||||||
|
|
||||||
group = parser.add_argument_group("Standard options")
|
group = parser.add_argument_group("Standard options")
|
||||||
group.add_argument('-a', '--address',
|
group.add_argument('-a', '--address',
|
||||||
|
66
scripts/ps.py
Executable file
66
scripts/ps.py
Executable file
@@ -0,0 +1,66 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
from nilmdb.client.httpclient import HTTPClient, ClientError, ServerError
|
||||||
|
from nilmdb.utils.printf import *
|
||||||
|
from nilmdb.utils import datetime_tz
|
||||||
|
import nilmrun
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""List NilmRun processes"""
|
||||||
|
def_url = os.environ.get("NILMRUN_URL", "http://localhost/nilmrun/")
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description = 'List NilmRun processes',
|
||||||
|
formatter_class = argparse.ArgumentDefaultsHelpFormatter,
|
||||||
|
version = nilmrun.__version__)
|
||||||
|
group = parser.add_argument_group("Standard options")
|
||||||
|
group.add_argument('-u', '--url',
|
||||||
|
help = 'NilmRun server URL', default = def_url)
|
||||||
|
group.add_argument('-n', '--noverify', action="store_true",
|
||||||
|
help = 'Disable SSL certificate verification')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
client = HTTPClient(baseurl = args.url, verify_ssl = not args.noverify)
|
||||||
|
# Print overall system info
|
||||||
|
info = client.get("process/info")
|
||||||
|
total = info['total']
|
||||||
|
system = info['system']
|
||||||
|
printf(" procs: %d nilm, %d other\n", info['total']['procs'],
|
||||||
|
info['system']['procs'] - info['total']['procs'])
|
||||||
|
printf(" cpu: %d%% nilm, %d%% other, %d%% max\n",
|
||||||
|
round(info['total']['cpu_percent']),
|
||||||
|
round(info['system']['cpu_percent'] - info['total']['cpu_percent']),
|
||||||
|
round(info['system']['cpu_max']))
|
||||||
|
printf(" mem: %d MiB used, %d MiB total, %d%%\n",
|
||||||
|
round(info['system']['mem_used'] / 1048576.0),
|
||||||
|
round(info['system']['mem_total'] / 1048576.0),
|
||||||
|
round(info['system']['mem_used'] * 100.0
|
||||||
|
/ info['system']['mem_total']))
|
||||||
|
|
||||||
|
# Print process detail for each managed process
|
||||||
|
fmt = "%-36s %-6s %-15s %-4s %-3s %-5s\n"
|
||||||
|
printf(fmt, "PID", "STATE", "SINCE", "PROC", "CPU", "LOG")
|
||||||
|
|
||||||
|
if len(info['pids']) == 0:
|
||||||
|
printf("No running processes\n")
|
||||||
|
raise SystemExit(0)
|
||||||
|
|
||||||
|
for pid in sorted(info['pids'].keys()):
|
||||||
|
pidinfo = client.get("process/status", { "pid": pid })
|
||||||
|
if pidinfo['alive']:
|
||||||
|
status = "alive"
|
||||||
|
else:
|
||||||
|
if pidinfo['exitcode']:
|
||||||
|
status = "error"
|
||||||
|
else:
|
||||||
|
status = "done"
|
||||||
|
dt = datetime_tz.datetime_tz.fromtimestamp(pidinfo['start_time'])
|
||||||
|
since = dt.strftime("%m/%d-%H:%M:%S")
|
||||||
|
printf(fmt, pid, status, since, info['pids'][pid]['procs'],
|
||||||
|
str(int(round(info['pids'][pid]['cpu_percent']))),
|
||||||
|
len(pidinfo['log']))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
59
scripts/run.py
Executable file
59
scripts/run.py
Executable file
@@ -0,0 +1,59 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
from nilmdb.client.httpclient import HTTPClient, ClientError, ServerError
|
||||||
|
from nilmdb.utils.printf import *
|
||||||
|
import nilmrun
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Run a command on the NilmRun server"""
|
||||||
|
def_url = os.environ.get("NILMRUN_URL", "http://localhost/nilmrun/")
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description = 'Run a command on the NilmRun server',
|
||||||
|
formatter_class = argparse.ArgumentDefaultsHelpFormatter,
|
||||||
|
version = nilmrun.__version__)
|
||||||
|
group = parser.add_argument_group("Standard options")
|
||||||
|
group.add_argument('-u', '--url',
|
||||||
|
help = 'NilmRun server URL', default = def_url)
|
||||||
|
group.add_argument('-n', '--noverify', action="store_true",
|
||||||
|
help = 'Disable SSL certificate verification')
|
||||||
|
group = parser.add_argument_group("Program")
|
||||||
|
group.add_argument('-d', '--detach', action="store_true",
|
||||||
|
help = 'Run process and return immediately without '
|
||||||
|
'printing its output')
|
||||||
|
group.add_argument('cmd', help="Command to run")
|
||||||
|
group.add_argument('arg', nargs=argparse.REMAINDER,
|
||||||
|
help="Arguments for command")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
client = HTTPClient(baseurl = args.url, verify_ssl = not args.noverify)
|
||||||
|
|
||||||
|
# Run command
|
||||||
|
pid = client.post("run/command", { "argv": [ args.cmd ] + args.arg })
|
||||||
|
|
||||||
|
# If we're detaching, just print the PID
|
||||||
|
if args.detach:
|
||||||
|
print pid
|
||||||
|
raise SystemExit(0)
|
||||||
|
|
||||||
|
# Otherwise, watch the log output, and kill the process when it's done
|
||||||
|
# or when this script terminates.
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
s = client.get("process/status", { "pid": pid, "clear": 1 })
|
||||||
|
sys.stdout.write(s['log'])
|
||||||
|
sys.stdout.flush()
|
||||||
|
if not s['alive']:
|
||||||
|
break
|
||||||
|
time.sleep(1)
|
||||||
|
finally:
|
||||||
|
s = client.post("process/remove", { "pid": pid })
|
||||||
|
|
||||||
|
raise SystemExit(s['exitcode'])
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
5
setup.py
5
setup.py
@@ -61,7 +61,7 @@ setup(name='nilmrun',
|
|||||||
long_description = "NILM Database Filter Runner",
|
long_description = "NILM Database Filter Runner",
|
||||||
license = "Proprietary",
|
license = "Proprietary",
|
||||||
author_email = 'jim@jtan.com',
|
author_email = 'jim@jtan.com',
|
||||||
install_requires = [ 'nilmdb >= 1.8.3',
|
install_requires = [ 'nilmdb >= 1.9.5',
|
||||||
'psutil >= 0.3.0',
|
'psutil >= 0.3.0',
|
||||||
'cherrypy >= 3.2',
|
'cherrypy >= 3.2',
|
||||||
'simplejson',
|
'simplejson',
|
||||||
@@ -75,6 +75,9 @@ setup(name='nilmrun',
|
|||||||
entry_points = {
|
entry_points = {
|
||||||
'console_scripts': [
|
'console_scripts': [
|
||||||
'nilmrun-server = nilmrun.scripts.nilmrun_server:main',
|
'nilmrun-server = nilmrun.scripts.nilmrun_server:main',
|
||||||
|
'nilmrun-ps = nilmrun.scripts.ps:main',
|
||||||
|
'nilmrun-run = nilmrun.scripts.run:main',
|
||||||
|
'nilmrun-kill = nilmrun.scripts.kill:main',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
zip_safe = False,
|
zip_safe = False,
|
||||||
|
@@ -352,8 +352,10 @@ class TestClient(object):
|
|||||||
# start some processes
|
# start some processes
|
||||||
a = client.post("run/command", { "argv": ["sleep","60"] } )
|
a = client.post("run/command", { "argv": ["sleep","60"] } )
|
||||||
b = client.post("run/command", { "argv": ["sh","-c","sleep 2;true"] } )
|
b = client.post("run/command", { "argv": ["sh","-c","sleep 2;true"] } )
|
||||||
c = client.post("run/command", { "argv": ["sh","-c","burnP5;true"] } )
|
c = client.post("run/command", { "argv": [
|
||||||
d = client.post("run/command", { "argv": ["burnP5" ] } )
|
"sh","-c","dd if=/dev/zero of=/dev/null;true"] } )
|
||||||
|
d = client.post("run/command", { "argv": [
|
||||||
|
"dd", "if=/dev/zero", "of=/dev/null" ] } )
|
||||||
|
|
||||||
info = client.get("process/info")
|
info = client.get("process/info")
|
||||||
eq_(info["pids"][a]["procs"], 1)
|
eq_(info["pids"][a]["procs"], 1)
|
||||||
@@ -365,9 +367,13 @@ class TestClient(object):
|
|||||||
lt_(20, info["pids"][c]["cpu_percent"])
|
lt_(20, info["pids"][c]["cpu_percent"])
|
||||||
lt_(80, info["system"]["cpu_percent"])
|
lt_(80, info["system"]["cpu_percent"])
|
||||||
|
|
||||||
time.sleep(2)
|
for x in range(10):
|
||||||
info = client.get("process/info")
|
time.sleep(1)
|
||||||
eq_(info["pids"][b]["procs"], 0)
|
info = client.get("process/info")
|
||||||
|
if info["pids"][b]["procs"] != 2:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise Exception("process B didn't die: " + str(info["pids"][b]))
|
||||||
|
|
||||||
# kill all processes
|
# kill all processes
|
||||||
for pid in client.get("process/list"):
|
for pid in client.get("process/list"):
|
||||||
@@ -377,7 +383,8 @@ class TestClient(object):
|
|||||||
client = HTTPClient(baseurl = testurl, post_json = True)
|
client = HTTPClient(baseurl = testurl, post_json = True)
|
||||||
eq_(client.get("process/list"), [])
|
eq_(client.get("process/list"), [])
|
||||||
def verify(cmd, result):
|
def verify(cmd, result):
|
||||||
pid = client.post("run/command", { "argv": [ "sh", "-c", cmd ] })
|
pid = client.post("run/command", { "argv": [
|
||||||
|
"/bin/bash", "-c", cmd ] })
|
||||||
eq_(client.get("process/list"), [pid])
|
eq_(client.get("process/list"), [pid])
|
||||||
status = self.wait_end(client, pid)
|
status = self.wait_end(client, pid)
|
||||||
eq_(result, status["log"])
|
eq_(result, status["log"])
|
||||||
|
Reference in New Issue
Block a user