diff options
-rwxr-xr-x | minimunin | 316 |
1 files changed, 316 insertions, 0 deletions
diff --git a/minimunin b/minimunin new file mode 100755 index 0000000..4743009 --- /dev/null +++ b/minimunin | |||
@@ -0,0 +1,316 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | PLUGIN_DIR=/usr/local/etc/minimunin-plugins | ||
4 | BUILTIN="cpu load swap systat iostat uptime memory open_files" | ||
5 | |||
6 | # list plugins | ||
7 | PLUGINS=`/usr/bin/find ${PLUGIN_DIR} -type f -perm +111 -exec basename {} \; 2> /dev/null | /usr/bin/xargs` | ||
8 | |||
9 | get_plugin() { | ||
10 | plug=`/usr/bin/find ${PLUGIN_DIR} -type f -perm +111 -name $1 | /usr/bin/head -n 1` | ||
11 | [ -z "${plug}" ] && return 1 | ||
12 | printf %s ${plug} | ||
13 | return 0 | ||
14 | } | ||
15 | |||
16 | main() { | ||
17 | # print banner | ||
18 | printf "# moonshell node at %s\n" `hostname` | ||
19 | |||
20 | # read commands in loop | ||
21 | while read command arg; do | ||
22 | |||
23 | # chomp | ||
24 | command=`printf ${command} | /usr/bin/tr -d '\r'` | ||
25 | |||
26 | # printf "%s %s\n" "$command" "$arg" >> /var/log/minimunin.log | ||
27 | # dispatch commands | ||
28 | case ${command} in | ||
29 | list) | ||
30 | printf "%s\n" "${BUILTIN} ${PLUGINS}" | ||
31 | ;; | ||
32 | fetch) | ||
33 | print_fetch $arg; printf ".\n" | ||
34 | ;; | ||
35 | config) | ||
36 | print_config $arg; printf ".\n" | ||
37 | ;; | ||
38 | quit) | ||
39 | exit 0 | ||
40 | ;; | ||
41 | *) | ||
42 | printf "# Unknown command.\n" | ||
43 | # printf %s ${command} | hexdump -C >> /var/log/minimunin.log | ||
44 | ;; | ||
45 | esac | ||
46 | done | ||
47 | } | ||
48 | |||
49 | print_fetch() { | ||
50 | |||
51 | # see if we're serving a plugin | ||
52 | plug=`get_plugin $1` | ||
53 | if [ $? = 0 ]; then | ||
54 | eval ${plug} | ||
55 | return | ||
56 | fi | ||
57 | |||
58 | # if not, let our builtins answer | ||
59 | case $1 in | ||
60 | open_files) | ||
61 | printf "max.value %d\n" `get_sys kern.maxfiles` | ||
62 | printf "used.value %d\n" `get_sys kern.openfiles` | ||
63 | ;; | ||
64 | load) | ||
65 | printf "load.value %s\n" `get_sys vm.loadavg | /usr/bin/cut -f3 -d ' '` | ||
66 | ;; | ||
67 | swap) | ||
68 | printf "swap_in.value %d\n" `get_sys vm.stats.vm.v_swappgsin` | ||
69 | printf "swap_out.value %d\n" `get_sys vm.stats.vm.v_swappgsout` | ||
70 | ;; | ||
71 | uptime) | ||
72 | boot=`get_sys kern.boottime`; boot=${boot#*sec =}; boot=${boot%%,*} | ||
73 | printf "uptime.value %d\n" $(( ( `/bin/date +%s` - boot ) / 84600 )) | ||
74 | ;; | ||
75 | memory) | ||
76 | pagesize=`get_sys vm.stats.vm.v_page_size` | ||
77 | printf "active.value %d\n" $(( pagesize * `get_sys vm.stats.vm.v_active_count` )) | ||
78 | printf "inactive.value %d\n" $(( pagesize * `get_sys vm.stats.vm.v_inactive_count` )) | ||
79 | printf "wired.value %d\n" $(( pagesize * `get_sys vm.stats.vm.v_wire_count` )) | ||
80 | printf "cached.value %d\n" $(( pagesize * `get_sys vm.stats.vm.v_cache_count` )) | ||
81 | printf "free.value %d\n" $(( pagesize * `get_sys vm.stats.vm.v_free_count` )) | ||
82 | printf "buffers.value %d\n" $(( `get_sys vfs.bufspace` )) | ||
83 | printf "swap.value %d\n" $(( `/usr/sbin/swapinfo -k | /usr/bin/tail -n 1 | /usr/bin/xargs | /usr/bin/cut -d ' ' -f 3` * 1024 )) | ||
84 | ;; | ||
85 | cpu) | ||
86 | set -- `get_sys kern.cp_time` | ||
87 | printf "user.value %d\nnice.value %d\nsystem.value %d\ninterrupt.value %d\nidle.value %d\n" "$1" "$2" "$3" "$4" "$5" | ||
88 | ;; | ||
89 | iostat) | ||
90 | for d in `/usr/sbin/iostat -Id | /usr/bin/head -n 1 | /usr/bin/xargs`; do | ||
91 | set -- `/usr/sbin/iostat -Idx ${d} | /usr/bin/tail -n 1 | /usr/bin/xargs` | ||
92 | printf "${d}_read.value %d\n${d}_write.value %d\n" "${4%.*}" "${5%.*}" | ||
93 | done | ||
94 | ;; | ||
95 | systat) | ||
96 | 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` | ||
97 | printf "softint.value %d\nhardint.value %d\nsyscall.value %d\ncs.value %d\nforks.value %d\n" "$1" "$2" "$3" "$4" $(( ${5}+${6}+${7} )) | ||
98 | ;; | ||
99 | *) | ||
100 | printf "# Unknown command.\n" | ||
101 | ;; | ||
102 | esac | ||
103 | } | ||
104 | |||
105 | print_config() { | ||
106 | |||
107 | # see if we're configuring a plugin | ||
108 | plug=`get_plugin $1` | ||
109 | if [ $? = 0 ]; then | ||
110 | eval ${plug} config | ||
111 | return | ||
112 | fi | ||
113 | |||
114 | # if not, execute built in commands | ||
115 | case $1 in | ||
116 | load) | ||
117 | cat <<-EOF | ||
118 | graph_title Load average | ||
119 | graph_info The load average of the machine describes how many processes are in the run-queue (scheduled to run "immediately"). | ||
120 | graph_category system | ||
121 | graph_args --base 1000 -l 0 | ||
122 | graph_vlabel load | ||
123 | graph_scale no | ||
124 | load.label load | ||
125 | load.info Average load for the last five minutes. | ||
126 | load.warning 10 | ||
127 | load.critical 120 | ||
128 | EOF | ||
129 | ;; | ||
130 | swap) | ||
131 | cat <<-EOF | ||
132 | graph_title Swap in/out | ||
133 | graph_args --base 1000 -l 0 | ||
134 | graph_info This graph shows the swap activity of the system. | ||
135 | graph_category system | ||
136 | graph_vlabel pages per second in (-) / out (+) | ||
137 | swap_in.label swap | ||
138 | swap_in.type DERIVE | ||
139 | swap_in.min 0 | ||
140 | swap_in.max 100000 | ||
141 | swap_in.graph no | ||
142 | swap_out.label swap | ||
143 | swap_out.type DERIVE | ||
144 | swap_out.min 0 | ||
145 | swap_out.max 100000 | ||
146 | swap_out.negative swap_in | ||
147 | EOF | ||
148 | ;; | ||
149 | iostat) | ||
150 | drives=`/usr/sbin/iostat -Id | /usr/bin/head -n 1 | /usr/bin/xargs` | ||
151 | cat <<-EOF | ||
152 | graph_title IOstat by bytes | ||
153 | graph_args --base 1024 -l 0 | ||
154 | graph_vlabel MB per second read+written | ||
155 | graph_category disk | ||
156 | graph_info This graph shows the I/O to and from block devices | ||
157 | EOF | ||
158 | printf "graph_order" | ||
159 | for d in ${drives}; do printf " %s_read %s_write" $d $d; done | ||
160 | printf "\n" | ||
161 | for d in ${drives}; do | ||
162 | cat <<-EOF | ||
163 | ${d}_read.label ${d} | ||
164 | ${d}_read.type DERIVE | ||
165 | ${d}_read.max 2000 | ||
166 | ${d}_read.min 0 | ||
167 | ${d}_read.graph no | ||
168 | ${d}_write.label ${d} | ||
169 | ${d}_write.info I/O on device ${d} | ||
170 | ${d}_write.type DERIVE | ||
171 | ${d}_write.max 2000 | ||
172 | ${d}_write.min 0 | ||
173 | ${d}_write.negative ${d}_read | ||
174 | EOF | ||
175 | done | ||
176 | ;; | ||
177 | uptime) | ||
178 | cat <<-EOF | ||
179 | graph_title Uptime | ||
180 | graph_args --base 1000 -l 0 | ||
181 | graph_vlabel uptime in days | ||
182 | graph_scale no | ||
183 | graph_category system | ||
184 | uptime.label uptime | ||
185 | uptime.draw AREA | ||
186 | EOF | ||
187 | ;; | ||
188 | memory) | ||
189 | cat <<-EOF | ||
190 | graph_title Memory usage | ||
191 | 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`)) | ||
192 | graph_category system | ||
193 | graph_info This graph shows what the machine uses its memory for. | ||
194 | graph_order active inactive wired buffers cached free swap | ||
195 | active.label active | ||
196 | active.info pages recently statistically used | ||
197 | active.draw AREA | ||
198 | inactive.label inactive | ||
199 | inactive.info pages recently statistically unused | ||
200 | inactive.draw STACK | ||
201 | wired.label wired | ||
202 | wired.info pages that are fixed into memory, usually for kernel purposes, but also sometimes for special use in processes | ||
203 | wired.draw STACK | ||
204 | buffers.label buffers | ||
205 | buffers.info pages used for filesystem buffers | ||
206 | buffers.draw STACK | ||
207 | cached.label cache | ||
208 | cached.info pages that have percolated from inactive to a status where they maintain their data, but can often be immediately reused | ||
209 | cached.draw STACK | ||
210 | free.label free | ||
211 | free.info pages without data content | ||
212 | free.draw STACK | ||
213 | swap.label swap | ||
214 | swap.info Swap space used | ||
215 | swap.draw STACK | ||
216 | EOF | ||
217 | ;; | ||
218 | open_files) | ||
219 | cat <<-EOF | ||
220 | graph_title File table usage | ||
221 | graph_args --base 1000 -l 0 | ||
222 | graph_vlabel number of open files | ||
223 | graph_category system | ||
224 | graph_info This graph monitors the number of open files. | ||
225 | used.label open files | ||
226 | used.info The number of currently open files. | ||
227 | used.warning $(( `get_sys kern.maxfiles` * 92 / 100 )) | ||
228 | used.critical $(( `get_sys kern.maxfiles` * 98 / 100 )) | ||
229 | max.label max open files | ||
230 | max.info The maximum supported number of open files. | ||
231 | EOF | ||
232 | ;; | ||
233 | cpu) | ||
234 | cdef=`get_sys kern.clockrate` ; cdef=${cdef#*stathz =}; cdef=${cdef% *}",/,100,*" | ||
235 | cat <<-EOF | ||
236 | graph_title CPU usage | ||
237 | graph_order system interrupt user nice idle | ||
238 | graph_args --base 1000 -r --lower-limit 0 --upper-limit $(( `get_sys hw.ncpu` * 100 )) | ||
239 | graph_vlabel % | ||
240 | graph_scale no | ||
241 | graph_info This graph shows how CPU time is spent. | ||
242 | graph_category system | ||
243 | graph_period second | ||
244 | system.label system | ||
245 | system.draw AREA | ||
246 | system.max 5000 | ||
247 | system.type DERIVE | ||
248 | system.min 0 | ||
249 | system.info CPU time spent by the kernel in system activities | ||
250 | system.cdef system,${cdef} | ||
251 | interrupt.label interrupt | ||
252 | interrupt.draw STACK | ||
253 | interrupt.max 5000 | ||
254 | interrupt.type DERIVE | ||
255 | interrupt.min 0 | ||
256 | interrupt.info CPU time spent by the kernel processing interrupts | ||
257 | interrupt.cdef interrupt,${cdef} | ||
258 | user.label user | ||
259 | user.draw STACK | ||
260 | user.max 5000 | ||
261 | user.type DERIVE | ||
262 | user.info CPU time spent by normal programs and daemons | ||
263 | user.min 0 | ||
264 | user.cdef user,${cdef} | ||
265 | nice.label nice | ||
266 | nice.draw STACK | ||
267 | nice.max 5000 | ||
268 | nice.type DERIVE | ||
269 | nice.info CPU time spent by nice(1)d programs | ||
270 | nice.min 0 | ||
271 | nice.cdef nice,${cdef} | ||
272 | idle.label idle | ||
273 | idle.draw STACK | ||
274 | idle.max 5000 | ||
275 | idle.type DERIVE | ||
276 | idle.info Idle CPU time | ||
277 | idle.min 0 | ||
278 | idle.cdef idle,${cdef} | ||
279 | EOF | ||
280 | ;; | ||
281 | systat) | ||
282 | cat <<-EOF | ||
283 | graph_title System Statistics | ||
284 | graph_vlabel per second | ||
285 | graph_scale no | ||
286 | graph_category system | ||
287 | graph_info FreeBSD systat plugin | ||
288 | softint.label Software interrupts | ||
289 | softint.type DERIVE | ||
290 | softint.min 0 | ||
291 | hardint.label Hardware interrupts | ||
292 | hardint.type DERIVE | ||
293 | hardint.min 0 | ||
294 | syscall.label System calls | ||
295 | syscall.type DERIVE | ||
296 | syscall.min 0 | ||
297 | cs.label Context switches | ||
298 | cs.type DERIVE | ||
299 | cs.min 0 | ||
300 | forks.label Fork rate | ||
301 | forks.type DERIVE | ||
302 | forks.min 0 | ||
303 | EOF | ||
304 | ;; | ||
305 | *) | ||
306 | printf "# Unknown command.\n" | ||
307 | ;; | ||
308 | esac | ||
309 | } | ||
310 | |||
311 | get_sys() { | ||
312 | LANG=C LC_ALL=C /sbin/sysctl -n $@ | ||
313 | } | ||
314 | |||
315 | # After function definitions, main() can use them | ||
316 | main "$@" | ||