def __init__(self): self.config = Config('giraffe.cfg') # configure RabbitMQ connector (and consumer) self.connector = Connector(username=self.config.get('rabbit', 'user'), password=self.config.get('rabbit', 'pass'), host=self.config.get('rabbit', 'host'), port=self.config.getint('rabbit', 'port')) self.queue = self.config.get('rabbit', 'queue') self.exchange = self.config.get('rabbit', 'exchange') self.routing_key = self.config.get('rabbit', 'routing_key') self.consumer = BasicConsumer(self.connector, self.queue, self.exchange, self._collector_callback) self.shared_secret = self.config.get('collector', 'shared_secret') # connect to giraffe database self.db = db.connect( '%s://%s:%s@%s/%s' % (self.config.get('db', 'vendor'), self.config.get('db', 'user'), self.config.get('db', 'pass'), self.config.get( 'db', 'host'), self.config.get('db', 'schema'))) # prepare connection to nova-client self._credentials = dict( username=self.config.get('agent', 'user'), password=self.config.get('agent', 'pass'), # tenant_id=self.config.get('agent', 'tenant_id'), tenant_name=self.config.get('agent', 'tenant_name'), auth_url=self.config.get('auth', 'admin_url'), insecure=True) # known servers/instances self.known_instances = {}
def setUpClass(cls): cls.config = Config('../../bin/giraffe.cfg') cls.gc = GiraffeClient(username=cls.config.get('client', 'user'), password=cls.config.get('client', 'pass'), tenant_name=cls.config.get( 'client', 'tenant_name'), tenant_id=cls.config.get('client', 'tenant_id'), auth_url=cls.config.get('auth', 'public_url'))
def setUp(self): if ClientTestCases.python_version < 270: self.config = Config('../../bin/giraffe.cfg') self.gc = GiraffeClient( username=self.config.get('client', 'user'), password=self.config.get('client', 'pass'), tenant_name=self.config.get('client', 'tenant_name'), tenant_id=self.config.get('client', 'tenant_id'), auth_url=self.config.get('auth', 'public_url'))
def __init__(self, auth_token=None, \ # username=None, \ # password=None, \ # tenant_name=None, \ # tenant_id=None, \ # protocol=None, \ # endpoint=None, \ # auth_url=None, \ **kwargs): """ Creates a new GiraffeClient instance: - if auth_token is not None, auth_token is used to authenticate client requests - alternatively, credentials (username, password, tenant_id, etc.) to be used to authenticate client requests can be passed to __init__() via named parameters """ # [DELETED] # - right now, also a fallback to giraffe.cfg is implemented (but not # to env. variables) self.config = Config('giraffe.cfg') if not auth_token: _username = kwargs.get('username') # , self.config.get('client', 'user')) _password = kwargs.get('password') # , self.config.get('client', 'pass')) _tenant_name = kwargs.get('tenant_name') # , self.config.get('client', 'tenant_name')) _tenant_id = kwargs.get('tenant_id') # , self.config.get('client', 'tenant_id')) _auth_url = kwargs.get('auth_url', \ self.config.get('auth', 'public_url')) auth_token = AuthProxy.get_token(username=_username, password=_password, tenant_name=_tenant_name, tenant_id=_tenant_id, auth_url=_auth_url) self.auth_header = dict([('X-Auth-Token', auth_token)]) #@[fbahr] - TODO: Exception handling self.protocol = kwargs.get('protocol', 'http') self.endpoint = kwargs.get('endpoint') if not self.endpoint: host = kwargs.get('host', self.config.get('rest_api', 'host')) port = kwargs.get('port', self.config.get('rest_api', 'port')) self.endpoint = ':'.join((host, port))
def __init__(self, agent): threading.Thread.__init__(self) self.agent = agent config = Config('giraffe.cfg') self.host_name = config.get('agent', 'host_name') self.flush_duration = config.getint('agent', 'duration') self.connector = Connector(username=config.get('rabbit', 'user'), password=config.get('rabbit', 'pass'), host=config.get('rabbit', 'host'), port=config.getint('rabbit', 'port')) self.queue = config.get('rabbit', 'queue') self.exchange = config.get('rabbit', 'exchange') self.routing_key = config.get('rabbit', 'routing_key') self.producer = BasicProducer(self.connector, self.exchange) self.shared_secret = config.get('agent', 'shared_secret') self.envelope = self._build_message() self.stopRequest = False self.lock = threading.Lock()
class Meta: """ Model that serves as a container for arguments and options common to all Giraffe CLI controllers """ label = 'abstract' # aliases = [...] # stacked_on = ... # stacked_type = ... # hide = [True|False] # usage = ... # description = ... # epilog = ... _config = Config('giraffe.cfg') # command line arguments # ! Warning: os.getenv over config.get, i.e., environment variables # override params defined in giraffe.cfg _credentials = [ (['--username', '-u'], \ dict(action='store', help='$OS_USERNAME', \ default=os.getenv('OS_USERNAME') or \ _config.get('client', 'user'))), (['--password', '-p'], \ dict(action='store', help='$OS_PASSWORD', \ default=os.getenv('OS_PASSWORD') or \ _config.get('client', 'pass'))), (['--tenant_id'], \ dict(action='store', help='$OS_TENANT_ID', \ default=os.getenv('OS_TENANT_ID') or \ _config.get('client', 'tenant_id'))), (['--tenant_name'], \ dict(action='store', help='$OS_TENANT_NAME', \ default=os.getenv('OS_TENANT_NAME') or \ _config.get('client', 'tenant_name'))) ] _services = [ (['--auth_url', '-a'], \ dict(action='store', help='$OS_AUTH_URL', \ default=os.getenv('OS_AUTH_URL') or \ _config.get('auth', 'public_url'))), (['--endpoint', '-e'], \ dict(action='store', help='Giraffe REST API endpoint (domain:port)', \ default=':'.join([_config.get('rest_api', 'host'), \ _config.get('rest_api', 'port')]))) ] _domains = [ (['--host'], \ dict(action='store', help='host name or ID', default=None)), (['--project', '--proj'], \ dict(action='store', help='project name or ID', default=None)), (['--user'], \ dict(action='store', help='user name or ID', \ default=None)), (['--instance', '--inst'], \ dict(action='store', help='instance name or ID', \ default=None)) ] _modifiers = [ (['--start'], \ dict(action='store', dest='start_time', help='', \ default=None)), (['--end'], \ dict(action='store', dest='end_time', help='', \ default=None)), (['--min'], \ dict(action='store_true', help='', \ default=None)), (['--max'], \ dict(action='store_true', help='', \ default=None)), (['--avg'], \ dict(action='store_true', help='', \ default=None)), (['--hourly'], \ dict(action='store_true', dest='hourly_avg', help='hourly ' 'averages of meter values within an period ' '[to be defined by --start and --end]', \ default=None)), (['--daily'], \ dict(action='store_true', dest='daily_avg', help='daily ' 'averages of meter values within an period ' '[to be defined by --start and --end]', \ default=None)), (['--sum'], \ dict(action='store_true', help='', \ default=None)), (['--count'], \ dict(action='store_true', help='', \ default=None)), (['--limit'], \ dict(action='store', help='max. number of objects to be ' 'retrieved', \ default=0)), (['--order'], \ dict(action='store', help='ORDER (ASC [default] or DESC)', \ default=None)), ] _aggregations = [ 'min', 'max', 'avg', 'hourly_avg', 'daily_avg', 'sum', 'count' ] _filters = ['start_time', 'end_time', 'limit', 'order'] _params = sum([_aggregations, _filters], []) _formats = [ # (['--jsn'], \ # dict(action='store_true', help='display output as plain JSON', \ # default=None)), (['--csv'], \ dict(action='store_true', help='output formatted as CSV', \ default=None)), (['--tab'], \ dict(action='store_true', help='output formatted as table', \ default=None)) ] arguments = sum([_modifiers, _formats, _credentials, _services], [])
def __init__(self, cfg=None): """ Initializes a new Agent. """ # fetch config parameters config = Config(cfg if cfg else 'giraffe.cfg') # configure agent self.timer = False self.tasks = [] self.publisher = publisher.AgentPublisher(self) # add publisher to tasks self.tasks.append(self.publisher) # HOST METERS --------------------------------------------------------- meters = config.items('agent') for (meter, value) in meters: try: if self.HOST_METER.get(meter): self.tasks.append(self.HOST_METER[meter](getattr( self, '_callback_' + meter), int(value))) except ValueError: logging.exception('Host meter %s ommitted due to ValueError: ' '\'%s\' is not of type \'int\'' % (meter, value)) # uptime = config.get('agent', 'host_uptime') # cpu_load = config.get('agent', 'host_cpu_load') # mem_usage = config.get('agent', 'host_memory_usage') # disk_io = config.get('agent', 'host_disk_io') # network_io = config.get('agent', 'host_network_io') # host meter `uptime` # if uptime: # self.tasks.append(Host_UPTIME( # self._callback_host_uptime, # int(uptime))) # host meter `CPU load` # if cpu_load: # self.tasks.append(Host_CPU_Load( # self._callback_host_cpu_load, # int(loadavg))) # host meter `memory` # if mem_usage: # self.tasks.append(Host_MEMORY_Usage( # self._callback_host_memory_usage, # int(mem_usage))) # host meter `network I/O` # if network_io: # self.tasks.append(Host_NETWORK_IO( # self._callback_host_network_io, # int(network_io))) # INSTANCE METERS ----------------------------------------------------- for (meter, value) in meters: try: if self.INST_METER.get(meter): self.tasks.append(self.INST_METER[meter](getattr( self, '_callback_' + meter), int(value))) except ValueError: logging.exception( 'Instance meter %s ommitted due to ValueError: ' '\'%s\' is not of type \'int\'' % (meter, value))
import subprocess from xml.etree import ElementTree as ETree import psutil # try: # from nova.virt import driver # except ImportError: import libvirt _LIBVIRT_SOCKET_URL = 'qemu:///system' from giraffe.common.task import PeriodicMeterTask from novaclient.v1_1.client import Client as NovaClient from giraffe.common.auth import AuthProxy from giraffe.common.config import Config _config = Config('giraffe.cfg') import logging logger = logging.getLogger("agent.instance_meter") # logger.setLevel(logging.DEBUG) # formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # fh = logging.FileHandler("agent.log") # fh.setFormatter(formatter) # logger.addHandler(fh) class PeriodicInstMeterTask(PeriodicMeterTask): def __init__(self, callback, period): super(PeriodicInstMeterTask, self).__init__(callback, period) self.conn = libvirt.openReadOnly(_LIBVIRT_SOCKET_URL)
def test_config(self): Config("giraffe.cfg")