Skip to content

Commit edd0f3c

Browse files
pawelcodilimePawel Ulita
authored and
Pawel Ulita
committed
Add logging when a server is added to haproxy
1 parent 8a743b3 commit edd0f3c

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

scripts/haproxy/haproxy.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313
###############################################################################
14-
14+
import csv
1515
import os
1616
import subprocess
1717
import tempfile
18+
from StringIO import StringIO
1819
from contextlib import contextmanager
1920

21+
import time
2022
from jinja2 import Template
23+
import requests
2124

2225
from cloudify_rest_client import exceptions as rest_exceptions
2326
from cloudify import ctx
@@ -58,18 +61,34 @@ def configure(subject=None):
5861

5962

6063
def add_backend(port, maxconn, backend_address=None):
64+
backends_before = len(_read_haproxy_stats())
65+
ctx.logger.info('backends before adding ' + str(backends_before))
66+
6167
with _backends_update() as backends:
6268
backends[ctx.source.instance.id] = {
6369
'address': backend_address or ctx.source.instance.host_ip,
6470
'port': port,
6571
'maxconn': maxconn
6672
}
6773

74+
time.sleep(10)
75+
76+
backends_after = len(_read_haproxy_stats())
77+
ctx.logger.info('backends after adding ' + str(backends_after))
78+
6879

6980
def remove_backend():
81+
backends_before = len(_read_haproxy_stats())
82+
ctx.logger.info('backends before removing ' + str(backends_before))
83+
7084
with _backends_update() as backends:
7185
backends.pop(ctx.source.instance.id, None)
7286

87+
time.sleep(10)
88+
89+
backends_after = len(_read_haproxy_stats())
90+
ctx.logger.info('backends after removing ' + str(backends_after))
91+
7392

7493
@contextmanager
7594
def _backends_update():
@@ -94,6 +113,21 @@ def _backends_update():
94113
raise
95114

96115

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

0 commit comments

Comments
 (0)