You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
2.6 KiB

  1. #!/usr/bin/env python3
  2. from nilmdb.client.httpclient import HTTPClient, ClientError, ServerError
  3. from nilmdb.utils.printf import *
  4. import datetime_tz
  5. import nilmrun
  6. import argparse
  7. import os
  8. def main():
  9. """List NilmRun processes"""
  10. def_url = os.environ.get("NILMRUN_URL", "http://localhost/nilmrun/")
  11. parser = argparse.ArgumentParser(
  12. description = 'List NilmRun processes',
  13. formatter_class = argparse.ArgumentDefaultsHelpFormatter)
  14. parser.add_argument("-v", "--version", action="version",
  15. version=nilmrun.__version__)
  16. group = parser.add_argument_group("Standard options")
  17. group.add_argument('-u', '--url',
  18. help = 'NilmRun server URL', default = def_url)
  19. group.add_argument('-n', '--noverify', action="store_true",
  20. help = 'Disable SSL certificate verification')
  21. args = parser.parse_args()
  22. client = HTTPClient(baseurl = args.url, verify_ssl = not args.noverify)
  23. # Print overall system info
  24. info = client.get("process/info")
  25. total = info['total']
  26. system = info['system']
  27. printf(" procs: %d nilm, %d other\n", info['total']['procs'],
  28. info['system']['procs'] - info['total']['procs'])
  29. printf(" cpu: %d%% nilm, %d%% other, %d%% max\n",
  30. round(info['total']['cpu_percent']),
  31. round(info['system']['cpu_percent'] - info['total']['cpu_percent']),
  32. round(info['system']['cpu_max']))
  33. printf(" mem: %d MiB used, %d MiB total, %d%%\n",
  34. round(info['system']['mem_used'] / 1048576.0),
  35. round(info['system']['mem_total'] / 1048576.0),
  36. round(info['system']['mem_used'] * 100.0
  37. / info['system']['mem_total']))
  38. # Print process detail for each managed process
  39. fmt = "%-36s %-6s %-15s %-4s %-3s %-5s\n"
  40. printf(fmt, "PID", "STATE", "SINCE", "PROC", "CPU", "LOG")
  41. if len(info['pids']) == 0:
  42. printf("No running processes\n")
  43. raise SystemExit(0)
  44. for pid in sorted(info['pids'].keys()):
  45. pidinfo = client.get("process/status", { "pid": pid })
  46. if pidinfo['alive']:
  47. status = "alive"
  48. else:
  49. if pidinfo['exitcode']:
  50. status = "error"
  51. else:
  52. status = "done"
  53. dt = datetime_tz.datetime_tz.fromtimestamp(pidinfo['start_time'])
  54. since = dt.strftime("%m/%d-%H:%M:%S")
  55. printf(fmt, pid, status, since, info['pids'][pid]['procs'],
  56. str(int(round(info['pids'][pid]['cpu_percent']))),
  57. len(pidinfo['log']))
  58. if __name__ == "__main__":
  59. main()