sysvitals_client 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. default_api_server = 'http://localhost:8892'
  17. def post():
  18. if not config.read(cfg_path):
  19. sys.exit('no config found at %s' % cfg_path)
  20. cpu = psutil.cpu_times()
  21. mem = psutil.virtual_memory()
  22. net = psutil.net_io_counters()
  23. datum = {
  24. 'cpu': cpu._asdict(),
  25. 'mem': {
  26. 'total': mem.total,
  27. 'used': mem.used,
  28. 'buffers': mem.buffers,
  29. 'cached': mem.cached,
  30. },
  31. 'net': net._asdict(),
  32. 'disk': {}
  33. }
  34. datum['cpu']['num_cpus'] = psutil.NUM_CPUS
  35. for partition in psutil.disk_partitions():
  36. usage = psutil.disk_usage(partition.mountpoint)
  37. datum['disk'][partition.mountpoint] = {
  38. 'total': usage.total,
  39. 'used': usage.used,
  40. }
  41. api_key = config.get('client', 'api_key')
  42. group_id = config.get('client', 'group_id')
  43. server_id = config.get('client', 'server_id')
  44. __query(group_id, 'datum/' + server_id, api_key, 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. config.add_section('client')
  55. config.set('client', 'api_server', default_api_server)
  56. group_id = None
  57. while group_id is None:
  58. group_id = raw_input('group_id: ')
  59. try:
  60. int(group_id)
  61. except ValueError:
  62. group_id = None
  63. print 'group_id should be numeric'
  64. config.set('client', 'group_id', group_id)
  65. api_key = raw_input('api_key: ')
  66. config.set('client', 'api_key', api_key)
  67. print 'checking existing servers'
  68. servers = __query(group_id, 'servers', api_key)
  69. our_hostname = socket.gethostname()
  70. server_id = None
  71. matches = 0
  72. for server in servers:
  73. if server['hostname'] == our_hostname:
  74. server_id = server['id']
  75. matches += 1
  76. if matches == 0:
  77. print 'no hostname matches found'
  78. elif matches == 1:
  79. print '1 hostname match found (%s, server id %d)' % (our_hostname, server_id)
  80. if raw_input('take over this server id? (only do this if the old host is dead) [y/N] ') != 'y':
  81. server_id = None
  82. else:
  83. print 'found %d servers with the same hostname... hopefully this is ok...' % matches
  84. server_id = None
  85. if server_id is None:
  86. print 'getting a brand new server id'
  87. response = __query(group_id, 'register_server', api_key, {'hostname': our_hostname})
  88. server_id = response['server_id']
  89. config.set('client', 'server_id', server_id)
  90. print 'writing config to', cfg_path
  91. with open(cfg_path, 'w') as f:
  92. config.write(f)
  93. def __query(group_id, endpoint, api_key, data=None):
  94. api_server = config.get('client', 'api_server')
  95. url = '%s/v1/%s/%s' % (api_server, group_id, endpoint)
  96. if data is not None:
  97. data = json.dumps(data)
  98. req = urllib2.Request(url, data, {'Authorization': api_key})
  99. try:
  100. return json.load(urllib2.urlopen(req))
  101. except urllib2.HTTPError as e:
  102. sys.exit('http error (%d):\n%s' % (e.code, e.read()))
  103. except Exception as e:
  104. sys.exit('an error occurred while querying%s:\n%r' % (endpoint, e))
  105. if len(sys.argv) > 1 and sys.argv[1] == '--configure':
  106. configure()
  107. else:
  108. post()