Welcome!

Blog Feed Post

20 Lines or Less # 54: Pools, Geo-IP, and Mobile

What could you do with your code in 20 Lines or Less? That's the question I ask (sometimes?) every week for the DevCentral community, and every week I go looking to find cool new examples that show just how flexible and powerful iRules can be without getting in over your head.

This week the forums provide us with more examples of iRules wizardry (or at least apprentice awesomeness) in a scant 20 lines or less each. The credit goes to the awesome community for providing such frequent and awesome examples. This week's installation of iRules goodness in particular is brought to you by hoolio, who despite the snow storm (perhaps because of it?) was iRuling away like the mad man he is. Showing off how to simplify geographical based redirection, how to smoothly access a particular pool given the right configuration, and how to do some fancy matching to search for strings of digits.

Mobile Redirects

http://bit.ly/xRJ6Ig

In this example user Ruchir is looking to do some matching based on some mobile device needs. They have a somewhat complex set of requirements to strip out a set of 4 or 6 digits from a URI that could take multiple forms. Making clever use of the URI::path depth command and some intelligence built into switch, Aaron shows that this can be near trivial if you know what knobs to turn.

 

   1: when HTTP_REQUEST {
   2:     # Get the index of the last URI directory
   3:     set depth [URI::path [HTTP::uri] depth]
   4:  
   5:     # Parse the last directory in the path
   6:     set last_dir [URI::path [HTTP::uri] $depth $depth]
   7:  
   8:     # Parse everything after the last hyphen in the last directory
   9:     set digits [string trimleft [string range $last_dir [expr {[string last - $last_dir]}] end-1] -/]
  10:  
  11:     log local0. "URI=[HTTP::uri], \$depth=$depth, \$last_dir=$last_dir, \$digits=$digits"
  12:  
  13:     # Check if we parsed 4 or 6 digits
  14:     switch $digits {
  15:         [0-9][0-9][0-9][0-9] -
  16:         [0-9][0-9][0-9][0-9][0-9][0-9] {
  17:             # Found 4 or 6 digits, send a redirect
  18:             HTTP::redirect "http://m.site.com/test/?gid=$digits"
  19:         }
  20:     }
  21: }

 

Geo-IP Redirection

http://bit.ly/yGw81z

Geolocation is not a new concept in our products, but it is new to many users out there, and it's fantastic to see people bringing it up in the forums. In this example, the desire was to separate out several countries into their own landing pages. Aaron came through and made a much simpler version using switch and some cleaned up matching logic that shows this can be pretty easy indeed. I removed some of the country cases for brevity, but the idea remains intact.

 

   1: when HTTP_REQUEST {
   2:     if { [string tolower [HTTP::host]] equals "www.example.com" && [HTTP::path] eq "/" }{
   3:         # Parse the client IP from the CDN header
   4:         set client_ip [HTTP::header value "Client-IP"]
   5:         if { $client_ip eq "" }{
   6:             # The header was empty/did not exist, so use the actual client IP
   7:             set client_ip [IP::client_addr]
   8:         }
   9:         set country [string tolower [whereis $client_ip abbrev]]
  10:         switch $country {
  11:             "af" -
  12:             "bh" -
  13:             "ye" { HTTP::redirect "http://www.example.com/home-${country}" } 
  14:             default {
  15:                 # Redirect all others
  16:                 HTTP::redirect "http://www.example.com/home"
  17:             }
  18:         }
  19:     } else {
  20:         pool example_web_pool
  21:     }
  22: }

 

Pool Based on Inbound Port

http://bit.ly/yxSIJa

Every so often we get a request from a user that wants to select a pool directly based off of something within the request. I.E. they want to add "/pool1" to the URI or they want to, as in this case, use the port number and append that to a pre-defined pool name and automatically direct traffic to a specific pool. In this case it is a way to specifically select a given node, as the user has one member per pool. That being said, we can do this, but not without one inherent issue in particular. If the pool doesn't exist, the connection will, understandably, fail. So what is a good way around this? The catch command! Aaron demonstrates how this works and gives a way for a backup plan in this snippet.

 

   1: when CLIENT_ACCPEPTED {
   2:  
   3:     # Try assigning the pool based on the client destination port
   4:     # If the pool assignment fails, use the VS default pool
   5:     if {[catch {pool pool_[TCP::local_port]} result]}{
   6:         # Pool did not exist, so log the value for testing
   7:         # The VS default pool will be used
   8:         log local0. "pool_[TCP::local_port] doe not exist"
   9:     } else {
  10:         # Pool assignment succeeded
  11:     }
  12: }

 

Check back again next week, or better yet subscribe to the feed, for more examples of iRules less than 21 lines that you can add to your bag of tricks.

#Colin

 

Read the original blog entry...

More Stories By Colin Walker

Coming from a *Nix Software Engineering background, Colin is no stranger to long hours of coding, testing and deployment. His personal experiences such as on-stage performance and the like have helped to foster the evangelist in him. These days he splits his time between coding, technical writing and evangalism. He can be found on the road to just about anywhere to preach the good word about ADCs, Application Aware networking, Network Side Scripting and geekery in general to anyone that will listen.

Colin currently helps manage and maintain DevCentral (http://devcentral.f5.com). He is also a contributor in many ways, from Articles to Videos to numerous forum posts, to iRules coding and whatever else he can get his hands on that might benefit the community and allow it to continue to grow.

Latest Stories
"Space Monkey by Vivent Smart Home is a product that is a distributed cloud-based edge storage network. Vivent Smart Home, our parent company, is a smart home provider that places a lot of hard drives across homes in North America," explained JT Olds, Director of Engineering, and Brandon Crowfeather, Product Manager, at Vivint Smart Home, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that Conference Guru has been named “Media Sponsor” of the 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. A valuable conference experience generates new contacts, sales leads, potential strategic partners and potential investors; helps gather competitive intelligence and even provides inspiration for new products and services. Conference Guru works with conference organizers to pass great deals to gre...
DevOps is under attack because developers don’t want to mess with infrastructure. They will happily own their code into production, but want to use platforms instead of raw automation. That’s changing the landscape that we understand as DevOps with both architecture concepts (CloudNative) and process redefinition (SRE). Rob Hirschfeld’s recent work in Kubernetes operations has led to the conclusion that containers and related platforms have changed the way we should be thinking about DevOps and...
The Internet of Things will challenge the status quo of how IT and development organizations operate. Or will it? Certainly the fog layer of IoT requires special insights about data ontology, security and transactional integrity. But the developmental challenges are the same: People, Process and Platform. In his session at @ThingsExpo, Craig Sproule, CEO of Metavine, demonstrated how to move beyond today's coding paradigm and shared the must-have mindsets for removing complexity from the develop...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
The next XaaS is CICDaaS. Why? Because CICD saves developers a huge amount of time. CD is an especially great option for projects that require multiple and frequent contributions to be integrated. But… securing CICD best practices is an emerging, essential, yet little understood practice for DevOps teams and their Cloud Service Providers. The only way to get CICD to work in a highly secure environment takes collaboration, patience and persistence. Building CICD in the cloud requires rigorous ar...
Companies are harnessing data in ways we once associated with science fiction. Analysts have access to a plethora of visualization and reporting tools, but considering the vast amount of data businesses collect and limitations of CPUs, end users are forced to design their structures and systems with limitations. Until now. As the cloud toolkit to analyze data has evolved, GPUs have stepped in to massively parallel SQL, visualization and machine learning.
"Evatronix provides design services to companies that need to integrate the IoT technology in their products but they don't necessarily have the expertise, knowledge and design team to do so," explained Adam Morawiec, VP of Business Development at Evatronix, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
"ZeroStack is a startup in Silicon Valley. We're solving a very interesting problem around bringing public cloud convenience with private cloud control for enterprises and mid-size companies," explained Kamesh Pemmaraju, VP of Product Management at ZeroStack, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Large industrial manufacturing organizations are adopting the agile principles of cloud software companies. The industrial manufacturing development process has not scaled over time. Now that design CAD teams are geographically distributed, centralizing their work is key. With large multi-gigabyte projects, outdated tools have stifled industrial team agility, time-to-market milestones, and impacted P&L stakeholders.
"Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Enterprises are adopting Kubernetes to accelerate the development and the delivery of cloud-native applications. However, sharing a Kubernetes cluster between members of the same team can be challenging. And, sharing clusters across multiple teams is even harder. Kubernetes offers several constructs to help implement segmentation and isolation. However, these primitives can be complex to understand and apply. As a result, it’s becoming common for enterprises to end up with several clusters. Thi...
"Infoblox does DNS, DHCP and IP address management for not only enterprise networks but cloud networks as well. Customers are looking for a single platform that can extend not only in their private enterprise environment but private cloud, public cloud, tracking all the IP space and everything that is going on in that environment," explained Steve Salo, Principal Systems Engineer at Infoblox, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Conventio...