reed-alert/probes.lisp

93 lines
2.4 KiB
Common Lisp
Raw Normal View History

2016-10-07 10:25:49 +00:00
(defmacro create-probe(name &body code)
`(progn (defun ,name(params) ,@code)))
(defun command-return-code(command)
(let ((code (nth-value 2 (uiop:run-program command :ignore-error-status t))))
(if (= 0 code)
t
(list nil (format nil "return code = ~a" code)))))
(create-probe
file-exists
(let ((result (probe-file (getf params :path))))
(if result
t
(list nil "file not found"))))
(create-probe
file-updated
(if (probe-file (getf params :path))
(with-open-file (file (getf params :path))
(let* ((write-date (file-write-date file))
(now (get-universal-time))
(result (floor (- now write-date) 60)))
(if (> (getf params :limit) result)
t
(list nil result))))
(list nil "file not found")))
(create-probe
pid-running
(if (probe-file (getf params :path))
(let ((pid-number (with-open-file (stream (getf params :path)) (read-line stream))))
(command-return-code (list "ps" "-p" pid-number)))
(list nil "file not found")))
(create-probe
disk-usage
(let* ((output (uiop:run-program (list "df" (getf params :path)) :output :lines)) (line (second output)))
(let ((percent-character-pos (position #\% line)))
(let ((used-disk
(parse-integer
(subseq line
(position #\Space line :end percent-character-pos :from-end t)
percent-character-pos))))
(if (< used-disk (getf params :limit))
t
(list nil "used-disk"))))))
(defun system-load(time)
(read-from-string
(let ((command (concatenate 'string
"uptime | awk '{ print $"
(princ-to-string time)
" }'")))
(uiop:run-program command :output :string))))
(create-probe
load-average-1
(let ((load (system-load 10)))
(if (< load (getf params :limit))
t
(list nil load))))
(create-probe
load-average-5
(let ((load (system-load 11)))
(if (< load (getf params :limit))
t
(list nil load))))
(create-probe
load-average-15
(let ((load (system-load 12)))
(if (< load (getf params :limit))
t
(list nil load))))
(create-probe
command
(command-return-code (getf params :command)))
(create-probe
ping
(command-return-code (list "ping" "-c2" (getf params :host))))
(create-probe
number-of-processes
(let* ((output (uiop:run-program (list "ps" "aux") :output :lines))
(result (length output)))
(if (> (getf params :limit) result)
t
(list nil result))))