diff --git a/geoip.R b/geoip.R index 891970c..23685a5 100644 --- a/geoip.R +++ b/geoip.R @@ -1,12 +1,32 @@ library(tidyverse) +library(RSQLite) library(ipapi) -system("grep GraphTV /var/log/nginx/access.log | grep -vi bot | cut -d ' ' -f 1 | sort | uniq", intern = T) -> ips +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';") -read_delim("/var/log/nginx/access.log", delim = " ", col_names = c("ip", "null", "user", "timestamp", "zone", "req", "status", "size", "referer", "agent")) %>% - select(-null, -user, -zone, -size) %>% - mutate(timestamp = timestamp %>% str_sub(2) %>% as.POSIXct(format = "%d/%b/%Y:%H:%M:%S")) %>% - mutate_at(vars(status), factor) -> access +dbConnect(SQLite(), "access.db") %>% + dbGetQuery("vacuum;") -geolocate(ips) -> geoip +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) +}