Projet

Général

Profil

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

root / plugins / router / mikrotik_system @ 74b7eb2a

Historique | Voir | Annoter | Télécharger (15,2 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
# setze Variablen mit default-Werten
43
ssh_user=${ssh_user:-user}
44
ssh_password=${ssh_password:-password}
45
ssh_host=${ssh_host:-192.168.2.1}
46
c=0 # zähler; wird für verschiedene Schleifen benötigt
47
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 74c7fc3d Michael Grote
# Funktionen
67
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
  # hole daten 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
  while read -r line; do # für jede zeile in... ; siehe "done"
119
    # gebe die zeile aus
120
    # suche mit awk nach "voltage:"
121
    # wenn gefunden:
122
    # gebe jede zeile per print text aus
123
    # externe/bash-variablen mit "'"<var>"'"
124
    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
    # funktion wie bei den "label"-funktionen
138
    # gib überschrift aus wenn dirtyconfig nicht gesetzt ist
139
    # weil aufruf dann nicht in "config" erfolgt
140
    # wenn dirtyconfig nicht gesetzt ist oder =0, gebe überschrift aus
141
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
142
      echo "$line" | awk '/voltage:/{
143
        print "multigraph voltage_graph_""'"$name"'";
144
        }'
145
    fi
146
    # entferne mit gsub das zeichen % in wert $2
147
    # gebe $2 aus
148
    echo "$line" | awk '/voltage:/{
149
      gsub(/V/,"",$2);
150
      print "voltage.value " $2
151
      }'
152
  done <<< "$data"
153
}
154
155
function get_bad_blocks_label {
156
  while read -r line; do
157
    echo "$line" | awk '/bad-blocks:/{
158
      print "multigraph bad_blocks_graph_""'"$name"'";
159
      print "graph_title bad blocks " "'"$name"'";
160
      print "graph_vlabel %";
161
      print "graph_category mikrotik";
162 144f333c Younes Ichiche
      print "graph_args -l 0 --upper-limit 5";
163 74c7fc3d Michael Grote
      print "bad_blocks.label bad_blocks";
164 144f333c Younes Ichiche
      print "bad_blocks.warning 3;
165
      print "bad_blocks.critical 4";
166 74c7fc3d Michael Grote
      print "graph_info Percentage of Bad Blocks."
167
      }'
168
  done <<< "$data"
169
}
170
function get_bad_blocks_value {
171
  while read -r line; do
172
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
173
    echo "$line" | awk '/bad-blocks:/{
174
      print "multigraph bad_blocks_graph_""'"$name"'";
175
      }'
176
    fi
177
    echo "$line" | awk '/bad-blocks:/{
178
      gsub(/%/,"",$2);
179
      print "bad_blocks.value " $2
180
      }'
181
  done <<< "$data"
182
}
183
184
function get_write_sect_total_label {
185
  while read -r line; do
186
    echo "$line" | awk '/write-sect-total:/{
187
      print "multigraph write_sect_total_graph_""'"$name"'";
188
      print "graph_title Total sector writes " "'"$name"'";
189
      print "graph_vlabel count";
190
      print "graph_category mikrotik";
191
      print "graph_args -l 0";
192
      print "write_sect_total.label write_sect_total";
193
      print "graph_info Total sector writes."
194
      }'
195
  done <<< "$data"
196
}
197
function get_write_sect_total_value {
198
  while read -r line; do
199
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
200
      echo "$line" | awk '/write-sect-total:/{
201
        print "multigraph write_sect_total_graph_'"$name"'";
202
        }'
203
    fi
204
    echo "$line" | awk '/write-sect-total:/{
205
      print "write_sect_total.value " $2
206
      }'
207
  done <<< "$data"
208
}
209
210
function get_write_sect_since_reboot_label {
211
  while read -r line; do
212
    echo "$line" | awk '/write-sect-since-reboot:/{
213
      print "multigraph write_sect_since_reboot_graph_'"$name"'";
214
      print "graph_title Sector writes since reboot " "'"$name"'";
215
      print "graph_vlabel count";
216
      print "graph_category mikrotik";
217
      print "graph_args -l 0";
218
      print "write_sect_since_reboot.label write_sect_since_reboot";
219
      print "graph_info Total Sector writes since last reboot."
220
      }'
221
  done <<< "$data"
222
}
223
function get_write_sect_since_reboot_value {
224
  while read -r line; do
225
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
226
      echo "$line" | awk '/write-sect-since-reboot:/{
227
        print "multigraph write_sect_since_reboot_graph_""'"$name"'";
228
      }'
229
    fi
230
    echo "$line" | awk '/write-sect-since-reboot:/{
231
      print "write_sect_since_reboot.value " $2
232
      }'
233
  done <<< "$data"
234
}
235
236
function get_temperature_label {
237
  while read -r line; do
238
    echo "$line" | awk '/temperature/{
239
      print "multigraph temperature_graph_""'"$name"'";
240
      print "graph_title temperature " "'"$name"'";
241
      print "graph_vlabel °C";
242
      print "graph_category mikrotik";
243
      print "graph_args -l 0";
244
      print "temperature.label cpu temperature";
245
      print "temperature.warning 75";
246
      print "temperature.critical 90"
247
      }'
248
  done <<< "$data"
249
}
250
function get_temperature_value {
251
  get_ros_version
252
  while read -r line; do
253
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
254
      echo "$line" | awk '/temperature/{
255
        print "multigraph temperature_graph_""'"$name"'";
256
        }'
257
    fi
258
    if [ "$ros_version" == "6" ]; then
259
      echo "$line" | awk '/temperature:/{
260
        gsub(/C/,"",$2);
261
        print "temperature.value " $2
262
        }'
263
    fi
264
    if [ "$ros_version" == "7" ]; then
265
      echo "$line" | awk '/cpu-temperature/{
266
        print "temperature.value " $3
267
        }'
268
    fi
269
  done <<< "$data"
270
}
271
272
function get_cpu_label {
273
  echo multigraph cpu_load_graph_"$name"
274
  echo graph_title cpu load "$name"
275
  echo graph_vlabel %
276
  echo graph_category mikrotik
277
  echo graph_args -l 0 --upper-limit 100
278
  echo cpu_total.label total load
279
  echo cpu_total.warning 75
280
  echo cpu_total.critical 90
281
  echo graph_info Total CPU Load and Load, IRQ and Disk per Core.
282
283
  while [ "$c" -lt "$anzahl_cpu" ]; do
284
    while read -r line; do
285
      echo "$line" | grep cpu$c | awk '{
286
        print "cpu"'"$c"'"_load.label " "cpu" "'"$c"'" " load"
287
        print "cpu"'"$c"'"_irq.label " "cpu" "'"$c"'" " irq"
288
        print "cpu"'"$c"'"_disk.label " "cpu" "'"$c"'" " disk"
289
        }'
290
    done <<< "$data"
291
    c=$(( c + 1 ))
292
  done
293
}
294
function get_cpu_value {
295
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
296
    echo multigraph cpu_load_graph_"$name"
297
  fi
298
  while read -r line; do
299
    echo "$line" | awk '/cpu-load:/{
300
      gsub(/%/,"",$2);
301
      print "cpu_total.value " $2
302
      }'
303
  done <<< "$data"
304
  c=0
305
  while [ "$c" -lt "$anzahl_cpu" ]; do
306
    while read -r line; do
307
      echo "$line" | grep cpu$c | awk '{
308
        gsub(/%/,"",$3);
309
        gsub(/%/,"",$4);
310
        gsub(/%/,"",$5);
311
        print "cpu"'"$c"'"_load.value " $3
312
        print "cpu"'"$c"'"_irq.value " $4
313
        print "cpu"'"$c"'"_disk.value " $5
314
        }'
315
    done <<< "$data"
316
    c=$(( c + 1 ))
317
  done
318
}
319
320
function get_memory_label {
321
  get_mem_total
322
  get_mem_free
323
  while read -r line; do
324 b43ec018 Younes Ichiche
    echo "$line" | awk -v name=$name '/free-memory:/{
325
      printf "multigraph memory_graph_%s\n", name;
326
      printf "graph_title memory %s\n", name;
327 74c7fc3d Michael Grote
      print "graph_vlabel MiB";
328
      print "graph_category mikrotik";
329
      print "graph_args -l 0";
330
      print "total_memory.label total memory";
331
      print "used_memory.label used memory";
332
      print "free_memory.label free memory";
333
      print "graph_info Total, Used & free RAM.";
334
      gsub(/MiB/,"",$2);
335 b43ec018 Younes Ichiche
      printf "used_memory.critical %.0f\n", $2*0.9;
336
      printf "used_memory.warning %.0f\n", $2*0.7 }'
337 74c7fc3d Michael Grote
  done <<< "$data"
338
}
339
function get_memory_value {
340
  get_mem_total
341
  get_mem_free
342
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
343 9c3855be Younes Ichiche
    echo multigraph memory_graph_"$name"
344 74c7fc3d Michael Grote
  fi
345
  while read -r line; do
346
    echo "$line" | awk '/total-memory:/{
347
        gsub(/MiB/,"",$2);
348 3dd16cc5 Younes Ichiche
        printf "total_memory.value %.0f\n", $2
349 74c7fc3d Michael Grote
      }'
350
  done <<< "$data"
351
  while read -r line; do
352
    echo "$line" | awk '/free-memory:/{
353
        gsub(/MiB/,"",$2);
354 3dd16cc5 Younes Ichiche
        printf "free_memory.value %.0f\n", $2
355 74c7fc3d Michael Grote
      }'
356
  done <<< "$data"
357
  # 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 74c7fc3d Michael Grote
}
361
362
function get_disk_label {
363
  while read -r line; do
364 9c3855be Younes Ichiche
    echo "$line" | awk -v name=$name '/free-hdd-space:/{
365
      printf "multigraph disk_graph_%s\n", name;
366
      printf "graph_title disk %s\n", name;
367 74c7fc3d Michael Grote
      print "graph_vlabel Bytes";
368
      print "graph_category mikrotik";
369
      print "graph_args -l 0";
370
      print "total_disk.label total disk space";
371
      print "free_disk.label free disk space";
372
      print "graph_info Total & free disk space."}'
373
  done <<< "$data"
374
}
375
function get_disk_value {
376
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
377 9c3855be Younes Ichiche
    echo multigraph disk_graph_"$name"
378 74c7fc3d Michael Grote
  fi
379
  while read -r line; do
380
    echo "$line" | grep KiB | awk '/free-hdd-space:/ {
381
      gsub(/KiB/,"",$2)
382 144f333c Younes Ichiche
      printf "free_disk.value %d\n", $2*1024 }'
383 74c7fc3d Michael Grote
    echo "$line" |  grep MiB | awk '/free-hdd-space:/ {
384
      gsub(/MiB/,"",$2)
385 144f333c Younes Ichiche
      printf "free_disk.value %d\n", $2*1048576}'
386 74c7fc3d Michael Grote
    echo "$line" | grep KiB | awk '/total-hdd-space:/ {
387
      gsub(/KiB/,"",$2)
388 144f333c Younes Ichiche
      printf "total_disk.value %d\n", $2*1024 }'
389 74c7fc3d Michael Grote
    echo "$line" |  grep MiB | awk '/total-hdd-space:/ {
390
      gsub(/MiB/,"",$2)
391 144f333c Younes Ichiche
      printf "total_disk.value %d\n", $2*1048576 }'
392 74c7fc3d Michael Grote
  done <<< "$data"
393
}
394
395
# rufe funktionen auf, reihenfolge ist wichtig
396
get_data
397 ee4f4ce2 Younes Ichiche
validate_data
398 74c7fc3d Michael Grote
get_name
399
get_cpu_count
400
# munin-Logik
401
# wenn $1 = X; dann
402
if [ "$1" = "autoconf" ]; then
403
    if ! command -v sshpass &> /dev/null; then
404
        echo "[ERROR] sshpass could not be found!"
405
    else
406
        echo "yes"
407
    fi
408
    exit 0
409
fi
410
if [ "$1" = "config" ]; then
411
  # gebe label aus
412
  # wenn dirtyconfig gesetzt rufe value funktion auf
413
  get_voltage_label
414
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
415
    get_voltage_value
416
  fi
417
  get_bad_blocks_label
418
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
419
    get_bad_blocks_value
420
  fi
421
  get_write_sect_total_label
422
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
423
    get_write_sect_total_value
424
  fi
425
  get_write_sect_since_reboot_label
426
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
427
    get_write_sect_since_reboot_value
428
  fi
429
  get_temperature_label
430
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
431
    get_temperature_value
432
  fi
433
  get_cpu_label
434
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
435
    get_cpu_value
436
  fi
437
  get_memory_label
438
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
439
    get_memory_value
440
  fi
441
  get_disk_label
442
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
443
    get_disk_value
444
  fi
445
  exit 0
446
fi
447
# wird nie aufgerufen wenn dirtyconfig gesetzt ist
448
# dann wird nur config aufgerufen und dabei werden zusätzlich die values ausgegeben
449
get_voltage_value
450
get_bad_blocks_value
451
get_write_sect_total_value
452
get_write_sect_since_reboot_value
453
get_temperature_value
454
get_cpu_value
455
get_memory_value
456
get_disk_value
457
exit 0
458
459
# Beispieloutput ROS 6.4*
460
#  voltage: 24.5V
461
#   cpu-temperature: 31C
462
#                    uptime: 4w3d21h28m58s
463
#                   version: 6.48.4 (stable)
464
#                build-time: Aug/18/2021 06:43:27
465
#          factory-software: 6.44.6
466
#               free-memory: 475.8MiB
467
#              total-memory: 512.0MiB
468
#                       cpu: ARMv7
469
#                 cpu-count: 2
470
#             cpu-frequency: 800MHz
471
#                  cpu-load: 9%
472
#            free-hdd-space: 2124.0KiB
473
#           total-hdd-space: 15.9MiB
474
#   write-sect-since-reboot: 339810
475
#          write-sect-total: 350544
476
#                bad-blocks: 0%
477
#         architecture-name: arm
478
#                board-name: CRS309-1G-8S+
479
#                  platform: MikroTik
480
#  # CPU                                                                                                                                         LOAD         IRQ        DISK
481
#  0 cpu0                                                                                                                                          0%          0%          0%
482
#  1 cpu1                                                                                                                                         19%          0%          0%
483
#   name: crs309
484
485
# Beispieloutput ROS 7*
486
# Columns: NAME, VALUE, TYPE
487
#   #  NAME             VA  T
488
#   0  cpu-temperature  44  C
489
#                    uptime: 3d1h30m
490
#                   version: 7.0.5 (stable)
491
#                build-time: Aug/06/2021 11:33:39
492
#          factory-software: 7.0.4
493
#               free-memory: 818.8MiB
494
#              total-memory: 1024.0MiB
495
#                 cpu-count: 4
496
#             cpu-frequency: 1400MHz
497
#                  cpu-load: 0%
498
#            free-hdd-space: 993.8MiB
499
#           total-hdd-space: 1025.0MiB
500
#   write-sect-since-reboot: 4802
501
#          write-sect-total: 4802
502
#                bad-blocks: 0%
503
#         architecture-name: arm64
504
#                board-name: RB5009UG+S+
505
#                  platform: MikroTik
506
# Columns: CPU, LOAD, IRQ, DISK
507
#   #  CPU   LO  IR  DI
508
#   0  cpu0  0%  0%  0%
509
#   1  cpu1  0%  0%  0%
510
#   2  cpu2  0%  0%  0%
511
#   3  cpu3  0%  0%  0%
512
#   name: rb5009
513
#