# Apply timing control to this host # This is run as a cron job to either "close" or "open" the blocking of # the host via a configured control implementation. # File control.dat defines the limits, and control mechanism. # File activity-$date.dat is the local activity. (define (die) (write-line 2 (join (map string args))) (exit 1)) (constant 'NOW (date-value)) # Set current time variables in local timezone (map set '(YEAR MONTH DATE HOUR MINUTE SECOND DOY DOW) (date-list (+ NOW (* 60 (now 0 -2))))) ;(println (list YEAR MONTH DATE HOUR MINUTE SECOND DOY DOW)) # Load "control.dat" # ( (control "file") (gap minutes) ( weekday start limit stop ) ... ) (setf CONTROL (read-expr (read-file "control.dat"))) (map set '(dow MODE START LIMIT END) (or (assoc DOW CONTROL) (list DOW (6 30) 120 (20 0)))) (setf DAY (list YEAR MONTH DATE) HM (list HOUR MINUTE) TOTAL '() GAP (or (lookup 'gap CONTROL) 15) CLIP (or (lookup 'clip CONTROL) 1000) ) # Load control mechanism (if (lookup 'control CONTROL) (load $it) (die "** Unknown control mechanism. Exiting!!")) (unless control (die "** Unknown control action. Exiting!!")) (define (do-control x r) (control x r) (exit 0)) (when (file? "control-extra.dat") (let ((f (file-info "control-extra.dat" 6)) (x (regex "([0-9]+) ([0-9]+)" (read-file "control-extra.dat") 0))) (when (and f x (<= NOW (+ f (* 3600 (int $1 0 10)) (* 60 (int $2 0 10))))) (setf OVERRIDE true)))) # Activity is lines of timestamps. Collect TOTAL as list of unique # time values (H M) within the start-end time span. (define (log-name-fmt t) (format "%d%02d%02d-.*\\.dat" (0 3 (date-list t)))) (define (log-lines f) (find-all "([0-9]+( \\S+)?).*" (read-file (string "activity/" f)) $1 0)) # Collect all timestamps of the UTC date of the given time stamp (define (logs t) (flat (map log-lines (directory "activity" (log-name-fmt t))))) # Translate timestamp into its local time (hour minute), if it's # within the applicable open time, null otherwise. (define (period-minute x) (when x (letn ((d (date-list (+ (int x 0 10) (* 60 (now 0 -2))))) (tm (3 2 d)) (on (if (regex "^[0-9]+ ([0-9]+)$" x 0) (> (int $1 0 10) CLIP) 1)) ) (and on (= (0 3 d) DAY) (>= tm START) (< tm END) tm)))) # Collect all mentioned minutes from the activity logs (setf TOTAL (unique (clean null? (map period-minute (sort (extend (logs (- NOW 86400)) (logs NOW))))))) # Add all mentioned minutes, and fill in any time periods of less than # the configured GAP minutes between them. (define (minutes x) (+ (* (x 0) 60) (x 1))) (setf SUM 0) (when TOTAL (setf LAST (minutes (pop TOTAL) SUM 1)) (dolist (x TOTAL) (letn ((M (minutes x)) (V (- M LAST))) (inc SUM (if (< V GAP) V 1)) (setf LAST M))) ) # Rework SUM into (h m) format (setf SUM (letn ((h (/ SUM 60)) (m (- SUM (* 60 h)))) (list h m))) (write-file ".usage.dat" (string SUM)) (rename-file ".usage.dat" "usage.dat") # Close host outside start-end times (case MODE (closed (do-control "close" "closed")) (opened (do-control "open" "open")) (timed (when OVERRIDE (do-control "open" "override")) (when (< HM START) (do-control "close" "early")) (when (>= HM END) (do-control "close" "late")) (when (> SUM LIMIT) (do-control "close" "usage")) (do-control "open" "usage") ) (true (die "Unknown control mode " MODE)) )