summaryrefslogtreecommitdiff
path: root/minimunin
blob: ada2a3ef54b18530982eaf2169b055ce5bf794a0 (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
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
#!/bin/sh
#
# This script was written by erdgeist@erdgeist.org
# The code is released under the beer ware license, this means do whatever you
# want with it, as long as you leave this notice along with the code.
# Should we meet some day and you find the code is worth it, let's enjoy a beer
# together.

PLUGIN_DIR=/usr/local/etc/minimunin-plugins
CONFIG_DIR=/usr/local/etc/minimunin-configs
BUILTIN="cpu processes load swap systat df iostat uptime memory open_files"
SYSCTL=/sbin/sysctl
[ -f ${SYSCTL} ] || SYSCTL=/usr/sbin/sysctl

# list plugins, read configs
PLUGINS=$(/usr/bin/find ${PLUGIN_DIR} \( -type l -or -type f \) -not -name '*_' -perm +111 -exec basename {} \; 2> /dev/null | /usr/bin/xargs)
CONFIGS=$(/usr/bin/find ${CONFIG_DIR} -type f -exec /usr/bin/grep -v -e ^\# -e ^$ {} \; -exec /bin/echo [] \; 2> /dev/null)

main() {
    # print banner
    printf "# munin node at %s\n" "$(/bin/hostname)"

    # read commands in loop
    while read -r command arg; do

        # chomp and sanitize variables
        command=$(printf %s "${command}" | /usr/bin/tr -cd '[:alnum:]_-. ')
        arg=$(printf %s "${arg}" | /usr/bin/tr -cd '[:alnum:]_-. ')

        # printf "%s %s\n" "$command" "$arg" >> /var/log/minimunin.log
        # dispatch commands
        case ${command} in
            list)
                printf "%s\n" "${BUILTIN} ${PLUGINS}"
                ;;
            fetch)
                print_fetch ${arg}; printf ".\n"
                ;;
            config)
                print_config ${arg}; printf ".\n"
                ;;
            quit)
                exit 0
                ;;
            *)
                 printf "# Unknown command %s.\n" "${command}"
                 # printf %s ${command} | hexdump -C >> /var/log/minimunin.log
                 ;;
        esac
    done
}

print_fetch() {
# see if we're serving a plugin
_plugin=$1
call_plugin ${_plugin} fetch && return 0

# if not, let our builtins answer
case ${_plugin} in
open_files)
    printf "max.value %d\n"      $(get_sys kern.maxfiles)
    printf "used.value %d\n"     $(get_sys kern.openfiles)
    ;;
load)
    printf "load.value %s\n"     $(get_sys vm.loadavg | /usr/bin/cut -f3 -d ' ')
    ;;
swap)
    printf "swap_in.value %d\n"  $(get_sys vm.stats.vm.v_swappgsin)
    printf "swap_out.value %d\n" $(get_sys vm.stats.vm.v_swappgsout)
    ;;
uptime)
    boot=$(get_sys kern.boottime); boot=${boot#*sec =}; boot=${boot%%,*}
    printf "uptime.value %d\n"   $(( ( `/bin/date +%s` - boot ) / 86400 ))
    ;;
memory)
    pagesize=$(get_sys vm.stats.vm.v_page_size)
    printf "active.value %d\n"   $(( pagesize * `get_sys vm.stats.vm.v_active_count` ))
    printf "inactive.value %d\n" $(( pagesize * `get_sys vm.stats.vm.v_inactive_count` ))
    printf "wired.value %d\n"    $(( pagesize * `get_sys vm.stats.vm.v_wire_count` ))
    printf "cached.value %d\n"   $(( pagesize * `get_sys vm.stats.vm.v_cache_count` ))
    printf "free.value %d\n"     $(( pagesize * `get_sys vm.stats.vm.v_free_count` ))
    printf "buffers.value %d\n"  $(( `get_sys vfs.bufspace` ))
    printf "swap.value %d\n"     $(( `/usr/sbin/swapinfo -k | /usr/bin/tail -n 1 | /usr/bin/xargs | /usr/bin/cut -d ' ' -f 3` * 1024 ))
    ;;
cpu)
    set -- $(get_sys kern.cp_time)
    printf "user.value %d\nnice.value %d\nsystem.value %d\ninterrupt.value %d\nidle.value %d\n" "$1" "$2" "$3" "$4" "$5"
    ;;
processes)
    printf "processes.value %d\n" $(/bin/pgrep -aS .* | /usr/bin/wc -l)
    printf "threads.value %d\n" $(( `ps auxwH | wc -l` - 1 ))
    printf "maxprocesses.value %d\n" $(get_sys kern.maxproc)
    ;;
iostat)
    for d in $(/usr/sbin/iostat -Id | /usr/bin/head -n 1 | /usr/bin/xargs); do
        set -- $(/usr/sbin/iostat -Idx ${d} | /usr/bin/tail -n 1 | /usr/bin/xargs)
        printf "${d}_read.value %d\n${d}_write.value %d\n" "${4%.*}" "${5%.*}"
    done
    ;;
systat)
    set -- $(get_sys vm.stats.sys.v_soft vm.stats.sys.v_intr vm.stats.sys.v_syscall vm.stats.sys.v_swtch vm.stats.vm.v_forks vm.stats.vm.v_rforks vm.stats.vm.v_vforks)
    printf "softint.value %d\nhardint.value %d\nsyscall.value %d\ncs.value %d\nforks.value %d\n" "$1" "$2" "$3" "$4" $(( ${5}+${6}+${7} ))
    ;;
df)
    /bin/df -P -t noprocfs,devfs,fdescfs,linprocfs,linsysfs,nfs,nullfs | /usr/bin/tail -n +2 |
    while read -r fs blocks used avail cap mount; do
        fs=$(printf "%s" ${fs} | /usr/bin/tr -c '[:alnum:]' _)
        printf "%s %d\n" "${fs}.value" $(( used * 512 ))
    done
    ;;
*)
    printf "# Unknown plugin %s.\n" "${_plugin}"
    ;;
esac
}

print_config() {
_plugin=$1
# see if we're configuring a plugin
call_plugin ${_plugin} config && return 0

# if not, execute built in commands
case ${_plugin} in
load)
cat <<-EOF
	graph_title Load average
	graph_info The load average of the machine describes how many processes are in the run-queue (scheduled to run "immediately").
	graph_category system
	graph_args --base 1000 -l 0
	graph_vlabel load
	graph_scale no
	load.label load
	load.info Average load for the last five minutes.
	load.warning 10
	load.critical 120
EOF
;;
swap)
cat <<-EOF
	graph_title Swap in/out
	graph_args --base 1000 -l 0
	graph_info This graph shows the swap activity of the system.
	graph_category system
	graph_vlabel pages per second in (-) / out (+)
	swap_in.label swap
	swap_in.type DERIVE
	swap_in.min 0
	swap_in.max 100000
	swap_in.graph no
	swap_out.label swap
	swap_out.type DERIVE
	swap_out.min 0
	swap_out.max 100000
	swap_out.negative swap_in
EOF
;;
iostat)
    drives=$(/usr/sbin/iostat -Id | /usr/bin/head -n 1 | /usr/bin/xargs)
cat <<-EOF
	graph_title IOstat by bytes
	graph_args --base 1024 -l 0
	graph_vlabel MB per second read+written
	graph_category disk
	graph_info This graph shows the I/O to and from block devices
EOF
    printf "graph_order"
for d in ${drives}; do printf " %s_read %s_write" "$d" "$d"; done
    printf "\n"
for d in ${drives}; do
    cat <<-EOF
		${d}_read.label ${d}
		${d}_read.type DERIVE
		${d}_read.max 2000
		${d}_read.min 0
		${d}_read.graph no
		${d}_write.label ${d}
		${d}_write.info I/O on device ${d}
		${d}_write.type DERIVE
		${d}_write.max 2000
		${d}_write.min 0
		${d}_write.negative ${d}_read
	EOF
done
;;
uptime)
cat <<-EOF
	graph_title Uptime
	graph_args --base 1000 -l 0
	graph_vlabel uptime in days
	graph_scale no
	graph_category system
	uptime.label uptime
	uptime.draw AREA
EOF
;;
memory)
cat <<-EOF
	graph_title Memory usage
	graph_args --base 1024 -l 0 --vertical-label Bytes --upper-limit $(( `get_sys vm.stats.vm.v_page_size` * `get_sys vm.stats.vm.v_page_count`))
	graph_category system
	graph_info This graph shows what the machine uses its memory for.
	graph_order active inactive wired buffers cached free swap
	active.label active
	active.info pages recently statistically used
	active.draw AREA
	inactive.label inactive
	inactive.info pages recently statistically unused
	inactive.draw STACK
	wired.label wired
	wired.info pages that are fixed into memory, usually for kernel purposes, but also sometimes for special use in processes
	wired.draw STACK
	buffers.label buffers
	buffers.info pages used for filesystem buffers
	buffers.draw STACK
	cached.label cache
	cached.info pages that have percolated from inactive to a status where they maintain their data, but can often be immediately reused
	cached.draw STACK
	free.label free
	free.info pages without data content
	free.draw STACK
	swap.label swap
	swap.info Swap space used
	swap.draw STACK
EOF
;;
open_files)
cat <<-EOF
	graph_title File table usage
	graph_args --base 1000 -l 0
	graph_vlabel number of open files
	graph_category system
	graph_info This graph monitors the number of open files.
	used.label open files
	used.info The number of currently open files.
	used.warning $(( `get_sys kern.maxfiles` * 92 / 100 ))
	used.critical $(( `get_sys kern.maxfiles` * 98 / 100 ))
	max.label max open files
	max.info The maximum supported number of open files.
EOF
;;
cpu)
    cdef=`get_sys kern.clockrate` ; cdef=${cdef#*stathz =}; cdef=${cdef% *}",/,100,*"
cat <<-EOF
	graph_title CPU usage
	graph_order system interrupt user nice idle
	graph_args --base 1000 -r --lower-limit 0 --upper-limit $(( `get_sys hw.ncpu` * 100 ))
	graph_vlabel %
	graph_scale no
	graph_info This graph shows how CPU time is spent.
	graph_category system
	graph_period second
	system.label system
	system.draw AREA
	system.max 5000
	system.type DERIVE
	system.min 0
	system.info CPU time spent by the kernel in system activities
	system.cdef system,${cdef}
	interrupt.label interrupt
	interrupt.draw STACK
	interrupt.max 5000
	interrupt.type DERIVE
	interrupt.min 0
	interrupt.info CPU time spent by the kernel processing interrupts
	interrupt.cdef interrupt,${cdef}
	user.label user
	user.draw STACK
	user.max 5000
	user.type DERIVE
	user.info CPU time spent by normal programs and daemons
	user.min 0
	user.cdef user,${cdef}
	nice.label nice
	nice.draw STACK
	nice.max 5000
	nice.type DERIVE
	nice.info CPU time spent by nice(1)d programs
	nice.min 0
	nice.cdef nice,${cdef}
	idle.label idle
	idle.draw STACK
	idle.max 5000
	idle.type DERIVE
	idle.info Idle CPU time
	idle.min 0
	idle.cdef idle,${cdef}
EOF
;;
systat)
cat <<-EOF
	graph_title System Statistics
	graph_vlabel per second
	graph_scale no
	graph_category system
	graph_info FreeBSD systat plugin
	softint.label Software interrupts
	softint.type DERIVE
	softint.min 0
	hardint.label Hardware interrupts
	hardint.type DERIVE
	hardint.min 0
	syscall.label System calls
	syscall.type DERIVE
	syscall.min 0
	cs.label Context switches
	cs.type DERIVE
	cs.min 0
	forks.label Fork rate
	forks.type DERIVE
	forks.min 0
EOF
;;
processes)
cat <<-EOF
	graph_title Process Statistics
	graph_args --base 1000 -l 0
	graph_vlabel number of processes
	graph_category system
	graph_info This graph monitors the number of running processes and threads.
	processes.label The number of processes in the system
	threads.label The number of threads in the system
	maxprocesses.label The maximum number of processes in the system
	processes.warning $(( `get_sys kern.maxproc` * 92 / 100 ))
	processes.critical $(( `get_sys kern.maxproc` * 98 / 100 ))
EOF
;;
df)
cat <<-EOF
	graph_title Filesystem usage (in bytes)
	graph_args --base 1024 --lower-limit 0
	graph_vlabel bytes
	graph_category disk
EOF
/bin/df -P -t noprocfs,devfs,fdescfs,linprocfs,linsysfs,nfs,nullfs | /usr/bin/tail -n +2 |
while read -r fs blocks used avail cap mount; do
    fs=$(printf "%s" ${fs} | /usr/bin/tr -c '[:alnum:]' _)
    printf "%s %s\n" "${fs}.label" "${mount}"
done
;;
*)
    printf "# Unknown plugin %s.\n" "${_plugin}"
    ;;
esac
}

get_sys() {
    LANG=C LC_ALL=C ${SYSCTL} -n $@
}

call_plugin() {
    _param=$2
    _plug=$(/usr/bin/find ${PLUGIN_DIR} \( -type l -or -type f \) -perm +111 -name "$1" 2>/dev/null | /usr/bin/head -n 1)
    [ -z "${_plug}" ] && return 1

    unset in_sect _env _user _command
    while read -r line; do
        case ${line## } in
            \[*\])    case [$1 in ${line%%]*}) in_sect=true;; *) unset in_sect ;; esac
                      ;;
            env.*)    [ "${in_sect}" ] || continue
                      _pref=${line#*env.}
                      _env="${_env} ${_pref%% *}='${_pref#* }'"
                      ;;
            user\ *)  _user=${line#*user }
                      ;;
            command\ *) _command=${line#*command }
                      ;;
        esac
    done <<EOF
${CONFIGS}

EOF

    if [ "${_command}" ]; then
        # if the command is overridden, do a replace on %c -- I wonder if multiple substitutions are intended
        case ${_command} in *%c*) _command=${_command%%%c*}${_plug}${_command#*%c};; esac
    else
        # if not, just use the command
        _command="${_plug}"
    fi

    # attach the config or fetch parameter
    _command="${_command} ${_param}"

    # if we're required to switch the user and able to do so (root) use su to become this user
    [ "${_user}" ] && [ $(id -u) -eq 0 ] && _command="/usr/bin/su -m ${_user} -c '${_command}'"

    # prepend environment and run the command
    eval env ${_env} ${_command}

    return 0
}

# After function definitions, main() can use them
main "$@"