| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- #!/usr/bin/python
- import ConfigParser
- import json
- import os
- from os import path
- import socket
- import sys
- import urllib2
- import psutil
- is_root = (os.getuid() == 0)
- config = ConfigParser.RawConfigParser()
- if is_root:
- cfg_path = '/etc/sysvitals.cfg'
- else:
- cfg_path = path.join(path.dirname(path.abspath(__file__)), 'sysvitals.cfg')
- default_api_server = 'http://api.sysvitals.com'
- def post():
- if not config.read(cfg_path):
- sys.exit('no config found at %s' % cfg_path)
- cpu = psutil.cpu_times()
- mem = psutil.virtual_memory()
- net = psutil.net_io_counters()
- disk = psutil.disk_io_counters()
- datum = {
- 'cpu': cpu._asdict(),
- 'mem': {
- 'total': mem.total,
- 'used': mem.used,
- 'buffers': mem.buffers,
- 'cached': mem.cached,
- },
- 'net': net._asdict(),
- 'disk': {
- 'read_bytes': disk.read_bytes,
- 'write_bytes': disk.write_bytes,
- },
- }
- datum['cpu']['num_cpus'] = psutil.NUM_CPUS
- for partition in psutil.disk_partitions():
- usage = psutil.disk_usage(partition.mountpoint)
- datum['disk'][partition.mountpoint] = {
- 'total': usage.total,
- 'used': usage.used,
- }
- api_key = config.get('client', 'api_key')
- group_id = config.get('client', 'group_id')
- server_id = config.get('client', 'server_id')
- from pprint import pprint
- pprint(datum)
- #__query(group_id, 'datum/' + server_id, api_key, datum)
- def configure():
- if not is_root:
- print 'configuring sysvitals client not as root will create a local config'
- if raw_input('are you sure you want to continue? [y/N] ') != 'y':
- return
- if path.exists(cfg_path):
- print cfg_path, 'already exists'
- if raw_input('are you sure you want to continue? [y/N] ') != 'y':
- return
- config.add_section('client')
- config.set('client', 'api_server', default_api_server)
- group_id = None
- while group_id is None:
- group_id = raw_input('group_id: ')
- try:
- int(group_id)
- except ValueError:
- group_id = None
- print 'group_id should be numeric'
- config.set('client', 'group_id', group_id)
- api_key = raw_input('api_key: ')
- config.set('client', 'api_key', api_key)
- print 'checking existing servers'
- servers = __query(group_id, 'servers', api_key)
- our_hostname = socket.gethostname()
- server_id = None
- matches = 0
- for server in servers:
- if server['hostname'] == our_hostname:
- server_id = server['id']
- matches += 1
- if matches == 0:
- print 'no hostname matches found'
- elif matches == 1:
- print '1 hostname match found (%s, server id %d)' % (our_hostname, server_id)
- if raw_input('take over this server id? (only do this if the old host is dead) [y/N] ') != 'y':
- server_id = None
- else:
- print 'found %d servers with the same hostname... hopefully this is ok...' % matches
- server_id = None
- if server_id is None:
- print 'getting a brand new server id'
- response = __query(group_id, 'register_server', api_key, {'hostname': our_hostname})
- server_id = response['server_id']
- config.set('client', 'server_id', server_id)
- print 'writing config to', cfg_path
- with open(cfg_path, 'w') as f:
- config.write(f)
- def __query(group_id, endpoint, api_key, data=None):
- api_server = config.get('client', 'api_server')
- url = '%s/v1/%s/%s' % (api_server, group_id, endpoint)
- if data is not None:
- data = json.dumps(data)
- req = urllib2.Request(url, data, {'Authorization': api_key})
- try:
- return json.load(urllib2.urlopen(req))
- except urllib2.HTTPError as e:
- sys.exit('http error (%d):\n%s' % (e.code, e.read()))
- except Exception as e:
- sys.exit('an error occurred while querying%s:\n%r' % (endpoint, e))
- if len(sys.argv) > 1 and sys.argv[1] == '--configure':
- configure()
- else:
- post()
|