Fix check function return value - 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 9698c24926a46f573d376851a397799f4f0fc41d
DIR parent b890363bf7bc8dbae4ac17dd0e65396969e09f2d
HTML Author: Solene Rapenne <solene@perso.pw>
Date: Tue, 9 Jul 2019 16:55:01 +0200
Fix check function return value
Diffstat:
M functions.lisp | 21 +++++++++++++--------
M test/test.lisp | 10 ++++++++++
2 files changed, 23 insertions(+), 8 deletions(-)
---
DIR diff --git a/functions.lisp b/functions.lisp
@@ -94,8 +94,8 @@
(result (funcall fonction params))
(filename (format nil "~a-~a-~a" level fonction hash))
(filepath (format nil "~a/~a" *states-dir* filename))
- (current-state 'failure) ;; default state is a failure
- (previous-state 'success)
+ (current-state nil) ;; default state is a failure
+ (previous-state nil)
(trigger-state 'no))
;; we open the file to read the number of tries
@@ -109,10 +109,12 @@
;; 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))
+
+ ;; SUCCESS HANDLING
(progn
;; mark state as success
- (setf current-state 'success)
+ (setf current-state t)
;; we delete the file with previous states
(when (probe-file filepath)
@@ -121,11 +123,11 @@
;; it was a failure and then it's back to normal state
(when triggered-before?
(uiop:run-program (trigger-alert level fonction params t 'success) :output t)
- (setf previous-state 'failure))
+ (setf previous-state nil))
;; in any case we return t because it's ok
t)
- ;; failure handling
+ ;; FAILURE HANDLING
(let ((trigger-now? (or
;; we add +1 to tries because it's failing right now
(and (= (+ 1 tries) (getf params :try *tries*))
@@ -138,7 +140,7 @@
;; more error than limit, send alert once
(when trigger-now?
- (setf trigger-state 'notified)
+ (setf trigger-state 'YES)
(uiop:run-program (trigger-alert level fonction params (cadr result) trigger-now?)))
;; increment the number of tries by 1
(with-open-file (stream-out filepath :direction :output
@@ -146,9 +148,12 @@
(format stream-out "~a~%~a~%" (+ 1 tries) params))
nil))
- (format t "~a ~A ~A ~A ~A ~A~%"
+ (format t "~a ~A ~A ~A ~A ~A ~A~%"
level fonction (format nil "~{~A ~}" params)
- previous-state current-state trigger-state))))
+ (if previous-state "SUCCESS" "ERROR")
+ (if current-state "SUCCESS" "ERROR")
+ trigger-state (+ 1 tries)))
+ current-state))
;; abort when using ctrl+c instead of dropping to debugger
#+ecl
DIR diff --git a/test/test.lisp b/test/test.lisp
@@ -12,3 +12,13 @@
(=> notification disk-usage :path "/" :limit 1 :reminder 2)
(=> notification disk-usage :path "/" :limit 1 :reminder 2)
(=> notification disk-usage :path "/" :limit 1 :reminder 2)
+
+(and
+ (=> notification disk-usage :path "/" :limit 99 :reminder 2 :desc "always OK")
+ (=> notification disk-usage :path "/" :limit 1 :reminder 2 :desc "always error")
+ (=> notification disk-usage :path "/" :limit 1 :reminder 2 :desc "always error, should not be displayed"))
+
+
+(or
+ (=> notification disk-usage :path "/" :limit 1 :reminder 2 :desc "always error, should be followed by always ok")
+ (=> notification disk-usage :path "/" :limit 99 :reminder 2 :desc "always OK"))