Advertisement
sa_drug

[MT]: Scripts - sys-ros-startup

Nov 21st, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.79 KB | None | 0 0
  1. /system script add comment="=SYS-ROS-STARTUP=" dont-require-permissions=no name=sys-ros-startup owner=admin policy=ftp,reboot,read,write,policy,test,sniff source="# ROS Start-up script [ ver. 2.3.6 ] \
  2.    \n# Functions:\
  3.    \n# 1. Update system note & log it to parse on remote syslog side;\
  4.    \n# 2. Load necessery global variables;\
  5.    \n# 3. Double reboot after ros upgrade;\
  6.    \n# Require:\
  7.    \n# 1. system identity must be set to reasonable value;\
  8.    \n# 2. snmp settings must contain location field set to reasonable value;\
  9.    \n# Links:\
  10.    \n# https://wiki.mikrotik.com/wiki/Manual:Scripting\
  11.    \n# https://wiki.mikrotik.com/wiki/Manual:Scripting_Tips_and_Trickss\
  12.    \n# Dmitry Shevchenko - 11.11.2019\
  13.    \n\
  14.    \n# 0. Loading functions\
  15.    \n:do { /system script run ros-functions } on-error={ \
  16.    \n\t:log error ( \"sys-ros-startup: can't load necessery functions\" );\
  17.    \n\t:error \"[ERR]: can't load necessery functions - inconsistent list of scrips\" ;\
  18.    \n}\
  19.    \n# 1. Global variables\
  20.    \n# 1.1 Functions\
  21.    \n:global funcIsFlash;\
  22.    \n# 1.2 Importing global variables from file\
  23.    \n:local globals \"globals.rsc\"\
  24.    \n# 2. Local variables\
  25.    \n# 2.2 System info\
  26.    \n:local rbname [ /system resource get board-name ];\
  27.    \n:local rbarch [ /system resource get architecture-name ];\
  28.    \n:local rbcpun [ /system resource get cpu ];\
  29.    \n:local rbcpuf [ /system resource get cpu-frequency ];\
  30.    \n:local rbcpuc [ /system resource get cpu-count ];\
  31.    \n:local rosver [ /system resource get version ];\
  32.    \n:local roshnm [ /system identity get name ];\
  33.    \n:local rbbldr [ /system routerboard get current-firmware ];\
  34.    \n:local rblctn [ /snmp get value-name=location ];\
  35.    \n# 2.3 System note file location\
  36.    \n:local snlctn \"sys-note.txt\";\
  37.    \n# 2.4 Second reboot after routerboard's bootloader upgrade   \
  38.    \n:local rbtFile \"ros-upgrade-once.txt\";\
  39.    \n:local rbtDelay 180s;\
  40.    \n# 2.5 Verbose output\
  41.    \n:local VERBOSE false;\
  42.    \n:local DEBUG;\
  43.    \n\
  44.    \n# 3. Main\
  45.    \n# 3.0 Redefine file-paths\
  46.    \n# TODO: Should parse \$rbname to detect which internal storage is used.\
  47.    \nif ( [ \$funcIsFlash ] ) do={ \
  48.    \n\t:set snlctn ( \"flash/\" . \$snlctn );\
  49.    \n\t:set globals ( \"flash/\" . \$globals );\
  50.    \n\t:set rbtFile ( \"flash/\" . \$rbtFile );\
  51.    \n}\
  52.    \n# 3.1 Allways create sys-note.txt (or upload it with initial configuration)\
  53.    \n:system note print file=\$snlctn\
  54.    \n# 3.2 System Info\
  55.    \n:log info ( \"sys-ros-startup: BOARD \$rbname / LDR \$rbbldr / ROS \$rosver.\$rbarch #\$rbcpuc \$rbcpun (\$rbcpuf Mhz)\" );\
  56.    \n# 3.3 Import global variables\
  57.    \nif ( [ :len [ file find name=\"\$globals\" ] ] != 0 ) do={\
  58.    \n\t:do { import \$globals } on-error={ :log error (\"sys-ros-startup: import of \$globals is failed\"); }\
  59.    \n\t:log info ( \"sys-ros-startup: list of globals was imported successfully (\$globals)\" );\
  60.    \n}\
  61.    \n:if ( \$VERBOSE ) do={\
  62.    \n\t:set DEBUG [ /system script environment find ];\
  63.    \n\t:foreach item in=\$DEBUG do={\
  64.    \n\t\t:local itemname [ /system script environment get \$item name ];\
  65.    \n\t\t:local itemvalue [ /system script environment get \$item value ];\
  66.    \n\t\t:log info ( \"startup,debug: \$itemname = \$itemvalue\" );\
  67.    \n\t};\
  68.    \n}\
  69.    \n# 3.4 Update system note file\
  70.    \n:if ( [ :len [ file find name=\"\$snlctn\" ] ] !=0 ) do={\
  71.    \n\t:log info ( \"sys-ros-startup: update system note\" );\
  72.    \n\t/system note set note=\"\\n[LOCATION]: \$rblctn (\$roshnm)\\n[HARDWARE]: BOARD \$rbname / LDR \$rbbldr / ROS \$rosver.\$rbarch #\$rbcpuc \$rbcpun (\$rbcpuf Mhz)\"\
  73.    \n\t:file set \$snlctn contents=\"\\n[LOCATION]: \$rblctn (\$roshnm)\\n[HARDWARE]: BOARD \$rbname / LDR \$rbbldr / ROS \$rosver.\$rbarch #\$rbcpuc \$rbcpun (\$rbcpuf Mhz)\"\
  74.    \n}\
  75.    \n# 3.5 First-run after ROS-upgrade\
  76.    \n:if ( [ :len [ file find name=\$rbtFile ] ] !=0 ) do={\
  77.    \n\t:log warning ( \"sys-ros-startup: first run after ros upgrade\" );\
  78.    \n\t:log info ( \"sys-ros-startup: reboot is scheduled in \$rbtDelay\" );\
  79.    \n\t:do { /file remove \$rbtFile } on-error={\
  80.    \n\t\t:log error ( \"sys-ros-startup: can't remove signal file - ros-upgrade-once.txt\" );\
  81.    \n\t\t:error \"Can't remove signal file - ros-upgrade-once.txt\"\
  82.    \n\t}\t\
  83.    \n\t:delay \$rbtDelay;\
  84.    \n\t/system reboot;\
  85.    \n}\
  86.    \n# 3.6 Environment cleanup\
  87.    \n:do { /system script environment remove [ find name~\"func\" ] } on-error={\
  88.    \n\t:log warning ( \"sys-ros-startup: script environment cleanup is failed\" );\
  89.    \n};\
  90.    \n# 4. End"
  91.  
  92. /system scheduler
  93. add comment="System startup" name=sys-startup on-event=sys-ros-startup policy=ftp,reboot,read,write,policy,test,sniff start-time=startup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement