Add admin_api plugin

Change-Id: Ibe18da992f7feb6fb0699a4ce29bd92fd54f507a
This commit is contained in:
Andrew Hutchings 2013-04-18 15:53:28 +01:00
parent 5f407569a7
commit 1206a22c5c
6 changed files with 67 additions and 6 deletions

View File

@ -15,6 +15,7 @@
import requests
import random
import sys
import json
class AdminAPI(object):
@ -52,12 +53,36 @@ class AdminAPI(object):
marker = marker + limit
return lb_list
def fail_device(self, device_id):
body = {
"status": "ERROR",
"statusDescription": "Load balancer failed ping test"
}
self._put(
'{url}/devices/{device_id}'.format(
url=self.url, device_id=device_id
), body
)
def _get_node_list(self, limit, marker):
return self._get(
'{url}/devices?marker={marker}&limit={limit}'
.format(url=self.url, marker=marker, limit=limit)
)
def _put(self, url, data):
try:
r = requests.put(url, data=json.dumps(data), verify=False)
except requests.exceptions.RequestException:
self.logger.exception('Exception communicating to server')
return False, None
if r.status_code != 200:
self.logger.error('Server returned error {code}'
.format(code=r.status_code))
return False, r.json()
return True, r.json()
def _get(self, url):
try:
r = requests.get(url, verify=False)

View File

@ -13,7 +13,8 @@
known_drivers = {
'dummy': 'libra.statsd.drivers.dummy.driver.DummyDriver',
'datadog': 'libra.statsd.driver.dummy.driver.DatadogDriver'
'datadog': 'libra.statsd.driver.dummy.driver.DatadogDriver',
'hp_rest': 'libra.statsd.driver.hp_rest.driver.HPRestDriver'
}
@ -21,5 +22,5 @@ class AlertDriver(object):
def __init__(self, logger):
self.logger = logger
def send_alert(self):
def send_alert(self, message, device_id):
raise NotImplementedError()

View File

@ -15,5 +15,5 @@ from libra.statsd.drivers.base import AlertDriver
class DummyDriver(AlertDriver):
def send_alert(self, message):
def send_alert(self, message, device_id):
self.logger.info('Dummy alert send of {0}'.format(message))

View File

@ -0,0 +1,13 @@
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

View File

@ -0,0 +1,22 @@
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
from libra.statsd.drivers.base import AlertDriver
from libra.statsd.admin_api import AdminAPI
class HPRestDriver(AlertDriver):
def send_alert(self, message, device_id):
api = AdminAPI(self.args.api_server, self.logger)
if api.is_online():
api.fail_device(device_id)

View File

@ -95,14 +95,14 @@ class Sched(object):
message = (
'Load balancer failed\n'
'ID: {0}\n'
'IP: {0}\n'
'tenant: {0}\n'.format(
'IP: (1}\n'
'tenant: {2}\n'.format(
data['id'], data['floatingIPAddr'],
data['loadBalancers'][0]['hpcs_tenantid']
)
)
for driver in self.drivers:
driver.send_alert(message)
driver.send_alert(message, data['id'])
def _get_node(self, node, node_list):
for n in node_list: