Hash :
408808cf
Author :
Thomas de Grivel
Date :
2022-11-03T21:12:39
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
;; Adams - UNIX system administration tool written in Common Lisp
;; Copyright 2013-2022 Thomas de Grivel <thodg@kmx.io>
(in-package :adams)
;; Group
(defmethod probe-group ((group group) (os os-unix))
(let ((id (resource-id group))
(ensure :absent))
(multiple-value-bind #1=(name passwd gid members)
(with-group<5> #1# (egrep (str "^" id ":") "/etc/group")
(when (string= id name)
(setq ensure nil)
(return (values* #1#))))
(properties* (ensure . #1#)))))
;; User
(defmethod probe-user ((user user) (os os-unix))
(let ((id (resource-id user))
(ensure :absent))
(multiple-value-bind #1=(login pass uid gid realname home shell)
(with-passwd<5> #1#
(egrep (str #\^ id #\:) "/etc/passwd")
(when (string= id login)
(with-parent-resource *host*
(setq ensure nil
home (resource 'directory home)
shell (resource 'file shell)))
(return (values* #1#))))
(properties* (ensure . #1#)))))
(defmethod probe-user-groups ((user user) (os os-unix))
(let ((id (resource-id user))
groups)
(unless (eq :absent (get-probed user :ensure))
(let* ((user-login id)
(user-gid (get-probed user :gid))
(user-group nil))
(with-group<5> (name passwd gid members)
(grep user-login "/etc/group")
(when name
(with-parent-resource *host*
(cond ((= user-gid gid)
(setq user-group (resource 'group name)))
((find user-login members :test #'string=)
(unless (find name groups :key #'resource-id :test #'string=)
(push (resource 'group name) groups)))))
(setq groups (sort groups #'string< :key #'resource-id)
groups (if user-group
(cons user-group
(remove (resource-id user-group)
groups :key #'resource-id
:test #'string=))
groups))))))
(properties* groups)))
;; VNode (filesystem node)
(defmethod probe-vnode-using-ls ((vnode vnode) (os os-unix))
(let ((id (resource-id vnode))
(ensure :absent))
(multiple-value-bind #1=(mode links owner group size mtime target)
(with-ls<1>-lT #.(cons 'name '#1#)
(ls "-ldT" id)
(when (string= id (the string name))
(setq mode (mode (mode-permissions mode))
owner (resource 'user owner)
group (resource 'group group)
size (parse-integer size)
ensure :present)
(return (values* #1#))))
(properties* (ensure . #1#)))))
(defmethod probe-vnode-using-stat ((vnode vnode) (os os-unix))
(let ((id (resource-id vnode))
(ensure :absent))
(multiple-value-bind #1=(dev ino mode links uid gid rdev size
atime mtime ctime blksize blocks flags)
(with-stat<1>-r (name . #1#) (stat "-r" (sh-quote id))
(when (and name (string= id (the string name)))
(setq mode (mode (mode-permissions mode))
size (parse-integer size)
ensure :present)
(return (values* #1#))))
(properties* (ensure . #1#)))))
;; Regular file
(eval-when (:compile-toplevel :load-toplevel)
(defparameter *cksum-defs* nil))
(eval-when (:compile-toplevel :load-toplevel)
(defun cksum-defs ()
(dolist (algorithm *cksum-algorithms*)
(declare (type symbol algorithm))
(let* ((name (sym 'probe-file-cksum- algorithm))
(legacy (member algorithm *cksum-legacy-algorithms*))
(iterator (if legacy 'with-cksum<1>-legacy
'with-cksum<1>))
(vars (if legacy '(sum size name) '(algo name sum)))
(cmd (str "cksum -a " algorithm " "))
(match-p (if legacy
`(string= id name)
`(and (string= ,algorithm algo)
(string= id name))))
(var (sym algorithm)))
(push `(defmethod ,name ((file file) (os os-unix))
(let* ((id (resource-id file))
(,var (,iterator ,vars
(run ,cmd (sh-quote id)))
(when ,match-p
(return sum))))
(properties* ,var)))
*cksum-defs*)
(push `(defgeneric ,name (file os)) *cksum-defs*)))
(setf *cksum-defs* (nreverse *cksum-defs*))
(push 'progn *cksum-defs*)))
(eval-when (:compile-toplevel :load-toplevel)
#.(cksum-defs))
(defmethod probe-file-content ((file file) (os os-unix))
(let* ((size (get-probed file :size))
(content (when size
(if (< size *probe-file-content-size-limit*)
(str (run "cat " (sh-quote (resource-id file))))
:file-too-large))))
(properties* content)))
;; Symlink
(defmethod probe-symlink-target ((symlink symlink) (os os-unix))
(let ((target (string-trim '(#\Newline)
(run-1 "readlink "
(sh-quote (resource-id symlink))))))
(properties* target)))
;; Directory
(defmethod probe-directory-content ((dir directory) (os os-unix))
(let ((content (remove-if (lambda (f)
(or (string= "." f)
(string= ".." f)))
(run "ls -1a " (resource-id dir)))))
(properties* content)))
;; Mounts
(defmethod probe-mount ((m mount) (os os-unix))
(let ((id (resource-id m)))
(multiple-value-bind (dev mp fstype options)
(with-mount<8> (dev mp fstype options)
(run "mount | grep " (sh-quote (str id " ")))
(when (or (string= id dev)
(string= id mp))
(return (values dev mp fstype options))))
(properties :mounted-device dev
:mounted-mount-point mp
:mounted-fstype fstype
:mounted-options options))))
(defmethod probe-fstab ((m mount) (os os-unix))
(let ((id (resource-id m)))
(multiple-value-bind (dev mp fstype options freq passno)
(with-fstab<5> (dev mp fstype options freq passno)
(run "grep " (sh-quote (str id " ")) " /etc/fstab")
(when (or (string= id dev)
(string= id mp))
(return (values dev mp fstype options freq passno))))
(properties :fstab-device dev
:fstab-mount-point mp
:fstab-fstype fstype
:fstab-options options
:fstab-freq freq
:fstab-passno passno))))
#+nil
(probe-all-properties (resource 'mount "/"))
;; Processes
(defmethod probe-ps-auxww ((process process) (os os-unix))
(let ((id (resource-id process)))
(multiple-value-bind #1=(user pid cpu mem vsz rss tt state start time cmd)
(with-ps<1>-u #1# (run "ps auxww | grep " (sh-quote id))
(print #.(cons 'list '#1#))
(when (and (<= (length id) (length cmd))
(string= id cmd :end2 (length id))
(or (= (length id) (length cmd))
(char= #\Space (char cmd (length id)))))
(return (values* #1#))))
(properties* #1#))))
#+nil
(probe-all-properties (resource 'process "svscan"))