-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm-mkr.el
446 lines (391 loc) · 15.2 KB
/
helm-mkr.el
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
;;; helm-mkr.el --- mkr interface for emacs-helm
;; Copyright (C) 2018 nabeo
;; Author: nabeo
;; URL: https://github.com/nabeo/helm-mkr
;; Version: 0.0.0
;; X-Original-Version: 0.0.0
;; Package-Requires: ((helm "1.5.3")(cl-lib "0.5")(s "1.9.0"))
;; Keywords:
;; This file is not part of GNU Emacs
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; mkr interface for emacs-helm
;; A call to `helm-mkr' will show a list of host on your mackerel account
;;
;; Requires a configured installation of mkr (https://github.com/mackerelio/mkr)
;;; Code:
(require 'json)
(require 'cl-lib)
(require 's)
(defgroup helm-mkr-faces nil
"Customize the appearance of helm-mkr."
:prefix "helm-"
:group 'helm-mkr
:group 'helm-faces)
(defface helm-mkr-host-status-working
'((t
:inherit font-lock-builtin-face
:foreground "blue"))
"Face used for working host in `helm-mkr'."
:group 'helm-mkr-faces)
(defface helm-mkr-host-status-standby
'((t
:inhertit font-lock-builtin-face
:foreground "green"))
"Face used for standby host in `helm-mkr'."
:group 'helm-mkr-faces)
(defface helm-mkr-host-status-maintenance
'((t :inhertit font-lock-builtin-face
:foreground "yellow"))
"Face used for maintenance host in `helm-mkr'."
:group 'helm-mkr-faces)
(defface helm-mkr-host-status-poweroff
'((t :inhertit font-lock-comment-face
:slant italic
:foreground "gray"))
"Face used for poweroff host in `helm-mkr'."
:group 'helm-mkr-faces)
(defface helm-mkr-alert-status-critical
'((t :inhertit font-lock-builtin-face
:foreground "red"))
"Face used for critical alert in `helm-mkr'."
:group 'helm-mkr-faces)
(defface helm-mkr-alert-status-warning
'((t :inhertit font-lock-builtin-face
:foreground "orange"))
"Face used for warning alert in `helm-mkr'."
:group 'helm-mkr-faces)
(defface helm-mkr-alert-status-unknown
'((t :inhertit font-lock-builtin-face
:slant italic
:foreground "gray"))
"Face used for unknown alert in `helm-mkr'."
:group 'helm-mkr-faces)
(defface helm-mkr-alert-status-default
'((t :inhertit font-lock-builtin-face
:foreground "gray"))
"Face used for default alert in `helm-mkr'."
:group 'helm-mkr-faces)
(defvar mkr-hosts-command
"mkr hosts"
"Command to list hosts.")
(defvar mkr-alerts-command
"mkr alerts"
"Command to list alerts.")
(defvar mkr-status-command
"mkr status"
"Command to get host info.")
(defvar mkr-services-command
"mkr services"
"Command to get service list.")
(defun mkr-run-hosts-command ()
"Execute mkr hosts."
(let ((mkr-resut-buffer (generate-new-buffer-name "*mkr-hosts*")))
(with-temp-buffer
(shell-command mkr-hosts-command (current-buffer) nil)
(buffer-substring-no-properties (point-min) (point-max)))))
(defun mkr-run-alerts-command ()
"Execute mkr alerts."
(let ((mkr-result-buffer (generate-new-buffer-name "*mkr-alerts*")))
(with-temp-buffer
(shell-command mkr-alerts-command (current-buffer) nil)
(buffer-substring-no-properties (point-min) (point-max)))))
(defun mkr-run-services-command ()
"Execute mkr services."
(let ((mkr-result-buffer (generate-new-buffer-name "*mkr-services*")))
(with-temp-buffer
(shell-command mkr-services-command (current-buffer) nil)
(buffer-substring-no-properties (point-min) (point-max)))))
(defun helm-mkr-run-org ()
"Execute mkr org."
(with-temp-buffer
(shell-command-on-region (point-min) (point-max)
"mkr org" t)
(buffer-string)))
(defun helm-mkr-parse-org (input)
"Extract org name, Argument INPUT json in straing form."
(let* ((json-object-type 'plist)
(helm-mkr-org-json (json-read-from-string input)))
helm-mkr-org-json))
(defun helm-mkr-get-org-name ()
"Get org."
(let* (
(helm-mkr-command-result (helm-mkr-run-org))
(mkr-org-name-list (helm-mkr-parse-org helm-mkr-command-result))
(mkr-org-name (plist-get mkr-org-name-list :name)))
mkr-org-name))
(defun mkr-run-status-command (host-id)
"Execute mkr status HOST-ID."
(with-temp-buffer
(shell-command-on-region (point-min) (point-max)
(concat mkr-status-command " " host-id) t)
(buffer-string)))
(defun mkr-parse-hosts-list (input)
"Extract hosts list.
Argument INPUT json input in string from."
(let* ((json-object-type 'plist)
(mkr-hosts-json (json-read-from-string input)))
mkr-hosts-json))
(defun mkr-parse-alerts-list (input)
"Extract alerts list.
Artgument INPUT json input in straing form."
(let* ((json-object-type 'plist)
(mkr-alerts-json (json-read-from-string input)))
mkr-alerts-json))
(defun mkr-parse-services-list (input)
"Extract services list.
Artgument INPUT json input in straing form."
(let* ((json-object-type 'plist)
(mkr-services-json (json-read-from-string input)))
mkr-services-json))
(defun mkr-get-host-status-from-id (host-id)
"Get host status from HOST-ID."
(let ((host-status (mkr-parse-status
(mkr-run-status-command host-id))))
host-status))
(defun mkr-parse-status (input)
"Extract host info.
Artgument INPUT json input in straing form."
(let* ((json-object-type 'plist)
(mkr-host-info-json (json-read-from-string input)))
mkr-host-info-json))
(defun mkr-get-host-name-from-host-id (host-id)
"Get hostname from HOST-ID."
(let* ((host-status (mkr-get-host-status-from-id host-id))
(host-name (plist-get host-status :name)))
host-name))
(defun mkr-format-hosts-helm-row (host)
"Constracts a human-readable string of a host.
show: <name>, <status>, <create date>.
Argument HOST is the mkr json in plist form."
(let* ((id (plist-get host :id))
(name (plist-get host :name))
(status (plist-get host :status))
(retired-p (plist-get host :isRetired))
(create-at (plist-get host :createdAt))
(create-date (car (split-string create-at "T")))
(format-string
(concat
name
" "
(propertize (format "%s" status)
'face (cond
((string= status "standby")
'helm-mkr-host-status-standby)
((string= status "maintenance")
'helm-mkr-host-status-maintenance)
((string= status "poweroff")
'helm-mkr-host-status-poweroff)
((string= status "working")
'helm-mkr-host-status-working)))
" " create-date)))
(cons format-string host)))
(defun mkr-format-alerts-helm-row (alert)
"Constracts a human-readable string of a host.
show: <create date time>, <alert status>, <hostname>, <message>.
Argument ALERT is the mkr json in plist form."
(let* (
(id (plist-get alert :id))
(status (plist-get alert :status))
(host-id (plist-get alert :hostId))
(alert-type (plist-get alert :type))
(host-info (cond
((plist-member alert :hostId)
(mkr-get-host-status-from-id host-id))))
(host-name (cond
((plist-member alert :hostId)
(plist-get host-info :name))))
(host-status (cond
((plist-member alert :hostId)
(plist-get host-info :status))))
(message (cond
((string= alert-type "check")
(car (s-lines (plist-get alert :message))))
((string= alert-type "host")
(plist-get alert :value))
(t
""
)))
(opened-date-time (format-time-string "%Y-%m-%d %H:%M:%S"
(seconds-to-time
(plist-get alert :openedAt))))
(format-string
(concat
opened-date-time " "
(propertize (format "%s" status)
'face (cond
((string= status "WARNING")
'helm-mkr-alert-status-warning)
((string= status "CRITICAL")
'helm-mkr-alert-status-critical)
((string= status "UNKNOWN")
'helm-mkr-alert-status-unknown)
(t
'helm-mkr-alert-status-default)))
" " host-name
" "
(propertize (format "%s" host-status)
'face (cond
((string= host-status "standby")
'helm-mkr-host-status-standby)
((string= host-status "maintenance")
'helm-mkr-host-status-maintenance)
((string= host-status "poweroff")
'helm-mkr-host-status-poweroff)
((string= host-status "working")
'helm-mkr-host-status-working)))
" " (format "%s" message))))
(cons format-string alert)
))
(defun mkr-format-services-helm-row (service)
"Constracts a human-readable string of a host.
show: <name>.
Argument SERVICE is the mkr json in plist form."
(let* (
(name (plist-get service :name))
(memo (cond
((plist-member service :memo)
(plist-get service :memo))))
(format-string
(concat
name " " memo)))
(cons format-string service)))
(defun mkr-sort-helm-rows (a b)
"Compare results from `mkr-format-hosts-helm-row' A and B."
(string< (downcase (car a)) (downcase (car b))))
(defun mkr-get-id-from-host (host-json)
"Extracts ID from HOST-JSON."
(plist-get host-json :id))
(defun mkr-get-name-from-host (host-json)
"Extracts name from HOST-JSON."
(plist-get host-json :name))
(defun mkr-browse-host (host-json)
"Browse mackerel.io from HOST-JSON with `browse-url-browser-function'."
(browse-url (concat
"https://mackerel.io/orgs/"
helm-mkr-get-org-name
"/hosts/"
(mkr-get-id-from-host host-json)))
)
(defun mkr-browse-alert (alert-json)
"Browse mackerel.io from ALERT-JSON with `browse-url-browser-function'."
(browse-url (concat
"https://mackerel.io/orgs/"
helm-mkr-get-org-name
"/alerts/"
(plist-get alert-json :id)))
)
(defun mkr-browse-service (service-json)
"Browse mackerel.io from SERVICE-JSON with `browse-url-browser-function'."
(browse-url (concat
"https://mackerel.io/orgs/"
helm-mkr-get-org-name
"/services/"
(plist-get service-json :name)))
)
(defun mkr-get-hosts ()
"Create host list from mackerel.io."
(let* ((mkr-command-result (mkr-run-hosts-command))
(hosts-info-list (mkr-parse-hosts-list mkr-command-result))
(hosts-info-list (mapcar 'mkr-format-hosts-helm-row hosts-info-list))
)
hosts-info-list))
(defun mkr-get-alerts ()
"Create alert list from mackerel.io."
(let* (
(mkr-command-result (mkr-run-alerts-command))
(alerts-info-list (mkr-parse-alerts-list mkr-command-result))
(alerts-info-list (mapcar 'mkr-format-alerts-helm-row alerts-info-list)))
alerts-info-list))
(defun mkr-get-services ()
"Create service list from mackerel.io."
(let* (
(mkr-command-result (mkr-run-services-command))
(services-info-list (mkr-parse-services-list mkr-command-result))
(services-info-list (mapcar 'mkr-format-services-helm-row services-info-list)))
services-info-list))
;;;###autoload
(defun helm-mkr ()
"Show helm with a table of host information."
(interactive)
(let ((choices (mkr-get-hosts)))
(helm
:buffer "*helm-mkr*"
:sources `(
(name . "Hosts")
(candidates . ,choices)
(candidate-number-limit . 99999)
(action . (
("Copy hostname" .
(lambda (host-json)
(x-select-text (mkr-get-name-from-host host-json))))
("Copy HostId" .
(lambda (host-json)
(x-select-text (mkr-get-id-from-host host-json))))
("Browse mackerel.io" .
(lambda (host-json)
(mkr-browse-host host-json)))
)))))
)
(defun helm-mkr-alert ()
"Show helm with a table of alert infomation."
(interactive)
(let ((choices (mkr-get-alerts)))
(helm
:buffer "*helm-mkr-alert*"
:sources `(
(name . "Alerts")
(candidates . ,choices)
(candidate-number-limit . 99999)
(action . (
("Browse Alert mackerel.io" .
(lambda (alert-json)
(mkr-browse-alert alert-json)))
("Browse Host at mackerel.io" .
(lambda (alert-json)
(browse-url (concat
"https://mackerel.io/orgs/"
helm-mkr-get-org-name
"/hosts/"
(plist-get alert-json :hostId)))))
("Browse monitor at mackerel.io" .
(lambda (alert-json)
(browse-url (concat
"https://mackerel.io/orgs/"
helm-mkr-get-org-name
"/monitors#monitor="
(plist-get alert-json :monitorId)))))
))
))))
(defun helm-mkr-service ()
"Show helm with a table of alert infomation."
(interactive)
(let ((choices (mkr-get-services)))
(helm
:buffer "*helm-mkr-service*"
:sources `(
(name . "Services")
(candidates . ,choices)
(candidate-number-limit . 99999)
(action . (
("Browse Service mackerel.io" .
(lambda (service-json)
(mkr-browse-service service-json)))
))
))))
(provide 'helm-mkr)
;; Local Variables:
;; byte-compile-warnings: (not obsolete)
;; coding: utf-8
;; indent-tabs-mode: nil
;; End:
;;; helm-mkr.el ends here