Projet

Général

Profil

Révision 74c7fc3d

ID74c7fc3d86cc61e3bcd60a4568745d8d21a597c2
Parent 367a4b37
Enfant f184607b

Ajouté par Michael Grote il y a environ 4 ans

add ssh-based mikrotik_system plugin (#1252)

Voir les différences:

plugins/router/mikrotik_system
1
#!/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
tested with a RB750GR3, RB5009 and CRS309.
10
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
# Funktionen
49
function get_name {
50
  while read -r line; do
51
    if echo "$line" | grep -q 'name:'; then
52
      name=$(echo "$line" | grep name: | awk '{ print $2 }')
53
    fi
54
  done <<< "$data"
55
}
56
function get_ros_version {
57
  while read -r line; do
58
    if echo "$line" | grep -q 'version:'; then
59
      if echo "$line" | cut -f2 -d" " | grep --quiet "^7\."; then
60
        ros_version="7"
61
      else
62
        ros_version="6"
63
      fi
64
    fi
65
  done <<< "$data"
66
}
67
function get_cpu_count {
68
  while read -r line; do
69
    if echo "$line" | grep -q 'cpu-count'; then
70
      anzahl_cpu=$(echo "$line" | grep cpu-count: | sed -r 's/(cpu-count: )([0-9]+)/\2/g' | tr -dc '0-9')
71
    fi
72
  done <<< "$data"
73
}
74
function get_data {
75
  # hole daten per ssh
76
  data=$(sshpass -p  "$ssh_password" ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_user"@"$ssh_host" -q ':delay 6s; /system health print; /system resource print; /system resource cpu print; /system identity print')
77
}
78
function get_mem_total {
79
  mem_total=$(
80
    while read -r line; do
81
      echo "$line" | awk '/total-memory:/{ gsub(/MiB/,"",$2); print $2 }' | tr -dc '0-9.'
82
    done <<< "$data")
83
}
84
function get_mem_free {
85
  mem_free=$(
86
    while read -r line; do
87
      echo "$line" | awk '/free-memory:/{ gsub(/MiB/,"",$2); print $2 }' | tr -dc '0-9.'
88
    done <<< "$data")
89
}
90

  
91
function get_voltage_label {
92
  while read -r line; do # für jede zeile in... ; siehe "done"
93
    # gebe die zeile aus
94
    # suche mit awk nach "voltage:"
95
    # wenn gefunden:
96
    # gebe jede zeile per print text aus
97
    # externe/bash-variablen mit "'"<var>"'"
98
    echo "$line" | awk '/voltage:/{
99
      print "multigraph voltage_graph_""'"$name"'";
100
      print "graph_title voltage " "'"$name"'";
101
      print "graph_vlabel volt";
102
      print "graph_category mikrotik";
103
      print "graph_args -l 0";
104
      print "voltage.label voltage";
105
      print "graph_info Input Voltage."
106
      }'
107
  done <<< "$data" # der variable data
108
}
109
function get_voltage_value {
110
  while read -r line; do
111
    # funktion wie bei den "label"-funktionen
112
    # gib überschrift aus wenn dirtyconfig nicht gesetzt ist
113
    # weil aufruf dann nicht in "config" erfolgt
114
    # wenn dirtyconfig nicht gesetzt ist oder =0, gebe überschrift aus
115
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
116
      echo "$line" | awk '/voltage:/{
117
        print "multigraph voltage_graph_""'"$name"'";
118
        }'
119
    fi
120
    # entferne mit gsub das zeichen % in wert $2
121
    # gebe $2 aus
122
    echo "$line" | awk '/voltage:/{
123
      gsub(/V/,"",$2);
124
      print "voltage.value " $2
125
      }'
126
  done <<< "$data"
127
}
128

  
129
function get_bad_blocks_label {
130
  while read -r line; do
131
    echo "$line" | awk '/bad-blocks:/{
132
      print "multigraph bad_blocks_graph_""'"$name"'";
133
      print "graph_title bad blocks " "'"$name"'";
134
      print "graph_vlabel %";
135
      print "graph_category mikrotik";
136
      print "graph_args -l 0 --upper-limit 100";
137
      print "bad_blocks.label bad_blocks";
138
      print "bad_blocks.warning 50";
139
      print "bad_blocks.critical 90";
140
      print "graph_info Percentage of Bad Blocks."
141
      }'
142
  done <<< "$data"
143
}
144
function get_bad_blocks_value {
145
  while read -r line; do
146
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
147
    echo "$line" | awk '/bad-blocks:/{
148
      print "multigraph bad_blocks_graph_""'"$name"'";
149
      }'
150
    fi
151
    echo "$line" | awk '/bad-blocks:/{
152
      gsub(/%/,"",$2);
153
      print "bad_blocks.value " $2
154
      }'
155
  done <<< "$data"
156
}
157

  
158
function get_write_sect_total_label {
159
  while read -r line; do
160
    echo "$line" | awk '/write-sect-total:/{
161
      print "multigraph write_sect_total_graph_""'"$name"'";
162
      print "graph_title Total sector writes " "'"$name"'";
163
      print "graph_vlabel count";
164
      print "graph_category mikrotik";
165
      print "graph_args -l 0";
166
      print "write_sect_total.label write_sect_total";
167
      print "graph_info Total sector writes."
168
      }'
169
  done <<< "$data"
170
}
171
function get_write_sect_total_value {
172
  while read -r line; do
173
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
174
      echo "$line" | awk '/write-sect-total:/{
175
        print "multigraph write_sect_total_graph_'"$name"'";
176
        }'
177
    fi
178
    echo "$line" | awk '/write-sect-total:/{
179
      print "write_sect_total.value " $2
180
      }'
181
  done <<< "$data"
182
}
183

  
184
function get_write_sect_since_reboot_label {
185
  while read -r line; do
186
    echo "$line" | awk '/write-sect-since-reboot:/{
187
      print "multigraph write_sect_since_reboot_graph_'"$name"'";
188
      print "graph_title Sector writes since reboot " "'"$name"'";
189
      print "graph_vlabel count";
190
      print "graph_category mikrotik";
191
      print "graph_args -l 0";
192
      print "write_sect_since_reboot.label write_sect_since_reboot";
193
      print "graph_info Total Sector writes since last reboot."
194
      }'
195
  done <<< "$data"
196
}
197
function get_write_sect_since_reboot_value {
198
  while read -r line; do
199
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
200
      echo "$line" | awk '/write-sect-since-reboot:/{
201
        print "multigraph write_sect_since_reboot_graph_""'"$name"'";
202
      }'
203
    fi
204
    echo "$line" | awk '/write-sect-since-reboot:/{
205
      print "write_sect_since_reboot.value " $2
206
      }'
207
  done <<< "$data"
208
}
209

  
210
function get_temperature_label {
211
  while read -r line; do
212
    echo "$line" | awk '/temperature/{
213
      print "multigraph temperature_graph_""'"$name"'";
214
      print "graph_title temperature " "'"$name"'";
215
      print "graph_vlabel °C";
216
      print "graph_category mikrotik";
217
      print "graph_args -l 0";
218
      print "temperature.label cpu temperature";
219
      print "temperature.warning 75";
220
      print "temperature.critical 90"
221
      }'
222
  done <<< "$data"
223
}
224
function get_temperature_value {
225
  get_ros_version
226
  while read -r line; do
227
    if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
228
      echo "$line" | awk '/temperature/{
229
        print "multigraph temperature_graph_""'"$name"'";
230
        }'
231
    fi
232
    if [ "$ros_version" == "6" ]; then
233
      echo "$line" | awk '/temperature:/{
234
        gsub(/C/,"",$2);
235
        print "temperature.value " $2
236
        }'
237
    fi
238
    if [ "$ros_version" == "7" ]; then
239
      echo "$line" | awk '/cpu-temperature/{
240
        print "temperature.value " $3
241
        }'
242
    fi
243
  done <<< "$data"
244
}
245

  
246
function get_cpu_label {
247
  echo multigraph cpu_load_graph_"$name"
248
  echo graph_title cpu load "$name"
249
  echo graph_vlabel %
250
  echo graph_category mikrotik
251
  echo graph_args -l 0 --upper-limit 100
252
  echo cpu_total.label total load
253
  echo cpu_total.warning 75
254
  echo cpu_total.critical 90
255
  echo graph_info Total CPU Load and Load, IRQ and Disk per Core.
256

  
257
  while [ "$c" -lt "$anzahl_cpu" ]; do
258
    while read -r line; do
259
      echo "$line" | grep cpu$c | awk '{
260
        print "cpu"'"$c"'"_load.label " "cpu" "'"$c"'" " load"
261
        print "cpu"'"$c"'"_irq.label " "cpu" "'"$c"'" " irq"
262
        print "cpu"'"$c"'"_disk.label " "cpu" "'"$c"'" " disk"
263
        }'
264
    done <<< "$data"
265
    c=$(( c + 1 ))
266
  done
267
}
268
function get_cpu_value {
269
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
270
    echo multigraph cpu_load_graph_"$name"
271
  fi
272
  while read -r line; do
273
    echo "$line" | awk '/cpu-load:/{
274
      gsub(/%/,"",$2);
275
      print "cpu_total.value " $2
276
      }'
277
  done <<< "$data"
278
  c=0
279
  while [ "$c" -lt "$anzahl_cpu" ]; do
280
    while read -r line; do
281
      echo "$line" | grep cpu$c | awk '{
282
        gsub(/%/,"",$3);
283
        gsub(/%/,"",$4);
284
        gsub(/%/,"",$5);
285
        print "cpu"'"$c"'"_load.value " $3
286
        print "cpu"'"$c"'"_irq.value " $4
287
        print "cpu"'"$c"'"_disk.value " $5
288
        }'
289
    done <<< "$data"
290
    c=$(( c + 1 ))
291
  done
292
}
293

  
294
function get_memory_label {
295
  get_mem_total
296
  get_mem_free
297
  while read -r line; do
298
    echo "$line" | awk '/free-memory:/{
299
      print "multigraph memory_graph_""'"$name"'";
300
      print "graph_title memory " "'"$name"'";
301
      print "graph_vlabel MiB";
302
      print "graph_category mikrotik";
303
      print "graph_args -l 0";
304
      print "total_memory.label total memory";
305
      print "used_memory.label used memory";
306
      print "free_memory.label free memory";
307
      print "graph_info Total, Used & free RAM.";
308
      gsub(/MiB/,"",$2);
309
      print "used_memory.critical " $2*0.9;
310
      print "used_memory.warning " $2*0.7 }'
311
  done <<< "$data"
312
}
313
function get_memory_value {
314
  get_mem_total
315
  get_mem_free
316
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
317
    echo multigraph memory_"$name"
318
  fi
319
  while read -r line; do
320
    echo "$line" | awk '/total-memory:/{
321
        gsub(/MiB/,"",$2);
322
        print "total_memory.value " $2
323
      }'
324
  done <<< "$data"
325
  while read -r line; do
326
    echo "$line" | awk '/free-memory:/{
327
        gsub(/MiB/,"",$2);
328
        print "free_memory.value " $2
329
      }'
330
  done <<< "$data"
331
  # berechne used-memory
332
  # gesamt + frei = benutzt
333
  echo used_memory.value "$(echo $mem_total $mem_free | awk '{print ($1 - $2)}')"
334
}
335

  
336
function get_disk_label {
337
  while read -r line; do
338
    echo "$line" | awk '/free-hdd-space:/{
339
      print "multigraph disk_graph_""'"$name"'";
340
      print "graph_title disk " "'"$name"'";
341
      print "graph_vlabel Bytes";
342
      print "graph_category mikrotik";
343
      print "graph_args -l 0";
344
      print "total_disk.label total disk space";
345
      print "free_disk.label free disk space";
346
      print "graph_info Total & free disk space."}'
347
  done <<< "$data"
348
}
349
function get_disk_value {
350
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
351
    echo multigraph disk_"$name"
352
  fi
353
  while read -r line; do
354
    echo "$line" | grep KiB | awk '/free-hdd-space:/ {
355
      gsub(/KiB/,"",$2)
356
      print "free_disk.value " $2*1024 }'
357
    echo "$line" |  grep MiB | awk '/free-hdd-space:/ {
358
      gsub(/MiB/,"",$2)
359
      print "free_disk.value " $2*1048576}'
360
    echo "$line" | grep KiB | awk '/total-hdd-space:/ {
361
      gsub(/KiB/,"",$2)
362
      print "total_disk.value " $2*1024 }'
363
    echo "$line" |  grep MiB | awk '/total-hdd-space:/ {
364
      gsub(/MiB/,"",$2)
365
      print "total_disk.value " $2*1048576 }'
366
  done <<< "$data"
367
}
368

  
369
# rufe funktionen auf, reihenfolge ist wichtig
370
get_data
371
get_name
372
get_cpu_count
373
# munin-Logik
374
# wenn $1 = X; dann
375
if [ "$1" = "autoconf" ]; then
376
    if ! command -v sshpass &> /dev/null; then
377
        echo "[ERROR] sshpass could not be found!"
378
    else
379
        echo "yes"
380
    fi
381
    exit 0
382
fi
383
if [ "$1" = "config" ]; then
384
  # gebe label aus
385
  # wenn dirtyconfig gesetzt rufe value funktion auf
386
  get_voltage_label
387
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
388
    get_voltage_value
389
  fi
390
  get_bad_blocks_label
391
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
392
    get_bad_blocks_value
393
  fi
394
  get_write_sect_total_label
395
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
396
    get_write_sect_total_value
397
  fi
398
  get_write_sect_since_reboot_label
399
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
400
    get_write_sect_since_reboot_value
401
  fi
402
  get_temperature_label
403
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
404
    get_temperature_value
405
  fi
406
  get_cpu_label
407
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
408
    get_cpu_value
409
  fi
410
  get_memory_label
411
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
412
    get_memory_value
413
  fi
414
  get_disk_label
415
  if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
416
    get_disk_value
417
  fi
418
  exit 0
419
fi
420
# wird nie aufgerufen wenn dirtyconfig gesetzt ist
421
# dann wird nur config aufgerufen und dabei werden zusätzlich die values ausgegeben
422
get_voltage_value
423
get_bad_blocks_value
424
get_write_sect_total_value
425
get_write_sect_since_reboot_value
426
get_temperature_value
427
get_cpu_value
428
get_memory_value
429
get_disk_value
430
exit 0
431

  
432
# Beispieloutput ROS 6.4*
433
#  voltage: 24.5V
434
#   cpu-temperature: 31C
435
#                    uptime: 4w3d21h28m58s
436
#                   version: 6.48.4 (stable)
437
#                build-time: Aug/18/2021 06:43:27
438
#          factory-software: 6.44.6
439
#               free-memory: 475.8MiB
440
#              total-memory: 512.0MiB
441
#                       cpu: ARMv7
442
#                 cpu-count: 2
443
#             cpu-frequency: 800MHz
444
#                  cpu-load: 9%
445
#            free-hdd-space: 2124.0KiB
446
#           total-hdd-space: 15.9MiB
447
#   write-sect-since-reboot: 339810
448
#          write-sect-total: 350544
449
#                bad-blocks: 0%
450
#         architecture-name: arm
451
#                board-name: CRS309-1G-8S+
452
#                  platform: MikroTik
453
#  # CPU                                                                                                                                         LOAD         IRQ        DISK
454
#  0 cpu0                                                                                                                                          0%          0%          0%
455
#  1 cpu1                                                                                                                                         19%          0%          0%
456
#   name: crs309
457

  
458
# Beispieloutput ROS 7*
459
# Columns: NAME, VALUE, TYPE
460
#   #  NAME             VA  T
461
#   0  cpu-temperature  44  C
462
#                    uptime: 3d1h30m
463
#                   version: 7.0.5 (stable)
464
#                build-time: Aug/06/2021 11:33:39
465
#          factory-software: 7.0.4
466
#               free-memory: 818.8MiB
467
#              total-memory: 1024.0MiB
468
#                 cpu-count: 4
469
#             cpu-frequency: 1400MHz
470
#                  cpu-load: 0%
471
#            free-hdd-space: 993.8MiB
472
#           total-hdd-space: 1025.0MiB
473
#   write-sect-since-reboot: 4802
474
#          write-sect-total: 4802
475
#                bad-blocks: 0%
476
#         architecture-name: arm64
477
#                board-name: RB5009UG+S+
478
#                  platform: MikroTik
479
# Columns: CPU, LOAD, IRQ, DISK
480
#   #  CPU   LO  IR  DI
481
#   0  cpu0  0%  0%  0%
482
#   1  cpu1  0%  0%  0%
483
#   2  cpu2  0%  0%  0%
484
#   3  cpu3  0%  0%  0%
485
#   name: rb5009
486
#

Formats disponibles : Unified diff