Megatest

Check-in [4642aef9b9]
Login
Overview
Comment:sysmon min-project initial checkin.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | v2.01
Files: files | file ages | folders
SHA1: 4642aef9b9213033778deae0554e9fc0d53f4be3
User & Date: mmgraham on 2019-08-30 11:43:33
Other Links: branch diff | manifest | tags
Context
2019-08-30
11:43
sysmon min-project initial checkin. Leaf check-in: 4642aef9b9 user: mmgraham tags: v2.01
2019-07-31
08:54
Added plugins directory and readme. check-in: e96dd8ce30 user: mrwellan tags: v2.01
Changes

Added plugins/sysmon/sysmon.scm version [dd6d04f89f].























































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
;;; dbi: Minimal gasket to postgresql, sqlite3 and mysql
;;;
;; Copyright (C) 2007-2016 Matt Welland
;; Redistribution and use in source and binary forms, with or without
;; modification, is permitted.
;;
;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
;; OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
;; USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
;; DAMAGE.

(use regex srfi-18 matchable)

(use (prefix ulex ulex:))

(use hostinfo)
(use shell)

(create-directory "ulexdb" #t)
(create-directory "pkts"   #f)

(define *area* (ulex:make-area
		dbdir:   (conc (current-directory) "/ulexdb")
		pktsdir: (conc (current-directory) "/pkts") 
		))
(define (toplevel-command . args) #f)
(use readline)

;; two reserved keys in the ulex registration hash table are:
;;   dbinitsql => a list of sql statements to be executed at db creation time
;;   dbinitfn  => a function of two params; dbh, the sql-de-lite db handle and
;;                dbfname, the database filename
;;


          ; totalmem usedmem sharedmem buffers cached adjbuffers adjcache totalswap usedswap freeswap


(ulex:register-batch
 *area*
 'dbwrite
 `((dbinitsql . ("CREATE TABLE IF NOT EXISTS cpuload (id INTEGER PRIMARY KEY, timestamp INTEGER, hostname TEXT, proc INTEGER, core INTEGER, oneminload NUMERIC);" 
       " CREATE TABLE IF NOT EXISTS mem (id INTEGER PRIMARY KEY, timestamp INTEGER, hostname TEXT, totalmem INTEGER, usedmem INTEGER, sharedmem INTEGER, buffers INTEGER, cached INTEGER, adjbuffers INTEGER, adjcache INTEGER, totalswap INTEGER, usedswap INTEGER, freeswap INTEGER);"))
   (savecpuload . "INSERT INTO cpuload (timestamp,hostname,proc,core,oneminload) VALUES (?,?,?,?,?)")
   (savemem . "INSERT INTO mem (timestamp,hostname,totalmem,usedmem,sharedmem,buffers,cached,adjbuffers,adjcache,totalswap,usedswap,freeswap) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)")
   ))
		 
(ulex:register-batch
 *area*
 'dbread
 `((dbinitsql . ("CREATE TABLE IF NOT EXISTS cpuload (id INTEGER PRIMARY KEY, timestamp INTEGER, hostname TEXT, proc INTEGER, core INTEGER, oneminload NUMERIC);" 
       " CREATE TABLE IF NOT EXISTS mem (id INTEGER PRIMARY KEY, timestamp INTEGER, hostname TEXT, totalmem INTEGER, usedmem INTEGER, sharedmem INTEGER, buffers INTEGER, cached INTEGER, adjbuffers INTEGER, adjcache INTEGER, totalswap INTEGER, usedswap INTEGER, freeswap INTEGER);"))
   (getnumcpuload    . "SELECT COUNT(*) FROM cpuload")
   (getsomecpuload   . "SELECT * FROM cpuload LIMIT 10")
   ))
		 
(define (worker mode-in)
 (let* ((start (current-milliseconds))
	 (iters-per-sample 10)
	 (mode (string->symbol mode-in))
	 (max-count (case mode
		      ((all) 60)
		     (else  1000)))
	(num-calls 0)
	(report (lambda ()		  
		  (let ((delta (- (current-milliseconds) start)))
		    (print "Completed " num-calls " in " delta
			   " for " (/ num-calls (/ delta 1000)) " calls per second")))))
    (if (eq? mode 'repl)
	(begin
	  (import extras) ;; might not be needed
	  ;; (import csi)
	  (import readline)
	  (import apropos)
	  (import (prefix ulex ulex:))
	  (install-history-file (get-environment-variable "HOME") ".example_history") ;;  [homedir] [filename] [nlines])
	  (current-input-port (make-readline-port "example> "))
	  (repl))
	(let loop ((count 0))
	     (case mode
	       ((all)
		     (let* ((start-time (current-milliseconds))
          (cpu-load-list (ulex:get-normalized-cpu-load-raw))
          (num-proc (cdr(assoc 'proc cpu-load-list)))
          (num-core (cdr(assoc 'core cpu-load-list)))
          (one-min-load (cdr(assoc '1m-load cpu-load-list)))
          (hostname (current-hostname))
          (free-list (string-split (capture free)))
          (totalmem (list-ref free-list 7))
          (usedmem (list-ref free-list 8))
          (sharedmem (list-ref free-list 9))
          (buffers (list-ref free-list 10))
          (cached (list-ref free-list 11))
          (adjbuffers (list-ref free-list 15))
          (adjcache (list-ref free-list 16))
          (totalswap (list-ref free-list 18))
          (usedswap (list-ref free-list 19))
          (freeswap (list-ref free-list 20))
         )
		      (ulex:call *area* "cpu_load.db" 'savecpuload (list start-time hostname num-proc num-core one-min-load))
		      (ulex:call *area* "mem.db" 'savemem (list start-time hostname totalmem usedmem sharedmem buffers cached adjbuffers adjcache totalswap usedswap freeswap))
		      (set! num-calls (+ num-calls 1))
          (thread-sleep! 5)
		      )
         )
     )
	  (if (< count max-count)
	      (loop (+ count 1)))))
    (report)
    (ulex:clear-server-pkt *area*)
    (thread-sleep! 5) ;; let others keep using this server (needs to be built in to ulex)
    (print "Done doing stuff")))

(define (run-worker)
  (thread-start!
   (make-thread (lambda ()
		  (thread-sleep! 5)
		  (worker "all"))
		"worker")))

(define (main . args)
    (if (member (car args) '("repl"))
	(print "NOTE: No exit timer started.")
	(thread-start! (make-thread (lambda ()
				      (thread-sleep! (* 60 5))
				      (ulex:clear-server-pkt *area*)
				      (thread-sleep! 5)
				      (exit 0)))))
    (print "Launching server")
    (ulex:launch *area*)
    (print "LAUNCHED.")
    (thread-sleep! 0.1) ;; chicken threads bit quirky? need little time for launch thread to get traction?
    (apply worker args)
    )

;;======================================================================
;; Strive for clean exit handling
;;======================================================================

;; Ulex shutdown is handled within Ulex itself.

#;(define (server-exit-procedure)
  (on-exit (lambda ()
	     ;; close the databases, ensure the pkt is removed!
	     ;; (thread-sleep! 2)
	     (ulex:shutdown *area*)
	     0)))

;; Copied from the SDL2 examples.
;;
;; Schedule quit! to be automatically called when your program exits normally.
#;(on-exit server-exit-procedure)

;; Install a custom exception handler that will call quit! and then
;; call the original exception handler. This ensures that quit! will
;; be called even if an unhandled exception reaches the top level.
#;(current-exception-handler
 (let ((original-handler (current-exception-handler)))
   (lambda (exception)
     (server-exit-procedure)
     (original-handler exception))))

(if (file-exists? ".examplerc")
    (load ".examplerc"))

(let ((args-in (argv))) ;; command-line-arguments)))
  (let ((args (match
	       args-in
	       (("csi" "--" args ...) args)
	       ((_ args ...) args)
	       (else args-in))))
    (if (null? args)
	(begin
	  (print "Usage: example [mode]")
	  (print "  where mode is one of:")
	  (print "   all      : do cpu and mem stat writes")
	  (exit))
	(apply main args))))