sysvitals_client 3.5 KB

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