summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordu.net>2009-08-21 16:47:09 +0200
committerLinus Nordberg <linus@nordu.net>2009-08-21 16:47:09 +0200
commit373afd140a28eb706282fd4891e2e75b507b18aa (patch)
tree9b0cb9e70871936cbc94511df8401c3d460741d6
parentc64ce57afa8700c52937cfbfd2669a383804e01b (diff)
Restructure and add bgpview.
There's one package, BGP-LOGGER, defined in src/package.lisp. There are two systems, BGPSTORE and BGPVIEW, defined in bgpstore.asd and bgpview.asd respectively. The package exports START-BGPSTORE, START-BGPVIEW and their STOP- counterparts.
-rw-r--r--.gitignore1
-rw-r--r--bgpstore.asd20
-rw-r--r--bgpview.asd19
-rw-r--r--src/bgpstore.asd10
-rw-r--r--src/bgpstore.lisp31
-rw-r--r--src/bgpstore/bgpstore.lisp (renamed from src/data.lisp)79
-rw-r--r--src/bgpview/bgpview.lisp41
-rw-r--r--src/defs.lisp55
-rw-r--r--src/package.lisp12
-rw-r--r--src/playground.lisp58
-rw-r--r--src/util.lisp2
-rwxr-xr-xstart-bgpstore.sh (renamed from src/start-bgpstore.sh)0
12 files changed, 196 insertions, 132 deletions
diff --git a/.gitignore b/.gitignore
index df2d1bc..0087e54 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
*.fasl
doc
tar-files
+src/bgpview/pub
diff --git a/bgpstore.asd b/bgpstore.asd
new file mode 100644
index 0000000..27324a4
--- /dev/null
+++ b/bgpstore.asd
@@ -0,0 +1,20 @@
+;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
+(defpackage #:bgpstore-asd
+ (:use :cl :asdf))
+
+(in-package :bgpstore-asd)
+
+(defsystem bgpstore
+ :name "bgpstore"
+ :version "0.1"
+ :maintainer "Linus Nordberg"
+ :description "Storing BGP updates in SQL database."
+ ;; FIXME: Split up packge BGP-LOGGER in two to avoid depending on WEBLOCKS.
+ :depends-on (:cxml :usocket :postmodern :weblocks)
+ :components ((:module src
+ :components ((:file "package")
+ (:file "util" :depends-on ("package"))
+ (:file "defs" :depends-on ("util"))
+ (:module bgpstore
+ :components ((:file "bgpstore"))
+ :depends-on ("defs"))))))
diff --git a/bgpview.asd b/bgpview.asd
new file mode 100644
index 0000000..3bfdd33
--- /dev/null
+++ b/bgpview.asd
@@ -0,0 +1,19 @@
+;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
+(defpackage #:bgpview-asd
+ (:use :cl :asdf))
+
+(in-package :bgpview-asd)
+
+(defsystem bgpview
+ :name "bgpview"
+ :version "0.1"
+ :maintainer "Linus Nordberg"
+ :description "A viewer for stored BGP updates."
+ :depends-on (:postmodern :weblocks :cl-who)
+ :components ((:module src
+ :components ((:file "package")
+ (:file "util" :depends-on ("package"))
+ (:file "defs" :depends-on ("util"))
+ (:module bgpview
+ :components ((:file "bgpview"))
+ :depends-on ("defs"))))))
diff --git a/src/bgpstore.asd b/src/bgpstore.asd
deleted file mode 100644
index 7b239f3..0000000
--- a/src/bgpstore.asd
+++ /dev/null
@@ -1,10 +0,0 @@
-;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
-
-(asdf:defsystem #:bgpstore
- :name "bgpstore"
- :version "0.1"
- :depends-on (#:cxml #:usocket #:postmodern)
- :components ((:file "package")
- (:file "util" :depends-on ("package"))
- (:file "data" :depends-on ("util"))
- (:file "bgpstore" :depends-on ("data"))))
diff --git a/src/bgpstore.lisp b/src/bgpstore.lisp
deleted file mode 100644
index 16cd696..0000000
--- a/src/bgpstore.lisp
+++ /dev/null
@@ -1,31 +0,0 @@
-;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
-
-(defparameter *db-spec* '("bgpstore" "bgpstore" "bgpstore" "localhost" ))
-
-(defun marker (mark)
- (format t mark)
- (force-output))
-
-(defun start-bgpstore (host port)
- (with-connection *db-spec*
- (let ((reader (new-reader host port))
- (count 0))
- (marker (format nil "~A: bgpstore started "
- (iso-date (get-universal-time) t)))
- (do ((e (next-xml-blurb reader "BGP_MESSAGE")
- (next-xml-blurb reader "BGP_MESSAGE")))
- ((null e))
- (dolist (obj (new-entries (xml-top-elem-from-octets e)))
- (insert-dao obj)
- (incf count)
- (if (= 0 (mod count 10000))
- (marker (format nil "~%~A: ~A "
- (iso-date (get-universal-time) t)
- count))
- (if (= 0 (mod count 1000))
- (marker "*")
- (if (= 0 (mod count 100))
- (marker "."))))))
- (close-reader))))
-
-(defun stop-bgpstore ())
diff --git a/src/data.lisp b/src/bgpstore/bgpstore.lisp
index b612851..eb4fc2a 100644
--- a/src/data.lisp
+++ b/src/bgpstore/bgpstore.lisp
@@ -1,55 +1,7 @@
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
-;; http://common-lisp.net/project/postmodern/
-(defparameter *xmlns* "urn:ietf:params:xml:ns:xfb-0.1")
-(defparameter *version* 0.1)
+(in-package :bgp-logger)
-;; XML attributes, all required:
-;; (xmlns :col-type string :initform *xmlns*)
-;; (version :col-type string :initform *version*)
-;; (length :col-type string :initarg length)
-
-; (require 'postmodern)
-; (use-package 'postmodern)
-(defclass bgp-message ()
- ((id :col-type serial)
- (timestamp :col-type integer :accessor timestamp :initform 0)
- (precision-time :col-type (or db-null smallint) :accessor precision-time :initform 0)
- (prefix :col-type cidr :accessor prefix :initarg :prefix)
- (label :col-type string ;FIXME: smallint or enum
- :accessor label :initarg :label
- :documentation "1-NANN, 2-WITH, 3-DANN, 4-DUPW, 5-DPATH, 6-SPATH")
- (path :col-type (or db-null integer[]) :accessor path :initform "{}")
- (nexthop :col-type (or db-null inet) :accessor nexthop :initform "0.0.0.0")
- (bgp-octets :col-type string :accessor bgp-octets)) ; FIXME: binary to save space.
- (:metaclass dao-class)
- (:keys id))
-
-;; Database.
-;; Create table by evaluating
-;; (connect-toplevel "bgpstore" "bgpstore" "bgpstore" "localhost")
-;; (execute (dao-table-definition 'bgp-message))
-
-;; XML.
-;; node elements have dom:tag-name
-;; text elements have dom:data
-
-(defun prefix-pair (node)
- )
-
-(defun new-bgp-message (templ pref)
- (let ((msg (make-instance 'bgp-message
- :prefix (car pref)
- :label (cadr pref))))
- ;; FIXME: Use accessor functions.
- ;; FIXME2: Move this to a method of the class.
- (setf (slot-value msg 'timestamp) (slot-value templ 'timestamp)
- (slot-value msg 'precision-time) (slot-value templ 'precision-time)
- (slot-value msg 'path) (slot-value templ 'path)
- (slot-value msg 'nexthop) (slot-value templ 'nexthop)
- (slot-value msg 'bgp-octets) (slot-value templ 'bgp-octets))
- msg))
-
(defun xml-top-elem-from-octets (xml-octets)
(dom:document-element
(cxml:parse xml-octets (cxml-dom:make-dom-builder))))
@@ -145,3 +97,32 @@ TOP-ELEM is an XML document element."
(setf (slot-value e (intern cur-name)) txt)))))
(klacks:consume s))))
+(defun marker (mark)
+ (format t mark)
+ (force-output))
+
+(defun start-bgpstore (host port)
+ (with-connection *db-spec*
+ (let ((reader (new-reader host port))
+ (count 0))
+ (marker (format nil "~A: bgpstore started "
+ (iso-date (get-universal-time) t)))
+ (do ((e (next-xml-blurb reader "BGP_MESSAGE")
+ (next-xml-blurb reader "BGP_MESSAGE")))
+ ((null e))
+ (dolist (obj (new-entries (xml-top-elem-from-octets e)))
+ (insert-dao obj)
+ (incf count)
+ (if (= 0 (mod count 10000))
+ (marker (format nil "~%~A: ~A "
+ (iso-date (get-universal-time) t)
+ count))
+ (if (= 0 (mod count 1000))
+ (marker "*")
+ (if (= 0 (mod count 100))
+ (marker "."))))))
+ (close-reader))))
+
+(defun stop-bgpstore ())
+
+;;;;
diff --git a/src/bgpview/bgpview.lisp b/src/bgpview/bgpview.lisp
new file mode 100644
index 0000000..d6adce4
--- /dev/null
+++ b/src/bgpview/bgpview.lisp
@@ -0,0 +1,41 @@
+(in-package :bgp-logger)
+
+(defwebapp bgpview-app
+ :prefix "/bgpview"
+ :description "FIXME: description of bgpview app"
+ :autostart nil
+ :debug t)
+
+(defstore *bgpview-store* :prevalence
+ (merge-pathnames (make-pathname :directory '(:relative "data"))
+ (asdf-system-directory :bgpview)))
+
+(defun db-test (&rest args)
+ (declare (ignore args))
+ (with-connection *db-spec*
+ (let ((msgs (query-dao 'bgp-message
+ (:limit
+ (:select 'timestamp 'prefix 'label 'path 'nexthop
+ :from 'bgp-message)
+ 20))))
+ ;(dolist (m msgs) (make-instance 'dataform :data m)))))
+ (render-object-view msgs '(table bgp-message)))))
+
+(defun init-user-session (comp)
+ (setf (composite-widgets comp)
+ (list "Welcome to BGP View"
+ (make-instance 'composite
+ :widgets (list "First widget."
+ (lambda (&rest args)
+ (declare (ignore args))
+ (with-html
+ (:p "Second widget.")))
+ #'db-test)))))
+
+(defun start-bgpview (&rest args)
+ (apply #'start-weblocks args)
+ (start-webapp 'bgpview-app))
+
+(defun stop-bgpview ()
+ (stop-webapp 'bgpview-app)
+ (stop-weblocks))
diff --git a/src/defs.lisp b/src/defs.lisp
new file mode 100644
index 0000000..a02a7e9
--- /dev/null
+++ b/src/defs.lisp
@@ -0,0 +1,55 @@
+;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
+
+(in-package :bgp-logger)
+
+;; FIXME: Remove password.
+(defparameter *db-spec* '("bgpstore" "bgpstore" "bgpstore" "localhost" ))
+
+;; http://common-lisp.net/project/postmodern/
+(defparameter *xmlns* "urn:ietf:params:xml:ns:xfb-0.1")
+(defparameter *version* 0.1)
+
+;; XML attributes, all required:
+;; (xmlns :col-type string :initform *xmlns*)
+;; (version :col-type string :initform *version*)
+;; (length :col-type string :initarg length)
+
+; (require 'postmodern)
+; (use-package 'postmodern)
+(defclass bgp-message ()
+ ((id :col-type serial)
+ (timestamp :col-type integer :accessor timestamp :initform 0)
+ (precision-time :col-type (or db-null smallint) :accessor precision-time :initform 0)
+ (prefix :col-type cidr :accessor prefix :initarg :prefix)
+ (label :col-type string ;FIXME: smallint or enum
+ :accessor label :initarg :label
+ :documentation "1-NANN, 2-WITH, 3-DANN, 4-DUPW, 5-DPATH, 6-SPATH")
+ (path :col-type (or db-null integer[]) :accessor path :initform "{}")
+ (nexthop :col-type (or db-null inet) :accessor nexthop :initform "0.0.0.0")
+ (bgp-octets :col-type string :accessor bgp-octets)) ; FIXME: binary to save space.
+ (:metaclass dao-class)
+ (:keys id))
+
+;; Database.
+;; Create table by evaluating
+;; (connect-toplevel "bgpstore" "bgpstore" "bgpstore" "localhost")
+;; (execute (dao-table-definition 'bgp-message))
+
+;; XML.
+;; node elements have dom:tag-name
+;; text elements have dom:data
+
+;(defun prefix-pair (node))
+
+(defun new-bgp-message (templ pref)
+ (let ((msg (make-instance 'bgp-message
+ :prefix (car pref)
+ :label (cadr pref))))
+ ;; FIXME: Use accessor functions.
+ ;; FIXME2: Move this to a method of the class.
+ (setf (slot-value msg 'timestamp) (slot-value templ 'timestamp)
+ (slot-value msg 'precision-time) (slot-value templ 'precision-time)
+ (slot-value msg 'path) (slot-value templ 'path)
+ (slot-value msg 'nexthop) (slot-value templ 'nexthop)
+ (slot-value msg 'bgp-octets) (slot-value templ 'bgp-octets))
+ msg))
diff --git a/src/package.lisp b/src/package.lisp
index 522607c..13bdf74 100644
--- a/src/package.lisp
+++ b/src/package.lisp
@@ -1,5 +1,7 @@
-(defpackage #:bgpstore
- (:use #:cl #:asdf #:postmodern) ;can't use #:dom -- it exports LENGTH :(
- (:documentation "Store BGP updates in SQL database.")
- (:export :start-bgpstore
- :stop-bgpstore))
+(defpackage #:bgp-logger
+ (:use :cl :asdf :postmodern :weblocks) ;can't use #:dom -- it exports LENGTH :(
+ (:shadowing-import-from :postmodern #:commit-transaction)
+ (:shadowing-import-from :weblocks #:text)
+ (:documentation "Store BGP updates in SQL database and view them in web.")
+ (:export :start-bgpstore :stop-bgpstore
+ :start-bgpview :stop-bgpview))
diff --git a/src/playground.lisp b/src/playground.lisp
index 4936cd0..9db5db4 100644
--- a/src/playground.lisp
+++ b/src/playground.lisp
@@ -1,3 +1,4 @@
+;;;; Parsing XML.
(require 'cxml)
(require 'usocket)
(require 'cl-xmlspam)
@@ -64,7 +65,8 @@
(:end-element (format t "}~%"))
(:characters (format t (klacks:current-characters s))))
(klacks:consume s))))
-;;
+
+;; Read stream from socket.
(let ((sock (usocket:socket-connect
"victoria.tug.nordu.net"
50001
@@ -77,52 +79,22 @@
(format t "Closing socket.~%")
(usocket:socket-close sock))
-;;;;
-(defun read-stream-into-list (host port)
- (let ((reader (new-reader host port))
+(defun play ()
+ (let ((reader (new-reader "victoria.tug" 50001))
(xmls-builder (cxml-xmls:make-xmls-builder)))
(do ((e (next-xml-blurb reader "BGP_MESSAGE")
(next-xml-blurb reader "BGP_MESSAGE")))
((null e))
(print (cxml:parse e xmls-builder)))
(close-reader)))
-
-;;
-(let ((s nil))
- (defun file-reader (fn)
- (when s
- (close s))
- (setf s (open fn :element-type '(unsigned-byte 8)))
- (lambda (n)
- (read-byte s nil)))
- (defun close-file ()
- (close s)))
-;;
-(defun read-file-into-list (fn)
- (let ((reader (file-reader fn))
- (xmls-builder (cxml-xmls:make-xmls-builder)))
- (do ((e (next-xml-blurb reader "BGP_MESSAGE")
- (next-xml-blurb reader "BGP_MESSAGE")))
- ((null e))
- (print (cxml:parse e xmls-builder)))
- (close-file)))
;;
(next-xml-blurb (new-reader "victoria.tug.nordu.net" 50001) "BGP_MESSAGE")
-;;;; Pattern matching using fare-matcher.
-(require :fare-matcher)
-(let ((expr '(foo bar)))
- (letm
- (list a b) ;pattern
- expr
- (list a b)))
-(let ((expr '(foo kaka bar)))
- (match
- expr
- ((list a b) (list 'plain a b))
- ((list a 'kaka b) (list 'cookie a b))))
-
;;;; db
+;; To connect to database, wrap your db call(s) in WITH-CONNECTION,
+;; passing a spec on the form '(database user password host), see
+;; http://common-lisp.net/project/postmodern/postmodern.html.
+
;; SQL
"select * from bgp_message where timestamp != 0 order by id desc limit 3;"
@@ -138,4 +110,16 @@
(dolist (m (select-dao 'bgp-message (:= 'label "NANN")))
(format t "~A ~A ~A~%" (timestamp m) (prefix m) (label m)))
+(with-connection '("bgpstore" "bgpstore" "bgpstore" "localhost" )
+ (length (select-dao 'bgp-message (:= 'prefix "91.206.67.0/24"))))
+(length (query (:limit (:select 'prefix
+ :from 'bgp-message
+ :where (:= 'prefix "91.206.67.0/24"))
+ 10)))
+(query-dao 'bgp-message (:limit (:select 'prefix
+ :from 'bgp-message
+ :where (:= 'prefix "91.206.67.0/24"))
+ 13))
+
(length (select-dao 'bgp-message))
+
diff --git a/src/util.lisp b/src/util.lisp
index c6e446c..90816a5 100644
--- a/src/util.lisp
+++ b/src/util.lisp
@@ -1,5 +1,7 @@
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
+(in-package :bgp-logger)
+
(defparameter *debug* nil)
(defun next-xml-blurb (reader tag)
diff --git a/src/start-bgpstore.sh b/start-bgpstore.sh
index 599068e..599068e 100755
--- a/src/start-bgpstore.sh
+++ b/start-bgpstore.sh