A shiny app to explore nginx access logs and geolocate the connections
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.6KB

  1. library(tidyverse)
  2. library(RSQLite)
  3. library(ipapi)
  4. library(leaflet)
  5. dbConnect(SQLite(), "access.db") -> db
  6. read_delim("access.log", delim = " ", col_names = c("ip", "null1", "user", "timestamp", "zone", "req", "status", "size", "referer", "agent", "null2")) %>%
  7. mutate_all(na_if, "-") %>%
  8. filter(user %>% is.na) %>%
  9. filter(!ip %in% c("192.168.0.254", "164.2.255.244")) %>%
  10. mutate(timestamp = timestamp %>% as.POSIXct(format = "[%d/%b/%Y:%H:%M:%S")) %>%
  11. separate(req, into = c("method", "url", "version"), sep = " ") %>%
  12. select(ip, timestamp, url, status, referer, agent) %>%
  13. dbWriteTable(conn = db, name = "access", value = ., append = T)
  14. known_ips <- character(0)
  15. src_sqlite("access.db") %>%
  16. tbl("geoip") %>%
  17. select(query) %>%
  18. collect %>%
  19. pull(query) -> known_ips
  20. src_sqlite("access.db") %>%
  21. tbl("access") %>%
  22. select(ip) %>%
  23. collect %>%
  24. pull(ip) %>%
  25. unique %>%
  26. setdiff(known_ips) -> ips
  27. if (length(ips) > 0)
  28. {
  29. ips %>%
  30. geolocate -> geoip
  31. dbConnect(SQLite(), "access.db") %>%
  32. dbWriteTable("geoip", geoip, append = T)
  33. }
  34. src_sqlite("access.db") %>%
  35. tbl("access") %>%
  36. collect %>%
  37. left_join(geoip, by = c("ip" = "query")) %>%
  38. select(-null1, -user, -zone, -size, -null2, -as, -countryCode, -org, -region, -regionName, -status.y, -timezone, -zip, -isp) %>%
  39. mutate(timestamp = timestamp %>% str_sub(2) %>% as.POSIXct(format = "%d/%b/%Y:%H:%M:%S"),
  40. req = req %>% str_replace("(HEAD|GET|POST) ", ""),
  41. req = req %>% str_replace("/(.*?/).*", "\\1")) -> geoaccess
  42. leaflet(data = geoaccess) %>%
  43. addProviderTiles(providers$CartoDB.Positron) %>%
  44. addMarkers(~lon, ~lat, clusterOptions = markerClusterOptions())