added usbreset.lsp
[rrq/newlisp-ftw.git] / nginx-access.lsp
1 #!/usr/bin/newlisp
2
3 ;; This script performs per-IP access analysis of the nginx access
4 ;; log. This is translated into a display format of:
5 ;; total-count hit-count IP path
6
7 (signal 2 (fn (x) (exit 0)))
8
9 (setf DISPLAY-TOTALS (and (find "--tot" (main-args)) true))
10
11 ; "Object" holding data for an IP
12 ; (self 1) = IP
13 ; (self 2) = total-count
14 ; (self 3) = a-list of (path hit-count)
15 (context 'ROW)
16 (define (ROW:ROW IP) (list (context) IP 0 (list)))
17
18 (define (add-path PATH)
19   (unless (assoc PATH (self 3)) (push (list PATH 0) (self 3)))
20   (inc (lookup PATH (self 3)))
21   (inc (self 2)))
22
23 (define (display)
24   (if MAIN:DISPLAY-TOTALS (list (list (self 2) (length (self 3)) (self 1)))
25     (sort (map (fn (P) (list (self 2) (P 1) (self 1) (P 0))) (self 3)))))
26
27 (context MAIN)
28
29 ; Table of rows, keyed by IP
30 (define IPS:IPS nil)
31
32 (while (setf LINE (read-line))
33   (when (regex "^([^ ]+)[^\"]*[^ ]+ ([^ ]+)" LINE 0)
34     (let ((IP $1) (PATH $2))
35       (unless (IPS IP) (IPS IP (ROW IP)))
36       (:add-path (IPS IP) PATH))))
37
38 (map println
39      (sort (flat (map (fn (R) (:display R)) (map last (IPS))) 1) >))
40 (exit 0)
41
42
43