Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / router / mikrotik_system @ 38649d31

Historique | Voir | Annoter | Télécharger (15,1 ko)

1 74c7fc3d Michael Grote
#!/bin/bash
2
#%# family=auto
3
4
: << EOF
5
=head1 NAME
6
mikrotik_system - This plugin fetches multiple Values from a mikrotik device.
7
8
=head1 CONFIGURATION
9 6a79efee Younes Ichiche
tested with a RB493G, RB750GR3, RB5009 and CRS309.
10 74c7fc3d Michael Grote
Dependencies:
11
- sshpass
12
13
A User is needed for this plugin to work:
14
/user add name=munin group=read password=hallowelt address=<munin-server-ip>
15
16
plugin config:
17
[mt_system_<name>]
18
user root
19
env.ssh_user munin
20
env.ssh_password hallowelt
21
env.ssh_host 192.168.2.1
22
23
=head1 AUTHOR
24
=over 4
25
=item * Michael Grote
26
=back
27
28
=head1 LICENSE
29
GPLv3 or later
30
31
SPDX-License-Identifier: GPL-3.0-or-later
32
33
=head1 MAGIC MARKERS
34
=begin comment
35
These magic markers are used by munin-node-configure when installing
36
munin-node.
37
=end comment
38
  #%# family=auto
39
=cut
40
EOF
41
42 38649d31 Michael Grote
# set variables with default values
43 74c7fc3d Michael Grote
ssh_user=${ssh_user:-user}
44
ssh_password=${ssh_password:-password}
45
ssh_host=${ssh_host:-192.168.2.1}
46 38649d31 Michael Grote
c=0 # counter, it is used in a few loops
47 74c7fc3d Michael Grote
48 ee4f4ce2 Younes Ichiche
# Function to get stderr from command
49
# USAGE: catch STDOUT STDERR cmd args..
50 58dbb4d0 Younes Ichiche
# Source: https://stackoverflow.com/a/41069638
51 ee4f4ce2 Younes Ichiche
catch()
52
{
53
eval "$({
54
__2="$(
55
  { __1="$("${@:3}")"; } 2>&1;
56
  ret=$?;
57
  printf '%q=%q\n' "$1" "$__1" >&2;
58
  exit $ret
59
  )";
60
ret="$?";
61
printf '%s=%q\n' "$2" "$__2" >&2;
62
printf '( exit %q )' "$ret" >&2;
63
} 2>&1 )";
64
}
65
66 38649d31 Michael Grote
# functions
67 74c7fc3d Michael Grote
function get_name {
68
  while read -r line; do
69
    if echo "$line" | grep -q 'name:'; then
70 74b7eb2a Younes Ichiche
      name="$(echo "$line" | grep -E '\s+name:' | cut -f2 -d: | sed 's/^ *//g' | sed 's/[^A-Za-z0-9]/_/')"
71 74c7fc3d Michael Grote
    fi
72
  done <<< "$data"
73
}
74
function get_ros_version {
75
  while read -r line; do
76
    if echo "$line" | grep -q 'version:'; then
77
      if echo "$line" | cut -f2 -d" " | grep --quiet "^7\."; then
78
        ros_version="7"
79
      else
80
        ros_version="6"
81
      fi
82
    fi
83
  done <<< "$data"
84
}
85
function get_cpu_count {
86
  while read -r line; do
87
    if echo "$line" | grep -q 'cpu-count'; then
88
      anzahl_cpu=$(echo "$line" | grep cpu-count: | sed -r 's/(cpu-count: )([0-9]+)/\2/g' | tr -dc '0-9')
89
    fi
90
  done <<< "$data"
91
}
92
function get_data {
93 38649d31 Michael Grote
  # fetch data per ssh
94 ee4f4ce2 Younes Ichiche
  catch data stderr sshpass -p  "$ssh_password" ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_user"@"$ssh_host" ':delay 6s; /system health print; /system resource print; /system resource cpu print; /system identity print'
95
  data_ret=$?
96
}
97
function validate_data {
98
  if [ $data_ret -ne 0 ]; then
99
    echo "SSH returned errorcode = $data_ret:"
100
    echo "$stderr"
101
    exit $data_ret
102
  fi
103 74c7fc3d Michael Grote
}
104
function get_mem_total {
105
  mem_total=$(
106
    while read -r line; do
107
      echo "$line" | awk '/total-memory:/{ gsub(/MiB/,"",$2); print $2 }' | tr -dc '0-9.'
108
    done <<< "$data")
109
}
110
function get_mem_free {
111
  mem_free=$(
112
    while read -r line; do
113
      echo "$line" | awk '/free-memory:/{ gsub(/MiB/,"",$2); print $2 }' | tr -dc '0-9.'
114
    done <<< "$data")
115
}
116
117
function get_voltage_label {
118 38649d31 Michael Grote
  while read -r line; do # for every line
119
    # output line
120
    # search with awk for "voltage:"
121
    # if found
122
    # print line
123
    # external/bash-variables with "'"<var>"'"
124 74c7fc3d Michael Grote
    echo "$line" | awk '/voltage:/{
125
      print "multigraph voltage_graph_""'"$name"'";
126
      print "graph_title voltage " "'"$name"'";
127
      print "graph_vlabel volt";
128
      print "graph_category mikrotik";
129
      print "graph_args -l 0";
130
      print "voltage.label voltage";
131
      print "graph_info Input Voltage."
132
      }'
133
  done <<< "$data" # der variable data
134
}
135
function get_voltage_value {
136
  while read -r line; do
137 38649d31 Michael Grote
    # like the label functions
138
    # print title if dirtyconfig is not set
139
    # because the call does not come in "config"
140 74c7fc3d Michael Grote
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
141
      echo "$line" | awk '/voltage:/{
142
        print "multigraph voltage_graph_""'"$name"'";
143
        }'
144
    fi
145 38649d31 Michael Grote
    # remove with gsub the char % in argument $2
146
    # print $2
147 74c7fc3d Michael Grote
    echo "$line" | awk '/voltage:/{
148
      gsub(/V/,"",$2);
149
      print "voltage.value " $2
150
      }'
151
  done <<< "$data"
152
}
153
154
function get_bad_blocks_label {
155
  while read -r line; do
156
    echo "$line" | awk '/bad-blocks:/{
157
      print "multigraph bad_blocks_graph_""'"$name"'";
158
      print "graph_title bad blocks " "'"$name"'";
159
      print "graph_vlabel %";
160
      print "graph_category mikrotik";
161 144f333c Younes Ichiche
      print "graph_args -l 0 --upper-limit 5";
162 74c7fc3d Michael Grote
      print "bad_blocks.label bad_blocks";
163 144f333c Younes Ichiche
      print "bad_blocks.warning 3;
164
      print "bad_blocks.critical 4";
165 74c7fc3d Michael Grote
      print "graph_info Percentage of Bad Blocks."
166
      }'
167
  done <<< "$data"
168
}
169
function get_bad_blocks_value {
170
  while read -r line; do
171
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
172
    echo "$line" | awk '/bad-blocks:/{
173
      print "multigraph bad_blocks_graph_""'"$name"'";
174
      }'
175
    fi
176
    echo "$line" | awk '/bad-blocks:/{
177
      gsub(/%/,"",$2);
178
      print "bad_blocks.value " $2
179
      }'
180
  done <<< "$data"
181
}
182
183
function get_write_sect_total_label {
184
  while read -r line; do
185
    echo "$line" | awk '/write-sect-total:/{
186
      print "multigraph write_sect_total_graph_""'"$name"'";
187
      print "graph_title Total sector writes " "'"$name"'";
188
      print "graph_vlabel count";
189
      print "graph_category mikrotik";
190
      print "graph_args -l 0";
191
      print "write_sect_total.label write_sect_total";
192
      print "graph_info Total sector writes."
193
      }'
194
  done <<< "$data"
195
}
196
function get_write_sect_total_value {
197
  while read -r line; do
198
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
199
      echo "$line" | awk '/write-sect-total:/{
200
        print "multigraph write_sect_total_graph_'"$name"'";
201
        }'
202
    fi
203
    echo "$line" | awk '/write-sect-total:/{
204
      print "write_sect_total.value " $2
205
      }'
206
  done <<< "$data"
207
}
208
209
function get_write_sect_since_reboot_label {
210
  while read -r line; do
211
    echo "$line" | awk '/write-sect-since-reboot:/{
212
      print "multigraph write_sect_since_reboot_graph_'"$name"'";
213
      print "graph_title Sector writes since reboot " "'"$name"'";
214
      print "graph_vlabel count";
215
      print "graph_category mikrotik";
216
      print "graph_args -l 0";
217
      print "write_sect_since_reboot.label write_sect_since_reboot";
218
      print "graph_info Total Sector writes since last reboot."
219
      }'
220
  done <<< "$data"
221
}
222
function get_write_sect_since_reboot_value {
223
  while read -r line; do
224
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
225
      echo "$line" | awk '/write-sect-since-reboot:/{
226
        print "multigraph write_sect_since_reboot_graph_""'"$name"'";
227
      }'
228
    fi
229
    echo "$line" | awk '/write-sect-since-reboot:/{
230
      print "write_sect_since_reboot.value " $2
231
      }'
232
  done <<< "$data"
233
}
234
235
function get_temperature_label {
236
  while read -r line; do
237
    echo "$line" | awk '/temperature/{
238
      print "multigraph temperature_graph_""'"$name"'";
239
      print "graph_title temperature " "'"$name"'";
240
      print "graph_vlabel °C";
241
      print "graph_category mikrotik";
242
      print "graph_args -l 0";
243
      print "temperature.label cpu temperature";
244
      print "temperature.warning 75";
245
      print "temperature.critical 90"
246
      }'
247
  done <<< "$data"
248
}
249
function get_temperature_value {
250
  get_ros_version
251
  while read -r line; do
252
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
253
      echo "$line" | awk '/temperature/{
254
        print "multigraph temperature_graph_""'"$name"'";
255
        }'
256
    fi
257
    if [ "$ros_version" == "6" ]; then
258
      echo "$line" | awk '/temperature:/{
259
        gsub(/C/,"",$2);
260
        print "temperature.value " $2
261
        }'
262
    fi
263
    if [ "$ros_version" == "7" ]; then
264
      echo "$line" | awk '/cpu-temperature/{
265
        print "temperature.value " $3
266
        }'
267
    fi
268
  done <<< "$data"
269
}
270
271
function get_cpu_label {
272
  echo multigraph cpu_load_graph_"$name"
273
  echo graph_title cpu load "$name"
274
  echo graph_vlabel %
275
  echo graph_category mikrotik
276
  echo graph_args -l 0 --upper-limit 100
277
  echo cpu_total.label total load
278
  echo cpu_total.warning 75
279
  echo cpu_total.critical 90
280
  echo graph_info Total CPU Load and Load, IRQ and Disk per Core.
281
282
  while [ "$c" -lt "$anzahl_cpu" ]; do
283
    while read -r line; do
284
      echo "$line" | grep cpu$c | awk '{
285
        print "cpu"'"$c"'"_load.label " "cpu" "'"$c"'" " load"
286
        print "cpu"'"$c"'"_irq.label " "cpu" "'"$c"'" " irq"
287
        print "cpu"'"$c"'"_disk.label " "cpu" "'"$c"'" " disk"
288
        }'
289
    done <<< "$data"
290
    c=$(( c + 1 ))
291
  done
292
}
293
function get_cpu_value {
294
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
295
    echo multigraph cpu_load_graph_"$name"
296
  fi
297
  while read -r line; do
298
    echo "$line" | awk '/cpu-load:/{
299
      gsub(/%/,"",$2);
300
      print "cpu_total.value " $2
301
      }'
302
  done <<< "$data"
303
  c=0
304
  while [ "$c" -lt "$anzahl_cpu" ]; do
305
    while read -r line; do
306
      echo "$line" | grep cpu$c | awk '{
307
        gsub(/%/,"",$3);
308
        gsub(/%/,"",$4);
309
        gsub(/%/,"",$5);
310
        print "cpu"'"$c"'"_load.value " $3
311
        print "cpu"'"$c"'"_irq.value " $4
312
        print "cpu"'"$c"'"_disk.value " $5
313
        }'
314
    done <<< "$data"
315
    c=$(( c + 1 ))
316
  done
317
}
318
319
function get_memory_label {
320
  get_mem_total
321
  get_mem_free
322
  while read -r line; do
323 b43ec018 Younes Ichiche
    echo "$line" | awk -v name=$name '/free-memory:/{
324
      printf "multigraph memory_graph_%s\n", name;
325
      printf "graph_title memory %s\n", name;
326 74c7fc3d Michael Grote
      print "graph_vlabel MiB";
327
      print "graph_category mikrotik";
328
      print "graph_args -l 0";
329
      print "total_memory.label total memory";
330
      print "used_memory.label used memory";
331
      print "free_memory.label free memory";
332
      print "graph_info Total, Used & free RAM.";
333
      gsub(/MiB/,"",$2);
334 b43ec018 Younes Ichiche
      printf "used_memory.critical %.0f\n", $2*0.9;
335
      printf "used_memory.warning %.0f\n", $2*0.7 }'
336 74c7fc3d Michael Grote
  done <<< "$data"
337
}
338
function get_memory_value {
339
  get_mem_total
340
  get_mem_free
341
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
342 9c3855be Younes Ichiche
    echo multigraph memory_graph_"$name"
343 74c7fc3d Michael Grote
  fi
344
  while read -r line; do
345
    echo "$line" | awk '/total-memory:/{
346
        gsub(/MiB/,"",$2);
347 3dd16cc5 Younes Ichiche
        printf "total_memory.value %.0f\n", $2
348 74c7fc3d Michael Grote
      }'
349
  done <<< "$data"
350
  while read -r line; do
351
    echo "$line" | awk '/free-memory:/{
352
        gsub(/MiB/,"",$2);
353 3dd16cc5 Younes Ichiche
        printf "free_memory.value %.0f\n", $2
354 74c7fc3d Michael Grote
      }'
355
  done <<< "$data"
356 38649d31 Michael Grote
<<<<<<< HEAD
357 74c7fc3d Michael Grote
  # berechne used-memory
358
  # gesamt + frei = benutzt
359 144f333c Younes Ichiche
  printf "used_memory.value %.0f\n" "$(echo $mem_total $mem_free | awk '{print ($1 - $2)}')"
360 38649d31 Michael Grote
=======
361
  # calculate used-memory
362
  # total - free = used
363
  echo used_memory.value "$(echo $mem_total $mem_free | awk '{print ($1 - $2)}')"
364
>>>>>>> fc66f426 (translated comments form german to englisch)
365 74c7fc3d Michael Grote
}
366
367
function get_disk_label {
368
  while read -r line; do
369 9c3855be Younes Ichiche
    echo "$line" | awk -v name=$name '/free-hdd-space:/{
370
      printf "multigraph disk_graph_%s\n", name;
371
      printf "graph_title disk %s\n", name;
372 74c7fc3d Michael Grote
      print "graph_vlabel Bytes";
373
      print "graph_category mikrotik";
374
      print "graph_args -l 0";
375
      print "total_disk.label total disk space";
376
      print "free_disk.label free disk space";
377
      print "graph_info Total & free disk space."}'
378
  done <<< "$data"
379
}
380
function get_disk_value {
381
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
382 9c3855be Younes Ichiche
    echo multigraph disk_graph_"$name"
383 74c7fc3d Michael Grote
  fi
384
  while read -r line; do
385
    echo "$line" | grep KiB | awk '/free-hdd-space:/ {
386
      gsub(/KiB/,"",$2)
387 144f333c Younes Ichiche
      printf "free_disk.value %d\n", $2*1024 }'
388 74c7fc3d Michael Grote
    echo "$line" |  grep MiB | awk '/free-hdd-space:/ {
389
      gsub(/MiB/,"",$2)
390 144f333c Younes Ichiche
      printf "free_disk.value %d\n", $2*1048576}'
391 74c7fc3d Michael Grote
    echo "$line" | grep KiB | awk '/total-hdd-space:/ {
392
      gsub(/KiB/,"",$2)
393 144f333c Younes Ichiche
      printf "total_disk.value %d\n", $2*1024 }'
394 74c7fc3d Michael Grote
    echo "$line" |  grep MiB | awk '/total-hdd-space:/ {
395
      gsub(/MiB/,"",$2)
396 144f333c Younes Ichiche
      printf "total_disk.value %d\n", $2*1048576 }'
397 74c7fc3d Michael Grote
  done <<< "$data"
398
}
399
400 38649d31 Michael Grote
# call functions, the order is important
401 74c7fc3d Michael Grote
get_data
402 ee4f4ce2 Younes Ichiche
validate_data
403 74c7fc3d Michael Grote
get_name
404
get_cpu_count
405 38649d31 Michael Grote
# munin-Logic
406
# id $1 = X; then
407 74c7fc3d Michael Grote
if [ "$1" = "autoconf" ]; then
408
    if ! command -v sshpass &> /dev/null; then
409
        echo "[ERROR] sshpass could not be found!"
410
    else
411
        echo "yes"
412
    fi
413
    exit 0
414
fi
415
if [ "$1" = "config" ]; then
416 38649d31 Michael Grote
  # print label
417
  # if dirtyconfig is set print the value
418 74c7fc3d Michael Grote
  get_voltage_label
419
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
420
    get_voltage_value
421
  fi
422
  get_bad_blocks_label
423
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
424
    get_bad_blocks_value
425
  fi
426
  get_write_sect_total_label
427
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
428
    get_write_sect_total_value
429
  fi
430
  get_write_sect_since_reboot_label
431
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
432
    get_write_sect_since_reboot_value
433
  fi
434
  get_temperature_label
435
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
436
    get_temperature_value
437
  fi
438
  get_cpu_label
439
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
440
    get_cpu_value
441
  fi
442
  get_memory_label
443
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
444
    get_memory_value
445
  fi
446
  get_disk_label
447
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
448
    get_disk_value
449
  fi
450
  exit 0
451
fi
452 38649d31 Michael Grote
# does not get called if dirtyconfig is set
453 74c7fc3d Michael Grote
get_voltage_value
454
get_bad_blocks_value
455
get_write_sect_total_value
456
get_write_sect_since_reboot_value
457
get_temperature_value
458
get_cpu_value
459
get_memory_value
460
get_disk_value
461
exit 0
462
463 38649d31 Michael Grote
# Example Output ROS 6.4*
464 74c7fc3d Michael Grote
#  voltage: 24.5V
465
#   cpu-temperature: 31C
466
#                    uptime: 4w3d21h28m58s
467
#                   version: 6.48.4 (stable)
468
#                build-time: Aug/18/2021 06:43:27
469
#          factory-software: 6.44.6
470
#               free-memory: 475.8MiB
471
#              total-memory: 512.0MiB
472
#                       cpu: ARMv7
473
#                 cpu-count: 2
474
#             cpu-frequency: 800MHz
475
#                  cpu-load: 9%
476
#            free-hdd-space: 2124.0KiB
477
#           total-hdd-space: 15.9MiB
478
#   write-sect-since-reboot: 339810
479
#          write-sect-total: 350544
480
#                bad-blocks: 0%
481
#         architecture-name: arm
482
#                board-name: CRS309-1G-8S+
483
#                  platform: MikroTik
484
#  # CPU                                                                                                                                         LOAD         IRQ        DISK
485
#  0 cpu0                                                                                                                                          0%          0%          0%
486
#  1 cpu1                                                                                                                                         19%          0%          0%
487
#   name: crs309
488
489 38649d31 Michael Grote
# Example Output ROS 7*
490 74c7fc3d Michael Grote
# Columns: NAME, VALUE, TYPE
491
#   #  NAME             VA  T
492
#   0  cpu-temperature  44  C
493
#                    uptime: 3d1h30m
494
#                   version: 7.0.5 (stable)
495
#                build-time: Aug/06/2021 11:33:39
496
#          factory-software: 7.0.4
497
#               free-memory: 818.8MiB
498
#              total-memory: 1024.0MiB
499
#                 cpu-count: 4
500
#             cpu-frequency: 1400MHz
501
#                  cpu-load: 0%
502
#            free-hdd-space: 993.8MiB
503
#           total-hdd-space: 1025.0MiB
504
#   write-sect-since-reboot: 4802
505
#          write-sect-total: 4802
506
#                bad-blocks: 0%
507
#         architecture-name: arm64
508
#                board-name: RB5009UG+S+
509
#                  platform: MikroTik
510
# Columns: CPU, LOAD, IRQ, DISK
511
#   #  CPU   LO  IR  DI
512
#   0  cpu0  0%  0%  0%
513
#   1  cpu1  0%  0%  0%
514
#   2  cpu2  0%  0%  0%
515
#   3  cpu3  0%  0%  0%
516
#   name: rb5009
517
#