aboutsummaryrefslogtreecommitdiffstats
path: root/elisp/cmk.el
blob: 7071a38918e9593b67c7aa3c2203cce491a79b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
;;; cmk.el --- CMK livestatus -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 Óscar Nájera
;;
;; Author: Óscar Nájera <hi@oscarnajera.com>
;; Maintainer: Óscar Nájera <hi@oscarnajera.com>
;; Created: July 08, 2022
;; Modified: July 08, 2022
;; 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 "27.1"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;;  CMK livestatus
;;
;;; Code:
(require 'dash)

(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
  ;; Columns: host_name service_description log_type log_plugin_output log_state log_state_type log_time
  ;; Filter: log_time >= 1657270676
  ;; Filter: class = 1
  ;; Filter: class = 3
  ;; 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
Filter: service_state = 0
Filter: service_has_been_checked = 1
And: 2
Negate:
Filter: service_has_been_checked = 1
Filter: service_scheduled_downtime_depth = 0
Filter: host_scheduled_downtime_depth = 0
And: 2
Filter: service_acknowledged = 0
Filter: host_state = 1
Filter: host_has_been_checked = 1
And: 2
Negate:
Filter: host_state = 2
Filter: host_has_been_checked = 1
And: 2
Negate:\n\n")
  (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 ~ CPU utilization
OutputFormat: csv
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))
    (json-parse-buffer)))


(provide 'cmk)
;;; cmk.el ends here