Alert declaration easier - 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 5bcf5ae779478fbb3233740aa9ce5583a6f0e0bf DIR parent eb64a97f9c6283cab21d9672be9c4c22ac27dcb0 HTML Author: Solene Rapenne <solene@perso.pw> Date: Wed, 10 Jan 2018 20:16:23 +0100 Alert declaration easier Diffstat: M functions.lisp | 69 ++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 25 deletions(-) --- DIR diff --git a/functions.lisp b/functions.lisp @@ -1,13 +1,29 @@ (require 'asdf) +(defparameter *alerts* '()) + (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 *red* (color 1 31)) +(defparameter *white* (color 0 70)) +(defparameter *green* (color 1 32)) (defparameter *yellow* (color 0 33)) +;; common-lisp don't have a split string function natively +(defun replace-all (string part replacement &key (test #'char=)) + (with-output-to-string (out) + (loop with part-length = (length part) + for old-pos = 0 then (+ pos part-length) + for pos = (search part string + :start2 old-pos + :test test) + do (write-string string out + :start old-pos + :end (or pos (length string))) + when pos do (write-string replacement out) + while pos))) + (defmacro create-probe(name &body code) `(progn (defun ,name(params) ,@code))) @@ -21,33 +37,36 @@ t (list nil (format nil "return code = ~a" code))))) -(defun trigger-alert(level function params result) - (format nil "~{~a~}" - (mapcar #'(lambda(x) - (if (symbolp x) - (case x - (+ "") - (result result) - (hostname (machine-instance)) - (date (multiple-value-bind - (second minute hour day month year) - (get-decoded-time) - (format nil "~a/~a/~a ~a:~a:~a" year month day hour minute second))) - (os (software-type)) - (function function) - (space " ") - (_ " ") - (params params) - (desc (getf params :desc "")) - (newline #\Newline) - (level level)) - x)) - (cadr (assoc level *alerts*))))) +(defmacro alert(name string) + `(progn (push (list ',name ,string) + *alerts*))) +(defun trigger-alert(level function params result) + (let* ((notifier-command (assoc level *alerts*)) + (command-string (cadr notifier-command))) + (setf command-string (replace-all command-string "%result%" (format nil "~a" result))) + (setf command-string (replace-all command-string "%hostname%" (machine-instance))) + (setf command-string (replace-all command-string "%os%" (software-type))) + (setf command-string (replace-all command-string "%function%" (format nil "~a" function))) + (setf command-string (replace-all command-string "%params%" (format nil "~a" params))) + (setf command-string (replace-all command-string "%desc%" (getf params :desc ""))) + (setf command-string (replace-all command-string "%newline%" (string #\Newline))) + (setf command-string (replace-all command-string "%level%" level)) + (setf command-string (replace-all command-string "%date%" + (multiple-value-bind + (second minute hour day month year) + (get-decoded-time) + (format nil "~a/~a/~a ~a:~a:~a" year month day hour minute second)))) + command-string)) + (defmacro stop-if-error(&body body) `(progn (and ,@body))) +(defmacro escalation(&body body) + `(progn + (or ,@body))) + (defmacro =>(level fonction params) `(progn (format t "[~a~a ~20A~a] ~35A" *yellow* ',level ',fonction *white* (getf ',params :desc ',params))