From dabc8f3fdf0cf8daebd3b9b7accf518fdb71f6ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Wed, 24 Aug 2022 16:58:42 +0200 Subject: cmk display data --- elisp/cmk.el | 133 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 73 insertions(+), 60 deletions(-) (limited to 'elisp') diff --git a/elisp/cmk.el b/elisp/cmk.el index 55e4435..7071a38 100644 --- a/elisp/cmk.el +++ b/elisp/cmk.el @@ -9,7 +9,7 @@ ;; Version: 0.0.1 ;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp ;; Homepage: https://github.com/titan/cmk -;; Package-Requires: ((emacs "24.3")) +;; Package-Requires: ((emacs "27.1")) ;; ;; This file is not part of GNU Emacs. ;; @@ -20,16 +20,44 @@ ;;; Code: (require 'dash) -(let* ((cmks (make-network-process - :name "Checkmk" - :remote "/tmp/ingridcmk.socket" - ;; :coding '(binary . binary) - :buffer "CMK" - ;; :filter (lambda (_process string) - ;; (message "%S" string)) - :sentinel (lambda (process event) - (message "Process: %s had the event '%s'" process event) - (kill-buffer (process-buffer process)))))) +(defun cmk-parse-rows-problems (buffer) + (with-current-buffer buffer + (goto-char (point-min)) + (let (data) + (while (not (eobp)) + (let* ((row + (split-string (buffer-substring-no-properties (point) (line-end-position)) ";")) + (state (pcase (string-to-number (car row)) + (0 (propertize "OK" 'face 'font-lock-string-face)) + (1 (propertize "WARN" 'face 'font-lock-warning-face)) + (2 (propertize "CRIT" 'face 'font-lock-keyword-face)) + (3 (propertize "UNKN" 'face 'font-lock-builtin-face)) + (a (number-to-string a)))) + (date (->> (nth 3 row) + (string-to-number) + (seconds-to-time) + (format-time-string "%Y-%m-%d %H:%M"))) + (msg (->> (nth 4 row) + (replace-regexp-in-string (rx "(" (+ "!") ")") "")))) + (push (list (count-lines 1 (point)) + (vector state (cadr row) + (caddr row) + date msg)) data)) + (forward-line)) + data))) + +(defun cmk-oneshot (&optional keep-buffer) + "One shot socket connection. non-nil KEEP-BUFFER after socket closes." + (make-network-process + :name "Checkmk" + :remote "/tmp/ingridcmk.socket" + :buffer "CMK" + :sentinel (lambda (process event) + (message "Process: %s had the event '%s'" process event) + (unless keep-buffer + (kill-buffer (process-buffer process)))))) + +(let* ((cmks (cmk-oneshot))) ;; (process-send-string cmks "GET hosts\nColumns: address name\n\n") ;; (process-send-string cmks ;; "GET log @@ -40,6 +68,7 @@ ;; Filter: class = 8 ;; Or: 3\n\n") + (when (buffer-live-p (get-buffer "CMK Problems")) (kill-buffer "CMK Problems")) (with-current-buffer (process-buffer cmks) (erase-buffer)) (process-send-string cmks "GET services Columns: service_state host_name service_description service_last_state_change service_plugin_output @@ -60,63 +89,47 @@ Filter: host_state = 2 Filter: host_has_been_checked = 1 And: 2 Negate:\n\n") - (accept-process-output cmks 0.1) - (with-current-buffer (process-buffer cmks) - (goto-char (point-min)) - (let (data) - (while (not (eobp)) - (let* ((row - (split-string (buffer-substring-no-properties (point) (line-end-position)) ";")) - (state (pcase (string-to-number (car row)) - (1 (propertize "WARN" 'face 'font-lock-warning-face)) - (2 (propertize "CRIT" 'face 'font-lock-keyword-face)) - (a (number-to-string a)))) - (date (->> (nth 3 row) - (string-to-number) - (seconds-to-time) - (format-time-string "%Y-%m-%d %H:%M"))) - (msg (->> (nth 4 row) - (replace-regexp-in-string (rx "(" (+ "!") ")") "")))) - (push (list (count-lines 1 (point)) - (vector state (cadr row) - (caddr row) - date msg)) data)) - (forward-line)) - (with-current-buffer (get-buffer-create "CMK Problems") - (erase-buffer) - (tabulated-list-mode) - (setq tabulated-list-format `[("STATE" 5 t) - ("HOST" 9 t) - ("Service" 20 t) - ("Since" 16) - ("Message" 18 t) - ]) - (setq tabulated-list-sort-key '("STATE" . nil)) - (setq tabulated-list-entries data) - (tabulated-list-init-header) - (tabulated-list-print) - (display-buffer (current-buffer)))))) - -(let* ((cmks (make-network-process - :name "Checkmk" - :remote "/tmp/ingridcmk.socket" - ;; :coding '(binary . binary) - :buffer "CMK" - ;; :filter (lambda (_process string) - ;; (message "%S" string)) - :sentinel (lambda (process event) - (message "Process: %s had the event '%s'" process event))))) + (with-current-buffer (get-buffer-create "CMK Problems") + (erase-buffer) + (tabulated-list-mode) + (setq tabulated-list-format `[("STATE" 5 t) + ("HOST" 9 t) + ("Service" 20 t) + ("Since" 16) + ("Message" 18 t) + ]) + (setq tabulated-list-sort-key '("STATE" . nil)) + (accept-process-output cmks 0.1) + (setq tabulated-list-entries (cmk-parse-rows-problems (process-buffer cmks))) + (tabulated-list-init-header) + (tabulated-list-print) + (display-buffer (current-buffer)))) +(let* ((cmks (cmk-oneshot t))) (with-current-buffer (process-buffer cmks) (erase-buffer)) (process-send-string cmks "GET services Columns: service_description host_name service_perf_data -Filter: service_description ~ Filesystem +Filter: service_description ~ CPU utilization OutputFormat: csv -Separators: 10 124 44 124\n\n") +Separators: 10 9 44 124\n\n") + (accept-process-output cmks 0.1) + (display-buffer (process-buffer cmks))) + +(let* ((now (time-convert nil 'integer)) + (start (- now 7200)) + (cmks (cmk-oneshot t))) + + (with-current-buffer (process-buffer cmks) (erase-buffer)) + (process-send-string cmks (format "GET services +Columns: host_name rrddata:1:user:%d:%d:1 +Filter: service_description ~ CPU utilization +OutputFormat: json\n\n" start now)) (accept-process-output cmks 0.1) (with-current-buffer (process-buffer cmks) (goto-char (point-min)) - (display-buffer (current-buffer)))) + (display-buffer (current-buffer)) + (json-parse-buffer))) + (provide 'cmk) ;;; cmk.el ends here -- cgit v1.2.3