Megatest

Check-in [c36bcea34e]
Login
Overview
Comment:wip
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | v1.80-servload
Files: files | file ages | folders
SHA1: c36bcea34e0a9e91ad1fce256f8b7bcc7c7a3b8c
User & Date: matt on 2023-04-26 02:08:57
Other Links: branch diff | manifest | tags
Context
2023-04-26
02:18
Added back old nmsg-transport.scm check-in: 0b5556b83e user: matt tags: v1.80-servload
02:08
wip check-in: c36bcea34e user: matt tags: v1.80-servload
2023-04-24
15:21
Merged fork check-in: 53dba4ce04 user: mrwellan tags: v1.80-servload
Changes

Added nn-transport.scm version [96f84cfada].























































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
;;start a server, returns the connection
;;
(define (start-nn-server portnum )
  (let ((rep (nn-socket 'rep)))
    (handle-exceptions
     exn
     (let ((emsg ((condition-property-accessor 'exn 'message) exn)))
       (print "ERROR: Failed to start server \"" emsg "\"")
       (exit 1))
      
     (nn-bind rep (conc "tcp://*:" portnum)))
    rep))
;; open connection to server, send message, close connection
;;
(define (open-send-close-nn host-port msg attrib #!key (timeout 3) ) ;; default timeout is 3 seconds
  (let ((req  (nn-socket 'req))
        (uri  (conc "tcp://" host-port))
        (res  #f)
        (contacts (alist-ref 'contact attrib))
        (mode (alist-ref 'mode attrib))) 
    (handle-exceptions
     exn
     (let ((emsg ((condition-property-accessor 'exn 'message) exn)))
       ;; Send notification       
       (print "ERROR: Failed to connect / send to " uri " message was \"" emsg "\"" )
         (if (equal? mode "production")
             (begin 
             (print " Sending email to contacts : " contacts )
             (let ((email-body (mtut:stml->string (s:body
										(s:p (conc "We could not send messages to the server on " uri "."  "Please check if the listner is running. It is possible that the host is overloaded due to which it may take too long to respond. \n Contact your system adminstrator if server load is high." (s:br)" Thank You ") )))))
             (sendmail (string-join (string-split contacts ";" )) (conc "[Listner Error] Filed to connect to listner on " uri) email-body  use_html: #t)))
             (print " mode : " mode " Not sending any emails" ))
       #f)
     (nn-connect req uri)
     (print "Connected to the server " )
     (nn-send req msg)
     (print "Request Sent")  
     (let* ((th1  (make-thread (lambda ()
                                 (let ((resp (nn-recv req)))
                                   (nn-close req)
                                   (set! res (if (equal? resp "ok")
                                                 #t
                                                 #f))))
                               "recv thread"))
            (th2 (make-thread (lambda ()
                                (thread-sleep! timeout)
                                (thread-terminate! th1))
                             "timer thread")))
       (thread-start! th1)
       (thread-start! th2)
       (thread-join! th1)
       res))))

(define (open-send-receive-nn host-port msg attrib #!key (timeout 3) ) ;; default timeout is 3 seconds
  (let ((req  (nn-socket 'req))
        (uri  (conc "tcp://" host-port))
        (res  #f)
        (contacts (alist-ref 'contact attrib))
        (mode (alist-ref 'mode attrib))) 
    (handle-exceptions
     exn
     (let ((emsg ((condition-property-accessor 'exn 'message) exn)))
       ;; Send notification      
       (print "ERROR: Failed to connect / send to " uri " message was \"" emsg "\"" )
         (if (equal? mode "production")
             (begin 
             (print " Sending email to contacts : " contacts )
             (let ((email-body (mtut:stml->string (s:body
										(s:p (conc "We could not send messages to the server on " uri "."  "Please check if the listner is running. It is possible that the host is overloaded due to which it may take too long to respond. \n Contact your system adminstrator if server load is high." (s:br)" Thank You ") )))))
             (sendmail (string-join (string-split contacts ";" )) (conc "[Listner Error] Filed to connect to listner on " uri) email-body  use_html: #t)))
             (print " mode : " mode " Not sending any emails" ))
       #f)
     (nn-connect req uri)
     (print "Connected to the server " )
     (nn-send req msg)
     (print "Request Sent")  
     ;; receive code here
     ;;(print (nn-recv req))
     (let* ((th1  (make-thread (lambda ()
                                 (let ((resp (nn-recv req)))
                                   (nn-close req)
                                   (print resp)
                                   (set! res (if (equal? resp "ok")
                                                 #t
                                                 #f))))
                               "recv thread"))
            (th2 (make-thread (lambda ()
                                (thread-sleep! timeout)
                                (thread-terminate! th1))
                             "timer thread")))
       (thread-start! th1)
       (thread-start! th2)
       (thread-join! th1)
       res))))


    ((tsend)
       (if (null? remargs)
	      (print "ERROR: missing data to send to trigger listeners")
	      (let* ((msg       (car remargs))
                  (mtconfdat (simple-setup (args:get-arg "-start-dir")))
                  (mtconf    (car mtconfdat))
                  (time-out  (if (args:get-arg "-time-out")
                                 (string->number (args:get-arg "-time-out")) 
                               5))
                  (listeners (configf:get-section mtconf "listeners"))
                  (user-info  (user-information (current-user-id)))
                  (prev-seen (make-hash-table))) ;; catch duplicates
             (if user-info
              (begin
               (for-each
              (lambda (listener)
                (let ((host-port (car listener))
                      (attrib (val->alist (cadr listener))))
                  (if (and (equal? msg "time-to-die") (not (can-user-kill-listner user-info attrib)))
                   (begin
                      (debug:print-error 0 *default-log-port* "User " (car user-info) " is not allowed to send message '" msg"'")
                      (exit 1)))
                  (print "sending " msg " to " host-port )
                  (open-send-close-nn host-port msg attrib timeout: time-out )))
              listeners))
              (begin
               (debug:print-error 0 *default-log-port* "Could not Identify executing user. Will not send any message")
               (exit 1))))))
     ((tquery)
       (if (null? remargs)
	      (print "ERROR: missing data to send to trigger listeners")
	      (let* ((msg       (car remargs))
                  (mtconfdat (simple-setup (args:get-arg "-start-dir")))
                  (mtconf    (car mtconfdat))
                  (time-out  (if (args:get-arg "-time-out")
                                 (string->number (args:get-arg "-time-out")) 
                               5))
                  (listeners (configf:get-section mtconf "listeners"))
                  (user-info  (user-information (current-user-id)))
                  (prev-seen (make-hash-table))) ;; catch duplicates
             (if user-info
              (begin
               (for-each
              (lambda (listener)
                (let ((host-port (car listener))
                      (attrib (val->alist (cadr listener))))
                  (if (and (equal? msg "time-to-die") (not (can-user-kill-listner user-info attrib)))
                   (begin
                      (debug:print-error 0 *default-log-port* "User " (car user-info) " is not allowed to send message '" msg"'")
                      (exit 1)))
                  (print "sending " msg " to " host-port )
                  (open-send-receive-nn host-port msg attrib timeout: time-out )))
              listeners))
              (begin
               (debug:print-error 0 *default-log-port* "Could not Identify executing user. Will not send any message")
               (exit 1))))))

     ((tquerylisten)
      (if (null? remargs)
          (print "ERROR: useage for tlisten is \"mtutil tlisten portnum\"")
          (let ((portnum (string->number (car remargs))))
            
            (if (not portnum)
                (print "ERROR: the portnumber parameter must be a number, you gave: " (car remargs))
                (begin
                  (if (not (is-port-in-use portnum))  
                      (let* ((rep       (start-nn-server portnum))
                             (mtconfdat (simple-setup (args:get-arg "-start-dir")))
                             (mtconf    (car mtconfdat))
                             (contact   (configf:lookup mtconf "listener" "owner"))
                             (script    (configf:lookup mtconf "listener" "script")))
                        (print "Listening on port " portnum " for messages.")
                        (set-signal-handler! signal/int  (lambda (signum) 
							   (set! *time-to-exit* #t)
  							   (debug:print-error 0 *default-log-port* "Received signal " signum " sending email befor exiting !!")
  							   (let ((email-body (mtut:stml->string (s:body
												 (s:p (conc "Received signal " signum ". Lister has been terminated on host " (get-environment-variable "HOST") ". "))))))
             					             (sendmail contact "Listner has been terminated." email-body  use_html: #t))
							   (exit)))
			(set-signal-handler! signal/term  (lambda (signum) 
							    (set! *time-to-exit* #t)
  							    (debug:print-error 0 *default-log-port* "Received signal " signum " sending email befor exiting !!")
  							    (let ((email-body (mtut:stml->string (s:body
												  (s:p (conc "Received signal " signum ". Lister has been terminated on host " (get-environment-variable "HOST") ". "))))))
             					              (sendmail contact "Listner has been terminated." email-body  use_html: #t))
							    (exit)))

					;(set-signal-handler! signal/term special-signal-handler)
                        
                        (let loop ((instr (nn-recv rep)))
                          ;;(nn-send rep "3.9")
                          (with-input-from-pipe (conc "/usr/bin/uptime | cut -d':' -f4 | awk '{print $1}' | cut -d',' -f1")
                             			(lambda()
                             			  (let loop ((inl (read-line)))
                          			    (if (not (eof-object? inl))
                              				(begin
                                			  ;;(print "fdk73: " inl ":")
                                			  ;;(set! current-list-ciaf (append! current-list-ciaf (list (string-substitute "\\s+$" "" inl))))
                                			  (nn-send rep inl)
                                			  (loop(read-line)))
                          				))

                             			  )
						)
                          ;;(print (isys "/usr/bin/uptime" foreach-stdout-thunk: foreach-stdout))
                          (let ((ctime (date->string (current-date)))) 
                            (if  (equal? instr "time-to-die")
				 (begin 
				   (debug:print 0 *default-log-port* ctime " received '" instr "'. Time to sucide." )
				   (let ((pid  (current-process-id)))
				     (debug:print 0 *default-log-port* "Killing current process (pid=" pid ")")
				     (system (conc "kill " pid))))  
				 (begin
				   (debug:print 0 *default-log-port* ctime " received " instr )
					;(nn-send rep "ok")
                                   (if (not (equal? instr "ping"))
                                       (begin
					 (debug:print 0 *default-log-port* ctime " running \"" script " " instr "\"")
					;(system (conc script " '" instr "'"))
					 (process-run script (list  instr ))  
					 (debug:print 0 *default-log-port* ctime " done" ))
                                       (begin
                                   	 (if (not (equal? instr "load"))
                                   	     (print "Checking load")

                                   	     ) 
					 )

                                       )

                                   )))
                          (loop (nn-recv rep))))
		      (print "ERROR: Port " portnum " already in use. Try another port")))))))

     ((tlisten)
      (if (null? remargs)
          (print "ERROR: useage for tlisten is \"mtutil tlisten portnum\"")
          (let ((portnum (string->number (car remargs))))
            
            (if (not portnum)
		(print "ERROR: the portnumber parameter must be a number, you gave: " (car remargs))
		(begin
                  (if (not (is-port-in-use portnum))  
                      (let* ((rep       (start-nn-server portnum))
                             (mtconfdat (simple-setup (args:get-arg "-start-dir")))
                             (mtconf    (car mtconfdat))
                             (contact   (configf:lookup mtconf "listener" "owner"))
                             (script    (configf:lookup mtconf "listener" "script")))
			(print "Listening on port " portnum " for messages.")
			(set-signal-handler! signal/int
					     (lambda (signum) 
					       (set! *time-to-exit* #t)
  					       (debug:print-error 0 *default-log-port* "Received signal " signum
								  " sending email befor exiting !!")
  					       (let ((email-body (mtut:stml->string
								  (s:body
								   (s:p (conc "Received signal " signum
									      ". Lister has been terminated on host "
									      (get-environment-variable "HOST") ". "))))))
             					 (sendmail contact "Listner has been terminated." email-body  use_html: #t))
					       (exit)))
			(set-signal-handler! signal/term  (lambda (signum) 
							    (set! *time-to-exit* #t)
  							    (debug:print-error 0 *default-log-port* "Received signal "
									       signum " sending email befor exiting !!")
  							    (let ((email-body (mtut:stml->string
									       (s:body
										(s:p (conc "Received signal " signum
											   ". Lister has been terminated on host "
											   (get-environment-variable "HOST") ". "))))))
             					              (sendmail contact "Listner has been terminated." email-body  use_html: #t))
							    (exit)))

			;; (set-signal-handler! signal/term special-signal-handler)
			
                        (let loop ((instr (nn-recv rep)))
                          (nn-send rep "ok")
                          (let ((ctime (date->string (current-date)))) 
                            (if  (equal? instr "time-to-die")
				 (begin 
				   (debug:print 0 *default-log-port* ctime " received '" instr "'. Time to sucide." )
				   (let ((pid  (current-process-id)))
				     (debug:print 0 *default-log-port* "Killing current process (pid=" pid ")")
				     (system (conc "kill " pid))))  
				 (begin
				   (debug:print 0 *default-log-port* ctime " received " instr )
					;(nn-send rep "ok")
                                   (if (not (equal? instr "ping"))
                                       (begin
					 (debug:print 0 *default-log-port* ctime " running \"" script " " instr "\"")
					 (system (conc script " '" instr "' &"))
					;(process-run script (list  instr ))  
					 (debug:print 0 *default-log-port* ctime " done" ))
                                       (begin
                                   	 (if (not (equal? instr "load"))
                                   	     (print "Checking load")

                                   	     ) 
					 )

                                       )

                                   )))
                          (loop (nn-recv rep))))
		      (print "ERROR: Port " portnum " already in use. Try another port")))))))

;; from bard

(define (start-nanomsg-server)
  (let* ((socket (socket-connect "inproc://my-server")))
    (begin
      (set-socket-option socket 'linger 1)
      (set-socket-option socket 'sndbuf 1024)
      (set-socket-option socket 'rcvbuf 1024)
      (loop
        (let ((msg (receive-message socket)))
          (if msg
              (handle-message msg)
              (exit))))))