summaryrefslogtreecommitdiff
path: root/minimunin
blob: 15289bc4b910ad35b884122713a70d27526b7297 (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
#!/bin/sh

PLUGIN_DIR=/usr/local/etc/minimunin-plugins
CONFIG_DIR=/usr/local/etc/minimunin-configs
BUILTIN="cpu load swap systat 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 f -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 "# moonshell node at %s\n" `hostname`

    # read commands in loop
    while read command arg; do

        # chomp
        command=`printf ${command} | /usr/bin/tr -d '\r'`

        # 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.\n"
                 # printf %s ${command} | hexdump -C >> /var/log/minimunin.log
                 ;;
        esac
    done
}

print_fetch() {

# see if we're serving a plugin
call_plugin $1 fetch && return 0

# if not, let our builtins answer
case $1 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 ) / 84600 ))
    ;;
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"
    ;;
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} ))
    ;;
*)
    printf "# Unknown command.\n"
    ;;
esac
}

print_config() {

# see if we're configuring a plugin
call_plugin $1 config && return 0

# if not, execute built in commands
case $1 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
;;
*)
    printf "# Unknown command.\n"
    ;;
esac
}

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

env_for_plugin_from_config() {
    unset in_sect
    while read line; do
        case ${line## } in
            \[*\])  v="${line#*[}"; v="${v%%]*}"
                    case $1 in ${v}) in_sect=true;; *) unset in_sect ;; esac
                    ;;
            env.*)  [ "${in_sect}" ] || continue
                    _pref=${line#*env.};
                    printf "%s " ${_pref%% *}=\'${_pref#* }\'
                    ;;
        esac
    done
}

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

    printf %s "${CONFIGS}" | while read line; do
      case line in
        \[*\]) start_section;;
        *)     in_section;;
      esac
    done

    _env=$( printf "%s\n" "${CONFIGS}" | env_for_plugin_from_config ${_plug} )
    eval env ${_env} ${_plug} ${_param}
    return $?
}

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