aboutsummaryrefslogtreecommitdiffstats
path: root/elisp
diff options
context:
space:
mode:
authorÓscar Nájera <hi@oscarnajera.com>2022-08-24 16:58:42 +0200
committerÓscar Nájera <hi@oscarnajera.com>2022-08-24 16:58:42 +0200
commitdabc8f3fdf0cf8daebd3b9b7accf518fdb71f6ef (patch)
treeddf3241fb227ec15e309f56cbcf0a82351141939 /elisp
parent7c6a35d245f5e0c38fd7e2536a2fb5c6cf8f3405 (diff)
downloaddotfiles-dabc8f3fdf0cf8daebd3b9b7accf518fdb71f6ef.tar.gz
dotfiles-dabc8f3fdf0cf8daebd3b9b7accf518fdb71f6ef.tar.bz2
dotfiles-dabc8f3fdf0cf8daebd3b9b7accf518fdb71f6ef.zip
cmk display data
Diffstat (limited to 'elisp')
-rw-r--r--elisp/cmk.el133
1 files changed, 73 insertions, 60 deletions
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