Skip to content

Instantly share code, notes, and snippets.

@nrktkt
Last active September 25, 2020 20:26
Show Gist options
  • Save nrktkt/77a67072c7477b76b1f21dd44f5621ed to your computer and use it in GitHub Desktop.
Save nrktkt/77a67072c7477b76b1f21dd44f5621ed to your computer and use it in GitHub Desktop.
Create a map of ELB traffic in GPX format.

Requires ammonite

Written to use the ipstack.com API, but can be easily modifed to work with any geolocation api by editing the geolocate function.

Usage

cat your-elb-log-file.log | ./elb-geo.sc --ipstackKey $YOUR_API_KEY > elb-map.gpx

The output file can be opened with any GPS software, like https://www.gpsvisualizer.com

#!/usr/bin/env amm
import $ivy.`com.github.alexandrnikitin::bloom-filter:0.13.1`
import bloomfilter.mutable.BloomFilter
@main
def main(ipstackKey: String, expectedElements: Int = 10000, falsePositiveRate: Double = .1) = {
def geolocate(ip: String) = {
val json = ujson.read(
requests.get(s"http://api.ipstack.com/$ip?access_key=$ipstackKey").text()
).obj
ip -> (json.get("latitude").get, json.get("longitude").get)
}
val bf = BloomFilter[String](expectedElements, falsePositiveRate)
Iterator("""<gpx version="1.1">""").++(
scala.io.Source.stdin.getLines()
.map(_.split(' ')(2))
.map(_.split(':')(0))
.filterNot { ip =>
if(bf.mightContain(ip)) true
else {
bf.add(ip)
false
}
}
.map(geolocate)
.map { case (ip, (lat, lon)) => s"""<wpt lat="$lat" lon="$lon"><desc>$ip</desc></wpt>""" } )
.++(Iterator("</gpx>"))
.foreach(println)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment