|
|
@@ -1,33 +1,91 @@
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
+import ConfigParser
|
|
|
import json
|
|
|
-import psutil
|
|
|
+import os
|
|
|
+from os import path
|
|
|
+import socket
|
|
|
+import sys
|
|
|
import urllib2
|
|
|
|
|
|
-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,
|
|
|
+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 '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': socket.getfqdn()}))
|
|
|
+ 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)
|
|
|
|
|
|
-API_SERVER = 'http://localhost:8892'
|
|
|
-GROUP_ID = 1
|
|
|
-SERVER_ID = 1
|
|
|
-url = '%s/v1/%d/datum/%d' % (API_SERVER, GROUP_ID, SERVER_ID)
|
|
|
-urllib2.urlopen(url, json.dumps(datum))
|
|
|
+if len(sys.argv) > 1 and sys.argv[1] == '--configure':
|
|
|
+ configure()
|
|
|
+else:
|
|
|
+ post()
|