From 94581741f42d809a1364accda57cdf1d57519ffa Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 12 Sep 2019 13:45:44 +0200 Subject: global-functions: introduce and use $DeviceInfo --- check-routeros-update | 12 ++---------- cloud-backup | 13 ++----------- email-backup | 17 ++++------------- global-functions | 29 +++++++++++++++++++++++++++++ upload-backup | 17 ++++------------- 5 files changed, 41 insertions(+), 47 deletions(-) diff --git a/check-routeros-update b/check-routeros-update index 78fd9c6..dccfcbe 100644 --- a/check-routeros-update +++ b/check-routeros-update @@ -8,6 +8,7 @@ :global SafeUpdateUrl; :global SentRouterosUpdateNotification; +:global DeviceInfo; :global SendNotification; :local DoUpdate do={ @@ -36,9 +37,6 @@ } :if ($Update->"installed-version" != $Update->"latest-version") do={ - :local BoardName [ / system resource get board-name ]; - :local RouterBoard [ / system routerboard get ]; - :if ([ :len $SafeUpdateUrl ] > 0) do={ :local Result; :do { @@ -74,13 +72,7 @@ $SendNotification ("RouterOS update") \ ("There is a RouterOS update available.\n\n" . \ - "Board name: " . $BoardName . "\n" . \ - "Model: " . $RouterBoard->"model" . "\n" . \ - "Serial number: " . $RouterBoard->"serial-number" . "\n" . \ - "Hostname: " . $Identity . "\n" . \ - "Channel: " . $Update->"channel" . "\n" . \ - "Installed: " . $Update->"installed-version" . "\n" . \ - "Available: " . $Update->"latest-version" . "\n\n" .\ + [ $DeviceInfo ] . "\n\n" . \ "https://mikrotik.com/download/changelogs/" . $Update->"channel" . "-release-tree") \ "" "true"; :set SentRouterosUpdateNotification ($Update->"latest-version"); diff --git a/cloud-backup b/cloud-backup index 5a36f29..d948f99 100644 --- a/cloud-backup +++ b/cloud-backup @@ -7,13 +7,9 @@ :global Identity; :global BackupPassword; +:global DeviceInfo; :global SendNotification; -# get some system information -:local BoardName [ / system resource get board-name ]; -:local RouterBoard [ / system routerboard get ]; -:local Update [ / system package update get ]; - :do { # we are not interested in output, but print without count-only is # required to fetch information from cloud @@ -29,12 +25,7 @@ $SendNotification "Cloud backup" \ ("Uploaded backup for " . $Identity . " to cloud.\n\n" . \ - "Board name: " . $BoardName . "\n" . \ - "Model: " . $RouterBoard->"model" . "\n" . \ - "Serial number: " . $RouterBoard->"serial-number" . "\n" . \ - "Hostname: " . $Identity . "\n" . \ - "Channel: " . $Update->"channel" . "\n" . \ - "RouterOS: " . $Update->"installed-version" . "\n\n" . \ + [ $DeviceInfo ] . "\n\n" . \ "Name: " . $Cloud->"name" . "\n" . \ "Size: " . $Cloud->"size" . "\n" . \ "Download key: " . $Cloud->"secret-download-key") "" "true"; diff --git a/email-backup b/email-backup index f1a2f9a..d5acb44 100644 --- a/email-backup +++ b/email-backup @@ -13,6 +13,7 @@ :global BackupPassword; :global CharacterReplace; +:global DeviceInfo; :if ($BackupSendBinary != true && \ $BackupSendExport != true) do={ @@ -26,11 +27,6 @@ :local ConfigFile "none"; :local Attach [ :toarray "" ]; -# get some system information -:local BoardName [ / system resource get board-name ]; -:local RouterBoard [ / system routerboard get ]; -:local Update [ / system package update get ]; - # binary backup :if ($BackupSendBinary = true) do={ / system backup save encryption=aes-sha256 name=$FileName password=$BackupPassword; @@ -49,13 +45,8 @@ / tool e-mail send to=$EmailBackupTo cc=$EmailBackupCc \ subject=("[" . $Identity . "] Backup & Config") \ body=("Backup and config export for " . $Identity . ".\n\n" . \ - "Board name: " . $BoardName . "\n" . \ - "Model: " . $RouterBoard->"model" . "\n" . \ - "Serial number: " . $RouterBoard->"serial-number" . "\n" . \ - "Hostname: " . $Identity . "\n" . \ - "Channel: " . $Update->"channel" . "\n" . \ - "RouterOS: " . $Update->"installed-version" . "\n\n" . \ - "Backup file: " . $BackupFile . "\n" . \ - "Config file: " . $ConfigFile) \ + [ $DeviceInfo ] . "\n\n" . \ + "Backup file: " . $BackupFile . "\n" . \ + "Config file: " . $ConfigFile) \ file=$Attach; } diff --git a/global-functions b/global-functions index 1b73b0a..87cda40 100644 --- a/global-functions +++ b/global-functions @@ -294,3 +294,32 @@ :delay ([ $GetRandom $1 ] . "s"); } + +# get readable device info +:global DeviceInfo do={ + :global Identity; + + :local BoardName [ / system resource get board-name ]; + :local RouterBoard [ / system routerboard get ]; + :local Update [ / system package update get ]; + + :local Info ( \ + "Hostname: " . $Identity . "\n" . \ + "Board name: " . $BoardName); + :if ($RouterBoard->"routerboard" = true) do={ + :set Info ($Info . "\n" . \ + "Model: " . $RouterBoard->"model" . "\n" . \ + "Serial number: " . $RouterBoard->"serial-number"); + } + :set Info ($Info . "\n" . \ + "RouterOS:\n" . \ + " Channel: " . $Update->"channel" . "\n" . \ + " Installed: " . $Update->"installed-version"); + :if ([ :typeof ($Update->"latest-version") ] != "nothing" && \ + $Update->"installed-version" != $Update->"latest-version") do={ + :set Info ($Info . "\n" . \ + " Available: " . $Update->"latest-version"); + } + + :return $Info; +} diff --git a/upload-backup b/upload-backup index b780299..a75f14a 100644 --- a/upload-backup +++ b/upload-backup @@ -14,6 +14,7 @@ :global BackupPassword; :global CharacterReplace; +:global DeviceInfo; :global SendNotification; :if ($BackupSendBinary != true && \ @@ -27,11 +28,6 @@ :local BackupFile "none"; :local ConfigFile "none"; -# get some system information -:local BoardName [ / system resource get board-name ]; -:local RouterBoard [ / system routerboard get ]; -:local Update [ / system package update get ]; - # binary backup :if ($BackupSendBinary = true) do={ / system backup save encryption=aes-sha256 name=$FileName password=$BackupPassword; @@ -62,12 +58,7 @@ $SendNotification "Backup & Config Upload" \ ("Backup and config export for " . $Identity . ".\n\n" . \ - "Board name: " . $BoardName . "\n" . \ - "Model: " . $RouterBoard->"model" . "\n" . \ - "Serial number: " . $RouterBoard->"serial-number" . "\n" . \ - "Hostname: " . $Identity . "\n" . \ - "Channel: " . $Update->"channel" . "\n" . \ - "RouterOS: " . $Update->"installed-version" . "\n\n" . \ - "Backup uploaded: " . $BackupFile . "\n" . \ - "Config uploaded: " . $ConfigFile) "" "true"; + [ $DeviceInfo ] . "\n\n" . \ + "Backup file: " . $BackupFile . "\n" . \ + "Config file: " . $ConfigFile) "" "true"; } -- cgit v1.2.3-70-g09d2