|
- library(tidyverse)
- library(RSQLite)
- library(ipapi)
- library(leaflet)
-
- dbConnect(SQLite(), "access.db") -> db
-
- read_delim("access.log", delim = " ", col_names = c("ip", "null1", "user", "timestamp", "zone", "req", "status", "size", "referer", "agent", "null2")) %>%
- mutate_all(na_if, "-") %>%
- filter(user %>% is.na) %>%
- filter(!ip %in% c("192.168.0.254", "164.2.255.244")) %>%
- mutate(timestamp = timestamp %>% as.POSIXct(format = "[%d/%b/%Y:%H:%M:%S")) %>%
- separate(req, into = c("method", "url", "version"), sep = " ") %>%
- select(ip, timestamp, url, status, referer, agent) %>%
- dbWriteTable(conn = db, name = "access", value = ., append = T)
-
- known_ips <- character(0)
-
- src_sqlite("access.db") %>%
- tbl("geoip") %>%
- select(query) %>%
- collect %>%
- pull(query) -> known_ips
-
- src_sqlite("access.db") %>%
- tbl("access") %>%
- select(ip) %>%
- collect %>%
- pull(ip) %>%
- unique %>%
- setdiff(known_ips) -> ips
-
- if (length(ips) > 0)
- {
- ips %>%
- geolocate -> geoip
-
- dbConnect(SQLite(), "access.db") %>%
- dbWriteTable("geoip", geoip, append = T)
- }
-
- src_sqlite("access.db") %>%
- tbl("access") %>%
- collect %>%
- left_join(geoip, by = c("ip" = "query")) %>%
- select(-null1, -user, -zone, -size, -null2, -as, -countryCode, -org, -region, -regionName, -status.y, -timezone, -zip, -isp) %>%
- mutate(timestamp = timestamp %>% str_sub(2) %>% as.POSIXct(format = "%d/%b/%Y:%H:%M:%S"),
- req = req %>% str_replace("(HEAD|GET|POST) ", ""),
- req = req %>% str_replace("/(.*?/).*", "\\1")) -> geoaccess
-
- leaflet(data = geoaccess) %>%
- addProviderTiles(providers$CartoDB.Positron) %>%
- addMarkers(~lon, ~lat, clusterOptions = markerClusterOptions())
|