{"id":647,"date":"2016-05-16T12:24:36","date_gmt":"2016-05-16T11:24:36","guid":{"rendered":"http:\/\/www.haxed.me.uk\/?p=647"},"modified":"2016-05-16T12:49:50","modified_gmt":"2016-05-16T11:49:50","slug":"checking-load-balancer-connectivity-automating-interesting-ways","status":"publish","type":"post","link":"https:\/\/haxed.me.uk\/index.php\/2016\/05\/16\/checking-load-balancer-connectivity-automating-interesting-ways\/","title":{"rendered":"Checking Load Balancer Connectivity &#038; Automating it in some interesting ways"},"content":{"rendered":"<p>So, in a dream last night, I woke up realising I had forgot to write my automated load balancer connectivity checker.<\/p>\n<p>Basically, sometimes a customer will complain their site is down because their &#8216;load balancer is broken&#8217;! In many cases, this is actually due to a firewall on one of the nodes behind the load balancer, or an issue with the webserver application listening on the port. So, I wrote a little piece of automation in the form of a BASH script, that accepts an Load Balancer ID and then uses the API to pull the server nodes behind that Load Balancer, including the ports being used to communicate, and then uses, either netcat or nmap to check that port for connectivity. There were a few ways to achieve this, but the below is what I was happiest with.<\/p>\n<pre>\r\n#!\/bin\/bash\r\n\r\n# Username used to login to control panel\r\nUSERNAME='mycloudusernamegoeshere'\r\n\r\n# Find the APIKey in the 'account settings' part of the menu of the control panel\r\nAPIKEY=\"apikeygoeshere\"\r\n\r\n# Your Rackspace account number (the number that is in the URL of the control panel after logging in)\r\nACCOUNT=100101010\r\n\r\n# Your Rackspace loadbalancerID\r\nLOADBALANCERID=157089\r\n\r\n# Rackspace LoadBalancer Endpoint\r\nENDPOINT=\"https:\/\/lon.loadbalancers.api.rackspacecloud.com\/v1.0\"\r\n\r\n# This section simply retrieves and sets the TOKEN\r\nTOKEN=`curl https:\/\/identity.api.rackspacecloud.com\/v2.0\/tokens -X POST -d '{ \"auth\":{\"RAX-KSKEY:apiKeyCredentials\": { \"username\":\"'$USERNAME'\", \"apiKey\": \"'$APIKEY'\" }} }' -H \"Content-type: application\/json\" |  python -mjson.tool | grep -A5 token | grep id | cut -d '\"' -f4`\r\n\r\n#   (UNUSED) METHOD 1Extract IP addresses (Currently assuming port 80 only)\r\n#curl -H \"X-Auth-Token: $TOKEN\" -H \"Accept: application\/json\" -X GET \"$ENDPOINT\/$ACCOUNT\/loadbalancers\/$LOADBALANCERID\/nodes\" | jq .nodes[].address | xargs -i nmap -p 80 {}\r\n#   (UNUSED) Extract ports\r\n# curl -H \"X-Auth-Token: $TOKEN\" -H \"Accept: application\/json\" -X GET \"$ENDPOINT\/$ACCOUNT\/loadbalancers\/$LOADBALANCERID\/nodes\" | jq .nodes[].port | xargs -i nmap -p 80 {}\r\n\r\n\r\n# I opted for using this method to extract the important detail\r\ncurl -H \"X-Auth-Token: $TOKEN\" -H \"Accept: application\/json\" -X GET \"$ENDPOINT\/$ACCOUNT\/loadbalancers\/$LOADBALANCERID\/nodes\" | jq .nodes[].address | sed 's\/\"\/\/g' > address.txt\r\ncurl -H \"X-Auth-Token: $TOKEN\" -H \"Accept: application\/json\" -X GET \"$ENDPOINT\/$ACCOUNT\/loadbalancers\/$LOADBALANCERID\/nodes\" | jq .nodes[].port > port.txt\r\n\r\n# Loop thru both output files sequentially, order is important\r\n# WARNING script does not ignore whitespace\r\n\r\nwhile read addressfile1 <&#038;3 &#038;&#038; read portfile2 <&4; do\r\n   ncat $addressfile1 $portfile2\r\ndone 3<address.txt 4< port.txt\r\n<\/pre>\n<p>Output looks a bit like;<\/p>\n<p># .\/lbtest.sh<br \/>\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current<br \/>\n                                 Dload  Upload   Total   Spent    Left  Speed<br \/>\n100  5143  100  5028  100   115   4731    108  0:00:01  0:00:01 --:--:--  4734<br \/>\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current<br \/>\n                                 Dload  Upload   Total   Spent    Left  Speed<br \/>\n100   225  100   225    0     0    488      0 --:--:-- --:--:-- --:--:--   488<br \/>\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current<br \/>\n                                 Dload  Upload   Total   Spent    Left  Speed<br \/>\n100   225  100   225    0     0    679      0 --:--:-- --:--:-- --:--:--   681<br \/>\nNcat: No route to host.<br \/>\nNcat: Connection timed out.<\/p>\n<p>I plan to add some additional support that will check the load balancer is up, AND the servicenet connection between the cloud servers. <\/p>\n<p>Please note that this script must be run on a machine with access to servicenet network, in the same Rackspace Datacenter to be able to check servicenet connectivity of servers. The script can give false positives if strict firewall rules are setup on the cloud server nodes behind the load balancer. It's kind of alpha-draft but I thought I would share it as a proof of concept.<\/p>\n<p>You will need to download and install jq to use it. To download jq please see; https:\/\/stedolan.github.io\/jq\/download\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, in a dream last night, I woke up realising I had forgot to write my automated load balancer connectivity checker. Basically, sometimes a customer will complain their site is down because their &#8216;load balancer is broken&#8217;! In many cases, &hellip; <a href=\"https:\/\/haxed.me.uk\/index.php\/2016\/05\/16\/checking-load-balancer-connectivity-automating-interesting-ways\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,42,51,28,9,22,7,20],"tags":[],"class_list":["post-647","post","type-post","status-publish","format-standard","hentry","category-api","category-automation","category-firewall","category-interweb","category-linux","category-load-balancers","category-management-tools","category-webhosting"],"_links":{"self":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/comments?post=647"}],"version-history":[{"count":10,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/647\/revisions"}],"predecessor-version":[{"id":657,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/647\/revisions\/657"}],"wp:attachment":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/media?parent=647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/categories?post=647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/tags?post=647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}