URI: 
       probes.lisp - reed-alert - Lightweight agentless alerting system for server
  HTML git clone git://bitreich.org/reed-alert/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/reed-alert/
   DIR Log
   DIR Files
   DIR Refs
   DIR Tags
   DIR README
   DIR LICENSE
       ---
       probes.lisp (4338B)
       ---
            1 (create-probe
            2  check-file-exists
            3  (let ((result (probe-file (getf params :path))))
            4    (if result
            5        t
            6        (list nil "file not found"))))
            7 
            8 (create-probe
            9  file-updated
           10  (if (probe-file (getf params :path))
           11      (with-open-file (file (getf params :path))
           12        (let* ((write-date (file-write-date file))
           13               (now (get-universal-time))
           14               (result (floor (- now write-date) 60)))
           15          (if (> (getf params :limit) result)
           16              t
           17              (list nil result))))
           18      (list nil "file not found")))
           19 
           20 (create-probe
           21  service
           22  (command-return-code
           23   #+openbsd
           24   (list "/usr/sbin/rcctl" "check" (getf params :name))
           25   #+freebsd
           26   (list "/usr/sbin/service" (getf params :name) "status")
           27   #+linux
           28   (if (probe-file "/etc/portage/make.conf")
           29       ;; gentoo
           30       (list "/sbin/rc-service" (getf params :name) "status")
           31       ;; most linux
           32       (if (probe-file "/usr/sbin/service")
           33          (list "/usr/sbin/service" (getf params :name) "status")
           34          ;; other linux with systemd only
           35          (list "/usr/bin/systemctl" "status" (getf params :name))))))
           36 
           37 (create-probe
           38  pid-running
           39  (if (probe-file (getf params :path))
           40      (let ((pid-number (with-open-file (stream (getf params :path)) (read-line stream))))
           41        (command-return-code (list "ps" "-p" pid-number)))
           42      (list nil "file not found")))
           43 
           44 (create-probe
           45  disk-usage
           46  (let* ((output (uiop:run-program (list "df" (getf params :path)) :output :lines)) (line (second output)))
           47    (let ((percent-character-pos (position #\% line)))
           48      (let ((used-disk
           49             (parse-integer
           50              (subseq line
           51                      (position #\Space line :end percent-character-pos :from-end t)
           52                      percent-character-pos))))
           53        (if (< used-disk (getf params :limit))
           54            t
           55            (list nil used-disk))))))
           56 
           57 (defun system-load(time)
           58   (read-from-string
           59    (let ((command (strcat
           60                    "uptime | awk '{ print $(NF-"
           61                    (princ-to-string time)
           62                    ") }'")))
           63      (uiop:run-program command :output :string))))
           64     
           65 (create-probe
           66  load-average-1
           67  (let ((load (system-load 2)))
           68    (if (< load (getf params :limit))
           69        t
           70        (list nil load))))
           71 
           72 (create-probe
           73  load-average-5
           74  (let ((load (system-load 1)))
           75    (if (< load (getf params :limit))
           76        t
           77        (list nil load))))
           78  
           79 (create-probe
           80  load-average-15
           81  (let ((load (system-load 0)))
           82    (if (< load (getf params :limit))
           83        t
           84        (list nil load))))
           85 
           86 (create-probe
           87  file-more-than
           88  (if (probe-file (getf params :path))
           89      (let ((result (get-file-size (getf params :path))))
           90        (if (< result (getf params :limit))
           91            t
           92            (list nil result)))
           93      "file not found"))
           94 
           95 (create-probe
           96  file-less-than
           97  (if (probe-file (getf params :path))
           98      (let ((result (get-file-size (getf params :path))))
           99        (if (> result (getf params :limit))
          100            t
          101            (list nil result)))
          102      "file not found"))
          103 
          104 (create-probe
          105  command
          106  (command-return-code (getf params :command)))
          107 
          108 (create-probe
          109  ping
          110  (command-return-code
          111    #+openbsd
          112    (list "ping" "-w1" "-c2" (getf params :host))
          113    #+freebsd
          114    (list "ping" "-W1" "-c2" (getf params :host))
          115    #+linux
          116    (list "ping" "-W1" "-c2" (getf params :host))))
          117 
          118 (create-probe
          119  number-of-processes
          120  (let* ((output (uiop:run-program (list "ps" "aux") :output :lines))
          121         (result (length output)))
          122    (if (> (getf params :limit) result)
          123        t
          124        (list nil result))))
          125 
          126 (create-probe
          127  curl-http-status
          128  (command-return-code
          129   (list "curl" "-f"
          130         (format nil "-m~a" (getf params :timeout 5))
          131         (getf params :url))))
          132 
          133 (create-probe
          134  ssl-expiration
          135  (command-return-code
          136   (let ((host (getf params :host))
          137         (port (princ-to-string (getf params :port 443)))
          138         (seconds (princ-to-string (getf params :seconds)))
          139         (starttls (getf params :starttls)))
          140     (strcat
          141      "echo | openssl s_client -showcerts -servername " host
          142      " -connect "  host ":" port " 2>/dev/null |"
          143      "openssl x509 -inform pem -noout "
          144      (when starttls (strcat " -starttls " starttls))
          145      " -checkend " seconds))))
          146 
          147 (create-probe
          148  write-to-file
          149  (let ((filepath (getf params :path nil))
          150        (text (getf params :text
          151                    (princ-to-string
          152                     (get-universal-time)))))
          153    (when filepath
          154      (with-open-file
          155          (stream-out filepath
          156                      :direction :output
          157                      :if-exists :supersede)
          158        (format stream-out "~a~%" text))
          159      t)))