Skip to content

Commit

Permalink
Add logic for positive reply to agent info.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tokunbo committed Feb 5, 2015
1 parent 0dc8f4b commit 26e2316
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 11 deletions.
108 changes: 108 additions & 0 deletions mimic/canned_responses/maas_agent_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""
Canned response for monitoring agent info
"""


def agent_info(entity_id, agent_id):
"""
Return example agent info from a windows machine
"""
return {
"values": [
{
"agent_id": agent_id,
"entity_id": entity_id,
"entity_uri": "https://ord.servers.api.mimic.co.jp/"+agent_id,
"host_info": {
"memory": {
"timestamp": 1423092674788,
"error": None,
"info": {
"used_percent": 36.033373792207,
"swap_free": 3406512128,
"free": 1311469568,
"swap_page_out": 3072,
"swap_used": 883855360,
"swap_total": 4290367488,
"actual_used": 772153344,
"swap_page_in": 59260928,
"total": 2142883840,
"free_percent": 63.966626207793,
"ram": 2048,
"actual_free": 1370730496,
"used": 831414272
}
},
"disks": {
"timestamp": 1423092675911,
"error": None,
"info": [
{
"name": "C:\\",
"wtime": 2718909792,
"rtime": 2718909792,
"read_bytes": 223028736,
"write_bytes": 561209344,
"time": 2718909792,
"writes": 135069,
"reads": 4425
}
]
},
"filesystems": {
"timestamp": 1423092675911,
"error": None,
"info": [
{
"free": 64707132,
"avail": 64707132,
"dev_name": "C:\\",
"total": 83884028,
"sys_type_name": "NTFS",
"options": "rw",
"dir_name": "C:\\",
"used": 19176896
}
]
},
"cpus": {
"timestamp": 1423092675911,
"error": None,
"info": [
{
"name": "cpu.0",
"vendor": "AMD",
"user": 36619627,
"model": "Opteron",
"total_sockets": 2,
"total_cores": 2,
"idle": 1714740481,
"total": 1801677388,
"sys": 50317280,
"mhz": 2094
},
{
"name": "cpu.1",
"vendor": "AMD",
"user": 20848223,
"model": "Opteron",
"total_sockets": 2,
"total_cores": 2,
"idle": 1749752815,
"total": 1801675750,
"sys": 31074712,
"mhz": 2094
}
]
}
}
}
],
"metadata": {
"count": 1,
"limit": 100,
"marker": None,
"next_marker": None,
"next_href": None
}
}
26 changes: 18 additions & 8 deletions mimic/rest/maas_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from mimic.rest.mimicapp import MimicApp
from mimic.imimic import IAPIMock
from mimic.canned_responses.maas_json_home import json_home
from mimic.canned_responses.maas_agent_info import agent_info
from mimic.canned_responses.maas_monitoring_zones import monitoring_zones
from mimic.canned_responses.maas_alarm_examples import alarm_examples
from mimic.util.helper import random_hex_generator
Expand Down Expand Up @@ -75,6 +76,7 @@ def __init__(self):
"""
Create the initial structs for cache
"""
self.agenthostinfo_querycount = collections.defaultdict(lambda: 0, {})
self.entities_list = []
self.checks_list = []
self.alarms_list = []
Expand Down Expand Up @@ -622,14 +624,22 @@ def view_agent_host_info(self, request, tenant_id):
"""
No agent monitoring. For now, always return 400.
"""
request.setResponseCode(400)
return json.dumps({
"type": "agentDoesNotExist",
"code": 400,
"message": "Agent does not exist",
"details": "Agent XYZ does not exist.",
"txnId": ".fake.mimic.transaction.id.c-1111111.ts-123444444.v-12344frf"
})
entity_id = request.args['entityId'][0].strip()
for e in self._entity_cache_for_tenant(tenant_id).entities_list:
if e['id'] == entity_id:
agent_id = e['agent_id']
self._entity_cache_for_tenant(tenant_id).agenthostinfo_querycount[entity_id] += 1
if self._entity_cache_for_tenant(tenant_id).agenthostinfo_querycount[entity_id] < 5:
request.setResponseCode(400)
return json.dumps({
"type": "agentDoesNotExist",
"code": 400,
"message": "Agent does not exist",
"details": "Agent XYZ does not exist.",
"txnId": ".fake.mimic.transaction.id.c-1111111.ts-123444444.v-12344frf"
})
else:
return json.dumps(agent_info(entity_id, agent_id))

@app.route('/v1.0/<string:tenant_id>/notifications', methods=['POST'])
def create_notification(self, request, tenant_id):
Expand Down
15 changes: 12 additions & 3 deletions mimic/test/test_maas.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,20 @@ def test_agenthostinfo(self):
"""
fetch agent host info
"""
req = request(self, self.root, "GET", self.uri + '/views/agent_host_info', '')
ecan = self.get_ecan_object_ids()
for q in range(4):
req = request(self, self.root, "GET",
self.uri + '/views/agent_host_info?entityId='+ecan['entity_id'], '')
resp = self.successResultOf(req)
self.assertEquals(resp.code, 400)
data = self.get_responsebody(resp)
self.assertEquals(True, 'Agent does not exist' in json.dumps(data))
req = request(self, self.root, "GET",
self.uri + '/views/agent_host_info?entityId='+ecan['entity_id'], '')
resp = self.successResultOf(req)
self.assertEquals(resp.code, 400)
self.assertEquals(resp.code, 200)
data = self.get_responsebody(resp)
self.assertEquals(True, 'Agent does not exist' in json.dumps(data))
self.assertEquals(True, ecan['entity_id'] == data['values'][0]['entity_id'])

def test_metriclist(self):
"""
Expand Down

0 comments on commit 26e2316

Please sign in to comment.