Prevent a end notif without a start - 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 --- DIR commit 9469c17e98862282e7e6e810f17d8a1ae6af2922 DIR parent a384acbcfd91a1067dc91a83d1f16fc23a6c881f HTML Author: Solene Rapenne <solene@perso.pw> Date: Mon, 22 Jan 2018 08:09:30 +0100 Prevent a end notif without a start Diffstat: M functions.lisp | 85 +++++++++++++++---------------- 1 file changed, 42 insertions(+), 43 deletions(-) --- DIR diff --git a/functions.lisp b/functions.lisp @@ -87,54 +87,53 @@ (or ,@body))) (defun =>(level fonction &rest params) - (format t "[~a~a ~20A~a] ~45A" *yellow* level fonction *white* (getf params :desc params)) + (format t "[~a~10a ~20A~a] ~45A" *yellow* level fonction *white* (getf params :desc params)) (let* ((hash (fnv-hash (format nil "~{~a~}" (remove-if #'symbolp params)))) (result (funcall fonction params)) (filename (format nil "~a-~a-~a" level fonction hash)) (filepath (format nil "states/~a" filename))) - (if (not (listp result)) - (progn - (if (probe-file filepath) - ;; last time was a failure - (progn - (uiop:run-program (trigger-alert level fonction params t 'success) :output t) - (delete-file filepath) - (format t " => ~afailure => success~a~%" *green* *white*)) - ;; last time was a success - (format t " => ~asuccess~a~%" *green* *white*)) - ;; we return t because it's ok - t) + + ;; we open the file to read the number of tries + ;; if no fail then we have 0 try + (let* ((tries (if (not (probe-file filepath)) + 0 + (with-open-file (stream filepath :direction :input) + (parse-integer (read-line stream 0 nil))))) + (trigger-now? (= tries (getf params :try *tries*))) + (triggered-before? (>= tries (getf params :try *tries*)))) + + ;; if result is a list then the check had fail a return both nil and the error value + ;; if result is not a list, then it was successful + (if (not (listp result)) + (progn + + ;; we delete the file with previous states + (when (probe-file filepath) + (delete-file filepath)) + + ;; it was a failure and then it's back to normal state + (if triggered-before? + (progn + (uiop:run-program (trigger-alert level fonction params t 'success) :output t) + (format t " => ~afailure => success~a~%" *green* *white*)) + (progn + ;; last time was a success + (format t " => ~asuccess~a~%" *green* *white*))) + ;; in any case we return t because it's ok + t) (progn - (if (probe-file filepath) - ;; error before - ;; but how many ? - (with-open-file (stream filepath :direction :input) - (let ((tries (parse-integer (read-line stream 0 nil)))) - (format t " => ~aerror (~a failures before)~a~%" *red* tries *white*) - - ;; more error than limit, send alert once - (when (= tries (getf params :try *tries*)) - (uiop:run-program (trigger-alert level fonction params (cadr result) 'error) :output t)) - - ;; increment the file - (progn - (with-open-file (stream-out filepath :direction :output - :if-exists :supersede) - (format stream-out "~a~%~a~%" (+ 1 tries) params))))) - - ;; file doesn't exist - (with-open-file (stream-out filepath :direction :output - :if-exists :supersede) - (format t " => ~aerror (first failure)~a~%" *red* *white*) - - ;; maybe we would be warned at first error ? - ;; code is duplicated from above because it - ;; requires reading the non existent file - (when (= 1 (getf params :try *tries*)) - (uiop:run-program (trigger-alert level fonction params (cadr result) 'error) :output t)) - - (format stream-out "1~%~a~%" params))) - nil)))) + (format t " => ~aerror (~a failure(s) before)~a~a~%" *red* tries *white* (if trigger-now? " NOTIFIED" "")) + + ;; more error than limit, send alert once + (when trigger-now? + (uiop:run-program (trigger-alert level fonction params (cadr result) 'error) :output t)) + + ;; increment the number of tries by 1 + (with-open-file (stream-out filepath :direction :output + :if-exists :supersede) + (format stream-out "~a~%~a~%" (+ 1 tries) params)) + + nil))))) (load "probes.lisp")