aboutsummaryrefslogtreecommitdiff
path: root/tojo-tokyo/monitoring.scm
blob: c9e34a10f37a6266c6d400151edd810f71a72ffe (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
;;; Monitoring --- Server monitoring
;;; Copyright © 2021 Masaya Tojo <masaya@tojo.tokyo>
;;;
;;; This file is part of Monitoring.
;;;
;;; Monitoring 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 of the License, or
;;; (at your option) any later version.
;;;
;;; Monitoring 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.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Monitoring.  If not, see <http://www.gnu.org/licenses/>.

(define-module (tojo-tokyo monitoring)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 textual-ports)
  #:use-module (ice-9 regex)
  #:use-module ((srfi srfi-1) #:select (any))
  #:use-module (web client))

(define-public current-heartbeat-url
  (make-parameter
   (let ((heartbeat-file "/etc/heartbeat-url"))
     (or (and (file-exists? heartbeat-file)
              (call-with-input-file heartbeat-file get-line))
         "https://heartbeat.test"))))

(define (df)
  (let ((port (open-input-pipe "df")))
    (get-line port)
    (let f ((line (get-line port)))
      (cond ((eof-object? line)
             (close-pipe port)
             '())
            (else
             (cons (map cons
                        '(filesystem 1k-blocks used available use% mounted-on)
                        (map match:substring (list-matches "[^ ]+" line)))
                   (f (get-line port))))))))

(define-public (disk-use%-over? threshold)
  (any (lambda (x)
         (let ((use% (string->number (string-delete #\% (assoc-ref x 'use%)))))
           (< threshold use%)))
       (filter (lambda (x)
                 (let ((fs (assoc-ref x 'filesystem)))
                   (and fs
                        (<= 5 (string-length fs))
                        (string=? (substring fs 0 5) "/dev/"))))
               (df))))

(define (df-i)
  (let ((port (open-input-pipe "df -i")))
    (get-line port)
    (let f ((line (get-line port)))
      (cond ((eof-object? line)
             (close-pipe port)
             '())
            (else
             (cons (map cons
                        '(filesystem inodes iused ifree iuse% mounted-on)
                        (map match:substring (list-matches "[^ ]+" line)))
                   (f (get-line port))))))))

(define-public (disk-iuse%-over? threshold)
  (any (lambda (x)
         (let ((iuse% (string->number (string-delete #\% (assoc-ref x 'iuse%)))))
           (and iuse%
                (< threshold iuse%))))
       (filter (lambda (x)
                 (let ((fs (assoc-ref x 'filesystem)))
                   (and fs
                        (<= 5 (string-length fs))
                        (string=? (substring fs 0 5) "/dev/"))))
               (df-i))))

(define-syntax-rule (heartbeat (p? body ...) ...)
  (let ((heartbeat-cancel? #f))
    (when p? body ... (set! heartbeat-cancel? #t)) ...
    (unless heartbeat-cancel?
      (http-request (current-heartbeat-url)))))
(export-syntax heartbeat)