Browse Source

Switched to rsqlite, and added process to clean db and fetch new IPs

geolocation
master
Maxime Wack 6 years ago
parent
commit
9f7104f195
1 changed files with 26 additions and 6 deletions
  1. +26
    -6
      geoip.R

+ 26
- 6
geoip.R View File

@@ -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)
}

Loading…
Cancel
Save