Skip to content

Commit 8621e9e

Browse files
pawelcodilimePawel Ulita
authored andcommitted
Add logging when a server is added to haproxy
1 parent 8a743b3 commit 8621e9e

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

scripts/haproxy/haproxy.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313
###############################################################################
14-
14+
import csv
1515
import os
16+
import random
1617
import subprocess
1718
import tempfile
19+
from StringIO import StringIO
1820
from contextlib import contextmanager
1921

22+
import time
2023
from jinja2 import Template
24+
import requests
2125

2226
from cloudify_rest_client import exceptions as rest_exceptions
2327
from cloudify import ctx
@@ -58,18 +62,36 @@ def configure(subject=None):
5862

5963

6064
def add_backend(port, maxconn, backend_address=None):
65+
time.sleep(random.randint(0, 5))
66+
backends_before = len(_read_haproxy_stats())
67+
ctx.logger.info('backends before adding {} ({})'.format(backends_before, time.time()))
68+
6169
with _backends_update() as backends:
6270
backends[ctx.source.instance.id] = {
6371
'address': backend_address or ctx.source.instance.host_ip,
6472
'port': port,
6573
'maxconn': maxconn
6674
}
6775

76+
time.sleep(10)
77+
78+
backends_after = len(_read_haproxy_stats())
79+
ctx.logger.info('backends after adding {} ({})'.format(backends_after, time.time()))
80+
6881

6982
def remove_backend():
83+
time.sleep(random.randint(0, 5))
84+
backends_before = len(_read_haproxy_stats())
85+
ctx.logger.info('backends before removing {} ({})'.format(backends_before, time.time()))
86+
7087
with _backends_update() as backends:
7188
backends.pop(ctx.source.instance.id, None)
7289

90+
time.sleep(10)
91+
92+
backends_after = len(_read_haproxy_stats())
93+
ctx.logger.info('backends after removing {} ({})'.format(backends_after, time.time()))
94+
7395

7496
@contextmanager
7597
def _backends_update():
@@ -94,6 +116,21 @@ def _backends_update():
94116
raise
95117

96118

119+
def _read_haproxy_stats():
120+
csv_data = requests.get(
121+
'http://localhost:9000/haproxy_stats;csv',
122+
auth=('admin', 'password')).text
123+
buff = StringIO(csv_data)
124+
parsed_csv_data = list(csv.reader(buff))
125+
headers = parsed_csv_data[0]
126+
structured_csv_data = [dict(zip(headers, row))
127+
for row in parsed_csv_data]
128+
return dict([(struct['svname'], int(struct['stot']))
129+
for struct in structured_csv_data
130+
if struct['# pxname'] == 'servers' and
131+
struct['svname'] != 'BACKEND'])
132+
133+
97134
def start():
98135
_service('start')
99136

0 commit comments

Comments
 (0)