sysvitals_client 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/python
  2. import ConfigParser
  3. import json
  4. import os
  5. from os import path
  6. import socket
  7. import sys
  8. import urllib2
  9. import psutil
  10. is_root = (os.getuid() == 0)
  11. config = ConfigParser.RawConfigParser()
  12. if is_root:
  13. cfg_path = '/etc/sysvitals.cfg'
  14. else:
  15. cfg_path = path.join(path.dirname(path.abspath(__file__)), 'sysvitals.cfg')
  16. def post():
  17. if not config.read(cfg_path):
  18. sys.exit('no config found at %s' % cfg_path)
  19. cpu = psutil.cpu_times()
  20. mem = psutil.virtual_memory()
  21. net = psutil.net_io_counters()
  22. datum = {
  23. 'cpu': cpu._asdict(),
  24. 'mem': {
  25. 'total': mem.total,
  26. 'used': mem.used,
  27. 'buffers': mem.buffers,
  28. 'cached': mem.cached,
  29. },
  30. 'net': net._asdict(),
  31. 'disk': {}
  32. }
  33. datum['cpu']['num_cpus'] = psutil.NUM_CPUS
  34. for partition in psutil.disk_partitions():
  35. usage = psutil.disk_usage(partition.mountpoint)
  36. datum['disk'][partition.mountpoint] = {
  37. 'total': usage.total,
  38. 'used': usage.used,
  39. }
  40. api_server = config.get('client', 'api_server')
  41. group_id = config.get('client', 'group_id')
  42. server_id = config.get('client', 'server_id')
  43. url = '%s/v1/%s/datum/%s' % (api_server, group_id, server_id)
  44. urllib2.urlopen(url, json.dumps(datum))
  45. def configure():
  46. if not is_root:
  47. print 'configuring sysvitals client not as root will create a local config'
  48. if raw_input('are you sure you want to continue? [y/N] ') != 'y':
  49. return
  50. if path.exists(cfg_path):
  51. print cfg_path, 'already exists'
  52. if raw_input('are you sure you want to continue? [y/N] ') != 'y':
  53. return
  54. default_api_server = 'http://localhost:8892'
  55. config.add_section('client')
  56. config.set('client', 'api_server', default_api_server)
  57. group_id = None
  58. while group_id is None:
  59. group_id = raw_input('group_id: ')
  60. try:
  61. int(group_id)
  62. except ValueError:
  63. group_id = None
  64. print 'group_id should be numeric'
  65. config.set('client', 'group_id', group_id)
  66. print 'checking existing servers'
  67. try:
  68. url = '%s/v1/%s/servers' % (default_api_server, group_id)
  69. r = urllib2.urlopen(url)
  70. servers = json.load(r)
  71. except Exception as e:
  72. sys.exit('an error occurred while fetching existing servers:\n%r' % e)
  73. our_hostname = socket.getfqdn()
  74. server_id = None
  75. matches = 0
  76. for server in servers:
  77. if server['hostname'] == our_hostname:
  78. server_id = server['id']
  79. matches += 1
  80. if matches == 0:
  81. print 'no hostname matches found'
  82. elif matches == 1:
  83. print '1 hostname match found (%s, server id %d)' % (our_hostname, server_id)
  84. if raw_input('take over this server id? (only do this if the old host is dead) [y/N] ') != 'y':
  85. server_id = None
  86. else:
  87. print 'found %d servers with the same hostname... hopefully this is ok...' % matches
  88. server_id = None
  89. if server_id is None:
  90. print 'getting a brand new server id'
  91. try:
  92. url = '%s/v1/%s/register_server' % (default_api_server, group_id)
  93. r = urllib2.urlopen(url, json.dumps({'hostname': our_hostname}))
  94. server_id = json.load(r)['server_id']
  95. except Exception as e:
  96. sys.exit('an error occurred while getting the new server id:\n%r' % e)
  97. config.set('client', 'server_id', server_id)
  98. print 'writing config to', cfg_path
  99. with open(cfg_path, 'w') as f:
  100. config.write(f)
  101. if len(sys.argv) > 1 and sys.argv[1] == '--configure':
  102. configure()
  103. else:
  104. post()