|
- library(tidyverse)
- library(RSQLite)
- library(ipapi)
- library(leaflet)
-
- # sqlite3 access.db
- # create table access(ip, null1, user, timestamp, zone, req, status, size, referer, agent, null2);
- # .separator " "
- # .import access.log access
-
- dbConnect(SQLite(), "access.db") %>%
- dbGetQuery("delete from access where ip = '192.168.0.254' or user = 'maxx' or agent like '%bot%' or ip = '164.2.255.244';")
-
- dbConnect(SQLite(), "access.db") %>%
- dbGetQuery("vacuum;")
-
- 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())
|