| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #!/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')
- 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()
- datum = {
- 'cpu': cpu._asdict(),
- 'mem': {
- 'total': mem.total,
- 'used': mem.used,
- 'buffers': mem.buffers,
- 'cached': mem.cached,
- },
- 'net': net._asdict(),
- 'disk': {}
- }
- 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_server = config.get('client', 'api_server')
- group_id = config.get('client', 'group_id')
- server_id = config.get('client', 'server_id')
- url = '%s/v1/%s/datum/%s' % (api_server, group_id, server_id)
- urllib2.urlopen(url, json.dumps(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
- default_api_server = 'http://localhost:8892'
- 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)
- print 'checking existing servers'
- try:
- url = '%s/v1/%s/servers' % (default_api_server, group_id)
- r = urllib2.urlopen(url)
- servers = json.load(r)
- except Exception as e:
- sys.exit('an error occurred while fetching existing servers:\n%r' % e)
- our_hostname = socket.getfqdn()
- 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'
- try:
- url = '%s/v1/%s/register_server' % (default_api_server, group_id)
- r = urllib2.urlopen(url, json.dumps({'hostname': our_hostname}))
- server_id = json.load(r)['server_id']
- except Exception as e:
- sys.exit('an error occurred while getting the new server id:\n%r' % e)
- config.set('client', 'server_id', server_id)
- print 'writing config to', cfg_path
- with open(cfg_path, 'w') as f:
- config.write(f)
- if len(sys.argv) > 1 and sys.argv[1] == '--configure':
- configure()
- else:
- post()
|