Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed dummy data generator for testing and development

Pushed more collector work into the ServerState class
  • Loading branch information...
commit ab1abef541fca4b2cc46a14ee7f82e9ae8645ef4 1 parent 9f03ff1
@rjnienaber authored
View
8 src/collectors/stats.py
@@ -4,16 +4,16 @@
import utils
from .base import CollectorBase
+VARNISH_COUNTERS = ['client_conn', 'client_req', 'cache_hit', 'cache_hitpass',
+ 'cache_miss', 'client_drop', 'backend_conn']
class VarnishStats(CollectorBase):
def __init__(self, host, username, password, hostname, server_state, stats_window):
super(VarnishStats, self).__init__(host, username, password,
hostname, server_state)
- counters = ['client_conn', 'client_req', 'cache_hit', 'cache_hitpass',
- 'cache_miss', 'client_drop', 'backend_conn']
-
- self.varnish_command = 'varnishstat -x -f ' + ','.join(counters)
+ counters = ','.join(VARNISH_COUNTERS)
+ self.varnish_command = 'varnishstat -x -f ' + counters
self.counter_records = []
self.record_limit = stats_window
View
8 src/daemon.py
@@ -21,7 +21,7 @@ def main(args, static_path):
password=password)
stats = VarnishStats(host, user, password, hostname, server_state,
- args.stat_window)
+ args.stat_window)
monitor.add_worker(Worker('Stats', stats.process_data, stats.stop))
health = VarnishHealth(host, user, password, hostname, server_state)
@@ -30,10 +30,10 @@ def main(args, static_path):
print('Started gathering varnish data')
else:
- import testing
- worker = Worker('Testing', testing.update_files, testing.stop)
+ from tests.data_generator import DummyDataGenerator
+ dummydata = DummyDataGenerator(server_state)
+ worker = Worker('Testing', dummydata.update_files, dummydata.stop)
monitor.add_worker(worker)
- print('Started generating test data')
Worker('WorkerMonitor', monitor.start, monitor.stop).start()
http_server.start(server_state, static_path, args.port, args.wsgi_server)
View
15 src/data/server_state.py
@@ -1,5 +1,6 @@
import utils
from datetime import datetime
+import re
BACKEND_KEY = 'backends'
PROCESS_KEY = 'process'
@@ -9,11 +10,12 @@
class ServerState(object):
def __init__(self):
self.servers = {}
+ self.stats_matcher = re.compile('\d+')
def _get_server(self, hostname):
if not hostname in self.servers:
- default_process = {'virtualmem': '0m', 'reservedmem': '0m',
- 'cpu': '0.0', 'memory': '0.0'}
+ default_process = {'virtualmem_mb': 0, 'reservedmem_mb': 0,
+ 'cpu': 0.0, 'memory': 0.0}
self.servers[hostname] = {BACKEND_KEY: [],
PROCESS_KEY: default_process,
VARNISH_STAT_KEY: []}
@@ -51,10 +53,11 @@ def get_backends(self, hostname):
def update_process(self, hostname, virtualmem, reservedmem, cpu, memory):
process = self._get_server(hostname)[PROCESS_KEY]
- process['virtualmem'] = virtualmem
- process['reservedmem'] = reservedmem
- process['cpu'] = cpu
- process['memory'] = memory
+ parse_memory = lambda s: int(self.stats_matcher.findall(s)[0])
+ process['virtualmem_mb'] = parse_memory(virtualmem)
+ process['reservedmem_mb'] = parse_memory(reservedmem)
+ process['cpu'] = float(cpu)
+ process['memory'] = float(memory)
def get_process(self, hostname):
if not hostname in self.servers:
View
45 src/testing.py
@@ -1,45 +0,0 @@
-import utils
-import time
-import json
-import random
-from datetime import datetime
-from threading import Thread
-
-should_continue = True
-
-
-def update_files():
- try:
- health_data = json.loads(utils.read_json(utils.HEALTH_JSON_FILE))
- health_data['name'] = 'testing'
- stats_data = json.loads(utils.read_json(utils.STATS_JSON_FILE))
- print('Generating test data')
- while should_continue:
- #randomly make a server change status every 10 seconds
- if random.randint(1, 10) == 3:
- health_data['last_update'] = datetime.now()
- index = random.randint(0, 4)
- backend = health_data['backends'][index]
-
- state = 'healthy' if backend['state'] == 'sick' else 'sick'
- backend['state'] = state
- backend['last_change'] = datetime.now()
-
- utils.dump_data(health_data, utils.HEALTH_JSON_FILE)
-
- #update stats
- stats_data['process']['cpu'] = random.randint(0, 10)
- for counter in stats_data['varnish']:
- counter['value'] = random.random() * 100
-
- utils.dump_data(stats_data, utils.STATS_JSON_FILE)
-
- time.sleep(1)
- except KeyboardInterrupt:
- pass
-
- print('Ending test thread')
-
-
-def stop():
- should_continue = False
View
56 src/tests/data_generator.py
@@ -0,0 +1,56 @@
+import utils
+import time
+import json
+import random
+from datetime import datetime
+from threading import Thread
+from collectors import stats
+
+FAKE_BACKEND_NAMES = ['FAKE-BACKEND1', 'FAKE-BACKEND2', 'FAKE-BACKEND3']
+FAKE_SERVER_NAME = 'TEST-SERVER'
+
+class DummyDataGenerator:
+ def __init__(self, server_state):
+ self.server_state = server_state
+ self.should_continue = True
+
+ def update_files(self):
+ server_state = self.server_state
+ print('Generating test data')
+
+ for backend_name in FAKE_BACKEND_NAMES:
+ server_state.update_backend(FAKE_SERVER_NAME, backend_name, \
+ 'healthy')
+
+ while self.should_continue:
+ #randomly make a server change status every 10 seconds
+ if random.randint(1, 10) == 3:
+ backend_name = random.choice(FAKE_BACKEND_NAMES)
+ state = random.choice(['healthy', 'sick'])
+ server_state.update_backend(FAKE_SERVER_NAME, backend_name,
+ state)
+
+ #update stats
+ cpu = random.random() * 25
+ server_state.update_process(FAKE_SERVER_NAME,
+ '1400m', '234m', cpu, 4.5)
+
+ counter = 0
+ varnishstats = []
+ for varnish_counter in stats.VARNISH_COUNTERS:
+ value = random.random() * 400
+ counter += 1
+ description = 'Counter ' + str(counter)
+ varnishstats.append({'name': varnish_counter,
+ 'value': value,
+ 'description': description})
+
+ server_state.update_varnishstats(FAKE_SERVER_NAME, varnishstats)
+
+ time.sleep(1)
+
+ print('Ending test thread')
+
+
+ def stop(self):
+ self.should_continue = False
View
14 src/tests/tests_data/test_serverstate.py
@@ -65,9 +65,9 @@ def test_should_return_zero_values_for_empty_process(self):
state._get_server('varnish1')
process_state = state.get_process('varnish1')
-
- self.assertEquals({'virtualmem': '0m', 'reservedmem': '0m',
- 'cpu': '0.0', 'memory': '0.0'}, process_state)
+
+ self.assertEquals({'virtualmem_mb': 0, 'reservedmem_mb': 0,
+ 'cpu': 0.0, 'memory': 0.0}, process_state)
def test_should_update_process_state(self):
state = ServerState()
@@ -75,8 +75,8 @@ def test_should_update_process_state(self):
state.update_process('varnish1', '410m', '109m', '2.0', '2.8')
process_state = state.get_process('varnish1')
- self.assertEquals({'virtualmem': '410m', 'reservedmem': '109m',
- 'cpu': '2.0', 'memory': '2.8'}, process_state)
+ self.assertEquals({'virtualmem_mb': 410, 'reservedmem_mb': 109,
+ 'cpu': 2.0, 'memory': 2.8}, process_state)
def test_should_update_existing_process_state(self):
state = ServerState()
@@ -85,8 +85,8 @@ def test_should_update_existing_process_state(self):
state.update_process('varnish1', '510m', '209m', '3.0', '3.8')
process_state = state.get_process('varnish1')
- self.assertEquals({'virtualmem': '510m', 'reservedmem': '209m',
- 'cpu': '3.0', 'memory': '3.8'}, process_state)
+ self.assertEquals({'virtualmem_mb': 510, 'reservedmem_mb': 209,
+ 'cpu': 3.0, 'memory': 3.8}, process_state)
def test_should_return_empty_dict_for_unknown_hostname_for_stat(self):
state = ServerState()
View
12 src/web/static/js/simverest.js
@@ -116,17 +116,23 @@ function updateServerStatusRow(row, backendData){
}
}
+function isFloat(value) {
+ return (value + '').indexOf('.') != -1
+}
+
function updateProcess(processInfo){
$.each(processInfo, function(key, val) {
var table = $('#process');
var row = table.find('#'+key);
-
+
+ value = isFloat(val) ? val.toFixed(1) : val;
+
if (row.html() == null){
createProcessRow(table, key);
- updateProcessRow(table.find('#'+key), key, val);
+ updateProcessRow(table.find('#'+key), key, value);
}else{
- updateProcessRow(row, key, val);
+ updateProcessRow(row, key, value);
}
});
}
Please sign in to comment.
Something went wrong with that request. Please try again.