Change output to a tab separated output to make it parseable - 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 e6d5e1bcf78191f5c5e8810564033eab29f7f686
DIR parent 17024e3560369b58c274e68fdd6128ad12fccd49
HTML Author: Solene Rapenne <solene@perso.pw>
Date: Tue, 9 Jul 2019 09:05:40 +0200
Change output to a tab separated output to make it parseable
Diffstat:
M functions.lisp | 37 +++++++++++++------------------
1 file changed, 16 insertions(+), 21 deletions(-)
---
DIR diff --git a/functions.lisp b/functions.lisp
@@ -8,14 +8,6 @@
(defparameter *states-dir* "~/.reed-alert/states/")
(ensure-directories-exist *states-dir*)
-(defun color(num1 num2)
- (format nil "~a[~a;~am" #\Escape num1 num2))
-
-(defparameter *red* (color 1 31))
-(defparameter *white* (color 0 70))
-(defparameter *green* (color 1 32))
-(defparameter *yellow* (color 0 33))
-
;; simple hash function (Fowler Noll Vo)
;; https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
(defun fnv-hash(string)
@@ -98,11 +90,13 @@
(or ,@body)))
(defun =>(level fonction &rest 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 "~a/~a" *states-dir* filename)))
+ (filepath (format nil "~a/~a" *states-dir* filename))
+ (current-state 'failure) ;; default state is a failure
+ (previous-state 'success)
+ (trigger-state 'no))
;; we open the file to read the number of tries
;; if no fail then we have 0 try
@@ -117,18 +111,17 @@
(if (not (listp result))
(progn
+ ;; mark state as success
+ (setf current-state 'success)
+
;; 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*)))
+ (when triggered-before?
+ (uiop:run-program (trigger-alert level fonction params t 'success) :output t)
+ (setf previous-state 'failure))
;; in any case we return t because it's ok
t)
@@ -143,16 +136,18 @@
(and (= 0 (mod (+ 1 tries) (getf params :reminder *reminder*)))
'REMINDER))))) ;; do we need to remind it's failing?
- (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) trigger-now?) :output t))
+ (setf trigger-state 'notified)
+ (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
:if-exists :supersede)
(format stream-out "~a~%~a~%" (+ 1 tries) params))
- nil)))))
+ nil))
+
+ (format t "~a ~A ~A ~A ~A ~A~%"
+ level fonction params previous-state current-state trigger-state))))
;; abort when using ctrl+c instead of dropping to debugger
#+ecl