aboutsummaryrefslogtreecommitdiffstats
path: root/elisp/cmk.el
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2025-01-17 19:50:23 +0100
committerOscar Najera <hi@oscarnajera.com>2025-01-17 19:50:23 +0100
commit78130697e5e13ba068df3fc1c45c4b0146bd11c8 (patch)
tree3adf0c62242392be1133ff51a7ca177b602b20a5 /elisp/cmk.el
parent84b0f73739386ebaa9ec986f025a51815a1204cb (diff)
downloaddotfiles-78130697e5e13ba068df3fc1c45c4b0146bd11c8.tar.gz
dotfiles-78130697e5e13ba068df3fc1c45c4b0146bd11c8.tar.bz2
dotfiles-78130697e5e13ba068df3fc1c45c4b0146bd11c8.zip
[cmk] Revert individually tables with new livestatus queryHEADmaster
Diffstat (limited to 'elisp/cmk.el')
-rw-r--r--elisp/cmk.el119
1 files changed, 60 insertions, 59 deletions
diff --git a/elisp/cmk.el b/elisp/cmk.el
index ebe1b55..98ea8d0 100644
--- a/elisp/cmk.el
+++ b/elisp/cmk.el
@@ -81,7 +81,7 @@ Default is \"%Y-%m-%d %H:%M\"."
(defconst cmk-log-col-spec
'(;("host_state" :column "host_state" :parser cmk-state-coloring)
- (:name "STATE" :min-width 5 :column "log_state" :formatter cmk-state-coloring)
+ (:name "STATE" :min-width 6 :column "log_state" :formatter cmk-state-coloring)
;; ("lsttype" :column "log_state_type")
(:name "Host" :column "host_name")
(:name "Service" :column "service_description")
@@ -126,6 +126,7 @@ Default is \"%Y-%m-%d %H:%M\"."
(,(rx whitespace (or "~" "=" "=~" "~~" "<" ">" "<=" ">=") whitespace) . font-lock-keyword-face)))))
(defun cmk-custom-livestatus-query ()
+ "Open buffer to write a livestatus query."
(interactive)
(let ((query cmk-livestatus-query)
(edit-buffer (get-buffer-create "*LQ*")))
@@ -144,7 +145,7 @@ Default is \"%Y-%m-%d %H:%M\"."
(setq-local cmk-livestatus-query new-query))))))
(switch-to-buffer edit-buffer)))
-(defun company-cmk-lq (command &optional arg &rest ignored)
+(defun company-cmk-lq (command &optional arg &rest _ignored)
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-cmk-lq))
@@ -161,11 +162,45 @@ Default is \"%Y-%m-%d %H:%M\"."
when (string-prefix-p (match-string-no-properties 0) header t)
collect (concat header ": ")))))
-(defun cmk-edit-lq (buffer)
+(defun cmk-livestatus->json (query)
+ "Send QUERY to livestatus then parse the result assuming is JSON."
+ (let ((cmks (cmk-livestatus-query query)))
+ (accept-process-output cmks 0.1)
+ (with-current-buffer (process-buffer cmks)
+ (goto-char (point-min))
+ (json-parse-buffer :array-type 'list))))
+
+(defun cmk-vtable-revert (livestatus-query)
+ "Regenerate the table under point with the updated LIVESTATUS-QUERY."
+ (let ((table (vtable-current-table))
+ (object (vtable-current-object))
+ (column (vtable-current-column))
+ (inhibit-read-only t))
+ (unless table
+ (user-error "No table under point"))
+ (setf (vtable-objects table) (cmk-livestatus->json livestatus-query))
+ (vtable--clear-cache table)
+ (delete-region (progn (text-property-search-backward 'vtable) (point))
+ (progn (text-property-search-forward 'vtable) (point)))
+ (vtable-insert table)
+ (cmk-add-text-properties-to-table (list 'livestatus-query livestatus-query))
+ (when object
+ (vtable-goto-object object))
+ (when column
+ (vtable-goto-column column))))
+
+(defun cmk-add-text-properties-to-table (properties)
+ "Add PROPERTIES a plist to the current table under point."
+ (add-text-properties (progn (text-property-search-backward 'vtable) (point))
+ (progn (text-property-search-forward 'vtable) (point))
+ properties))
+
+(cl-defun cmk-edit-lq ()
"Open window to edit the livestatus query of current BUFFER."
- (interactive (list (current-buffer)))
- (let ((query cmk-livestatus-query)
- (edit-buffer (get-buffer-create "*LQ*")))
+ (interactive)
+ (let ((buffer (current-buffer))
+ (edit-buffer (get-buffer-create "*LQ*"))
+ (query (get-text-property (point) 'livestatus-query)))
(with-current-buffer edit-buffer
(cmk-livestatus-mode)
(insert query)
@@ -175,8 +210,7 @@ Default is \"%Y-%m-%d %H:%M\"."
(let ((new-query (-> (buffer-string) (string-trim) (concat "\n\n"))))
(kill-buffer)
(switch-to-buffer buffer)
- (setq-local cmk-livestatus-query new-query)
- (vtable-revert-command)))))
+ (cmk-vtable-revert new-query)))))
(switch-to-buffer edit-buffer)))
(defun cmk-vtable (table spec filters)
@@ -190,28 +224,21 @@ Default is \"%Y-%m-%d %H:%M\"."
(make-vtable
:columns (mapcar (lambda (li) (map-delete (map-copy li) :column)) spec)
:use-header-line nil
- :objects-function
- (lambda ()
- (let ((cmks (cmk-livestatus-query livestatus-query)))
- (accept-process-output cmks 0.1)
- (with-current-buffer (process-buffer cmks)
- (goto-char (point-min))
- (json-parse-buffer :array-type 'list))))
+ :objects (cmk-livestatus->json livestatus-query)
:keymap (define-keymap
"Q" #'cmk-edit-lq
- "q" #'kill-buffer-and-window
- "v" (lambda ()
- (interactive)
- (message "loc %S" livestatus-query))))))
+ "q" #'kill-current-buffer))
+ (cmk-add-text-properties-to-table (list 'livestatus-query livestatus-query))))
(defun cmk-problems ()
"Render default problems view."
(interactive)
- (with-current-buffer (get-buffer-create "*CMK View*")
- (fundamental-mode)
- (erase-buffer)
- (cmk-vtable "services" cmk-problem-col-spec
- "Filter: service_state = 0
+ (let ((inhibit-read-only t))
+ (with-current-buffer (get-buffer-create "*CMK View*")
+ (special-mode)
+ (erase-buffer)
+ (cmk-vtable "services" cmk-problem-col-spec
+ "Filter: service_state = 0
Filter: service_has_been_checked = 1
And: 2
Negate:
@@ -228,46 +255,20 @@ Filter: host_state = 2
Filter: host_has_been_checked = 1
And: 2
Negate:")
- (goto-char (point-max))
- (insert "\n")
- (cmk-vtable "log" cmk-log-col-spec
- (format
- "Filter: log_time >= %d
+ (goto-char (point-max))
+ (insert "\n")
+ (cmk-vtable "log" cmk-log-col-spec
+ (format
+ "Filter: log_time >= %d
Filter: class = 1
Filter: class = 3
Filter: class = 8
Or: 3
Filter: log_state_type = HARD"
- (floor
- (- (time-to-seconds (current-time))
- (* 24 3600)))))
- (switch-to-buffer (current-buffer))))
-
-;;; Get a different csv
-;; (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 ~ CPU utilization
-;; OutputFormat: csv
-;; Separators: 10 9 44 124\n\n")
-;; (accept-process-output cmks 0.1)
-;; (display-buffer (process-buffer cmks)))
-
-;;; Get some metric data
-;; (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))
-;; (json-parse-buffer)))
+ (floor
+ (- (time-to-seconds (current-time))
+ (* 24 3600)))))
+ (display-buffer (current-buffer)))))
(provide 'cmk)
;;; cmk.el ends here