Permalink
Browse files

added content

  • Loading branch information...
1 parent f2492dc commit 7097296b978951ef6940af204570c360aa432406 @cormullion committed Feb 14, 2011
View
5 README
@@ -0,0 +1,5 @@
+Here are some bits of newLISP code I've written over the years.
+
+Happy hacking
+
+Cormullion 2011-02-14 14:43:20
View
@@ -0,0 +1,80 @@
+#!/usr/bin/env newlisp
+;; @module analyse-router-traffic
+;; @author cormullion@mac.com
+;; @version 0.1 2011-02-14 12:18:06
+;;
+;; This script lets you access a DD-WRT software-based
+;; router such as a Linksys using Telnet and get the traffic data from
+;; NVRAM, suitable for graphing, analysis, etc.
+;; I consider this a hack. :)
+
+(define (get-traffic-data site (user-name "admin") (password "password"))
+ ; eg (get-traffic-data "DD-WRT")
+ (local (str-buffer nvram-contents telnetSite socket result)
+ (set 'str-buffer {} 'nvram-contents {} 'telnetSite (string site))
+ (set 'socket (net-connect telnetSite 23))
+ (when socket
+ (net-receive socket str-buffer 512 "DD-WRT login:")
+ (net-send socket (string user-name "\r\n"))
+ (net-receive socket str-buffer 2000 "Password:")
+ (net-send socket (string password "\r\n"))
+ (net-receive socket str-buffer 2000 "root@DD-WRT")
+ (net-send socket (string {nvram show | grep "traff-"} "\r\n"))
+ (net-receive socket nvram-contents 3000 "root@DD-WRT:~# ")
+ (net-send socket (string {exit} "\n"))
+ (net-close socket)
+ ; we now should have some data
+ ; tidy up the output of telnet to leave just the traff-01 to traff-12 lines
+ (map (fn (l) (replace {(.*?)(traff-\d\d.*)} l (push $2 result -1) 0))
+ (parse nvram-contents "\n"))
+ result)))
+
+(define (add-data line)
+ ; line is a line from the traffdata file
+ ; it represents a month's worth of data from the DD-WRT traffdata
+ (local (l m-data month-number year-number day-number incoming outgoing month-table)
+ (set 'l (parse line "[= ]" 0))
+ (set 'm-data (pop l))
+ (set 'm-data (parse m-data "-"))
+ (set 'month-number (int (m-data 1)))
+ (set 'year-number (int (m-data 2)))
+ (set 'day-number 1)
+ (dolist (d (chop l)) ; drop last item, which are totals for month
+ ; d contains a pair of numbers eg "0:23"
+ (set 'incoming (first (parse d ":")))
+ (set 'outgoing (last (parse d ":")))
+ (push
+ (list (format {%d %02d %02d}
+ (list year-number month-number day-number))
+ (map int (list incoming outgoing)))
+ month-table)
+ (inc day-number))
+ month-table))
+
+(define (totalize the-data (day (date (date-value) 0 {%Y %m %d})) (number-of-days 30))
+ ; get the last n days of data
+ (println "Usage for the last " number-of-days " days, as at " day)
+ (set 'start-ref (ref day the-data))
+ (set 'data-slice (slice the-data (first start-ref) number-of-days))
+ ; (("2011 02 10" (456 1356)) ("2011 02 09" (852 1951)) ("2011 02 08" (703 2361)) ("2011 02 07"
+ ; (1251 2223)) ("2011 02 06" (776 776))
+ (list
+ (apply + (map (fn (f) (first (last f))) data-slice))
+ (apply + (map (fn (f) (last (last f))) data-slice))))
+
+; get lines of data from router
+(set 'raw-data (get-traffic-data "DD-WRT" "root" "13142"))
+
+; should check before continuing - have we any data?
+
+(set 'data-table '())
+(map (fn (a) (extend data-table (add-data a))) raw-data)
+
+; sort so that most recent entries are first
+
+(println (sort data-table >))
+
+; I want to know what the most recent 30 days worth of usage is, starting from today
+(println (totalize data-table (date (date-value) 0 {%Y %m %d}) 30))
+
+(exit)
View
@@ -0,0 +1,125 @@
+; File: plugins-active/blog.lsp
+
+
+; this is the main routing file for the blog
+; was used by unbalanced-parentheses.nfshost.com
+; incoming routing requests are matched against the database (uses nldb not sqlite) and
+; sets actions accordingly
+; I hope Greg or Marc don't see this code - I bet I'm doing it all wrong :)
+
+(new Route 'Route.Blog)
+
+(context Route.Blog)
+
+(define (matches?)
+ (set 'request (clean empty? (parse QUERY_STRING "?")))
+ ; load my nldb database of blog posts
+ (Dragonfly:load-database "blog.nldb")
+ (cond
+ ((empty? request)
+ ; nldb:select-rows table selection column-list sort-column sort-function
+ ; select all rows, all columns, most recent row first
+ (set 'query (nldb:select-rows 'nldb:stories true true 'nldb:story-date >))
+ (set 'action 'latest))
+
+ ((= "welcome" (first request))
+ (set 'query (nldb:select-rows 'nldb:stories true true 'nldb:story-date >))
+ (set 'action 'latest))
+
+ ((= "random" (first request))
+ (seed (date-value))
+ (set 'action 'random-post)
+ ; choose row at random
+ (set 'query (list (apply amb (nldb:select-rows 'nldb:stories true)))))
+
+ ((= "search" (first request))
+ (set 'search-request ($POST "inputstring"))
+ (set 'action 'search-action))
+
+ ((= "twitter-search" (first request))
+ (set 'twitter-search-request ($POST "inputstring"))
+ (set 'action 'twitter-search-action))
+
+ ((= "lambdalator" (first request))
+ (set 'action 'lambdalator-action))
+
+ ((= "aboutthissite" (first request))
+ (set 'action 'a-story)
+ ; find the 'about' post
+ (set 'query (nldb:select-rows 'nldb:stories '(= nldb:story-id {aboutthissite}))))
+
+ ((= "links" (first request))
+ (set 'action 'links))
+
+ ((set 'query (nldb:select-rows 'nldb:stories '(= nldb:story-id (first request))))
+ (set 'action 'a-story))
+
+ ((starts-with (first request) "downloads/")
+ (set 'f (first request))
+ (set 'action 'pass-through))
+
+ ; all other possibilities, pass on to next
+ (true nil)))
+
+(define (run)
+ (cond
+ ((= action 'links)
+ (DF:display-view "links"))
+
+ ((= action 'search-action)
+ (DF:display-view "search"))
+
+ ((= action 'lambdalator-action)
+ ;(DF:display-view "lambdalator")
+ (DF:include "../lambdalator/index.cgi")
+ )
+
+ ((= action 'pass-through)
+ (SET_DF_SELF f)
+ (DF:include f))
+
+ ((= action 'twitter-search-action)
+ (DF:display-view "twitter"))
+
+ ((= action 'latest)
+ ; choose the most recent 3 rows
+ (set 'stories (0 3 query))
+ (DF:display-view "recent"))
+
+ (true
+ (set 'story (first query))
+ (DF:display-view "welcome"))))
+
+(define (present-story story)
+; given a post in story, output it as HTML
+ (print {<h1>} (story 2) {</h1>})
+ (println {<small><a class="info" href="#">}
+ (story 0)
+ {<span>}
+ (date (parse-date (story 0) {%Y-%m-%dT%H:%M:%SZ}) 0 {%A %d %B, %X})
+ {</span></a></small>})
+ (print (render (last story))))
+
+(define (render t)
+ (replace {-{3}(.*?)-{3}}
+ t (if (catch
+ (eval-string $1) 'result)
+ (string result)
+ (string {---} t {---}))
+ 4))
+
+(define (pie-chart
+ (chs "400x200") ; size
+ (chd "t:60,40") ; date percentages
+ (cht "p3") ; type
+ (chl "Hello|World") ;
+ (chalt "Sample Google Chart") ;
+ )
+ (format {<img src="http://chart.apis.google.com/chart?chs=%s&amp;chd=%s&amp;cht=%s&amp;chl=%s" alt="%s" />} chs chd cht chl chalt))
+
+; to include pie charts in blog pages, use this:
+; <p>-- -(Route.Blog:pie-chart "600x200" "t:19.46,71.74,2.58,6.19" "p3" "comments 165384 |stories 609457|newLISP code 21981|Javascript 52618") -- -</p> all on one line ?!?!?!??!
+
+; add the route to the list of routes
+(push (Route.Blog) DF:dragonfly-routes)
+;
Oops, something went wrong.

0 comments on commit 7097296

Please sign in to comment.